useArrowFunction (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v1.0.0 - Diagnostic Category:
lint/complexity/useArrowFunction - This rule is recommended, meaning it is enabled by default.
- This rule has a safe fix.
- The default severity of this rule is warning.
- Sources:
- Inspired from
prefer-arrow-callback
- Inspired from
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "complexity": { "useArrowFunction": "error" } } }}Description
Section titled βDescriptionβUse arrow functions over function expressions.
An arrow function expression is a compact alternative to a regular function expression,
with an important distinction:
this is not bound to the arrow function. It inherits this from its parent scope.
This rule proposes turning all function expressions that are not generators (function*) and donβt use this into arrow functions.
This rule does not modify top-level function declarations (discuss here).
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβconst z = function() { return 0;}code-block.js:1:11 lint/complexity/useArrowFunction FIXABLE ββββββββββββββββββββββββββββββββββββββ
β This function expression can be turned into an arrow function.
> 1 β const z = function() {
β ^^^^^^^^^^^^
> 2 β return 0;
> 3 β }
β ^
4 β
βΉ Function expressions that don't use this can be turned into arrow functions.
βΉ Safe fix: Use an arrow function instead.
1 β - constΒ·zΒ·=Β·function()Β·{
2 β - Β·Β·Β·Β·returnΒ·0;
3 β - }
1 β + constΒ·zΒ·=Β·()Β·=>Β·0
4 2 β
const delegatedFetch = async function(url) { return await fetch(url);}code-block.js:1:24 lint/complexity/useArrowFunction FIXABLE ββββββββββββββββββββββββββββββββββββββ
β This function expression can be turned into an arrow function.
> 1 β const delegatedFetch = async function(url) {
β ^^^^^^^^^^^^^^^^^^^^^
> 2 β return await fetch(url);
> 3 β }
β ^
4 β
βΉ Function expressions that don't use this can be turned into arrow functions.
βΉ Safe fix: Use an arrow function instead.
1 β - constΒ·delegatedFetchΒ·=Β·asyncΒ·function(url)Β·{
2 β - Β·Β·Β·Β·returnΒ·awaitΒ·fetch(url);
3 β - }
1 β + constΒ·delegatedFetchΒ·=Β·asyncΒ·(url)Β·=>Β·awaitΒ·fetch(url)
4 2 β
const f = function() { return this.prop;}Named function expressions are ignored:
const z = function z() { return 0;}Functions that reference the arguments object are ignored because the arguments object is not available to arrow functions.
const q = function () { return arguments[0];}Function expressions that declare the type of this are also ignored:
const z = function(this: A): number { return 0;}See Also
Section titled βSee AlsoβRelated links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.