eslint/func-name-matching Style
What it does
Requires function expression names to match the variable or property names they are assigned to, or disallows such matches with "never".
Why is this bad?
Matching names keep stack traces and source code easier to connect. If a project prefers distinct names, the "never" option enforces that convention consistently.
Examples
Examples of incorrect code for this rule:
/*eslint func-name-matching: "error"*/
let foo = function bar() {};
foo = function bar() {};
const obj = { foo: function bar() {} };
obj.foo = function bar() {};
obj["foo"] = function bar() {};
class C {
foo = function bar() {};
}
/*eslint func-name-matching: ["error", "never"] */
let foo = function foo() {};
foo = function foo() {};
const obj = { foo: function foo() {} };
obj.foo = function foo() {};
obj["foo"] = function foo() {};
class C {
foo = function foo() {};
}Examples of correct code for this rule:
/*eslint func-name-matching: "error"*/
// equivalent to /*eslint func-name-matching: ["error", "always"]*/
const foo = function foo() {};
const foo1 = function () {};
const foo2 = () => {};
foo = function foo() {};
const obj = { foo: function foo() {} };
obj.foo = function foo() {};
obj["foo"] = function foo() {};
const obj1 = { [foo]: function bar() {} };
const obj3 = { foo: function () {} };
obj["x" + 2] = function bar() {};
const [bar] = [function bar() {}];
class C {
foo = function foo() {};
baz = function () {};
}
// private names are ignored
class D {
#foo = function foo() {};
#bar = function foo() {};
baz() {
this.#foo = function foo() {};
this.#foo = function bar() {};
}
}
module.exports = function foo(name) {};
/*eslint func-name-matching: ["error", "never"] */
let foo = function bar() {};
const foo1 = function () {};
const foo2 = () => {};
foo = function bar() {};
const obj = { foo: function bar() {} };
obj.foo = function bar() {};
obj["foo"] = function bar() {};
const obj1 = { foo: function bar() {} };
const obj2 = { [foo]: function foo() {} };
const obj4 = { foo: function () {} };
obj["x" + 2] = function bar() {};
const [bar] = [function bar() {}];
class C {
foo = function bar() {};
baz = function () {};
}
// private names are ignored
class D {
#foo = function foo() {};
#bar = function foo() {};
baz() {
this.#foo = function foo() {};
this.#foo = function bar() {};
}
}
module.exports = function foo(name) {};Configuration
This rule takes an optional string of "always" or "never" (when omitted, it defaults to "always"), and an optional options object with two properties considerPropertyDescriptor and includeCommonJSModuleExports.
The 1st option
type: "always" | "never"
The 2nd option
This option is an object with the following properties:
considerPropertyDescriptor
type: boolean
default: false
If considerPropertyDescriptor is set to true, the check will take into account the use of Object.create, Object.defineProperty, Object.defineProperties, and Reflect.defineProperty.
includeCommonJSModuleExports
type: boolean
default: false
If includeCommonJSModuleExports is set to true, module.exports and module["exports"] will be checked by this rule.
How to use
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"func-name-matching": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"func-name-matching": "error",
},
});oxlint --deny func-name-matchingVersion
This rule was added in v1.62.0.
