Skip to content

Commit 2c6430d

Browse files
bhollander-indeedljharb
authored andcommitted
[New] jsx-no-literals: Add allowedStrings option
Fixes #2377
1 parent 398b7d7 commit 2c6430d

File tree

3 files changed

+61
-3
lines changed

3 files changed

+61
-3
lines changed

docs/rules/jsx-no-literals.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ var Hello = <div>{'test'}</div>;
2121

2222
### Options
2323

24-
There is only one option:
24+
There are two options:
2525

2626
* `noStrings` - Enforces no string literals used as children, wrapped or unwrapped.
27+
* `allowedStrings` - an array of unique string values that would otherwise warn, but will be ignored.
2728

2829
To use, you can specify like the following:
2930

3031
```js
31-
"react/jsx-no-literals": [<enabled>, {"noStrings": true}]
32+
"react/jsx-no-literals": [<enabled>, {"noStrings": true, "allowedStrings": ["allowed"]}]
3233
```
3334

3435
In this configuration, the following are considered warnings:
@@ -53,6 +54,11 @@ var Hello = <div><Text {...message} /></div>
5354
var Hello = <div>{translate('my.translation.key')}</div>
5455
```
5556

57+
```jsx
58+
// an allowed string
59+
var Hello = <div>allowed</div>
60+
```
61+
5662
## When Not To Use It
5763

5864
If you do not want to enforce any style JSX literals, then you can disable this rule.

lib/rules/jsx-no-literals.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ module.exports = {
2626
properties: {
2727
noStrings: {
2828
type: 'boolean'
29+
},
30+
allowedStrings: {
31+
type: 'array',
32+
uniqueItems: true,
33+
items: {
34+
type: 'string'
35+
}
2936
}
3037
},
3138
additionalProperties: false
@@ -34,6 +41,7 @@ module.exports = {
3441

3542
create(context) {
3643
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
44+
const allowedStrings = context.options[0] ? new Set(context.options[0].allowedStrings) : false;
3745

3846
const message = isNoStrings ?
3947
'Strings not allowed in JSX files' :
@@ -55,6 +63,9 @@ module.exports = {
5563
}
5664

5765
function getValidation(node) {
66+
if (allowedStrings && allowedStrings.has(node.value)) {
67+
return false;
68+
}
5869
const parent = getParentIgnoringBinaryExpressions(node);
5970
const standard = !/^[\s]+$/.test(node.value) &&
6071
typeof node.value === 'string' &&

tests/lib/rules/jsx-no-literals.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,36 @@ ruleTester.run('jsx-no-literals', rule, {
198198
}
199199
`,
200200
options: [{noStrings: true}]
201+
}, {
202+
code: `
203+
class Comp1 extends Component {
204+
render() {
205+
return <div>asdf</div>
206+
}
207+
}
208+
`,
209+
options: [{allowedStrings: ['asdf']}]
210+
},
211+
{
212+
code: `
213+
class Comp1 extends Component {
214+
render() {
215+
return <div>&nbsp;</div>
216+
}
217+
}
218+
`,
219+
options: [{noStrings: true, allowedStrings: ['&nbsp;']}]
220+
},
221+
{
222+
code: `
223+
class Comp1 extends Component {
224+
render() {
225+
return <div>foo: {bar}*</div>
226+
}
227+
}
228+
`,
229+
options: [{noStrings: true, allowedStrings: ['foo: ', '*']}]
201230
}
202-
203231
],
204232

205233
invalid: [
@@ -385,6 +413,19 @@ ruleTester.run('jsx-no-literals', rule, {
385413
{message: stringsMessage('\'foo\'')},
386414
{message: stringsMessage('`bar`')}
387415
]
416+
}, {
417+
code: `
418+
class Comp1 extends Component {
419+
render() {
420+
return <div bar={'foo'}>asdf</div>
421+
}
422+
}
423+
`,
424+
options: [{noStrings: true, allowedStrings: ['asd']}],
425+
errors: [
426+
{message: stringsMessage('\'foo\'')},
427+
{message: stringsMessage('asdf')}
428+
]
388429
}
389430
]
390431
});

0 commit comments

Comments
 (0)