Skip to content

Commit

Permalink
fix(eslint-plugin): [unbound-method] handling of logical expr (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher authored Jan 16, 2020
1 parent e329397 commit 9c5b857
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 238 deletions.
21 changes: 14 additions & 7 deletions packages/eslint-plugin/src/rules/unbound-method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ interface Config {
ignoreStatic: boolean;
}

type Options = [Config];
export type Options = [Config];

type MessageIds = 'unbound';
export type MessageIds = 'unbound';

export default util.createRule<Options, MessageIds>({
name: 'unbound-method',
Expand Down Expand Up @@ -99,9 +99,9 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean {
}

function isSafeUse(node: TSESTree.Node): boolean {
const parent = node.parent!;
const parent = node.parent;

switch (parent.type) {
switch (parent?.type) {
case AST_NODE_TYPES.IfStatement:
case AST_NODE_TYPES.ForStatement:
case AST_NODE_TYPES.MemberExpression:
Expand All @@ -118,9 +118,6 @@ function isSafeUse(node: TSESTree.Node): boolean {
case AST_NODE_TYPES.ConditionalExpression:
return parent.test === node;

case AST_NODE_TYPES.LogicalExpression:
return parent.operator !== '||';

case AST_NODE_TYPES.TaggedTemplateExpression:
return parent.tag === node;

Expand All @@ -134,6 +131,16 @@ function isSafeUse(node: TSESTree.Node): boolean {
case AST_NODE_TYPES.TSAsExpression:
case AST_NODE_TYPES.TSTypeAssertion:
return isSafeUse(parent);

case AST_NODE_TYPES.LogicalExpression:
if (parent.operator === '&&' && parent.left === node) {
// this is safe, as && will return the left if and only if it's falsy
return true;
}

// in all other cases, it's likely the logical expression will return the method ref
// so make sure the parent is a safe usage
return isSafeUse(parent);
}

return false;
Expand Down
Loading

0 comments on commit 9c5b857

Please sign in to comment.