Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: support class fields in func-name-matching (refs #14857) #14964

Merged
merged 1 commit into from Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions docs/rules/func-name-matching.md
Expand Up @@ -15,6 +15,10 @@ obj.foo = function bar() {};
obj['foo'] = function bar() {};
var obj = {foo: function bar() {}};
({['foo']: function bar() {}});

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

```js
Expand All @@ -26,6 +30,10 @@ obj.foo = function foo() {};
obj['foo'] = function foo() {};
var obj = {foo: function foo() {}};
({['foo']: function foo() {}});

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

Examples of **correct** code for this rule:
Expand Down Expand Up @@ -54,6 +62,21 @@ obj['x' + 2] = function bar(){};
var [ 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) {};
```
Expand Down Expand Up @@ -81,6 +104,21 @@ obj['x' + 2] = function bar(){};
var [ 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) {};
```
Expand Down
14 changes: 9 additions & 5 deletions lib/rules/func-name-matching.js
Expand Up @@ -196,21 +196,25 @@ module.exports = {
const isProp = node.left.type === "MemberExpression";
const name = isProp ? astUtils.getStaticPropertyName(node.left) : node.left.name;

if (node.right.id && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
if (node.right.id && name && isIdentifier(name) && shouldWarn(name, node.right.id.name)) {
report(node, name, node.right.id.name, isProp);
}
},

Property(node) {
if (node.value.type !== "FunctionExpression" || !node.value.id || node.computed && !isStringLiteral(node.key)) {
"Property, PropertyDefinition[value]"(node) {
if (!(node.value.type === "FunctionExpression" && node.value.id)) {
return;
}

if (node.key.type === "Identifier") {
if (node.key.type === "Identifier" && !node.computed) {
const functionName = node.value.id.name;
let propertyName = node.key.name;

if (considerPropertyDescriptor && propertyName === "value") {
if (
considerPropertyDescriptor &&
propertyName === "value" &&
node.parent.type === "ObjectExpression"
) {
if (isPropertyCall("Object", "defineProperty", node.parent.parent) || isPropertyCall("Reflect", "defineProperty", node.parent.parent)) {
const property = node.parent.parent.arguments[1];

Expand Down