Versions

func-name-matching

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

❄️ Frozen

This rule is currently frozen and is not accepting feature requests.

Rule Details

This rule requires function names to match the name of the variable or property to which they are assigned. The rule will ignore property assignments where the property name is a literal that is not a valid identifier in the ECMAScript version specified in your configuration (default ES5).

Examples of incorrect code for this rule:

Open in Playground
/*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() {};
({['foo']: function bar() {}});

class C {
    foo = function bar() {};
}
Open in Playground
/*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() {};
({['foo']: function foo() {}});

class C {
    foo = function foo() {};
}

Examples of correct code for this rule:

Open in Playground
/*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() {};
obj['foo//bar'] = function foo() {};
obj[foo] = function bar() {};

const obj1 = {[foo]: function bar() {}};
const obj2 = {'foo//bar': function foo() {}};
const obj3 = {foo: function() {}};

obj['x' + 2] = function bar(){};
const [ bar ] = [ function bar(){} ];
({[foo]: 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) {};
module['exports'] = function foo(name) {};
Open in Playground
/*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() {};
obj['foo//bar'] = function foo() {};
obj[foo] = function foo() {};

const obj1 = {foo: function bar() {}};
const obj2 = {[foo]: function foo() {}};
const obj3 = {'foo//bar': function foo() {}};
const obj4 = {foo: function() {}};

obj['x' + 2] = function bar(){};
const [ bar ] = [ function bar(){} ];
({[foo]: 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) {};
module['exports'] = function foo(name) {};

Options

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.

considerPropertyDescriptor

A boolean value that defaults to 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.

Examples of correct code for the { considerPropertyDescriptor: true } option:

Open in Playground
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
const obj = {};
Object.create(obj, {foo:{value: function foo() {}}});
Object.defineProperty(obj, 'bar', {value: function bar() {}});
Object.defineProperties(obj, {baz:{value: function baz() {} }});
Reflect.defineProperty(obj, 'foo', {value: function foo() {}});

Examples of incorrect code for the { considerPropertyDescriptor: true } option:

Open in Playground
/*eslint func-name-matching: ["error", { "considerPropertyDescriptor": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "considerPropertyDescriptor": true }]*/
const obj = {};
Object.create(obj, {foo:{value: function bar() {}}});
Object.defineProperty(obj, 'bar', {value: function baz() {}});
Object.defineProperties(obj, {baz:{value: function foo() {} }});
Reflect.defineProperty(obj, 'foo', {value: function value() {}});

includeCommonJSModuleExports

A boolean value that defaults to false. If includeCommonJSModuleExports is set to true, module.exports and module["exports"] will be checked by this rule.

Examples of incorrect code for the { includeCommonJSModuleExports: true } option:

Open in Playground
/*eslint func-name-matching: ["error", { "includeCommonJSModuleExports": true }]*/
// equivalent to /*eslint func-name-matching: ["error", "always", { "includeCommonJSModuleExports": true }]*/

module.exports = function foo(name) {};
module['exports'] = function foo(name) {};

When Not To Use It

Do not use this rule if you want to allow named functions to have different names from the variable or property to which they are assigned.

Compatibility

Version

This rule was introduced in ESLint v3.8.0.

Resources

Change Language