Package detail

eslint-plugin-prefer-arrow-functions

JamieMason375.8kMIT3.6.2

Convert functions to arrow functions

es6, eslint, eslint-plugin

readme

eslint-plugin-prefer-arrow-functions

An ESLint Plugin to Lint and auto-fix plain Functions into Arrow Functions, in all cases where conversion would result in the same behaviour (Arrow Functions do not support this, arguments, or new.target for example).

Installation

npm install --save-dev eslint eslint-plugin-prefer-arrow-functions

Configuration

Add the plugin to the plugins section and the rule to the rules section in your .eslintrc. The default values for options are listed in this example.

{
  "plugins": ["prefer-arrow-functions"],
  "rules": {
    "prefer-arrow-functions/prefer-arrow-functions": [
      "warn",
      {
        "allowedNames": [],
        "allowNamedFunctions": false,
        "allowObjectProperties": false,
        "classPropertiesAllowed": false,
        "disallowPrototype": false,
        "returnStyle": "unchanged",
        "singleReturnOnly": false
      }
    ]
  }
}

Options

allowedNames

An optional array of function names to ignore. When set, the rule won't report named functions such as function foo() {} whose name is identical to a member of this array.

allowNamedFunctions

If set to true, the rule won't report named functions such as function foo() {}. Anonymous function such as const foo = function() {} will still be reported.

allowObjectProperties

If set to true, the rule won't report named methods such as

const myObj = {
  hello() {}
}

classPropertiesAllowed

When true, functions defined as class instance fields will be converted to arrow functions when doing so would not alter or break their behaviour.

disallowPrototype

When true, functions assigned to a prototype will be converted to arrow functions when doing so would not alter or break their behaviour.

returnStyle

  • When "implicit", arrow functions such as x => { return x; } will be converted to x => x.
  • When "explicit", arrow functions such as x => x will be converted to x => { return x; }.
  • When "unchanged" or not set, arrow functions will be left as they were.

singleReturnOnly

When true, only function declarations which only contain a return statement will be converted. Functions containing block statements will be ignored.

This option works well in conjunction with ESLint's built-in arrow-body-style set to as-needed.

Credits

This project is a fork of https://github.com/TristonJ/eslint-plugin-prefer-arrow by Triston Jones.

Badges

  • support on ko-fi
  • NPM version
  • NPM downloads
  • Build Status

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

3.6.2

3.6.1

12 January 2025

  • test: add scenario from issue #35 c08cac1
  • chore(release): 3.6.1 b53da74
  • fix: restore semver range in peerDependencies 6e04024

3.6.0

9 January 2025

  • feat: allow methods on objects #15
  • chore(release): 3.6.0 ca7bcd0

3.5.0

9 January 2025

  • feat: allow functions with certain names #9
  • refactor: improve internal typings 949b6e3
  • refactor: move writer to a module 7625a59
  • refactor: move guards to a module 32c9fa7

3.4.2

9 January 2025

  • fix: format static private methods correctly with classPropertiesAllowed: true #46 #47
  • fix: update npm dependencies (patch and minor) ace5f32
  • chore(release): 3.4.2 8b086f2
  • chore: credit contributors 143ef3e

3.4.1

14 August 2024

  • fix(npm): set eslint peer to v8 #43
  • chore(release): 3.4.1 e39d1b0

3.4.0

11 August 2024

  • feat: upgrade to eslint v9 #34
  • fix: adjusts fix for generic type arguments on TSX files #27
  • feat: do not flag constructors without this #29
  • chore: set minimum node version to 18 #41
  • fix: TypeError on function f() { return; } #38
  • chore: switch to pnpm a8b8c93
  • fix: update npm dependencies 6fa62ec
  • chore: various fixes and breaking changes ab198e5

3.3.2

27 February 2024

  • fix: handle missing case for function overloads #31
  • chore(release): 3.3.2 2778c2d

3.3.1

25 February 2024

  • fix: preserve $ characters in function bodies #28
  • chore(release): 3.3.1 a34f8f9
  • test: add case for $ in function body 78342f5
  • style(format): format source 7d5dd71

3.3.0

25 February 2024

  • fix: protect static modifier on static methods #30
  • feat: support function overloads #26
  • feat: support assertion functions #25
  • feat: support function overloads (#26) #17
  • feat: support assertion functions (#25) #22
  • chore(github): add release-it 93e6f71
  • chore(release): 3.3.0 3051a81
  • style(format): format source 1b140aa

3.2.4

23 October 2023

  • chore(github): update funding.yml #18 #19
  • feat(config): add support for allowNamedFunctions 50fb3b6
  • chore(release): 3.2.4 9b76409

3.1.4

13 November 2021

  • fix(yarn): regenerate lockfile b0afea1
  • chore(codeclimate): remove codeclimate 0f338c2
  • chore(release): 3.1.4 4520ba6

3.1.3

13 November 2021

  • feat(types): add support for generics #10
  • fix(types): ensure return type is not lost #14
  • fix(npm): update dependencies 30a64e1
  • style(format): format source 1a01195
  • docs(readme): update readme template 7d8b273

3.0.1

4 August 2019

  • fix(npm): update dependencies 274db0d
  • chore(readme): move sponsorship info 18af90f
  • docs: add link to try it yourself bb899c4

3.0.0

7 May 2019

  • Add support for ESLint >=2 #6
  • Add support for ESLint 4 #5
  • Allow functions with reference to 'this' #4
  • Skip generator functions. Fixes #8 #8
  • Breaking change: feat: add formal support for async functions 45b597b
  • Breaking change: feat: skip functions containing this, arguments, super, and new.target f1cee8c
  • refactor: transpile using typescript 370d0b6