useNamedFunction (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.0 - Diagnostic Category:
lint/nursery/useNamedFunction - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
func-names
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useNamedFunction": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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() {}See Also
Section titled “See Also”- If you want to replace
functionexpressions with arrow functions, seeuseArrowFunction.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.