Skip to content

Commit 153eac8

Browse files
tanmoyopenrootljharb
authored andcommitted
[New] jsx-no-literals: add noAttributeStrings option
Co-authored-by: TaLea Carpenter <[email protected]> Co-authored-by: tanmoyopenroot <[email protected]> Fixes ##2486
1 parent 7434f19 commit 153eac8

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Added
99
* [`button-has-type`]: support trivial ternary expressions ([#2748][] @Hypnosphi)
1010
* [`jsx-handler-names`]: add `checkInlineFunction` option ([#2761][] @dididy)
11+
* [`jsx-no-literals`]: add `noAttributeStrings` option ([#2782][] @TaLeaMonet)
1112

1213
### Fixed
1314
* [`function-component-definition`]: ignore object properties ([#2771][] @stefan-wullems)
@@ -21,6 +22,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
2122
[#2796]: https://github.com/yannickcr/eslint-plugin-react/pull/2796
2223
[#2791]: https://github.com/yannickcr/eslint-plugin-react/pull/2791
2324
[#2789]: https://github.com/yannickcr/eslint-plugin-react/pull/2789
25+
[#2782]: https://github.com/yannickcr/eslint-plugin-react/pull/2782
2426
[#2780]: https://github.com/yannickcr/eslint-plugin-react/pull/2780
2527
[#2779]: https://github.com/yannickcr/eslint-plugin-react/pull/2779
2628
[#2772]: https://github.com/yannickcr/eslint-plugin-react/pull/2772

docs/rules/jsx-no-literals.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ var Hello = <div>
2626

2727
## Rule Options
2828

29-
There are two options:
29+
The supported options are:
3030

3131
* `noStrings` (default: `false`) - Enforces no string literals used as children, wrapped or unwrapped.
3232
* `allowedStrings` - An array of unique string values that would otherwise warn, but will be ignored.
3333
* `ignoreProps` (default: `false`) - When `true` the rule ignores literals used in props, wrapped or unwrapped.
34+
* `noAttributeStrings` (default: `false`) - Enforces no string literals used in attributes when set to `true`.
3435

3536
To use, you can specify as follows:
3637

3738
```js
38-
"react/jsx-no-literals": [<enabled>, {"noStrings": true, "allowedStrings": ["allowed"], "ignoreProps": false}]
39+
"react/jsx-no-literals": [<enabled>, {"noStrings": true, "allowedStrings": ["allowed"], "ignoreProps": false, "noAttributeStrings": true }]
3940
```
4041

4142
In this configuration, the following are considered warnings:
@@ -54,6 +55,12 @@ var Hello = <div>
5455
</div>;
5556
```
5657

58+
```jsx
59+
var Hello = <div>
60+
<img alt="test"> </img>
61+
</div>;
62+
```
63+
5764
```jsx
5865
var Hello = <div class='xx' />;
5966
```
@@ -67,6 +74,7 @@ var Hello = <div class={`xx`} />;
6774
```
6875

6976

77+
7078
The following are **not** considered warnings:
7179

7280
```jsx
@@ -91,6 +99,13 @@ var Hello = <div>
9199
</div>;
92100
```
93101

102+
```jsx
103+
// a string value stored within a variable used as an attribute's value
104+
var Hello = <div>
105+
<img alt={imageDescription} {...props} />
106+
</div>;
107+
```
108+
94109
```jsx
95110
// spread props object
96111
var Hello = <Text {...props} />

lib/rules/jsx-no-literals.js

+35-9
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,37 @@ module.exports = {
4040
},
4141
ignoreProps: {
4242
type: 'boolean'
43+
},
44+
noAttributeStrings: {
45+
type: 'boolean'
4346
}
4447
},
4548
additionalProperties: false
4649
}]
4750
},
4851

4952
create(context) {
50-
const defaults = {noStrings: false, allowedStrings: [], ignoreProps: false};
53+
const defaults = {
54+
noStrings: false,
55+
allowedStrings: [],
56+
ignoreProps: false,
57+
noAttributeStrings: false
58+
};
5159
const config = Object.assign({}, defaults, context.options[0] || {});
5260
config.allowedStrings = new Set(config.allowedStrings.map(trimIfString));
5361

54-
const message = config.noStrings
55-
? 'Strings not allowed in JSX files'
56-
: 'Missing JSX expression container around literal string';
62+
function defaultMessage() {
63+
if (config.noAttributeStrings) {
64+
return 'Strings not allowed in attributes';
65+
}
66+
if (config.noStrings) {
67+
return 'Strings not allowed in JSX files';
68+
}
69+
return 'Missing JSX expression container around literal string';
70+
}
5771

5872
function reportLiteralNode(node, customMessage) {
59-
const errorMessage = customMessage || message;
73+
const errorMessage = customMessage || defaultMessage();
6074

6175
context.report({
6276
node,
@@ -77,10 +91,22 @@ module.exports = {
7791
return false;
7892
}
7993
const parent = getParentIgnoringBinaryExpressions(node);
80-
const standard = !/^[\s]+$/.test(node.value)
81-
&& typeof node.value === 'string'
82-
&& parent.type.indexOf('JSX') !== -1
83-
&& parent.type !== 'JSXAttribute';
94+
95+
function isParentNodeStandard() {
96+
if (!/^[\s]+$/.test(node.value) && typeof node.value === 'string' && parent.type.includes('JSX')) {
97+
if (config.noAttributeStrings) {
98+
return parent.type === 'JSXAttribute';
99+
}
100+
if (!config.noAttributeStrings) {
101+
return parent.type !== 'JSXAttribute';
102+
}
103+
}
104+
105+
return false;
106+
}
107+
108+
const standard = isParentNodeStandard();
109+
84110
if (config.noStrings) {
85111
return standard;
86112
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ function invalidProp(str) {
3939
return `Invalid prop value: “${str}”`;
4040
}
4141

42+
function attributeMessage(str) {
43+
return `Strings not allowed in attributes: “${str}”`;
44+
}
45+
4246
const ruleTester = new RuleTester({parserOptions});
4347
ruleTester.run('jsx-no-literals', rule, {
4448

@@ -331,6 +335,11 @@ ruleTester.run('jsx-no-literals', rule, {
331335
} `,
332336
parser: parsers.BABEL_ESLINT,
333337
options: [{noStrings: true, ignoreProps: false}]
338+
},
339+
{
340+
code: `
341+
<img alt='blank image'></img>
342+
`
334343
}
335344
),
336345

@@ -527,6 +536,14 @@ ruleTester.run('jsx-no-literals', rule, {
527536
errors: [
528537
{message: stringsMessage('\'bar\'')}
529538
]
539+
},
540+
{
541+
code: `
542+
<img alt='blank image'></img>
543+
`,
544+
options: [{noAttributeStrings: true}],
545+
errors: [
546+
{message: attributeMessage('\'blank image\'')}]
530547
}
531548
]
532549
});

0 commit comments

Comments
 (0)