From de268ec00ee1b07465ba4129d11b5e5a289d85d8 Mon Sep 17 00:00:00 2001 From: Karolina Benitez Date: Fri, 31 Jul 2020 14:18:22 -0700 Subject: [PATCH] [Fix] jsx-no-literals with allowStrings doesn't work in props Fixes #2720 --- lib/rules/jsx-no-literals.js | 2 +- tests/lib/rules/jsx-no-literals.js | 59 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index 9ae90d8daf..c74ab82487 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -120,7 +120,7 @@ module.exports = { }, JSXAttribute(node) { - const isNodeValueString = node.value && node.value && node.value.type === 'Literal' && typeof node.value.value === 'string'; + const isNodeValueString = node && node.value && node.value.type === 'Literal' && typeof node.value.value === 'string' && !config.allowedStrings.has(node.value.value); if (config.noStrings && !config.ignoreProps && isNodeValueString) { const customMessage = 'Invalid prop value'; diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index c8851b2e24..c06eec5994 100644 --- a/tests/lib/rules/jsx-no-literals.js +++ b/tests/lib/rules/jsx-no-literals.js @@ -42,8 +42,63 @@ function invalidProp(str) { const ruleTester = new RuleTester({parserOptions}); ruleTester.run('jsx-no-literals', rule, { - valid: [ + valid: [].concat( { + code: ` + class Comp1 extends Component { + render() { + return ( +
+ +
+ ); + } + } + `, + options: [{noStrings: true, allowedStrings: ['button', 'submit']}] + }, { + code: ` + class Comp1 extends Component { + render() { + return ( +
+ +
+ ); + } + } + `, + options: [{noStrings: true, allowedStrings: ['button', 'submit']}], + parser: parsers.BABEL_ESLINT + }, parsers.TS([{ + code: ` + class Comp1 extends Component { + render() { + return ( +
+ +
+ ); + } + } + `, + options: [{noStrings: true, allowedStrings: ['button', 'submit']}], + parser: parsers.TYPESCRIPT_ESLINT + }, { + code: ` + class Comp1 extends Component { + render() { + return ( +
+ +
+ ); + } + } + `, + options: [{noStrings: true, allowedStrings: ['button', 'submit']}], + parser: parsers['@TYPESCRIPT_ESLINT'] + }]), { code: ` class Comp1 extends Component { render() { @@ -277,7 +332,7 @@ ruleTester.run('jsx-no-literals', rule, { parser: parsers.BABEL_ESLINT, options: [{noStrings: true, ignoreProps: false}] } - ], + ), invalid: [ {