Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: class-methods-use-this reports 'undefined' names (#12103)
  • Loading branch information
mdjermanovic authored and platinumazure committed Aug 18, 2019
1 parent 92ec2cb commit acce6de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lib/rules/class-methods-use-this.js
Expand Up @@ -5,6 +5,12 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -34,7 +40,7 @@ module.exports = {
}],

messages: {
missingThis: "Expected 'this' to be used by class method '{{name}}'."
missingThis: "Expected 'this' to be used by class {{name}}."
}
},
create(context) {
Expand Down Expand Up @@ -90,7 +96,7 @@ module.exports = {
node,
messageId: "missingThis",
data: {
name: node.parent.key.name
name: astUtils.getFunctionNameWithKind(node)
}
});
}
Expand Down
35 changes: 25 additions & 10 deletions tests/lib/rules/class-methods-use-this.js
Expand Up @@ -37,73 +37,88 @@ ruleTester.run("class-methods-use-this", rule, {
code: "class A { foo() {} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {/**this**/} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {var a = function () {this};} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {var a = function () {var b = function(){this}};} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {window.this} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {that.this = 'this';} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() { () => undefined; } }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {} bar() {} }",
options: [{ exceptMethods: ["bar"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 14, messageId: "missingThis", data: { name: "method 'foo'" } }
]
},
{
code: "class A { foo() {} hasOwnProperty() {} }",
options: [{ exceptMethods: ["foo"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 34, messageId: "missingThis", data: { name: "hasOwnProperty" } }
{ type: "FunctionExpression", line: 1, column: 34, messageId: "missingThis", data: { name: "method 'hasOwnProperty'" } }
]
},
{
code: "class A { [foo]() {} }",
options: [{ exceptMethods: ["foo"] }],
parserOptions: { ecmaVersion: 6 },
errors: [
{ type: "FunctionExpression", line: 1, column: 16, messageId: "missingThis", data: { name: "foo" } }
{ type: "FunctionExpression", line: 1, column: 16, messageId: "missingThis", data: { name: "method" } }
]
},
{
code: "class A { foo(){} 'bar'(){} 123(){} [`baz`](){} [a](){} [f(a)](){} get quux(){} set[a](b){} *quuux(){} }",
parserOptions: { ecmaVersion: 6 },
errors: [
{ message: "Expected 'this' to be used by class method 'foo'.", type: "FunctionExpression", column: 14 },
{ message: "Expected 'this' to be used by class method 'bar'.", type: "FunctionExpression", column: 24 },
{ message: "Expected 'this' to be used by class method '123'.", type: "FunctionExpression", column: 32 },
{ message: "Expected 'this' to be used by class method 'baz'.", type: "FunctionExpression", column: 44 },
{ message: "Expected 'this' to be used by class method.", type: "FunctionExpression", column: 52 },
{ message: "Expected 'this' to be used by class method.", type: "FunctionExpression", column: 63 },
{ message: "Expected 'this' to be used by class getter 'quux'.", type: "FunctionExpression", column: 76 },
{ message: "Expected 'this' to be used by class setter.", type: "FunctionExpression", column: 87 },
{ message: "Expected 'this' to be used by class generator method 'quuux'.", type: "FunctionExpression", column: 99 }
]
}
]
Expand Down

0 comments on commit acce6de

Please sign in to comment.