Skip to content

useMatchingFunctionName (JavaScript)

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

Require function names to match the name of the variable or property they are assigned to.

In const foo = function bar() {}, the function has two names: bar, its own name, and foo, the variable that stores it. The code calls the function as foo, while stack traces and debugging tools show bar. When the two names differ, it’s harder to tell which function an error comes from.

The rule checks functions written with the function keyword and given their own name when they are:

  • used to initialize a variable;
  • assigned to a variable or property with =, &&=, ||=, or ??=;
  • used as the value of an object property or class property.

The rule ignores anonymous functions, arrow functions, private class properties, properties whose name isn’t known before the code runs, such as obj[key], and assignments to module.exports. It also ignores names that a function can’t have, such as "my-key" or class.

const foo = function bar() {};
code-block.js:1:7 lint/nursery/useMatchingFunctionName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The function name bar doesn't match the variable name foo.

> 1 │ const foo = function bar() {};
│ ^^^^^^^^^^^^^^^^^^
2 │

ℹ The code calls this function through the variable name, but stack traces and debugging tools show the function name. Different names make it harder to tell which function an error comes from.

ℹ Rename the function or the variable so that both names are the same.

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

obj.foo = function bar() {};
code-block.js:1:1 lint/nursery/useMatchingFunctionName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The function name bar doesn't match the property name foo.

> 1 │ obj.foo = function bar() {};
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The code calls this function through the property name, but stack traces and debugging tools show the function name. Different names make it harder to tell which function an error comes from.

ℹ Rename the function or the property so that both names are the same.

ℹ 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 obj = { foo: function bar() {} };
code-block.js:1:15 lint/nursery/useMatchingFunctionName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The function name bar doesn't match the property name foo.

> 1 │ const obj = { foo: function bar() {} };
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ The code calls this function through the property name, but stack traces and debugging tools show the function name. Different names make it harder to tell which function an error comes from.

ℹ Rename the function or the property so that both names are the same.

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

class Foo {
foo = function bar() {};
}
code-block.js:2:5 lint/nursery/useMatchingFunctionName ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

ℹ The function name bar doesn't match the property name foo.

1 │ class Foo {
> 2 │ foo = function bar() {};
│ ^^^^^^^^^^^^^^^^^^
3 │ }
4 │

ℹ The code calls this function through the property name, but stack traces and debugging tools show the function name. Different names make it harder to tell which function an error comes from.

ℹ Rename the function or the property so that both names are the same.

ℹ 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 foo = function foo() {};
const bar = function () {};
obj.foo = function foo() {};
obj[key] = function bar() {};
obj["my-key"] = function bar() {};
module.exports = function foo() {};
const obj = { foo: function foo() {}, bar: () => {} };
class Foo {
foo = function foo() {};
#bar = function baz() {};
}