-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add requireTypeCheck option to guard-super-call #135
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,13 +21,18 @@ const rule: Rule.RuleModule = { | |
messages: { | ||
guardSuperCall: | ||
'Super calls to lifecycle callbacks should be guarded in case the base class does not implement them' | ||
} | ||
}, | ||
schema: [{ | ||
requireTypeCheck: {type: 'boolean'} | ||
}] | ||
}, | ||
|
||
create(context): Rule.RuleListener { | ||
let insideNonNativeElement = false; | ||
let errNode = null; | ||
const source = context.getSourceCode(); | ||
const options = context.options?.[0] ?? {}; | ||
const requireTypeCheck = options.requireTypeCheck ?? false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. its probably better to just compare here, like |
||
|
||
const nativeHooks = [ | ||
'connectedCallback', | ||
|
@@ -90,6 +95,20 @@ const rule: Rule.RuleModule = { | |
); | ||
} | ||
|
||
/** | ||
* Determines if an if statement is a correct super hook guard | ||
* @param {ESTree.IfStatement} node Node to test | ||
* @param {string} hook hook to test | ||
* @return {boolean} | ||
*/ | ||
function isCorrectSuperHookGuard(node, hook) { | ||
return node.test.type === 'BinaryExpression' && | ||
node.test.left.operator === 'typeof' && | ||
isSuperHook(node.test.left.argument, hook) && | ||
node.test.right.type === 'Literal' && | ||
node.test.right.value === 'function'; | ||
} | ||
|
||
/** | ||
* Determines if a statement is an unguarded super hook | ||
* @param {ESTree.Statement} node Node to test | ||
|
@@ -100,7 +119,11 @@ const rule: Rule.RuleModule = { | |
if (isSuperHookExpression(node, hook)) { | ||
errNode = node; | ||
return true; | ||
} else if (node.type === 'IfStatement' && !isSuperHook(node.test, hook)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think i'd leave this as it is and just change inside if (requireTypeCheck) {
return /* whatever a type checked super hook looks like */
}
return /* whatever a regular super hook looks like */
|
||
} else if ( | ||
node.type === 'IfStatement' && | ||
!isCorrectSuperHookGuard(node, hook) && | ||
!(!requireTypeCheck && isSuperHook(node.test, hook)) | ||
) { | ||
return isUnguardedSuperHook(node.consequent, hook); | ||
} else if ( | ||
node.type === 'BlockStatement' && | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this object is meant to have a type i think, like: