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

no-unbound-method: Allow use as condition #2834

Merged
merged 1 commit into from
May 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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