Skip to content

useArrowFunction (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"complexity": {
"useArrowFunction": "error"
}
}
}
}

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).

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;
}