Skip to content

Commit c2c7a2a

Browse files
authored
Merge pull request #1616 from jaaberg/jsx-no-literals-binary
Find string literals as a part of BinaryExpressions in jsx-no-literals
2 parents 8237551 + 28581df commit c2c7a2a

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

lib/rules/jsx-no-literals.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,24 @@ module.exports = {
4545
});
4646
}
4747

48+
function getParentIgnoringBinaryExpressions(node) {
49+
let current = node;
50+
while (current.parent.type === 'BinaryExpression') {
51+
current = current.parent;
52+
}
53+
return current.parent;
54+
}
55+
4856
function getValidation(node) {
57+
const parent = getParentIgnoringBinaryExpressions(node);
4958
const standard = !/^[\s]+$/.test(node.value) &&
5059
typeof node.value === 'string' &&
51-
node.parent &&
52-
node.parent.type.indexOf('JSX') !== -1 &&
53-
node.parent.type !== 'JSXAttribute';
60+
parent.type.indexOf('JSX') !== -1 &&
61+
parent.type !== 'JSXAttribute';
5462
if (isNoStrings) {
5563
return standard;
5664
}
57-
return standard && node.parent.type !== 'JSXExpressionContainer';
65+
return standard && parent.type !== 'JSXExpressionContainer';
5866
}
5967

6068
// --------------------------------------------------------------------------
@@ -70,7 +78,8 @@ module.exports = {
7078
},
7179

7280
TemplateLiteral: function(node) {
73-
if (isNoStrings && node.parent.type === 'JSXExpressionContainer') {
81+
const parent = getParentIgnoringBinaryExpressions(node);
82+
if (isNoStrings && parent.type === 'JSXExpressionContainer') {
7483
reportLiteralNode(node);
7584
}
7685
}

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

+37
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,14 @@ ruleTester.run('jsx-no-literals', rule, {
286286
`,
287287
options: [{noStrings: true}],
288288
errors: [{message: 'Strings not allowed in JSX files'}]
289+
}, {
290+
code: `
291+
<Foo bar="test">
292+
{'Test' + name}
293+
</Foo>
294+
`,
295+
options: [{noStrings: true}],
296+
errors: [{message: 'Strings not allowed in JSX files'}]
289297
}, {
290298
code: `
291299
<Foo bar="test">
@@ -315,6 +323,35 @@ ruleTester.run('jsx-no-literals', rule, {
315323
code: '<Foo bar={`Test`} />',
316324
options: [{noStrings: true}],
317325
errors: [{message: 'Strings not allowed in JSX files'}]
326+
}, {
327+
code: '<Foo bar={`${baz}`} />',
328+
options: [{noStrings: true}],
329+
errors: [{message: 'Strings not allowed in JSX files'}]
330+
}, {
331+
code: '<Foo bar={`Test ${baz}`} />',
332+
options: [{noStrings: true}],
333+
errors: [{message: 'Strings not allowed in JSX files'}]
334+
}, {
335+
code: '<Foo bar={`foo` + \'bar\'} />',
336+
options: [{noStrings: true}],
337+
errors: [
338+
{message: 'Strings not allowed in JSX files'},
339+
{message: 'Strings not allowed in JSX files'}
340+
]
341+
}, {
342+
code: '<Foo bar={`foo` + `bar`} />',
343+
options: [{noStrings: true}],
344+
errors: [
345+
{message: 'Strings not allowed in JSX files'},
346+
{message: 'Strings not allowed in JSX files'}
347+
]
348+
}, {
349+
code: '<Foo bar={\'foo\' + `bar`} />',
350+
options: [{noStrings: true}],
351+
errors: [
352+
{message: 'Strings not allowed in JSX files'},
353+
{message: 'Strings not allowed in JSX files'}
354+
]
318355
}
319356
]
320357
});

0 commit comments

Comments
 (0)