-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: no-restricted-{imports,modules}
: add “patterns” (fixes #6963)
#7433
Conversation
@ljharb, thanks for your PR! By analyzing the history of the files in this pull request, we identified @vitorbal, @mysticatea and @makepanic to be potential reviewers. |
LGTM |
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.
Besides the inline changes, could you update the docs to specify that we're using gitignore-style patterns here (and bonus if you can link to useful documentation on the supported syntax)?
Also, for no-restricted-imports, should a named import also be matched against a pattern (e.g., foo/bar
pattern should match statement import bar from "foo"
?).
```js | ||
/*eslint no-restricted-imports: ["error", { "patterns": ["lodash/*"] }]*/ | ||
|
||
import pick from 'lodash/pick '; |
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.
Trailing space in the module path, presumably a typo?
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.
whoops, yes, i'll remove that
context.report(node, "'{{importName}}' import is restricted from being used.", { | ||
importName: value | ||
importName |
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.
While you're in here, can we switch this rule to use new-style reporting? That takes a single object argument and looks like this:
context.report({
node,
message: MESSAGE,
data: { importName }
});
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.
Sure!
node, | ||
"'{{importName}}' import is restricted from being used by a pattern.", | ||
{ importName } | ||
); |
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.
Could this use new-style reporting as well?
if (restrictedPaths.has(moduleName)) { | ||
context.report(node, "'{{moduleName}}' module is restricted from being used.", { | ||
moduleName | ||
}); |
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.
Using new-style reporting here would be awesome as well!
node, | ||
"'{{moduleName}}' module is restricted from being used by a pattern.", | ||
{ moduleName } | ||
); |
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.
Another opportunity for new-style reporting.
@platinumazure no, Edit: another alternative is to keep the current single-object schema, but allow each of the strings to alternatively be one of the objects in question - a bit messy, but that is a path from the current PR's schema to named-import-blocking-per-path. |
e5b6f52
to
a7d24e5
Compare
LGTM |
a7d24e5
to
c616828
Compare
LGTM |
If the rule hadn't been handling named imports before now, I don't think it On Oct 23, 2016 12:07 PM, "Jordan Harband" notifications@github.com wrote:
|
I'll champion this. We need one more 👍 from the team (someone who isn't vitorbal or not-an-aardvark) before this can be accepted and (hopefully) merged. |
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.
@ljharb I don't see anything about what sort of patterns are accepted (i.e., that they're gitignore-like patterns). Can that be added to documentation somewhere? Doesn't have to be in-depth at all. And maybe a few examples (and test cases?) about **
could be cool too?
c616828
to
6932e47
Compare
LGTM |
@platinumazure updated! I added mentions and examples to both readmes, as well as tests to both rules. |
Just a heads up: we have moved to a new CLA checker on pull requests. Even if you've previously signed our CLA, we will need to you sign the new one. To do so, look at the status checks for licence/cla and click the "Details" link. Sorry for the inconvenience. |
@platinumazure ping! :-) |
Thanks everyone! Please let me know what else I should do to get this merged :-) |
6932e47
to
3748523
Compare
LGTM |
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What rule do you want to change?
no-restricted-imports
andno-restricted-modules
Does this change cause the rule to produce more or fewer warnings?
More, only when the "patterns" option is set.
How will the change be implemented? (New option, new default behavior, etc.)?
New schema - the existing schema of an array of strings will continue to work; an alternative object schema with "paths" will also be accepted. A "patterns" property can be added to this object to opt in to the new behavior.
Please provide some example code that this change will affect:
What does the rule currently do for this code?
Does not warn unless the exact full path is provided.
What will the rule do after it's changed?
Allow a pattern to be provided such that a partial path may be provided.
Fixes #6963.