@@ -21,13 +21,18 @@ const rule: Rule.RuleModule = {
21
21
messages : {
22
22
guardSuperCall :
23
23
'Super calls to lifecycle callbacks should be guarded in case the base class does not implement them'
24
- }
24
+ } ,
25
+ schema : [ {
26
+ requireTypeCheck : { type : 'boolean' }
27
+ } ]
25
28
} ,
26
29
27
30
create ( context ) : Rule . RuleListener {
28
31
let insideNonNativeElement = false ;
29
32
let errNode = null ;
30
33
const source = context . getSourceCode ( ) ;
34
+ const options = context . options ?. [ 0 ] ?? { } ;
35
+ const requireTypeCheck = options . requireTypeCheck ?? false ;
31
36
32
37
const nativeHooks = [
33
38
'connectedCallback' ,
@@ -90,6 +95,20 @@ const rule: Rule.RuleModule = {
90
95
) ;
91
96
}
92
97
98
+ /**
99
+ * Determines if an if statement is a correct super hook guard
100
+ * @param {ESTree.IfStatement } node Node to test
101
+ * @param {string } hook hook to test
102
+ * @return {boolean }
103
+ */
104
+ function isCorrectSuperHookGuard ( node , hook ) {
105
+ return node . test . type === 'BinaryExpression' &&
106
+ node . test . left . operator === 'typeof' &&
107
+ isSuperHook ( node . test . left . argument , hook ) &&
108
+ node . test . right . type === 'Literal' &&
109
+ node . test . right . value === 'function' ;
110
+ }
111
+
93
112
/**
94
113
* Determines if a statement is an unguarded super hook
95
114
* @param {ESTree.Statement } node Node to test
@@ -100,7 +119,11 @@ const rule: Rule.RuleModule = {
100
119
if ( isSuperHookExpression ( node , hook ) ) {
101
120
errNode = node ;
102
121
return true ;
103
- } else if ( node . type === 'IfStatement' && ! isSuperHook ( node . test , hook ) ) {
122
+ } else if (
123
+ node . type === 'IfStatement' &&
124
+ ! isCorrectSuperHookGuard ( node , hook ) &&
125
+ ! ( ! requireTypeCheck && isSuperHook ( node . test , hook ) )
126
+ ) {
104
127
return isUnguardedSuperHook ( node . consequent , hook ) ;
105
128
} else if (
106
129
node . type === 'BlockStatement' &&
0 commit comments