![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
eslint-plugin-prefer-arrow-function
Advanced tools
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.
Install the npm package
# If eslint is installed globally
npm install -g eslint-plugin-prefer-arrow-function
# If eslint is installed locally
npm install -D eslint-plugin-prefer-arrow-function
Add the plugin to the plugins
section and the rule to the rules
section in your .eslintrc
"plugins": [
"prefer-arrow-function"
],
"rules": {
"prefer-arrow-function/prefer-arrow-function": [
"warn",
{
"disallowPrototype": true,
"singleReturnOnly": false,
"classPropertiesAllowed": false
}
]
}
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).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
FAQs
Prefer ES arrow functions
The npm package eslint-plugin-prefer-arrow-function receives a total of 0 weekly downloads. As such, eslint-plugin-prefer-arrow-function popularity was classified as not popular.
We found that eslint-plugin-prefer-arrow-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.