diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index daec7eafeb..628f8c55d9 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -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'; } // -------------------------------------------------------------------------- @@ -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); } } diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index 5fc70fec6a..ac77c4375d 100644 --- a/tests/lib/rules/jsx-no-literals.js +++ b/tests/lib/rules/jsx-no-literals.js @@ -286,6 +286,14 @@ ruleTester.run('jsx-no-literals', rule, { `, options: [{noStrings: true}], errors: [{message: 'Strings not allowed in JSX files'}] + }, { + code: ` + + {'Test' + name} + + `, + options: [{noStrings: true}], + errors: [{message: 'Strings not allowed in JSX files'}] }, { code: ` @@ -315,6 +323,35 @@ ruleTester.run('jsx-no-literals', rule, { code: '', options: [{noStrings: true}], errors: [{message: 'Strings not allowed in JSX files'}] + }, { + code: '', + options: [{noStrings: true}], + errors: [{message: 'Strings not allowed in JSX files'}] + }, { + code: '', + options: [{noStrings: true}], + errors: [{message: 'Strings not allowed in JSX files'}] + }, { + code: '', + options: [{noStrings: true}], + errors: [ + {message: 'Strings not allowed in JSX files'}, + {message: 'Strings not allowed in JSX files'} + ] + }, { + code: '', + options: [{noStrings: true}], + errors: [ + {message: 'Strings not allowed in JSX files'}, + {message: 'Strings not allowed in JSX files'} + ] + }, { + code: '', + options: [{noStrings: true}], + errors: [ + {message: 'Strings not allowed in JSX files'}, + {message: 'Strings not allowed in JSX files'} + ] } ] });