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

Update: no-restricted-{imports,modules}: add “patterns” (fixes #6963) #7433

Merged
merged 1 commit into from
Nov 5, 2016

Conversation

ljharb
Copy link
Sponsor Contributor

@ljharb ljharb commented Oct 23, 2016

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 and no-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:

import pick from 'lodash/pick';
/* or */
var pick = require('lodash/pick');

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.

@mention-bot
Copy link

@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.

@eslintbot
Copy link

LGTM

Copy link
Member

@platinumazure platinumazure left a 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 ';
Copy link
Member

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?

Copy link
Sponsor Contributor Author

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
Copy link
Member

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 }
});

Copy link
Sponsor Contributor Author

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 }
);
Copy link
Member

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
});
Copy link
Member

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 }
);
Copy link
Member

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.

@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Oct 23, 2016

@platinumazure no, import from 'foo/bar' is not the same as import { bar } from 'foo' in any way, unless the package author has gone out of their way to make it so. If you want to forbid specific named imports from a specific path or pattern (which I hadn't thought of), then I think perhaps a better schema is one that is always a single array of items - and an item can be a string path, { path: "some string" }, { pattern: "some pattern/*" }, and either of the object forms could then (now or in a followup PR) add something to handle named imports. The current schema doesn't really fit well with adding per-path named import blocking. Thoughts?

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.

@ljharb ljharb force-pushed the no_restricted_partial_paths branch from e5b6f52 to a7d24e5 Compare October 23, 2016 18:07
@eslintbot
Copy link

LGTM

@ljharb ljharb force-pushed the no_restricted_partial_paths branch from a7d24e5 to c616828 Compare October 23, 2016 18:28
@eslintbot
Copy link

LGTM

@ilyavolodin ilyavolodin added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Oct 23, 2016
@platinumazure
Copy link
Member

If the rule hadn't been handling named imports before now, I don't think it
needs to with this PR. I just wanted clarification, which you have
provided. Thanks!

On Oct 23, 2016 12:07 PM, "Jordan Harband" notifications@github.com wrote:

@platinumazure https://github.com/platinumazure no, import from
'foo/bar' is not the same as import { bar } from 'foo' in any way, unless
the package author has gone out of their way to make it so. If you want to
forbid specific named imports from a specific path or pattern (which I
hadn't thought of), then I think perhaps a better schema is one that is
always a single array of items - and an item can be a string path, {
path: "some string" }, { pattern: "some pattern/*" }, and either of the
object forms could then (now or in a followup PR) add something to handle
named imports. The current schema doesn't really fit well with adding
per-path named import blocking. Thoughts?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#7433 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AARWeuDvN-JmL8mvUWgmIpkLKP6YBpXXks5q25Q0gaJpZM4KeFen
.

@platinumazure
Copy link
Member

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.

@platinumazure platinumazure self-assigned this Oct 27, 2016
@ilyavolodin ilyavolodin added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Oct 27, 2016
Copy link
Member

@platinumazure platinumazure left a 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?

@ljharb ljharb force-pushed the no_restricted_partial_paths branch from c616828 to 6932e47 Compare October 31, 2016 07:19
@eslintbot
Copy link

LGTM

@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Oct 31, 2016

@platinumazure updated! I added mentions and examples to both readmes, as well as tests to both rules.

@nzakas
Copy link
Member

nzakas commented Oct 31, 2016

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.

@nzakas nzakas removed the CLA: Valid label Oct 31, 2016
@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Nov 4, 2016

@platinumazure ping! :-)

@ljharb
Copy link
Sponsor Contributor Author

ljharb commented Nov 4, 2016

Thanks everyone! Please let me know what else I should do to get this merged :-)

@eslintbot
Copy link

LGTM

@ilyavolodin ilyavolodin merged commit 3c379ff into eslint:master Nov 5, 2016
@ljharb ljharb deleted the no_restricted_partial_paths branch November 5, 2016 03:32
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow no-restricted-imports to restrict sub-paths of imports
9 participants