Skip to content

Commit

Permalink
fix(eslint-plugin): [unbound-method] allow super expressions in `th…
Browse files Browse the repository at this point in the history
…is` assignments (#3010)
  • Loading branch information
armano2 committed Feb 28, 2021
1 parent b0475af commit c65a139
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -305,7 +305,14 @@ function isSafeUse(node: TSESTree.Node): boolean {
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);

case AST_NODE_TYPES.AssignmentExpression:
return parent.operator === '=' && node === parent.left;
return (
parent.operator === '=' &&
(node === parent.left ||
(node.type === AST_NODE_TYPES.MemberExpression &&
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:
case AST_NODE_TYPES.TSNonNullExpression:
Expand Down
59 changes: 59 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -265,6 +265,22 @@ class Foo {
}
const { bound } = new Foo();
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
`
class BaseClass {
x: number = 42;
logThis() {}
}
class OtherClass extends BaseClass {
superLogThis: any;
constructor() {
super();
this.superLogThis = super.logThis;
}
}
const oc = new OtherClass();
oc.superLogThis();
`,
],
invalid: [
{
Expand Down Expand Up @@ -529,5 +545,48 @@ const { log } = console;
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
{
code: `
class BaseClass {
logThis() {}
}
class OtherClass extends BaseClass {
constructor() {
super();
const x = super.logThis;
}
}
`,
errors: [
{
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 c65a139

Please sign in to comment.