Skip to content

useNamedFunction (JavaScript)

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

Require function expressions to have a name.

A function expression is a function written with the function keyword inside other code, for example when it is passed to another function or stored in a property. Unlike a function declaration, it does not need a name.

When a function has no name, error messages and debugging tools can show it as <anonymous>, which makes it harder to find where an error came from.

JavaScript automatically names some functions, such as a function stored in a variable. This rule still asks for a name in those cases, so that every function is named the same way no matter where it is written.

The rule also checks async functions, generator functions (function*), and export default function() {}. Arrow functions and methods are not checked.

Foo.prototype.bar = function() {};
code-block.js:1:21 lint/nursery/useNamedFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function has no name.

> 1 │ Foo.prototype.bar = function() {};
│ ^^^^^^^^
2 │

ℹ Error messages and debugging tools can show a function without a name as <anonymous>, which makes it harder to find where an error came from.

ℹ Give this function a name.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const cat = {
meow: function() {}
};
code-block.js:2:11 lint/nursery/useNamedFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function has no name.

1 │ const cat = {
> 2 │ meow: function() {}
│ ^^^^^^^^
3 │ };
4 │

ℹ Error messages and debugging tools can show a function without a name as <anonymous>, which makes it harder to find where an error came from.

ℹ Give this function a name.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

(function() {
// ...
}());
code-block.js:1:2 lint/nursery/useNamedFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function has no name.

> 1 │ (function() {
│ ^^^^^^^^
2 │ // ...
3 │ }());

ℹ Error messages and debugging tools can show a function without a name as <anonymous>, which makes it harder to find where an error came from.

ℹ Give this function a name.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

export default function() {}
code-block.js:1:16 lint/nursery/useNamedFunction ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ This function has no name.

> 1 │ export default function() {}
│ ^^^^^^^^
2 │

ℹ Error messages and debugging tools can show a function without a name as <anonymous>, which makes it harder to find where an error came from.

ℹ Give this function a name.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Foo.prototype.bar = function bar() {};
const cat = {
meow() {}
};
(function setup() {
// ...
}());
const handler = () => {};
export default function main() {}
  • If you want to replace function expressions with arrow functions, see useArrowFunction.