Skip to content

Commit c65a139

Browse files
authored
fix(eslint-plugin): [unbound-method] allow super expressions in this assignments (#3010)
1 parent b0475af commit c65a139

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

packages/eslint-plugin/src/rules/unbound-method.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,14 @@ function isSafeUse(node: TSESTree.Node): boolean {
305305
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);
306306

307307
case AST_NODE_TYPES.AssignmentExpression:
308-
return parent.operator === '=' && node === parent.left;
308+
return (
309+
parent.operator === '=' &&
310+
(node === parent.left ||
311+
(node.type === AST_NODE_TYPES.MemberExpression &&
312+
node.object.type === AST_NODE_TYPES.Super &&
313+
parent.left.type === AST_NODE_TYPES.MemberExpression &&
314+
parent.left.object.type === AST_NODE_TYPES.ThisExpression))
315+
);
309316

310317
case AST_NODE_TYPES.ChainExpression:
311318
case AST_NODE_TYPES.TSNonNullExpression:

packages/eslint-plugin/tests/rules/unbound-method.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,22 @@ class Foo {
265265
}
266266
const { bound } = new Foo();
267267
`,
268+
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
269+
`
270+
class BaseClass {
271+
x: number = 42;
272+
logThis() {}
273+
}
274+
class OtherClass extends BaseClass {
275+
superLogThis: any;
276+
constructor() {
277+
super();
278+
this.superLogThis = super.logThis;
279+
}
280+
}
281+
const oc = new OtherClass();
282+
oc.superLogThis();
283+
`,
268284
],
269285
invalid: [
270286
{
@@ -529,5 +545,48 @@ const { log } = console;
529545
},
530546
],
531547
},
548+
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
549+
{
550+
code: `
551+
class BaseClass {
552+
logThis() {}
553+
}
554+
class OtherClass extends BaseClass {
555+
constructor() {
556+
super();
557+
const x = super.logThis;
558+
}
559+
}
560+
`,
561+
errors: [
562+
{
563+
line: 8,
564+
column: 15,
565+
messageId: 'unbound',
566+
},
567+
],
568+
},
569+
// https://github.com/typescript-eslint/typescript-eslint/issues/1866
570+
{
571+
code: `
572+
class BaseClass {
573+
logThis() {}
574+
}
575+
class OtherClass extends BaseClass {
576+
constructor() {
577+
super();
578+
let x;
579+
x = super.logThis;
580+
}
581+
}
582+
`,
583+
errors: [
584+
{
585+
line: 9,
586+
column: 9,
587+
messageId: 'unbound',
588+
},
589+
],
590+
},
532591
],
533592
});

0 commit comments

Comments
 (0)