Rule: only-arrow-functions
Disallows traditional (non-arrow) function expressions.
Note that non-arrow functions are allowed if ‘this’ appears somewhere in its body (as such functions cannot be converted to arrow functions).
Rationale
Traditional functions don’t bind lexical scope, which can lead to unexpected behavior when accessing ‘this’.
Config
Two arguments may be optionally provided:
"allow-declarations"
allows standalone function declarations."allow-named-functions"
allows the expressionfunction foo() {}
but notfunction() {}
.
Config examples
"only-arrow-functions": true
"only-arrow-functions": [true, "allow-declarations", "allow-named-functions"]
Schema
{ "type": "array", "items": { "type": "string", "enum": [ "allow-declarations", "allow-named-functions" ] }, "minLength": 0, "maxLength": 1 }
Code examples:
Disallows functions with the function keyword
"rules": { "only-arrow-functions": true }
Passes
const myFunc = () => {
// do something ...
};
const myFunc = function() {
this.doSomething();
};
Fails
function myFunc() {
// do something ...
};
const myFunc = function() {
// do something ...
};