From 4cf675294fa7578e1551b77c76d9ff3db5583ccf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Aaberg?= Date: Sun, 17 Dec 2017 09:36:30 +0100 Subject: [PATCH 1/4] Add failing tests for jsx-no-literals with literal as BinaryExpression --- tests/lib/rules/jsx-no-literals.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index 5fc70fec6a..3ff05e8d99 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: ` From 2ae8d5edfbb6e1951d141e3cb88e88132375c3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Aaberg?= Date: Sun, 17 Dec 2017 09:36:46 +0100 Subject: [PATCH 2/4] Find usages of literals as a part of BinaryExpressions in jsx-no-literals --- lib/rules/jsx-no-literals.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 797f5fc566..3a363f9fd1 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -43,11 +43,15 @@ module.exports = { } function getValidation(node) { + let current = node; + while (current.parent.type === 'BinaryExpression') { + current = current.parent; + } const standard = !/^[\s]+$/.test(node.value) && typeof node.value === 'string' && - node.parent && - node.parent.type.indexOf('JSX') !== -1 && - node.parent.type !== 'JSXAttribute'; + current.parent && + current.parent.type.indexOf('JSX') !== -1 && + current.parent.type !== 'JSXAttribute'; if (isNoStrings) { return standard; } From f27ebc28ec11b1e99232dae2763246b2293413d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Aaberg?= Date: Mon, 18 Dec 2017 09:49:55 +0100 Subject: [PATCH 3/4] jsx-no-literals Add tests combining literals and strings --- tests/lib/rules/jsx-no-literals.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index 3ff05e8d99..ac77c4375d 100644 --- a/tests/lib/rules/jsx-no-literals.js +++ b/tests/lib/rules/jsx-no-literals.js @@ -323,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'} + ] } ] }); From 28581df71a7cde4285054b894a2e6759ca0d26ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Aaberg?= Date: Mon, 18 Dec 2017 09:51:08 +0100 Subject: [PATCH 4/4] jsx-no-literals Find all usages of literals combined with strings --- lib/rules/jsx-no-literals.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 3a363f9fd1..52bef3d400 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -42,20 +42,24 @@ module.exports = { }); } - function getValidation(node) { + 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' && - current.parent && - current.parent.type.indexOf('JSX') !== -1 && - current.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'; } // -------------------------------------------------------------------------- @@ -71,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); } }