Home

Awesome

eslint-plugin-prefer-arrow

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.

Class methods will not produce errors unless the classPropertiesAllowed flag is set.

This plugin will automatically fix your code using ESLint's --fix option, as long as you use the singleReturnOnly option.

Installation

Install the npm package

# If eslint is installed globally
npm install -g eslint-plugin-prefer-arrow

# If eslint is installed locally
npm install -D eslint-plugin-prefer-arrow

Add the plugin to the plugins section and the rule to the rules section in your .eslintrc

"plugins": [
  "prefer-arrow"
],
"rules": {
  "prefer-arrow/prefer-arrow-functions": [
    "warn",
    {
      "disallowPrototype": true,
      "singleReturnOnly": false,
      "classPropertiesAllowed": false
    }
  ]
}

Configuration

Autofixing

To autofix your code, simply run ESLint with the --fix option. Note that this only works when the singleReturnOnly option is set to true.

eslint --fix src