Skip to content

Commit

Permalink
Update: support class fields in func-name-matching (refs #14857) (#14964
Browse files Browse the repository at this point in the history
)
  • Loading branch information
mdjermanovic committed Aug 24, 2021
1 parent 44f7de5 commit 44c6fc8
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 5 deletions.
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 @@ -195,21 +195,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

0 comments on commit 44c6fc8

Please sign in to comment.