Skip to content

Commit bdbd2a4

Browse files
Joe AttardiROSSROSALES
authored andcommitted
[Fix] jsx-no-literals: properly handle HTML entities when allowed
When an HTML entity such as &mdash; is checked with ESLint, the `value` property becomes the actual unicode character instead of the raw literal. This means if `&mdash;` is in the allowed strings array, and `&mdash;` is in the code being checked, the actual value used in the JSXText check will be `—`, which will not match against `&mdash;`, so it causes a false positive error. When checking against the set of allowed strings, this will now check both the `value` and the `raw` properties of the node. For `&mdash;`, the `raw` property will still be `&mdash;`. If _either_ of these match the list of allowed values, then no error will be returned. Fixes #3130 Co-authored-by: Joe Attardi <[email protected]> Co-authored-by: Ross <[email protected]>
1 parent 3963193 commit bdbd2a4

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/rules/jsx-no-literals.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ module.exports = {
8989
}
9090

9191
function getValidation(node) {
92-
if (config.allowedStrings.has(trimIfString(node.value))) {
92+
const values = [trimIfString(node.raw), trimIfString(node.value)];
93+
if (values.some((value) => config.allowedStrings.has(value))) {
9394
return false;
9495
}
96+
9597
const parent = getParentIgnoringBinaryExpressions(node);
9698

9799
function isParentNodeStandard() {

tests/lib/rules/jsx-no-literals.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ ruleTester.run('jsx-no-literals', rule, {
284284
<img alt='blank image'></img>
285285
`,
286286
},
287+
{
288+
code: `
289+
<div>&mdash;</div>
290+
`,
291+
options: [{ noStrings: true, allowedStrings: ['&mdash;', '—'] }],
292+
},
293+
{
294+
code: `
295+
<div>—</div>
296+
`,
297+
options: [{ noStrings: true, allowedStrings: ['&mdash;', '—'] }],
298+
},
287299
]),
288300

289301
invalid: parsers.all([

0 commit comments

Comments
 (0)