Skip to content

Commit 7fa31df

Browse files
cainlevyljharb
authored andcommitted
[Fix] jsx-no-literals: trim whitespace for allowedStrings check
1 parent 2a595fd commit 7fa31df

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

docs/rules/jsx-no-literals.md

+24-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# Prevent usage of string literals in JSX (react/jsx-no-literals)
22

3-
There are a couple of scenarios where you want to avoid string literals in JSX. Either to enforce consistency and reducing strange behaviour, or for enforcing that literals aren't kept in JSX so they can be translated.
3+
There are a few scenarios where you want to avoid string literals in JSX. You may want to enforce consistency, reduce syntax highlighting issues, or ensure that strings are part of a translation system.
44

55
## Rule Details
66

7-
In JSX when using a literal string you can wrap it in a JSX container `{'TEXT'}`. This rules by default requires that you wrap all literal strings.
8-
Prevents any odd artifacts of highlighters if your unwrapped string contains an enclosing character like `'` in contractions and enforces consistency.
7+
By default this rule requires that you wrap all literal strings in a JSX container `{'TEXT'}`.
98

109
The following patterns are considered warnings:
1110

@@ -19,14 +18,20 @@ The following patterns are **not** considered warnings:
1918
var Hello = <div>{'test'}</div>;
2019
```
2120

22-
### Options
21+
```jsx
22+
var Hello = <div>
23+
{'test'}
24+
</div>;
25+
```
26+
27+
## Rule Options
2328

2429
There are two options:
2530

2631
* `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.
32+
* `allowedStrings` - An array of unique string values that would otherwise warn, but will be ignored.
2833

29-
To use, you can specify like the following:
34+
To use, you can specify as follows:
3035

3136
```js
3237
"react/jsx-no-literals": [<enabled>, {"noStrings": true, "allowedStrings": ["allowed"]}]
@@ -42,6 +47,12 @@ var Hello = <div>test</div>;
4247
var Hello = <div>{'test'}</div>;
4348
```
4449

50+
```jsx
51+
var Hello = <div>
52+
{'test'}
53+
</div>;
54+
```
55+
4556
The following are **not** considered warnings:
4657

4758
```jsx
@@ -59,6 +70,13 @@ var Hello = <div>{translate('my.translation.key')}</div>
5970
var Hello = <div>allowed</div>
6071
```
6172

73+
```jsx
74+
// an allowed string surrounded by only whitespace
75+
var Hello = <div>
76+
allowed
77+
</div>;
78+
```
79+
6280
## When Not To Use It
6381

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

lib/rules/jsx-no-literals.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,15 @@ module.exports = {
4040
},
4141

4242
create(context) {
43-
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
44-
const allowedStrings = context.options[0] ? new Set(context.options[0].allowedStrings) : false;
43+
function trimIfString(val) {
44+
return typeof val === 'string' ? val.trim() : val;
45+
}
46+
47+
const defaults = {noStrings: false, allowedStrings: []};
48+
const config = Object.assign({}, defaults, context.options[0] || {});
49+
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));
4550

46-
const message = isNoStrings ?
51+
const message = config.noStrings ?
4752
'Strings not allowed in JSX files' :
4853
'Missing JSX expression container around literal string';
4954

@@ -63,15 +68,15 @@ module.exports = {
6368
}
6469

6570
function getValidation(node) {
66-
if (allowedStrings && allowedStrings.has(node.value)) {
71+
if (config.allowedStrings.has(trimIfString(node.value))) {
6772
return false;
6873
}
6974
const parent = getParentIgnoringBinaryExpressions(node);
7075
const standard = !/^[\s]+$/.test(node.value) &&
7176
typeof node.value === 'string' &&
7277
parent.type.indexOf('JSX') !== -1 &&
7378
parent.type !== 'JSXAttribute';
74-
if (isNoStrings) {
79+
if (config.noStrings) {
7580
return standard;
7681
}
7782
return standard && parent.type !== 'JSXExpressionContainer';
@@ -97,7 +102,7 @@ module.exports = {
97102

98103
TemplateLiteral(node) {
99104
const parent = getParentIgnoringBinaryExpressions(node);
100-
if (isNoStrings && parent.type === 'JSXExpressionContainer') {
105+
if (config.noStrings && parent.type === 'JSXExpressionContainer') {
101106
reportLiteralNode(node);
102107
}
103108
}

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

+33
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ ruleTester.run('jsx-no-literals', rule, {
207207
}
208208
`,
209209
options: [{allowedStrings: ['asdf']}]
210+
}, {
211+
code: `
212+
class Comp1 extends Component {
213+
render() {
214+
return <div>asdf</div>
215+
}
216+
}
217+
`,
218+
options: [{noStrings: false, allowedStrings: ['asdf']}]
210219
},
211220
{
212221
code: `
@@ -218,6 +227,20 @@ ruleTester.run('jsx-no-literals', rule, {
218227
`,
219228
options: [{noStrings: true, allowedStrings: ['&nbsp;']}]
220229
},
230+
{
231+
code: `
232+
class Comp1 extends Component {
233+
render() {
234+
return (
235+
<div>
236+
&nbsp;
237+
</div>
238+
);
239+
}
240+
}
241+
`,
242+
options: [{noStrings: true, allowedStrings: ['&nbsp;']}]
243+
},
221244
{
222245
code: `
223246
class Comp1 extends Component {
@@ -227,6 +250,16 @@ ruleTester.run('jsx-no-literals', rule, {
227250
}
228251
`,
229252
options: [{noStrings: true, allowedStrings: ['foo: ', '*']}]
253+
},
254+
{
255+
code: `
256+
class Comp1 extends Component {
257+
render() {
258+
return <div>foo</div>
259+
}
260+
}
261+
`,
262+
options: [{noStrings: true, allowedStrings: [' foo ']}]
230263
}
231264
],
232265

0 commit comments

Comments
 (0)