@@ -14,9 +14,9 @@ interface Config {
14
14
ignoreStatic : boolean ;
15
15
}
16
16
17
- type Options = [ Config ] ;
17
+ export type Options = [ Config ] ;
18
18
19
- type MessageIds = 'unbound' ;
19
+ export type MessageIds = 'unbound' ;
20
20
21
21
export default util . createRule < Options , MessageIds > ( {
22
22
name : 'unbound-method' ,
@@ -99,9 +99,9 @@ function isDangerousMethod(symbol: ts.Symbol, ignoreStatic: boolean): boolean {
99
99
}
100
100
101
101
function isSafeUse ( node : TSESTree . Node ) : boolean {
102
- const parent = node . parent ! ;
102
+ const parent = node . parent ;
103
103
104
- switch ( parent . type ) {
104
+ switch ( parent ? .type ) {
105
105
case AST_NODE_TYPES . IfStatement :
106
106
case AST_NODE_TYPES . ForStatement :
107
107
case AST_NODE_TYPES . MemberExpression :
@@ -118,9 +118,6 @@ function isSafeUse(node: TSESTree.Node): boolean {
118
118
case AST_NODE_TYPES . ConditionalExpression :
119
119
return parent . test === node ;
120
120
121
- case AST_NODE_TYPES . LogicalExpression :
122
- return parent . operator !== '||' ;
123
-
124
121
case AST_NODE_TYPES . TaggedTemplateExpression :
125
122
return parent . tag === node ;
126
123
@@ -134,6 +131,16 @@ function isSafeUse(node: TSESTree.Node): boolean {
134
131
case AST_NODE_TYPES . TSAsExpression :
135
132
case AST_NODE_TYPES . TSTypeAssertion :
136
133
return isSafeUse ( parent ) ;
134
+
135
+ case AST_NODE_TYPES . LogicalExpression :
136
+ if ( parent . operator === '&&' && parent . left === node ) {
137
+ // this is safe, as && will return the left if and only if it's falsy
138
+ return true ;
139
+ }
140
+
141
+ // in all other cases, it's likely the logical expression will return the method ref
142
+ // so make sure the parent is a safe usage
143
+ return isSafeUse ( parent ) ;
137
144
}
138
145
139
146
return false ;
0 commit comments