eslint/prefer-arrow-callback Style
What it does
Requires using arrow functions for callbacks.
Why is this bad?
Arrow functions are generally better suited for callbacks because they:
- inherit
thisfrom the surrounding scope, avoiding a common source of bugs; - are shorter and easier to read;
- cannot be used as constructors, which is desirable for callbacks.
Options
json
{
"prefer-arrow-callback": [
"error",
{
"allowNamedFunctions": false,
"allowUnboundThis": true
}
]
}allowNamedFunctions(defaultfalse) — whentrue, named function expressions are allowed.allowUnboundThis(defaulttrue) — whenfalse, function expressions that referencethisare reported even when they are not bound to athisvalue.
Examples
Examples of incorrect code for this rule:
js
foo(function (a) {
return a;
});
foo(
function () {
return this.a;
}.bind(this),
);Examples of correct code for this rule:
js
foo((a) => a);
foo(function* () {
yield;
});
foo(function () {
this;
});
foo(function bar() {
bar();
});Configuration
allowNamedFunctions
type: boolean
default: false
allowUnboundThis
type: boolean
default: true
How to use
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"prefer-arrow-callback": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"prefer-arrow-callback": "error",
},
});bash
oxlint --deny prefer-arrow-callbackVersion
This rule was added in vnext.
