diff --git a/packages/eslint-plugin/src/rules/unbound-method.ts b/packages/eslint-plugin/src/rules/unbound-method.ts index 407d04c11ec..2ec66bc2aa3 100644 --- a/packages/eslint-plugin/src/rules/unbound-method.ts +++ b/packages/eslint-plugin/src/rules/unbound-method.ts @@ -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: diff --git a/packages/eslint-plugin/tests/rules/unbound-method.test.ts b/packages/eslint-plugin/tests/rules/unbound-method.test.ts index 83f3b93524c..911206b1f21 100644 --- a/packages/eslint-plugin/tests/rules/unbound-method.test.ts +++ b/packages/eslint-plugin/tests/rules/unbound-method.test.ts @@ -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: [ { @@ -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', + }, + ], + }, ], });