Skip to content

Commit 8fa7280

Browse files
Pearce-Ropionljharb
authored andcommitted
[New] jsx-no-literals Add elementOverrides option and the ability to ignore this rule on specific elements
1 parent 07503b7 commit 8fa7280

File tree

4 files changed

+1027
-89
lines changed

4 files changed

+1027
-89
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](https://keepachange
88

99
### Added
1010
* [`no-string-refs`]: allow this.refs in > 18.3.0 ([#3807][] @henryqdineen)
11+
* [`jsx-no-literals`] Add `elementOverrides` option and the ability to ignore this rule on specific elements ([#3812][] @Pearce-Ropion)
1112

1213
### Fixed
1314
* [`function-component-definition`], [`boolean-prop-naming`], [`jsx-first-prop-new-line`], [`jsx-props-no-multi-spaces`], `propTypes`: use type args ([#3629][] @HenryBrown0)
@@ -20,6 +21,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2021

2122
[#3632]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3632
2223

24+
[#3812]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3812
2325
[#3629]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3629
2426
[#3817]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3817
2527
[#3807]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3807

docs/rules/jsx-no-literals.md

+71
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,77 @@ The supported options are:
3434
- `allowedStrings` - An array of unique string values that would otherwise warn, but will be ignored.
3535
- `ignoreProps` (default: `false`) - When `true` the rule ignores literals used in props, wrapped or unwrapped.
3636
- `noAttributeStrings` (default: `false`) - Enforces no string literals used in attributes when set to `true`.
37+
- `elementOverrides` - An object where the keys are the element names and the
38+
values are objects with the same options as above. This allows you to specify
39+
different options for different elements.
40+
41+
### `elementOverrides`
42+
43+
The `elementOverrides` option allows you to specify different options for
44+
different elements. This is useful when you want to enforce different rules for
45+
different elements. For example, you may want to allow string literals in
46+
`Button` elements, but not in the rest of your application.
47+
48+
The element name only accepts component names. HTML element tag names are not supported.
49+
Component names are case-sensitive and should exactly match the name of
50+
the component as it is used in the JSX. It can also be the name of a compound
51+
component (ie. `Modal.Button`).
52+
53+
Specifying options creates a new context for the rule, so the rule will only
54+
apply the new options to the specified element and its children (if
55+
`applyToNestedElements` is `true` - see below). This means that the root rule options will
56+
not apply to the specified element.
57+
58+
In addition to the options above (`noStrings`, `allowedStrings`,
59+
`noAttributeStrings` and `ignoreProps`), you can also specify the the following options
60+
that are specific to `elementOverrides`:
61+
62+
- `allowElement` (default: `false`) - When `true` the rule will allow the
63+
specified element to have string literals as children, wrapped or unwrapped
64+
without warning.
65+
- `applyToNestedElements` (default: `true`) - When `false` the rule will not
66+
apply the current options set to nested elements. This is useful when you want
67+
to apply the rule to a specific element, but not to its children.
68+
69+
**Note**: As this rule has no way of differentiating
70+
between different componets with the same name, it is recommended to use this
71+
option with specific components that are unique to your application.
72+
73+
#### `elementOverrides` Examples
74+
75+
The following are **correct** examples that demonstrate how to use the `elementOverrides` option:
76+
77+
```js
78+
// "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Button": {"allowElement": true} }}]
79+
80+
var Hello = <div>{'test'}</div>;
81+
var World = <Button>test</Button>;
82+
```
83+
84+
```js
85+
// "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Text": {"allowElement": true} }}]
86+
87+
var World = <Text>Hello <a href="a">world</a></Text>;
88+
```
89+
90+
```js
91+
// "react/jsx-no-literals": [<enabled>, {"elementOverrides": { "Text": {"allowElement": true, "applyToNestedElements": false} }}]
92+
93+
var linkText = 'world';
94+
var World = <Text>Hello <a href="a">{linkText}</a></Text>;
95+
```
96+
97+
```js
98+
// "react/jsx-no-literals": [<enabled>, {"noStrings": true, "elementOverrides": { "Button": {"noStrings": false} }}]
99+
// OR
100+
// "react/jsx-no-literals": [<enabled>, {"noStrings": true, "elementOverrides": { "Button": {} }}]
101+
102+
var test = 'test'
103+
var Hello = <div>{test}</div>;
104+
var World = <Button>{'test'}</Button>;
105+
```
106+
107+
## Examples
37108

38109
To use, you can specify as follows:
39110

0 commit comments

Comments
 (0)