Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/rules/guard-super-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'}
Copy link
Owner

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:

    schema: [
      {
        type: 'object',
        properties: {
          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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its probably better to just compare here, like options.requireTypeCheck === true


const nativeHooks = [
'connectedCallback',
Expand Down Expand Up @@ -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
Expand All @@ -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)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i'd leave this as it is and just change isSuperHook to accept an extra requireTypeCheck parameter

inside isSuperHook, we could then:

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' &&
Expand Down