Skip to content

Commit

Permalink
fix(eslint-plugin): [unbound-method] ignore assignments _to_ methods (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Zzzen committed Apr 12, 2020
1 parent d44c0f9 commit 6b4680b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -241,6 +241,9 @@ function isSafeUse(node: TSESTree.Node): boolean {
case AST_NODE_TYPES.BinaryExpression:
return ['instanceof', '==', '!=', '===', '!=='].includes(parent.operator);

case AST_NODE_TYPES.AssignmentExpression:
return parent.operator === '=' && node === parent.left;

case AST_NODE_TYPES.TSNonNullExpression:
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
Expand Down
31 changes: 31 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -147,6 +147,9 @@ ruleTester.run('unbound-method', rule, {

"typeof ContainsMethods.boundStatic === 'function';",
"typeof ContainsMethods.unboundStatic === 'function';",

'instance.unbound = () => {};',
'instance.unbound = instance.unbound.bind(instance);',
].map(addContainsMethodsClass),
`
interface RecordA {
Expand Down Expand Up @@ -211,6 +214,15 @@ const x: OptionalMethod = {};
declare const myCondition: boolean;
if (myCondition || x.mightBeDefined) {
console.log('hello world');
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1256
`
class A {
unbound(): void {
this.unbound = undefined;
this.unbound = this.unbound.bind(this);
}
}
`,
],
Expand Down Expand Up @@ -336,5 +348,24 @@ const x = CommunicationError.prototype.foo;
},
],
},
{
code: `
class Foo {
unbound() {}
}
const instance = new Foo();
let x;
x = instance.unbound; // THIS SHOULD ERROR
instance.unbound = x; // THIS SHOULD NOT
`,
errors: [
{
line: 9,
messageId: 'unbound',
},
],
},
],
});

0 comments on commit 6b4680b

Please sign in to comment.