diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index bebddb067a..95d5412cb5 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -26,6 +26,9 @@ module.exports = { properties: { noStrings: { type: 'boolean' + }, + allowedStrings: { + type: 'array' } }, additionalProperties: false @@ -34,6 +37,7 @@ module.exports = { create(context) { const isNoStrings = context.options[0] ? context.options[0].noStrings : false; + const allowedStrings = context.options[0] ? context.options[0].allowedStrings : false; const message = isNoStrings ? 'Strings not allowed in JSX files' : @@ -55,6 +59,9 @@ module.exports = { } function getValidation(node) { + if (allowedStrings && allowedStrings.indexOf(node.value) > -1) { + return false; + } const parent = getParentIgnoringBinaryExpressions(node); const standard = !/^[\s]+$/.test(node.value) && typeof node.value === 'string' && diff --git a/tests/lib/rules/jsx-no-literals.js b/tests/lib/rules/jsx-no-literals.js index bd622123f0..150d1c71e1 100644 --- a/tests/lib/rules/jsx-no-literals.js +++ b/tests/lib/rules/jsx-no-literals.js @@ -198,8 +198,36 @@ ruleTester.run('jsx-no-literals', rule, { } `, options: [{noStrings: true}] + }, { + code: ` + class Comp1 extends Component { + render() { + return
asdf
+ } + } + `, + options: [{allowedStrings: ['asdf']}] + }, + { + code: ` + class Comp1 extends Component { + render() { + return
 
+ } + } + `, + options: [{noStrings: true, allowedStrings: [' ']}] + }, + { + code: ` + class Comp1 extends Component { + render() { + return
foo: {bar}*
+ } + } + `, + options: [{noStrings: true, allowedStrings: ['foo: ', '*']}] } - ], invalid: [ @@ -385,6 +413,21 @@ ruleTester.run('jsx-no-literals', rule, { {message: stringsMessage('\'foo\'')}, {message: stringsMessage('`bar`')} ] + }, { + /* eslint-disable no-useless-escape */ + code: ` + class Comp1 extends Component { + render() { + return
asdf
+ } + } + `, + /* eslint-enable no-useless-escape */ + options: [{noStrings: true, allowedStrings: ['asd']}], + errors: [ + {message: stringsMessage('\'foo\'')}, + {message: stringsMessage('asdf')} + ] } ] });