Skip to content

Find string literals as a part of BinaryExpressions in jsx-no-literals #1616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions lib/rules/jsx-no-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,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 @@ -67,7 +75,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
Original file line number Diff line number Diff line change
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'}
]
}
]
});