diff --git a/docs/rules/jsx-no-literals.md b/docs/rules/jsx-no-literals.md index 3933357f52..0b1b977dd8 100644 --- a/docs/rules/jsx-no-literals.md +++ b/docs/rules/jsx-no-literals.md @@ -21,14 +21,15 @@ var Hello =
{'test'}
; ### Options -There is only one option: +There are two options: * `noStrings` - Enforces no string literals used as children, wrapped or unwrapped. +* `allowedStrings` - an array of unique string values that would otherwise warn, but will be ignored. To use, you can specify like the following: ```js -"react/jsx-no-literals": [, {"noStrings": true}] +"react/jsx-no-literals": [, {"noStrings": true, "allowedStrings": ["allowed"]}] ``` In this configuration, the following are considered warnings: @@ -53,6 +54,11 @@ var Hello =
var Hello =
{translate('my.translation.key')}
``` +```jsx +// an allowed string +var Hello =
allowed
+``` + ## When Not To Use It If you do not want to enforce any style JSX literals, then you can disable this rule. diff --git a/lib/rules/jsx-no-literals.js b/lib/rules/jsx-no-literals.js index bebddb067a..fd4d91be06 100644 --- a/lib/rules/jsx-no-literals.js +++ b/lib/rules/jsx-no-literals.js @@ -26,6 +26,13 @@ module.exports = { properties: { noStrings: { type: 'boolean' + }, + allowedStrings: { + type: 'array', + uniqueItems: true, + items: { + type: 'string' + } } }, additionalProperties: false @@ -34,6 +41,7 @@ module.exports = { create(context) { const isNoStrings = context.options[0] ? context.options[0].noStrings : false; + const allowedStrings = context.options[0] ? new Set(context.options[0].allowedStrings) : false; const message = isNoStrings ? 'Strings not allowed in JSX files' : @@ -55,6 +63,9 @@ module.exports = { } function getValidation(node) { + if (allowedStrings && allowedStrings.has(node.value)) { + 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..5f1267757e 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,19 @@ ruleTester.run('jsx-no-literals', rule, { {message: stringsMessage('\'foo\'')}, {message: stringsMessage('`bar`')} ] + }, { + code: ` + class Comp1 extends Component { + render() { + return
asdf
+ } + } + `, + options: [{noStrings: true, allowedStrings: ['asd']}], + errors: [ + {message: stringsMessage('\'foo\'')}, + {message: stringsMessage('asdf')} + ] } ] });