-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[refactor] namespace parent member expression validation into separate helper function #2584
base: main
Are you sure you want to change the base?
[refactor] namespace parent member expression validation into separate helper function #2584
Conversation
src/core/namespaceValidator.js
Outdated
if (onComputed(node) === 'return') { | ||
return; | ||
} | ||
} | ||
|
||
if (onNamespaceNotFound) { | ||
if (onNamespaceNotFound(node, namespace, namepath) === 'return') { | ||
return; | ||
} |
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.
hm, do we need these early returns?
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.
I re-produced exact flow as in namespace
rules: https://github.com/import-js/eslint-plugin-import/blob/main/src/rules/namespace.js. Lines 124-140.
Line 139 of original namespace rule has break
instead of return, but because nothing comes after while
loop, break
does the same thing as return
?
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.
Are there any cases of calling onNamespaceNotFound
where we don't want to break/return?
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.
Yes. onNamespaceNotFound
is only used in namespace rule.
If we can find node's name in namespace map, then but just proceed execution. We only return/break when we cannot find node's name in namespace map.
Below is onNamespaceNotFound
in namespace
rule, we pass what's below to our helper function.
function onNamespaceNotFound(context) {
return function inner(node, namespace, namepath) {
if (!namespace.has(node.property.name)) {
context.report(
node.property,
makeMessage(node.property, namepath),
);
return 'return';
}
};
}
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.
gotcha, hmm.
not a fan of returning the string "return", maybe it could be a true or something, but returning a sentinel value seems to be the only solution.
ahhhh wait! what if the inner
callback took a fourth argument, that meant "return", for it to return? that way "what kind of value if is" is up to the helper implementation
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.
That's brilliant! I implemented it here: azyzz228@5985656. Because no-deprecated rule for now just skips computed, meanwhile namespace
one works through it and takes in node
as an argument, I used arguments[2]
in no-deprecated's onComputed
to communicate return to namespaceValidator
e305ed0
to
5985656
Compare
5985656
to
516b80a
Compare
@azyzz228 this PR will need a rebase, and unfortunately it doesn't seem trivial. |
Two slowest rules
namespace
andno-deprecated
used almost same lines of code for validating member expressions. I refactored validation function into separate helper functionnamespaceValidator
. Then, I changednamespace
andno-deprecated
to usenamespaceValidator
function.