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
Update: fix no-restricted-imports importNames reporting (fixes #12282) #12711
Update: fix no-restricted-imports importNames reporting (fixes #12282) #12711
Conversation
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.
Thanks for the PR! I didn't check everything, just noticed one change and have a question about that.
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 left some notes about *
and default
.
Perhaps we should also consider refactoring the rule.
The actual flow in this PR is:
checkNode()
collectsimportNames
and callsisRestrictedPath()
to find out is the import invalid.isRestrictedPath()
usesimportNames
and configuration to returntrue
orfalse
.- If it's
true
,reportPath()
is called. Then, this function is supposed to report something, and do it with the precise locations.
The problem is - reportPath()
doesn't get any useful info from the previous steps, apart from knowing that there's something invalid because it's being called? So, it basically repeats steps 1 and 2., meaning that these steps were almost useless. Maybe we should do everything in checkNode()
, instead.
I refactored it a bit. |
Thanks for the changes! I think there is no need to keep the existing structure of the code if it doesn't provide value for the new behavior. In particular, it feels like function reportPath(importSource, importNames, node) {
if (!restrictedPathMessages[importSource]) {
return;
}
// ....
} then we could rename My idea was actually to simplify the code and do most of the logic in |
Seems like a good idea! In this case I think that |
I agree if it seems that the code would become easier to follow.
Agreed, making the |
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.
Thanks! I left a couple of notes just about naming, and one detail that I believe should be fixed.
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 looks very good! I just found a couple of minor details and I think there is one possible regression.
Thank you for review and suggestions. And sorry for the long journey with this PR :) |
Thanks for making all the changes and quickly addressing all concerns! It turned out that fixing this issue requires a quite big diff in the code. As for the title of this PR, it might be safe to mark this as a semver-minor change ( Thoughts about something like this?
|
About edge cases where the same name is imported multiple times in the same declaration, such as these: import { a, a as b } from "mod";
export { x as y, x as z } from "mod";
import foo, { default as bar } from "mod"; It might be correct to report all specifiers for a restricted name instead of just the last one, if it doesn't add too much complexity to the code. Maybe we could store arrays in It also seems that the configuration allows duplicates in |
Sounds good! |
Changed the title to Regarding the cases where the same name is imported multiple times, I think it's fine to report just 1 specifier for now. Maybe we could change that later in a separate PR, if it looks useful. |
The problem here is the clause |
I don't think it would be a problem. If there is In the In the above examples: import { a, a as b } from "mod";
export { x as y, x as z } from "mod";
AST explorer link with the examples What I was thinking is, for example in the first case if |
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 looks great, thanks for the fix and the refactoring!
Sorry for the delay. It's just new year holidays that is over and I've got a big task at work :) But I would like to add your proposal about duplicate import names. I think I could add it till Monday. And also don't you think it would be better to put all logic from |
That would be great, thanks!
Something like my initial idea? I'm not sure about that anymore, the way it's implemented now looks very nice (i.e. deal with different kinds of imports/exports first to get the Also, it looks like the actual version in this PR might make it easier to add some new things in the future, like to fix #12737 and support #12629. At least I think so, could be wrong. If you think that the other approach might be better for future maintenance and easier to follow, I'm fine with both. |
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.
Thanks, looks very good! Just two small details.
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.
LGTM, thanks!
Both rule and the tests look great 👍
@@ -95,6 +99,11 @@ module.exports = { | |||
const restrictedPaths = (isPathAndPatternsObject ? options[0].paths : context.options) || []; | |||
const restrictedPatterns = (isPathAndPatternsObject ? options[0].patterns : []) || []; | |||
|
|||
// if no imports are restricted we don"t need to check | |||
if (Object.keys(restrictedPaths).length === 0 && restrictedPatterns.length === 0) { |
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.
👍
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.
LGTM. Thanks for contributing to ESLint!
Thank you for your review and suggestions 👍 |
@fanich37 thanks again for the really good work and I hope to see more PRs from you in the future! 👍 |
…#12282) (eslint#12711) * Process importNames * Update test cases * Fix rebase issue * Update importNames logic * Remove useless funcs, update tests * Fix naming, fix everything imported w/o importNames * Fix typo, fix specifier clause, fix rebase issue * Process importNames with the same name * Clean code in receiving specifier data, remove debug * Fix type def, add empty name check, replace concat with push
What is the purpose of this pull request? (put an "X" next to item)
[X] Bug fix (it should fix #12282)
What changes did you make? (Give an overview)
Now rule can produce multiple errors depends on
importNames
option. This change allows applying directive comments on each item imported from the module.Is there anything you'd like reviewers to focus on?
Yes, the test cases since i'm too bad at it.