Skip to content

Commit

Permalink
no-unbound-method: Skip past casts and parentheses (palantir#2838)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and adidahiya committed May 31, 2017
1 parent 6a46287 commit d3177d3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ function isSafeUse(node: ts.Node): boolean {
// Allow most binary operators, but don't allow e.g. `myArray.forEach(obj.method || otherObj.otherMethod)`.
case ts.SyntaxKind.BinaryExpression:
return (parent as ts.BinaryExpression).operatorToken.kind !== ts.SyntaxKind.BarBarToken;
case ts.SyntaxKind.NonNullExpression:
case ts.SyntaxKind.AsExpression:
case ts.SyntaxKind.TypeAssertionExpression:
case ts.SyntaxKind.ParenthesizedExpression:
return isSafeUse(parent);
// Allow use in conditions
case ts.SyntaxKind.ConditionalExpression:
return (parent as ts.ConditionalExpression).condition === node;
Expand Down
10 changes: 10 additions & 0 deletions test/rules/no-unbound-method/default/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
interface I { m?(): void; }
function f(i: I) {
i.m!();
(i.m as any)(1);
(<any> i.m)(1);
return i.m!;
~~~ [0]
}

[0]: Avoid referencing unbound methods which may cause unintentional scoping of 'this'.
2 changes: 1 addition & 1 deletion test/rules/no-unbound-method/default/test.tsx.lint
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ c.method ? 1 : 2;
~~~~~~~~ [0]
if (c.method) {}
while (c.method) {}
do () while (c.method);
do {} while (c.method);
for (c.method; c.method; c.method) {}


Expand Down

0 comments on commit d3177d3

Please sign in to comment.