UNPKG

19.1 kBMarkdownView Raw
1`eslint-plugin-react`
2===================
3
4[![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][deps-image]][deps-url] [![Code Climate][climate-image]][climate-url] [![Tidelift][tidelift-image]][tidelift-url]
5
6React specific linting rules for `eslint`
7
8# Installation
9
10Install [`eslint`](https://www.github.com/eslint/eslint) either locally or globally. (Note that locally, per project, is strongly preferred)
11
12```sh
13$ npm install eslint@7 --save-dev
14```
15
16If you installed `eslint` globally, you have to install the React plugin globally too. Otherwise, install it locally (strongly preferred)
17
18```sh
19$ npm install eslint-plugin-react --save-dev
20```
21
22# Configuration
23
24
25Use [our preset](#recommended) to get reasonable defaults:
26
27```json
28 "extends": [
29 "eslint:recommended",
30 "plugin:react/recommended"
31 ]
32```
33
34If you are using the [new JSX transform from React 17](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html#removing-unused-react-imports), extend [`react/jsx-runtime`](https://github.com/yannickcr/eslint-plugin-react/blob/HEAD/index.js#L163-L176) in your eslint config (add `"plugin:react/jsx-runtime"` to `"extends"`) to disable the relevant rules.
35
36You should also specify settings that will be shared across all the plugin rules. ([More about eslint shared settings](https://eslint.org/docs/user-guide/configuring/configuration-files#adding-shared-settings))
37
38```json5
39{
40 "settings": {
41 "react": {
42 "createClass": "createReactClass", // Regex for Component Factory to use,
43 // default to "createReactClass"
44 "pragma": "React", // Pragma to use, default to "React"
45 "fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
46 "version": "detect", // React version. "detect" automatically picks the version you have installed.
47 // You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
48 // default to latest and warns if missing
49 // It will default to "detect" in the future
50 "flowVersion": "0.53" // Flow version
51 },
52 "propWrapperFunctions": [
53 // The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
54 "forbidExtraProps",
55 {"property": "freeze", "object": "Object"},
56 {"property": "myFavoriteWrapper"},
57 // for rules that check exact prop wrappers
58 {"property": "forbidExtraProps", "exact": true}
59 ],
60 "componentWrapperFunctions": [
61 // The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn't set, components wrapped by these functions will be skipped.
62 "observer", // `property`
63 {"property": "styled"}, // `object` is optional
64 {"property": "observer", "object": "Mobx"},
65 {"property": "observer", "object": "<pragma>"} // sets `object` to whatever value `settings.react.pragma` is set to
66 ],
67 "formComponents": [
68 // Components used as alternatives to <form> for forms, eg. <Form endpoint={ url } />
69 "CustomForm",
70 {"name": "Form", "formAttribute": "endpoint"}
71 ],
72 "linkComponents": [
73 // Components used as alternatives to <a> for linking, eg. <Link to={ url } />
74 "Hyperlink",
75 {"name": "Link", "linkAttribute": "to"}
76 ]
77 }
78}
79```
80
81If you do not use a preset you will need to specify individual rules and add extra configuration.
82
83Add "react" to the plugins section.
84
85```json
86{
87 "plugins": [
88 "react"
89 ]
90}
91```
92
93Enable JSX support.
94
95With `eslint` 2+
96
97```json
98{
99 "parserOptions": {
100 "ecmaFeatures": {
101 "jsx": true
102 }
103 }
104}
105```
106
107Enable the rules that you would like to use.
108
109```json
110 "rules": {
111 "react/jsx-uses-react": "error",
112 "react/jsx-uses-vars": "error",
113 }
114```
115
116# List of supported rules
117
118✔: Enabled in the [`recommended`](#recommended) configuration.\
119🔧: Fixable with [`eslint --fix`](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).
120
121<!-- AUTO-GENERATED-CONTENT:START (BASIC_RULES) -->
122| ✔ | 🔧 | Rule | Description |
123| :---: | :---: | :--- | :--- |
124| | | [react/boolean-prop-naming](docs/rules/boolean-prop-naming.md) | Enforces consistent naming for boolean props |
125| | | [react/button-has-type](docs/rules/button-has-type.md) | Forbid "button" element without an explicit "type" attribute |
126| | | [react/default-props-match-prop-types](docs/rules/default-props-match-prop-types.md) | Enforce all defaultProps are defined and not "required" in propTypes. |
127| | | [react/destructuring-assignment](docs/rules/destructuring-assignment.md) | Enforce consistent usage of destructuring assignment of props, state, and context |
128| ✔ | | [react/display-name](docs/rules/display-name.md) | Prevent missing displayName in a React component definition |
129| | | [react/forbid-component-props](docs/rules/forbid-component-props.md) | Forbid certain props on components |
130| | | [react/forbid-dom-props](docs/rules/forbid-dom-props.md) | Forbid certain props on DOM Nodes |
131| | | [react/forbid-elements](docs/rules/forbid-elements.md) | Forbid certain elements |
132| | | [react/forbid-foreign-prop-types](docs/rules/forbid-foreign-prop-types.md) | Forbid using another component's propTypes |
133| | | [react/forbid-prop-types](docs/rules/forbid-prop-types.md) | Forbid certain propTypes |
134| | 🔧 | [react/function-component-definition](docs/rules/function-component-definition.md) | Standardize the way function component get defined |
135| | | [react/no-access-state-in-setstate](docs/rules/no-access-state-in-setstate.md) | Reports when this.state is accessed within setState |
136| | | [react/no-adjacent-inline-elements](docs/rules/no-adjacent-inline-elements.md) | Prevent adjacent inline elements not separated by whitespace. |
137| | | [react/no-array-index-key](docs/rules/no-array-index-key.md) | Prevent usage of Array index in keys |
138| | 🔧 | [react/no-arrow-function-lifecycle](docs/rules/no-arrow-function-lifecycle.md) | Lifecycle methods should be methods on the prototype, not class fields |
139| ✔ | | [react/no-children-prop](docs/rules/no-children-prop.md) | Prevent passing of children as props. |
140| | | [react/no-danger](docs/rules/no-danger.md) | Prevent usage of dangerous JSX props |
141| ✔ | | [react/no-danger-with-children](docs/rules/no-danger-with-children.md) | Report when a DOM element is using both children and dangerouslySetInnerHTML |
142| ✔ | | [react/no-deprecated](docs/rules/no-deprecated.md) | Prevent usage of deprecated methods |
143| | | [react/no-did-mount-set-state](docs/rules/no-did-mount-set-state.md) | Prevent usage of setState in componentDidMount |
144| | | [react/no-did-update-set-state](docs/rules/no-did-update-set-state.md) | Prevent usage of setState in componentDidUpdate |
145| ✔ | | [react/no-direct-mutation-state](docs/rules/no-direct-mutation-state.md) | Prevent direct mutation of this.state |
146| ✔ | | [react/no-find-dom-node](docs/rules/no-find-dom-node.md) | Prevent usage of findDOMNode |
147| | 🔧 | [react/no-invalid-html-attribute](docs/rules/no-invalid-html-attribute.md) | Forbid attribute with an invalid values` |
148| ✔ | | [react/no-is-mounted](docs/rules/no-is-mounted.md) | Prevent usage of isMounted |
149| | | [react/no-multi-comp](docs/rules/no-multi-comp.md) | Prevent multiple component definition per file |
150| | | [react/no-namespace](docs/rules/no-namespace.md) | Enforce that namespaces are not used in React elements |
151| | | [react/no-redundant-should-component-update](docs/rules/no-redundant-should-component-update.md) | Flag shouldComponentUpdate when extending PureComponent |
152| ✔ | | [react/no-render-return-value](docs/rules/no-render-return-value.md) | Prevent usage of the return value of React.render |
153| | | [react/no-set-state](docs/rules/no-set-state.md) | Prevent usage of setState |
154| ✔ | | [react/no-string-refs](docs/rules/no-string-refs.md) | Prevent string definitions for references and prevent referencing this.refs |
155| | | [react/no-this-in-sfc](docs/rules/no-this-in-sfc.md) | Report "this" being used in stateless components |
156| | | [react/no-typos](docs/rules/no-typos.md) | Prevent common typos |
157| ✔ | | [react/no-unescaped-entities](docs/rules/no-unescaped-entities.md) | Detect unescaped HTML entities, which might represent malformed tags |
158| ✔ | 🔧 | [react/no-unknown-property](docs/rules/no-unknown-property.md) | Prevent usage of unknown DOM property |
159| | | [react/no-unsafe](docs/rules/no-unsafe.md) | Prevent usage of unsafe lifecycle methods |
160| | | [react/no-unstable-nested-components](docs/rules/no-unstable-nested-components.md) | Prevent creating unstable components inside components |
161| | | [react/no-unused-class-component-methods](docs/rules/no-unused-class-component-methods.md) | Prevent declaring unused methods of component class |
162| | | [react/no-unused-prop-types](docs/rules/no-unused-prop-types.md) | Prevent definitions of unused prop types |
163| | | [react/no-unused-state](docs/rules/no-unused-state.md) | Prevent definition of unused state fields |
164| | | [react/no-will-update-set-state](docs/rules/no-will-update-set-state.md) | Prevent usage of setState in componentWillUpdate |
165| | | [react/prefer-es6-class](docs/rules/prefer-es6-class.md) | Enforce ES5 or ES6 class for React Components |
166| | | [react/prefer-exact-props](docs/rules/prefer-exact-props.md) | Prefer exact proptype definitions |
167| | 🔧 | [react/prefer-read-only-props](docs/rules/prefer-read-only-props.md) | Require read-only props. |
168| | | [react/prefer-stateless-function](docs/rules/prefer-stateless-function.md) | Enforce stateless components to be written as a pure function |
169| ✔ | | [react/prop-types](docs/rules/prop-types.md) | Prevent missing props validation in a React component definition |
170| ✔ | | [react/react-in-jsx-scope](docs/rules/react-in-jsx-scope.md) | Prevent missing React when using JSX |
171| | | [react/require-default-props](docs/rules/require-default-props.md) | Enforce a defaultProps definition for every prop that is not a required prop. |
172| | | [react/require-optimization](docs/rules/require-optimization.md) | Enforce React components to have a shouldComponentUpdate method |
173| ✔ | | [react/require-render-return](docs/rules/require-render-return.md) | Enforce ES5 or ES6 class for returning value in render function |
174| | 🔧 | [react/self-closing-comp](docs/rules/self-closing-comp.md) | Prevent extra closing tags for components without children |
175| | | [react/sort-comp](docs/rules/sort-comp.md) | Enforce component methods order |
176| | | [react/sort-prop-types](docs/rules/sort-prop-types.md) | Enforce propTypes declarations alphabetical sorting |
177| | | [react/state-in-constructor](docs/rules/state-in-constructor.md) | State initialization in an ES6 class component should be in a constructor |
178| | | [react/static-property-placement](docs/rules/static-property-placement.md) | Defines where React component static properties should be positioned. |
179| | | [react/style-prop-object](docs/rules/style-prop-object.md) | Enforce style prop value is an object |
180| | | [react/void-dom-elements-no-children](docs/rules/void-dom-elements-no-children.md) | Prevent passing of children to void DOM elements (e.g. `<br />`). |
181<!-- AUTO-GENERATED-CONTENT:END -->
182
183## JSX-specific rules
184
185<!-- AUTO-GENERATED-CONTENT:START (JSX_RULES) -->
186| ✔ | 🔧 | Rule | Description |
187| :---: | :---: | :--- | :--- |
188| | 🔧 | [react/jsx-boolean-value](docs/rules/jsx-boolean-value.md) | Enforce boolean attributes notation in JSX |
189| | | [react/jsx-child-element-spacing](docs/rules/jsx-child-element-spacing.md) | Ensures inline tags are not rendered without spaces between them |
190| | 🔧 | [react/jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md) | Validate closing bracket location in JSX |
191| | 🔧 | [react/jsx-closing-tag-location](docs/rules/jsx-closing-tag-location.md) | Validate closing tag location for multiline JSX |
192| | 🔧 | [react/jsx-curly-brace-presence](docs/rules/jsx-curly-brace-presence.md) | Disallow unnecessary JSX expressions when literals alone are sufficient or enfore JSX expressions on literals in JSX children or attributes |
193| | 🔧 | [react/jsx-curly-newline](docs/rules/jsx-curly-newline.md) | Enforce consistent line breaks inside jsx curly |
194| | 🔧 | [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md) | Enforce or disallow spaces inside of curly braces in JSX attributes |
195| | 🔧 | [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md) | Disallow or enforce spaces around equal signs in JSX attributes |
196| | | [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md) | Restrict file extensions that may contain JSX |
197| | 🔧 | [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md) | Ensure proper position of the first property in JSX |
198| | 🔧 | [react/jsx-fragments](docs/rules/jsx-fragments.md) | Enforce shorthand or standard form for React fragments |
199| | | [react/jsx-handler-names](docs/rules/jsx-handler-names.md) | Enforce event handler naming conventions in JSX |
200| | 🔧 | [react/jsx-indent](docs/rules/jsx-indent.md) | Validate JSX indentation |
201| | 🔧 | [react/jsx-indent-props](docs/rules/jsx-indent-props.md) | Validate props indentation in JSX |
202| ✔ | | [react/jsx-key](docs/rules/jsx-key.md) | Report missing `key` props in iterators/collection literals |
203| | | [react/jsx-max-depth](docs/rules/jsx-max-depth.md) | Validate JSX maximum depth |
204| | 🔧 | [react/jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md) | Limit maximum of props on a single line in JSX |
205| | 🔧 | [react/jsx-newline](docs/rules/jsx-newline.md) | Require or prevent a new line after jsx elements and expressions. |
206| | | [react/jsx-no-bind](docs/rules/jsx-no-bind.md) | Prevents usage of Function.prototype.bind and arrow functions in React component props |
207| ✔ | | [react/jsx-no-comment-textnodes](docs/rules/jsx-no-comment-textnodes.md) | Comments inside children section of tag should be placed inside braces |
208| | | [react/jsx-no-constructed-context-values](docs/rules/jsx-no-constructed-context-values.md) | Prevents JSX context provider values from taking values that will cause needless rerenders. |
209| ✔ | | [react/jsx-no-duplicate-props](docs/rules/jsx-no-duplicate-props.md) | Enforce no duplicate props |
210| | | [react/jsx-no-literals](docs/rules/jsx-no-literals.md) | Prevent using string literals in React component definition |
211| | | [react/jsx-no-script-url](docs/rules/jsx-no-script-url.md) | Forbid `javascript:` URLs |
212| ✔ | 🔧 | [react/jsx-no-target-blank](docs/rules/jsx-no-target-blank.md) | Forbid `target="_blank"` attribute without `rel="noreferrer"` |
213| ✔ | | [react/jsx-no-undef](docs/rules/jsx-no-undef.md) | Disallow undeclared variables in JSX |
214| | 🔧 | [react/jsx-no-useless-fragment](docs/rules/jsx-no-useless-fragment.md) | Disallow unnecessary fragments |
215| | 🔧 | [react/jsx-one-expression-per-line](docs/rules/jsx-one-expression-per-line.md) | Limit to one expression per line in JSX |
216| | | [react/jsx-pascal-case](docs/rules/jsx-pascal-case.md) | Enforce PascalCase for user-defined JSX components |
217| | 🔧 | [react/jsx-props-no-multi-spaces](docs/rules/jsx-props-no-multi-spaces.md) | Disallow multiple spaces between inline JSX props |
218| | | [react/jsx-props-no-spreading](docs/rules/jsx-props-no-spreading.md) | Prevent JSX prop spreading |
219| | | [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md) | Enforce default props alphabetical sorting |
220| | 🔧 | [react/jsx-sort-props](docs/rules/jsx-sort-props.md) | Enforce props alphabetical sorting |
221| | 🔧 | [react/jsx-space-before-closing](docs/rules/jsx-space-before-closing.md) | Validate spacing before closing bracket in JSX |
222| | 🔧 | [react/jsx-tag-spacing](docs/rules/jsx-tag-spacing.md) | Validate whitespace in and around the JSX opening and closing brackets |
223| ✔ | | [react/jsx-uses-react](docs/rules/jsx-uses-react.md) | Prevent React to be marked as unused |
224| ✔ | | [react/jsx-uses-vars](docs/rules/jsx-uses-vars.md) | Prevent variables used in JSX to be marked as unused |
225| | 🔧 | [react/jsx-wrap-multilines](docs/rules/jsx-wrap-multilines.md) | Prevent missing parentheses around multilines JSX |
226<!-- AUTO-GENERATED-CONTENT:END -->
227
228## Other useful plugins
229
230- Rules of Hooks: [eslint-plugin-react-hooks](https://github.com/facebook/react/tree/master/packages/eslint-plugin-react-hooks)
231- JSX accessibility: [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y)
232- React Native: [eslint-plugin-react-native](https://github.com/Intellicode/eslint-plugin-react-native)
233
234# Shareable configurations
235
236## Recommended
237
238This plugin exports a `recommended` configuration that enforces React good practices.
239
240To enable this configuration use the `extends` property in your `.eslintrc` config file:
241
242```json
243{
244 "extends": ["eslint:recommended", "plugin:react/recommended"]
245}
246```
247
248See [`eslint` documentation](https://eslint.org/docs/user-guide/configuring/configuration-files#extending-configuration-files) for more information about extending configuration files.
249
250## All
251
252This plugin also exports an `all` configuration that includes every available rule.
253This pairs well with the `eslint:all` rule.
254
255```json
256{
257 "plugins": [
258 "react"
259 ],
260 "extends": ["eslint:all", "plugin:react/all"]
261}
262```
263
264**Note**: These configurations will import `eslint-plugin-react` and enable JSX in [parser options](http://eslint.org/docs/user-guide/configuring#specifying-parser-options).
265
266# License
267
268`eslint-plugin-react` is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
269
270
271[npm-url]: https://npmjs.org/package/eslint-plugin-react
272[npm-image]: https://img.shields.io/npm/v/eslint-plugin-react.svg
273
274[travis-url]: https://travis-ci.org/yannickcr/eslint-plugin-react
275[travis-image]: https://img.shields.io/travis/yannickcr/eslint-plugin-react/master.svg
276
277[deps-url]: https://david-dm.org/yannickcr/eslint-plugin-react
278[deps-image]: https://img.shields.io/david/dev/yannickcr/eslint-plugin-react.svg
279
280[climate-url]: https://codeclimate.com/github/yannickcr/eslint-plugin-react
281[climate-image]: https://img.shields.io/codeclimate/maintainability/yannickcr/eslint-plugin-react.svg
282
283[status-url]: https://github.com/yannickcr/eslint-plugin-react/pulse
284[status-image]: https://img.shields.io/github/last-commit/yannickcr/eslint-plugin-react.svg
285
286[tidelift-url]: https://tidelift.com/subscription/pkg/npm-eslint-plugin-react?utm_source=npm-eslint-plugin-react&utm_medium=referral&utm_campaign=readme
287[tidelift-image]: https://tidelift.com/badges/github/yannickcr/eslint-plugin-react?style=flat