Skip to content

Commit

Permalink
Merge pull request #1616 from jaaberg/jsx-no-literals-binary
Browse files Browse the repository at this point in the history
Find string literals as a part of BinaryExpressions in jsx-no-literals
  • Loading branch information
ljharb committed Jan 16, 2018
2 parents 8237551 + 28581df commit c2c7a2a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/rules/jsx-no-literals.js
Expand Up @@ -45,16 +45,24 @@ module.exports = {
});
}

function getParentIgnoringBinaryExpressions(node) {
let current = node;
while (current.parent.type === 'BinaryExpression') {
current = current.parent;
}
return current.parent;
}

function getValidation(node) {
const parent = getParentIgnoringBinaryExpressions(node);
const standard = !/^[\s]+$/.test(node.value) &&
typeof node.value === 'string' &&
node.parent &&
node.parent.type.indexOf('JSX') !== -1 &&
node.parent.type !== 'JSXAttribute';
parent.type.indexOf('JSX') !== -1 &&
parent.type !== 'JSXAttribute';
if (isNoStrings) {
return standard;
}
return standard && node.parent.type !== 'JSXExpressionContainer';
return standard && parent.type !== 'JSXExpressionContainer';
}

// --------------------------------------------------------------------------
Expand All @@ -70,7 +78,8 @@ module.exports = {
},

TemplateLiteral: function(node) {
if (isNoStrings && node.parent.type === 'JSXExpressionContainer') {
const parent = getParentIgnoringBinaryExpressions(node);
if (isNoStrings && parent.type === 'JSXExpressionContainer') {
reportLiteralNode(node);
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/rules/jsx-no-literals.js
Expand Up @@ -286,6 +286,14 @@ ruleTester.run('jsx-no-literals', rule, {
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
}, {
code: `
<Foo bar="test">
{'Test' + name}
</Foo>
`,
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
}, {
code: `
<Foo bar="test">
Expand Down Expand Up @@ -315,6 +323,35 @@ ruleTester.run('jsx-no-literals', rule, {
code: '<Foo bar={`Test`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
}, {
code: '<Foo bar={`${baz}`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
}, {
code: '<Foo bar={`Test ${baz}`} />',
options: [{noStrings: true}],
errors: [{message: 'Strings not allowed in JSX files'}]
}, {
code: '<Foo bar={`foo` + \'bar\'} />',
options: [{noStrings: true}],
errors: [
{message: 'Strings not allowed in JSX files'},
{message: 'Strings not allowed in JSX files'}
]
}, {
code: '<Foo bar={`foo` + `bar`} />',
options: [{noStrings: true}],
errors: [
{message: 'Strings not allowed in JSX files'},
{message: 'Strings not allowed in JSX files'}
]
}, {
code: '<Foo bar={\'foo\' + `bar`} />',
options: [{noStrings: true}],
errors: [
{message: 'Strings not allowed in JSX files'},
{message: 'Strings not allowed in JSX files'}
]
}
]
});

0 comments on commit c2c7a2a

Please sign in to comment.