Skip to content

Commit

Permalink
fix(eslint-plugin): improve safeuse check
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Feb 8, 2021
1 parent c047e51 commit 1a24d0c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
4 changes: 3 additions & 1 deletion packages/eslint-plugin/src/rules/unbound-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ function isSafeUse(node: TSESTree.Node): boolean {
parent.operator === '=' &&
(node === parent.left ||
(node.type === AST_NODE_TYPES.MemberExpression &&
node.object.type === AST_NODE_TYPES.Super))
node.object.type === AST_NODE_TYPES.Super &&
parent.left.type === AST_NODE_TYPES.MemberExpression &&
parent.left.object.type === AST_NODE_TYPES.ThisExpression))
);

case AST_NODE_TYPES.ChainExpression:
Expand Down
36 changes: 26 additions & 10 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,7 @@ const { bound } = new Foo();
`
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
logThis() {}
}
class OtherClass extends BaseClass {
superLogThis: any;
Expand Down Expand Up @@ -551,26 +549,44 @@ const { log } = console;
{
code: `
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
logThis() {}
}
class OtherClass extends BaseClass {
superLogThis: any;
constructor() {
super();
const x = super.logThis; // ERROR - unbound method
const x = super.logThis;
}
}
`,
errors: [
{
line: 12,
line: 8,
column: 15,
messageId: 'unbound',
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
{
code: `
class BaseClass {
logThis() {}
}
class OtherClass extends BaseClass {
constructor() {
super();
let x;
x = super.logThis;
}
}
`,
errors: [
{
line: 9,
column: 9,
messageId: 'unbound',
},
],
},
],
});

0 comments on commit 1a24d0c

Please sign in to comment.