Skip to content

Commit

Permalink
no-unbound-method: Allow use as condition (palantir#2834)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-hanson authored and adidahiya committed May 30, 2017
1 parent f9113e9 commit 8ea3c35
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/rules/noUnboundMethodRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ 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;
// Allow use in conditions
case ts.SyntaxKind.ConditionalExpression:
return (parent as ts.ConditionalExpression).condition === node;
case ts.SyntaxKind.IfStatement:
case ts.SyntaxKind.WhileStatement:
case ts.SyntaxKind.DoStatement:
case ts.SyntaxKind.ForStatement:
return true;
default:
return false;
}
Expand Down
12 changes: 12 additions & 0 deletions test/rules/no-unbound-method/default/test.tsx.lint
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ i.foo;
i.bar;

c.method === i.foo;

// OK in condition
c.method ? 1 : 2;
1 ? c.method : c.method;
~~~~~~~~ [0]
~~~~~~~~ [0]
if (c.method) {}
while (c.method) {}
do () while (c.method);
for (c.method; c.method; c.method) {}


[0].forEach(c.method || i.foo);
~~~~~~~~ [0]
~~~~~ [0]
Expand Down

0 comments on commit 8ea3c35

Please sign in to comment.