Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
no-unbound-method: add another exception
Browse files Browse the repository at this point in the history
Reading a property with square brackets, though rare, should be allowed because it won't be effected by an unbound this.
  • Loading branch information
ethanresnick committed Dec 27, 2017
1 parent 3c702fb commit 0a7d5e6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ function isSafeUse(node: ts.Node): boolean {
return (parent as ts.CallExpression).expression === node;
case ts.SyntaxKind.TaggedTemplateExpression:
return (parent as ts.TaggedTemplateExpression).tag === node;
// E.g. `obj.method.bind(obj)`.
case ts.SyntaxKind.PropertyAccessExpression:
// E.g. `obj.method.bind(obj) or obj.method["prop"]`.
case ts.SyntaxKind.PropertyAccessExpression:
case ts.SyntaxKind.ElementAccessExpression:
return true;
// Allow most binary operators, but don't allow e.g. `myArray.forEach(obj.method || otherObj.otherMethod)`.
case ts.SyntaxKind.BinaryExpression:
Expand Down
1 change: 1 addition & 0 deletions test/rules/no-unbound-method/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function f(i: I) {
i.m ? 1 : 2;
if (i.m) {}
!i.m;
i.m["length"];
return i.m!;
~~~ [0]
}
Expand Down

0 comments on commit 0a7d5e6

Please sign in to comment.