Skip to content

jsx-no-literals: HTML entities in allowedValues are ignored #3130

Closed
@joeattardi

Description

@joeattardi

Hello! I'm using the jsx-no-literals rule with some exceptions (in the allowedStrings option). In particular I want to allow the — literal. However, the rule is still flagging the usage as an error even with this set.

Consider this simple file:

<div>&mdash;</div>

And the following configuration:

{
  noStrings: true,
  allowedStrings: [
    '&mdash;',
   ],
}

This will result in an error:

  {
    ruleId: 'jsx-no-literals',
    severity: 1,
    message: 'Strings not allowed in JSX files: "&mdash;"',
    line: 2,
    column: 14,
    nodeType: 'JSXText',
    messageId: 'noStringsInJSX',
    endLine: 2,
    endColumn: 21
  }

I took a quick look at the code for this rule.

  • When the JSXText check is done, node.value no longer is &mdash; but rather the actual symbol it represents (). This causes the error
  • Later when the Literal check is done, node.value is the original &mdash;. This passes the check.

So the JSXText check causes the rule to flag the error.

If I change the check, in the getValidation function, in jsx-no-literals.js to this:

      if (config.allowedStrings.has(trimIfString(node.raw)))

then node.raw is still &mdash; even for the JSXText check. This small change seems to fix the issue, but I was hesitant to submit a PR because I'm not sure what other implications using the raw vs. the value has.

For what it's worth, after this change, all the test cases for jsx-no-literals still pass.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions