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

fix(eslint-plugin): [unbound-method] allow super expressions in this assignments #3010

Merged
merged 7 commits into from Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
7 changes: 6 additions & 1 deletion packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -305,7 +305,12 @@ 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 &&
armano2 marked this conversation as resolved.
Show resolved Hide resolved
node.object.type === AST_NODE_TYPES.Super))
armano2 marked this conversation as resolved.
Show resolved Hide resolved
);

case AST_NODE_TYPES.ChainExpression:
case AST_NODE_TYPES.TSNonNullExpression:
Expand Down
43 changes: 43 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -265,6 +265,24 @@ class Foo {
}
const { bound } = new Foo();
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
`
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
}
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 +547,30 @@ const { log } = console;
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
{
code: `
class BaseClass {
x: number = 42;
logThis() {
console.log('x is ');
}
}
class OtherClass extends BaseClass {
superLogThis: any;
constructor() {
super();
const x = super.logThis; // ERROR - unbound method
}
}
`,
errors: [
{
line: 12,
column: 15,
messageId: 'unbound',
},
],
},
],
});