Skip to content

Commit 31e55d2

Browse files
authored
Merge pull request #1202 from deecewan/master
Allow for stricter no-literals rule
2 parents 4a35536 + 8a2bc81 commit 31e55d2

File tree

3 files changed

+129
-13
lines changed

3 files changed

+129
-13
lines changed

docs/rules/jsx-no-literals.md

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# Prevent usage of unwrapped JSX strings (react/jsx-no-literals)
1+
# Prevent usage of string literals in JSX (react/jsx-no-literals)
22

3-
In JSX when using a literal string you can wrap it in a JSX container `{'TEXT'}`.
4-
This rules requires that you wrap all literal strings.
5-
Prevents any odd artifacts of highlighters if your unwrapped string contains an enclosing character like `'` in contractions and enforces consistency.
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.
64

75
## Rule Details
86

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.
9+
910
The following patterns are considered warnings:
1011

1112
```jsx
@@ -18,6 +19,40 @@ The following patterns are not considered warnings:
1819
var Hello = <div>{'test'}</div>;
1920
```
2021

22+
### Options
23+
24+
There is only one option:
25+
26+
* `noStrings` - Enforces no string literals used as children, wrapped or unwrapped.
27+
28+
To use, you can specify like the following:
29+
30+
```json
31+
"react/jsx-no-literals": [{"noStrings": true}]
32+
```
33+
34+
In this configuration, the following are considered warnings:
35+
36+
```jsx
37+
var Hello = <div>test</div>;
38+
```
39+
40+
```jsx
41+
var Hello = <div>{'test'}</div>;
42+
```
43+
44+
The following are not considered warnings:
45+
46+
```jsx
47+
// When using something like `react-intl`
48+
var Hello = <div><Text {...message} /></div>
49+
```
50+
51+
```jsx
52+
// When using something similar to Rails translations
53+
var Hello = <div>{translate('my.translation.key')}</div>
54+
```
55+
2156
## When Not To Use It
2257

2358
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: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Prevent using string literals in React component definition
33
* @author Caleb Morris
4+
* @author David Buchan-Swanson
45
*/
56
'use strict';
67

@@ -18,33 +19,48 @@ module.exports = {
1819

1920
schema: [{
2021
type: 'object',
21-
properties: {},
22+
properties: {
23+
noStrings: {
24+
type: 'boolean'
25+
}
26+
},
2227
additionalProperties: false
2328
}]
2429
},
2530

2631
create: function(context) {
32+
const isNoStrings = context.options[0] ? context.options[0].noStrings : false;
33+
34+
const message = isNoStrings ?
35+
'Strings not allowed in JSX files' :
36+
'Missing JSX expression container around literal string';
37+
2738
function reportLiteralNode(node) {
2839
context.report({
2940
node: node,
30-
message: 'Missing JSX expression container around literal string'
41+
message: message
3142
});
3243
}
3344

45+
function getValidation(node) {
46+
const standard = !/^[\s]+$/.test(node.value) &&
47+
node.parent &&
48+
node.parent.type.indexOf('JSX') !== -1 &&
49+
node.parent.type !== 'JSXAttribute';
50+
if (isNoStrings) {
51+
return standard;
52+
}
53+
return standard && node.parent.type !== 'JSXExpressionContainer';
54+
}
55+
3456
// --------------------------------------------------------------------------
3557
// Public
3658
// --------------------------------------------------------------------------
3759

3860
return {
3961

4062
Literal: function(node) {
41-
if (
42-
!/^[\s]+$/.test(node.value) &&
43-
node.parent &&
44-
node.parent.type !== 'JSXExpressionContainer' &&
45-
node.parent.type !== 'JSXAttribute' &&
46-
node.parent.type.indexOf('JSX') !== -1
47-
) {
63+
if (getValidation(node)) {
4864
reportLiteralNode(node);
4965
}
5066
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @fileoverview Prevent using unwrapped literals in a React component definition
33
* @author Caleb morris
4+
* @author David Buchan-Swanson
45
*/
56
'use strict';
67

@@ -109,6 +110,36 @@ ruleTester.run('jsx-no-literals', rule, {
109110
'</Foo>'
110111
].join('\n'),
111112
parser: 'babel-eslint'
113+
}, {
114+
code: `
115+
<Foo bar="test">
116+
{intl.formatText(message)}
117+
</Foo>
118+
`,
119+
parser: 'babel-eslint',
120+
options: [{noStrings: true}]
121+
}, {
122+
code: `
123+
<Foo bar="test">
124+
{translate('my.translate.key')}
125+
</Foo>
126+
`,
127+
parser: 'babel-eslint',
128+
options: [{noStrings: true}]
129+
}, {
130+
code: `
131+
<Foo bar="test">
132+
{intl.formatText(message)}
133+
</Foo>
134+
`,
135+
options: [{noStrings: true}]
136+
}, {
137+
code: `
138+
<Foo bar="test">
139+
{translate('my.translate.key')}
140+
</Foo>
141+
`,
142+
options: [{noStrings: true}]
112143
}
113144
],
114145

@@ -202,6 +233,40 @@ ruleTester.run('jsx-no-literals', rule, {
202233
].join('\n'),
203234
parser: 'babel-eslint',
204235
errors: [{message: 'Missing JSX expression container around literal string'}]
236+
}, {
237+
code: `
238+
<Foo bar="test">
239+
{'Test'}
240+
</Foo>
241+
`,
242+
parser: 'babel-eslint',
243+
options: [{noStrings: true}],
244+
errors: [{message: 'Strings not allowed in JSX files'}]
245+
}, {
246+
code: `
247+
<Foo bar="test">
248+
{'Test'}
249+
</Foo>
250+
`,
251+
options: [{noStrings: true}],
252+
errors: [{message: 'Strings not allowed in JSX files'}]
253+
}, {
254+
code: `
255+
<Foo bar="test">
256+
Test
257+
</Foo>
258+
`,
259+
parser: 'babel-eslint',
260+
options: [{noStrings: true}],
261+
errors: [{message: 'Strings not allowed in JSX files'}]
262+
}, {
263+
code: `
264+
<Foo bar="test">
265+
Test
266+
</Foo>
267+
`,
268+
options: [{noStrings: true}],
269+
errors: [{message: 'Strings not allowed in JSX files'}]
205270
}
206271
]
207272
});

0 commit comments

Comments
 (0)