1.2.3 • Published 4 years ago

eslint-plugin-prefer-arrow v1.2.3

Weekly downloads
287,106
License
MIT
Repository
github
Last release
4 years ago

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

  • 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.
  • 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 set to as-needed.
  • classPropertiesAllowed: If set to true, the plugin will warn about functions which could be replaced with arrow functions defined as class instance fields. Enable if you're using Babel's transform-class-properties plugin.
  • 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).

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
@michedot/eslint-plugin@vasiliicuhar/eslint-config-preseteslint-ts-config-mailonlineeslint-config-slick@dvch/eslint-config@pamls/tslint-config@michedot/eslint-plugin-t2eslint-config-vicasaconfig@joseirrazabal/kit@darkobits/ts-unified@happ/eslint-config@happ/eslint-plugineslint-config-leedavidcs@kidwen/eslint-angular@blockone/eslint-config-blockone@norathefurry/eslint-config@nelkinfo/config-ts@everything-registry/sub-chunk-1613eslint-config-dmrebird-https-letsencryptsupercluster-googlemaps-adapter-clonesteststream-jestvadiminator-eslint-configloading-infoheadless-custom-datepickergix-polar-typescriptionic-input-br-maskgsellernchat-dev-commonms-archetype-coreopenapi-aggregatoromega-dlvrng2-accordionsng2-button-loaderng2-preloaderng-eslintnotion-page-repeaterts-prettier-group@aidenlx/eslint-config@arlequins/eslint-config-typescript@arlequins/eslint-config-typescript-sveltejs@auditorium-ai/eslint-config@ark120202/eslint-config@avenga/eslint-config@aldahick/eslint-rules@algoan/eslint-config@alamar/kit@allthings/eslint-config@awardit/eslint-config-xo@babybeet/eslint-config-base@b2x/eslint-config@bifravst/eslint-config-typescript@bud-tools/dev-scripts@breadstone-infrastructure/eslint-config@carforyou/eslint-config@chieforz/eslint-config-tslint-base@chieforz/tslint-config@chewbaccacookie/eslint-config-react@certainperformance/eslint-config@1password/eslint-config@1password/front-end-style@hoishin/eslint-config@hoishin/eslint-config-ts@hoishin/eslintrc-ts@bentley/eslint-plugin@danieljimeneznz/eslint-config@consta/widgets-configszotero-plugintsjs@dword-design/eslint-config@dword-design/eslint-config-fp@dword-design/base-core@dword-design/base-eslint-config@dword-design/base-lint@dword-design/base-type-fp@econominhas/eslint-config@easternmotors/eslint-config-ts@grnx-utils/eslint@grizzlyinnovations/eslint-config@focus21/eslint-config@fracturedrocks/eslint-config@hubtype/angular-dx@hubtype/eslint-config@hubtype/eslint-config-angular@huckleberry-inc/eslint-config@hug/eslint-config@firestitch/lint@elementx-ai/eslint-config@elumixor/eslint@elumixor/translator@eth-optimism/actor-tests@eui/deps-base@ghyston/eslint-config-ghyston@gobstones/gobstones-scripts@infinitebrahmanuniverse/nolb-eslint-plugin-p@kukoomomo/core-utils@logifox/react-ui@lazycuh/eslint-config-base@lazyorange/eslint-config-lazyorange@lesnoypudge/eslint-base-config
1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

7 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago