Skip to content

Commit

Permalink
fix(eslint-plugin): fix unbound metod triggering on class method bind
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shulyak committed Oct 18, 2019
1 parent 0c85ac3 commit 618badc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Expand Up @@ -111,6 +111,9 @@ function isSafeUse(node: TSESTree.Node): boolean {
case AST_NODE_TYPES.CallExpression:
return parent.callee === node;

case AST_NODE_TYPES.AssignmentExpression:
return isBindCalled(parent.right);

case AST_NODE_TYPES.ConditionalExpression:
return parent.test === node;

Expand All @@ -134,3 +137,12 @@ function isSafeUse(node: TSESTree.Node): boolean {

return false;
}

function isBindCalled(parent: TSESTree.Expression): boolean {
return (
parent.type === AST_NODE_TYPES.CallExpression &&
parent.callee.type === AST_NODE_TYPES.MemberExpression &&
parent.callee.property.type === AST_NODE_TYPES.Identifier &&
parent.callee.property.name === 'bind'
);
}
10 changes: 10 additions & 0 deletions packages/eslint-plugin/tests/rules/unbound-method.test.ts
Expand Up @@ -143,6 +143,16 @@ class CommunicationError {
class CommunicationError {}
const x = CommunicationError.prototype;
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/700
`
class ContainsBoundMethod {
constructor() {
this.handleSomething = this.handleSomething.bind(this);
}
handleSomething(): void {}
}
`,
],
invalid: [
{
Expand Down

0 comments on commit 618badc

Please sign in to comment.