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

no-unbound-method: Skip past casts and parentheses #2838

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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