Skip to content

Commit 965fe08

Browse files
Add requireTypeCheck option to guard-super-call
This fixes 43081j#134.
1 parent 5836853 commit 965fe08

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/rules/guard-super-call.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,18 @@ const rule: Rule.RuleModule = {
2121
messages: {
2222
guardSuperCall:
2323
'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+
}]
2528
},
2629

2730
create(context): Rule.RuleListener {
2831
let insideNonNativeElement = false;
2932
let errNode = null;
3033
const source = context.getSourceCode();
34+
const options = context.options?.[0] ?? {};
35+
const requireTypeCheck = options.requireTypeCheck ?? false;
3136

3237
const nativeHooks = [
3338
'connectedCallback',
@@ -90,6 +95,20 @@ const rule: Rule.RuleModule = {
9095
);
9196
}
9297

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+
93112
/**
94113
* Determines if a statement is an unguarded super hook
95114
* @param {ESTree.Statement} node Node to test
@@ -100,7 +119,11 @@ const rule: Rule.RuleModule = {
100119
if (isSuperHookExpression(node, hook)) {
101120
errNode = node;
102121
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+
) {
104127
return isUnguardedSuperHook(node.consequent, hook);
105128
} else if (
106129
node.type === 'BlockStatement' &&

0 commit comments

Comments
 (0)