Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

README.md 16 KiB

3 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ESLint-plugin-React
  2. ===================
  3. [![Maintenance Status][status-image]][status-url] [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Build Status][appveyor-image]][appveyor-url] [![Dependency Status][deps-image]][deps-url] [![Coverage Status][coverage-image]][coverage-url] [![Code Climate][climate-image]][climate-url]
  4. React specific linting rules for ESLint
  5. # Installation
  6. Install [ESLint](https://www.github.com/eslint/eslint) either locally or globally.
  7. ```sh
  8. $ npm install eslint --save-dev
  9. ```
  10. If you installed `ESLint` globally, you have to install React plugin globally too. Otherwise, install it locally.
  11. ```sh
  12. $ npm install eslint-plugin-react --save-dev
  13. ```
  14. # Configuration
  15. Use [our preset](#recommended) to get reasonable defaults:
  16. ```json
  17. "extends": [
  18. "eslint:recommended",
  19. "plugin:react/recommended"
  20. ]
  21. ```
  22. You should also specify settings that will be shared across all the plugin rules.
  23. ```json5
  24. {
  25. "settings": {
  26. "react": {
  27. "createClass": "createReactClass", // Regex for Component Factory to use,
  28. // default to "createReactClass"
  29. "pragma": "React", // Pragma to use, default to "React"
  30. "version": "15.0", // React version, default to the latest React stable release
  31. "flowVersion": "0.53" // Flow version
  32. },
  33. "propWrapperFunctions": [ "forbidExtraProps" ] // The names of any functions used to wrap the
  34. // propTypes object, e.g. `forbidExtraProps`.
  35. // If this isn't set, any propTypes wrapped in
  36. // a function will be skipped.
  37. }
  38. }
  39. ```
  40. If you do not use a preset you will need to specify individual rules and add extra configuration.
  41. Add "react" to the plugins section.
  42. ```json
  43. {
  44. "plugins": [
  45. "react"
  46. ]
  47. }
  48. ```
  49. Enable JSX support.
  50. With ESLint 2+
  51. ```json
  52. {
  53. "parserOptions": {
  54. "ecmaFeatures": {
  55. "jsx": true
  56. }
  57. }
  58. }
  59. ```
  60. Enable the rules that you would like to use.
  61. ```json
  62. "rules": {
  63. "react/jsx-uses-react": "error",
  64. "react/jsx-uses-vars": "error",
  65. }
  66. ```
  67. # List of supported rules
  68. * [react/boolean-prop-naming](docs/rules/boolean-prop-naming.md): Enforces consistent naming for boolean props
  69. * [react/button-has-type](docs/rules/button-has-type.md): Forbid "button" element without an explicit "type" attribute
  70. * [react/default-props-match-prop-types](docs/rules/default-props-match-prop-types.md): Prevent extraneous defaultProps on components
  71. * [react/destructuring-assignment](docs/rules/destructuring-assignment.md): Rule enforces consistent usage of destructuring assignment in component
  72. * [react/display-name](docs/rules/display-name.md): Prevent missing `displayName` in a React component definition
  73. * [react/forbid-component-props](docs/rules/forbid-component-props.md): Forbid certain props on Components
  74. * [react/forbid-dom-props](docs/rules/forbid-dom-props.md): Forbid certain props on DOM Nodes
  75. * [react/forbid-elements](docs/rules/forbid-elements.md): Forbid certain elements
  76. * [react/forbid-prop-types](docs/rules/forbid-prop-types.md): Forbid certain propTypes
  77. * [react/forbid-foreign-prop-types](docs/rules/forbid-foreign-prop-types.md): Forbid foreign propTypes
  78. * [react/no-access-state-in-setstate](docs/rules/no-access-state-in-setstate.md): Prevent using this.state inside this.setState
  79. * [react/no-array-index-key](docs/rules/no-array-index-key.md): Prevent using Array index in `key` props
  80. * [react/no-children-prop](docs/rules/no-children-prop.md): Prevent passing children as props
  81. * [react/no-danger](docs/rules/no-danger.md): Prevent usage of dangerous JSX properties
  82. * [react/no-danger-with-children](docs/rules/no-danger-with-children.md): Prevent problem with children and props.dangerouslySetInnerHTML
  83. * [react/no-deprecated](docs/rules/no-deprecated.md): Prevent usage of deprecated methods, including component lifecyle methods
  84. * [react/no-did-mount-set-state](docs/rules/no-did-mount-set-state.md): Prevent usage of `setState` in `componentDidMount`
  85. * [react/no-did-update-set-state](docs/rules/no-did-update-set-state.md): Prevent usage of `setState` in `componentDidUpdate`
  86. * [react/no-direct-mutation-state](docs/rules/no-direct-mutation-state.md): Prevent direct mutation of `this.state`
  87. * [react/no-find-dom-node](docs/rules/no-find-dom-node.md): Prevent usage of `findDOMNode`
  88. * [react/no-is-mounted](docs/rules/no-is-mounted.md): Prevent usage of `isMounted`
  89. * [react/no-multi-comp](docs/rules/no-multi-comp.md): Prevent multiple component definition per file
  90. * [react/no-redundant-should-component-update](docs/rules/no-redundant-should-component-update.md): Prevent usage of `shouldComponentUpdate` when extending React.PureComponent
  91. * [react/no-render-return-value](docs/rules/no-render-return-value.md): Prevent usage of the return value of `React.render`
  92. * [react/no-set-state](docs/rules/no-set-state.md): Prevent usage of `setState`
  93. * [react/no-typos](docs/rules/no-typos.md): Prevent common casing typos
  94. * [react/no-string-refs](docs/rules/no-string-refs.md): Prevent using string references in `ref` attribute.
  95. * [react/no-this-in-sfc](docs/rules/no-this-in-sfc.md): Prevent using `this` in stateless functional components
  96. * [react/no-unescaped-entities](docs/rules/no-unescaped-entities.md): Prevent invalid characters from appearing in markup
  97. * [react/no-unknown-property](docs/rules/no-unknown-property.md): Prevent usage of unknown DOM property (fixable)
  98. * [react/no-unsafe](docs/rules/no-unsafe.md): Prevent usage of `UNSAFE_` methods
  99. * [react/no-unused-prop-types](docs/rules/no-unused-prop-types.md): Prevent definitions of unused prop types
  100. * [react/no-unused-state](docs/rules/no-unused-state.md): Prevent definitions of unused state properties
  101. * [react/no-will-update-set-state](docs/rules/no-will-update-set-state.md): Prevent usage of `setState` in `componentWillUpdate`
  102. * [react/prefer-es6-class](docs/rules/prefer-es6-class.md): Enforce ES5 or ES6 class for React Components
  103. * [react/prefer-stateless-function](docs/rules/prefer-stateless-function.md): Enforce stateless React Components to be written as a pure function
  104. * [react/prop-types](docs/rules/prop-types.md): Prevent missing props validation in a React component definition
  105. * [react/react-in-jsx-scope](docs/rules/react-in-jsx-scope.md): Prevent missing `React` when using JSX
  106. * [react/require-default-props](docs/rules/require-default-props.md): Enforce a defaultProps definition for every prop that is not a required prop
  107. * [react/require-optimization](docs/rules/require-optimization.md): Enforce React components to have a `shouldComponentUpdate` method
  108. * [react/require-render-return](docs/rules/require-render-return.md): Enforce ES5 or ES6 class for returning value in render function
  109. * [react/self-closing-comp](docs/rules/self-closing-comp.md): Prevent extra closing tags for components without children (fixable)
  110. * [react/sort-comp](docs/rules/sort-comp.md): Enforce component methods order (fixable)
  111. * [react/sort-prop-types](docs/rules/sort-prop-types.md): Enforce propTypes declarations alphabetical sorting
  112. * [react/style-prop-object](docs/rules/style-prop-object.md): Enforce style prop value being an object
  113. * [react/void-dom-elements-no-children](docs/rules/void-dom-elements-no-children.md): Prevent void DOM elements (e.g. `<img />`, `<br />`) from receiving children
  114. ## JSX-specific rules
  115. * [react/jsx-boolean-value](docs/rules/jsx-boolean-value.md): Enforce boolean attributes notation in JSX (fixable)
  116. * [react/jsx-child-element-spacing](docs/rules/jsx-child-element-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes and expressions.
  117. * [react/jsx-closing-bracket-location](docs/rules/jsx-closing-bracket-location.md): Validate closing bracket location in JSX (fixable)
  118. * [react/jsx-closing-tag-location](docs/rules/jsx-closing-tag-location.md): Validate closing tag location in JSX (fixable)
  119. * [react/jsx-curly-spacing](docs/rules/jsx-curly-spacing.md): Enforce or disallow spaces inside of curly braces in JSX attributes and expressions (fixable)
  120. * [react/jsx-equals-spacing](docs/rules/jsx-equals-spacing.md): Enforce or disallow spaces around equal signs in JSX attributes (fixable)
  121. * [react/jsx-filename-extension](docs/rules/jsx-filename-extension.md): Restrict file extensions that may contain JSX
  122. * [react/jsx-first-prop-new-line](docs/rules/jsx-first-prop-new-line.md): Enforce position of the first prop in JSX (fixable)
  123. * [react/jsx-handler-names](docs/rules/jsx-handler-names.md): Enforce event handler naming conventions in JSX
  124. * [react/jsx-indent](docs/rules/jsx-indent.md): Validate JSX indentation (fixable)
  125. * [react/jsx-indent-props](docs/rules/jsx-indent-props.md): Validate props indentation in JSX (fixable)
  126. * [react/jsx-key](docs/rules/jsx-key.md): Validate JSX has key prop when in array or iterator
  127. * [react/jsx-max-depth](docs/rules/jsx-max-depth.md): Validate JSX maximum depth
  128. * [react/jsx-max-props-per-line](docs/rules/jsx-max-props-per-line.md): Limit maximum of props on a single line in JSX (fixable)
  129. * [react/jsx-no-bind](docs/rules/jsx-no-bind.md): Prevent usage of `.bind()` and arrow functions in JSX props
  130. * [react/jsx-no-comment-textnodes](docs/rules/jsx-no-comment-textnodes.md): Prevent comments from being inserted as text nodes
  131. * [react/jsx-no-duplicate-props](docs/rules/jsx-no-duplicate-props.md): Prevent duplicate props in JSX
  132. * [react/jsx-no-literals](docs/rules/jsx-no-literals.md): Prevent usage of unwrapped JSX strings
  133. * [react/jsx-no-target-blank](docs/rules/jsx-no-target-blank.md): Prevent usage of unsafe `target='_blank'`
  134. * [react/jsx-no-undef](docs/rules/jsx-no-undef.md): Disallow undeclared variables in JSX
  135. * [react/jsx-one-expression-per-line](docs/rules/jsx-one-expression-per-line.md): Limit to one expression per line in JSX
  136. * [react/jsx-curly-brace-presence](docs/rules/jsx-curly-brace-presence.md): Enforce curly braces or disallow unnecessary curly braces in JSX
  137. * [react/jsx-pascal-case](docs/rules/jsx-pascal-case.md): Enforce PascalCase for user-defined JSX components
  138. * [react/jsx-props-no-multi-spaces](docs/rules/jsx-props-no-multi-spaces.md): Disallow multiple spaces between inline JSX props (fixable)
  139. * [react/jsx-sort-default-props](docs/rules/jsx-sort-default-props.md): Enforce default props alphabetical sorting
  140. * [react/jsx-sort-props](docs/rules/jsx-sort-props.md): Enforce props alphabetical sorting (fixable)
  141. * [react/jsx-space-before-closing](docs/rules/jsx-space-before-closing.md): Validate spacing before closing bracket in JSX (fixable)
  142. * [react/jsx-tag-spacing](docs/rules/jsx-tag-spacing.md): Validate whitespace in and around the JSX opening and closing brackets (fixable)
  143. * [react/jsx-uses-react](docs/rules/jsx-uses-react.md): Prevent React to be incorrectly marked as unused
  144. * [react/jsx-uses-vars](docs/rules/jsx-uses-vars.md): Prevent variables used in JSX to be incorrectly marked as unused
  145. * [react/jsx-wrap-multilines](docs/rules/jsx-wrap-multilines.md): Prevent missing parentheses around multilines JSX (fixable)
  146. ## Other useful plugins
  147. - JSX accessibility: [eslint-plugin-jsx-a11y](https://github.com/evcohen/eslint-plugin-jsx-a11y)
  148. - React Native: [eslint-plugin-react-native](https://github.com/Intellicode/eslint-plugin-react-native)
  149. # Shareable configurations
  150. ## Recommended
  151. This plugin exports a `recommended` configuration that enforces React good practices.
  152. To enable this configuration use the `extends` property in your `.eslintrc` config file:
  153. ```json
  154. {
  155. "extends": ["eslint:recommended", "plugin:react/recommended"]
  156. }
  157. ```
  158. See [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information about extending configuration files.
  159. The rules enabled in this configuration are:
  160. * [react/display-name](docs/rules/display-name.md)
  161. * [react/jsx-key](docs/rules/jsx-key.md)
  162. * [react/jsx-no-comment-textnodes](docs/rules/jsx-no-comment-textnodes.md)
  163. * [react/jsx-no-duplicate-props](docs/rules/jsx-no-duplicate-props.md)
  164. * [react/jsx-no-target-blank](docs/rules/jsx-no-target-blank.md)
  165. * [react/jsx-no-undef](docs/rules/jsx-no-undef.md)
  166. * [react/jsx-uses-react](docs/rules/jsx-uses-react.md)
  167. * [react/jsx-uses-vars](docs/rules/jsx-uses-vars.md)
  168. * [react/no-children-prop](docs/rules/no-children-prop.md)
  169. * [react/no-danger-with-children](docs/rules/no-danger-with-children.md)
  170. * [react/no-deprecated](docs/rules/no-deprecated.md)
  171. * [react/no-direct-mutation-state](docs/rules/no-direct-mutation-state.md)
  172. * [react/no-find-dom-node](docs/rules/no-find-dom-node.md)
  173. * [react/no-is-mounted](docs/rules/no-is-mounted.md)
  174. * [react/no-render-return-value](docs/rules/no-render-return-value.md)
  175. * [react/no-string-refs](docs/rules/no-string-refs.md)
  176. * [react/no-unescaped-entities](docs/rules/no-unescaped-entities.md)
  177. * [react/no-unknown-property](docs/rules/no-unknown-property.md)
  178. * [react/prop-types](docs/rules/prop-types.md)
  179. * [react/react-in-jsx-scope](docs/rules/react-in-jsx-scope.md)
  180. * [react/require-render-return](docs/rules/require-render-return.md)
  181. ## All
  182. This plugin also exports an `all` configuration that includes every available rule.
  183. This pairs well with the `eslint:all` rule.
  184. ```json
  185. {
  186. "plugins": [
  187. "react"
  188. ],
  189. "extends": ["eslint:all", "plugin:react/all"]
  190. }
  191. ```
  192. **Note**: These configurations will import `eslint-plugin-react` and enable JSX in [parser options](http://eslint.org/docs/user-guide/configuring#specifying-parser-options).
  193. # License
  194. ESLint-plugin-React is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
  195. [npm-url]: https://npmjs.org/package/eslint-plugin-react
  196. [npm-image]: https://img.shields.io/npm/v/eslint-plugin-react.svg
  197. [travis-url]: https://travis-ci.org/yannickcr/eslint-plugin-react
  198. [travis-image]: https://img.shields.io/travis/yannickcr/eslint-plugin-react/master.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB2aWV3Qm94PSItMTQyLjUgLTE0Mi41IDI4NSAyODUiPjxjaXJjbGUgcj0iMTQxLjciIGZpbGw9IiNERDQ4MTQiLz48ZyBpZD0iYSIgZmlsbD0iI0ZGRiI%2BPGNpcmNsZSBjeD0iLTk2LjQiIHI9IjE4LjkiLz48cGF0aCBkPSJNLTQ1LjYgNjguNGMtMTYuNi0xMS0yOS0yOC0zNC00Ny44IDYtNSA5LjgtMTIuMyA5LjgtMjAuNnMtMy44LTE1LjctOS44LTIwLjZjNS0xOS44IDE3LjQtMzYuNyAzNC00Ny44bDEzLjggMjMuMkMtNDYtMzUuMi01NS4zLTE4LjctNTUuMyAwYzAgMTguNyA5LjMgMzUuMiAyMy41IDQ1LjJ6Ii8%2BPC9nPjx1c2UgeGxpbms6aHJlZj0iI2EiIHRyYW5zZm9ybT0icm90YXRlKDEyMCkiLz48dXNlIHhsaW5rOmhyZWY9IiNhIiB0cmFuc2Zvcm09InJvdGF0ZSgyNDApIi8%2BPC9zdmc%2B
  199. [appveyor-url]: https://ci.appveyor.com/project/yannickcr/eslint-plugin-react
  200. [appveyor-image]: https://img.shields.io/appveyor/ci/yannickcr/eslint-plugin-react/master.svg?logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZlcnNpb249IjEuMSIgd2lkdGg9IjEyOCIgaGVpZ2h0PSIxMjgiIHZpZXdCb3g9IjAgMCAxMjggMTI4Ij48ZyBmaWxsPSIjMUJBMUUyIiB0cmFuc2Zvcm09InNjYWxlKDgpIj48cGF0aCBkPSJNMCAyLjI2NWw2LjUzOS0uODg4LjAwMyA2LjI4OC02LjUzNi4wMzd6Ii8%2BPHBhdGggZD0iTTYuNTM2IDguMzlsLjAwNSA2LjI5My02LjUzNi0uODk2di01LjQ0eiIvPjxwYXRoIGQ9Ik03LjMyOCAxLjI2MWw4LjY3LTEuMjYxdjcuNTg1bC04LjY3LjA2OXoiLz48cGF0aCBkPSJNMTYgOC40NDlsLS4wMDIgNy41NTEtOC42Ny0xLjIyLS4wMTItNi4zNDV6Ii8%2BPC9nPjwvc3ZnPg==
  201. [deps-url]: https://david-dm.org/yannickcr/eslint-plugin-react
  202. [deps-image]: https://img.shields.io/david/dev/yannickcr/eslint-plugin-react.svg
  203. [coverage-url]: https://coveralls.io/r/yannickcr/eslint-plugin-react?branch=master
  204. [coverage-image]: https://img.shields.io/coveralls/yannickcr/eslint-plugin-react/master.svg
  205. [climate-url]: https://codeclimate.com/github/yannickcr/eslint-plugin-react
  206. [climate-image]: https://img.shields.io/codeclimate/maintainability/yannickcr/eslint-plugin-react.svg
  207. [status-url]: https://github.com/yannickcr/eslint-plugin-react/pulse
  208. [status-image]: https://img.shields.io/github/last-commit/yannickcr/eslint-plugin-react.svg