You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
2.4 KiB

преди 3 години
  1. # eslint-plugin-prefer-arrow
  2. ESLint plugin to prefer arrow functions. By default, the plugin allows usage of `function` as a member of an Object's prototype, but this can be changed with the property `disallowPrototype`. Functions referencing `this` will also be allowed. Alternatively, with the `singleReturnOnly` option, this plugin only reports functions where converting to an arrow function would dramatically simplify the code.
  3. Class methods will not produce errors unless the `classPropertiesAllowed` flag is set.
  4. This plugin will automatically fix your code using ESLint's `--fix` option, as long as you use the `singleReturnOnly` option.
  5. # Installation
  6. Install the npm package
  7. ```bash
  8. # If eslint is installed globally
  9. npm install -g eslint-plugin-prefer-arrow
  10. # If eslint is installed locally
  11. npm install -D eslint-plugin-prefer-arrow
  12. ```
  13. Add the plugin to the `plugins` section and the rule to the `rules` section in your .eslintrc
  14. ```js
  15. "plugins": [
  16. "prefer-arrow"
  17. ],
  18. "rules": [
  19. "prefer-arrow/prefer-arrow-functions": [
  20. "warn",
  21. {
  22. "disallowPrototype": true,
  23. "singleReturnOnly": false,
  24. "classPropertiesAllowed": false
  25. }
  26. ]
  27. ]
  28. ```
  29. # Configuration
  30. * `disallowPrototype`: If set to true, the plugin will warn if `function` is used anytime. Otherwise, the plugin allows usage of `function` if it is a member of an Object's prototype.
  31. * `singleReturnOnly`: If set to true, the plugin will only warn for `function` declarations which *only* contain a return statement. These often look much better when declared as arrow functions without braces. Works well in conjunction with ESLint's built-in [arrow-body-style](http://eslint.org/docs/rules/arrow-body-style) set to `as-needed`.
  32. * `classPropertiesAllowed`: If set to true, the plugin will warn about functions which could be replaced with arrow functions defined as [class instance fields](https://github.com/jeffmo/es-class-static-properties-and-fields). Enable if you're using Babel's [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/) plugin.
  33. * `allowStandaloneDeclarations`: If set to true, the plugin will ignore top-level function declarations (the plugin will still warn about "inner" functions, for example, function declarations inside other functions).
  34. # Autofixing
  35. To autofix your code, simply run ESLint with the `--fix` option. Note that this only works when the `singleReturnOnly` option is set to true.
  36. ```bash
  37. eslint --fix src
  38. ```