$ npm install 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, ornew.targetfor example).
npm install --save-dev eslint eslint-plugin-prefer-arrow-functions
For ESLint 9 and above, use the shared config all in eslint.config.mjs:
import pluginJs from '@eslint/js';
import preferArrowFunctions from 'eslint-plugin-prefer-arrow-functions';
export default [
pluginJs.configs.all,
preferArrowFunctions.configs.all,
];
Or configure the rule(s) on your own:
import pluginJs from '@eslint/js';
import preferArrowFunctions from 'eslint-plugin-prefer-arrow-functions';
export default [
pluginJs.configs.all,
{
plugins: {
'prefer-arrow-functions': preferArrowFunctions
},
rules: {
'prefer-arrow-functions/prefer-arrow-functions': [
'warn',
{
allowedNames: [],
allowNamedFunctions: false,
allowObjectProperties: false,
classPropertiesAllowed: false,
disallowPrototype: false,
returnStyle: 'unchanged',
singleReturnOnly: false,
},
],
},
},
];
For ESLint 8 (8.57.0 or newer).
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
}
]
}
}
allowedNamesAn 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.
For compatibility with @angular-eslint/require-lifecycle-on-prototype, you can do:
import { ASTUtils } from "@angular-eslint/utils"
// ...
{
rules: {
'prefer-arrow-functions/prefer-arrow-functions': [
'warn',
{
// ...
allowedNames: [...ASTUtils.ANGULAR_LIFECYCLE_METHODS],
},
],
},
},
allowNamedFunctionsControls how named functions are handled:
true, the rule won't report any named functions such as function foo() {}. Anonymous functions such as
const foo = function() {} will still be reported."only-expressions", the rule will allow named function expressions (like callback(function namedFn() {})) but
will still transform named function declarations (like function foo() {}).false (default), all functions will be transformed to arrow functions when safe to do so.allowObjectPropertiesIf set to true, the rule won't report named methods such as
const myObj = {
hello() {},
};
classPropertiesAllowedWhen true, functions defined as
class instance fields
will be converted to arrow functions when doing so would not alter or break their behaviour.
disallowPrototypeWhen true, functions assigned to a prototype will be converted to arrow functions when doing so would not alter or
break their behaviour.
returnStyle"implicit", arrow functions such as x => { return x; } will be converted to x => x."explicit", arrow functions such as x => x will be converted to x => { return x; }."unchanged" or not set, arrow functions will be left as they were.singleReturnOnlyWhen 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.
This project is a fork of https://github.com/TristonJ/eslint-plugin-prefer-arrow by Triston Jones.
© 2010 - cnpmjs.org x YWFE | Home | YWFE