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

feat!: no-restricted-imports allow multiple config entries for same path #18021

Merged
merged 3 commits into from Jan 24, 2024

Conversation

mdjermanovic
Copy link
Member

@mdjermanovic mdjermanovic commented Jan 23, 2024

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:

Fixes #15261

What changes did you make? (Give an overview)

Updated no-restricted-imports to check all config entries that have a matching name instead of checking just the last one.

Is there anything you'd like reviewers to focus on?

Matching entries produce errors separately from each other. I believe this is the simplest yet the most flexible solution, but a downside may be that in some cases the rule produces multiple errors (see the added test cases).

TODO: update migration guide

@mdjermanovic mdjermanovic added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion feature This change adds a new feature to ESLint labels Jan 23, 2024
@mdjermanovic mdjermanovic requested a review from a team as a code owner January 23, 2024 11:56
@eslint-github-bot eslint-github-bot bot added the breaking This change is backwards-incompatible label Jan 23, 2024
Copy link

netlify bot commented Jan 23, 2024

Deploy Preview for docs-eslint ready!

Name Link
🔨 Latest commit af634b6
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/65b107ac187e120008fd279c
😎 Deploy Preview https://deploy-preview-18021--docs-eslint.netlify.app/use/migrate-to-9.0.0
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Comment on lines +1119 to +1151
{
code: "import * as mod from 'mod'",
options: [{
paths: [
{
name: "mod",
importNames: ["foo"],
message: "Import foo from qux instead."
},
{
name: "mod",
importNames: ["bar"],
message: "Import bar from qux instead."
}
]
}],
errors: [
{
message: "* import is invalid because 'foo' from 'mod' is restricted. Import foo from qux instead.",
type: "ImportDeclaration",
line: 1,
column: 8,
endColumn: 16
},
{
message: "* import is invalid because 'bar' from 'mod' is restricted. Import bar from qux instead.",
type: "ImportDeclaration",
line: 1,
column: 8,
endColumn: 16
}
]
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps this could be a single error with a concatenated message, but that would be complicated as these texts are generated by interpolating data, and I'm not sure if one big message would be better than multiple errors.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's fine the way it is. If people are configuring the rule this way then we can let them decide to change how it's configured if this isn't the behavior they want.

Comment on lines +1180 to +1214
{
code: "import { bar } from 'mod'",
options: [{
paths: [

// restricts importing anything from the module
{
name: "mod"
},

// message for a specific import name
{
name: "mod",
importNames: ["bar"],
message: "Import bar from qux instead."
}
]
}],
errors: [
{
message: "'mod' import is restricted from being used.",
type: "ImportDeclaration",
line: 1,
column: 1,
endColumn: 26
},
{
message: "'bar' import from 'mod' is restricted. Import bar from qux instead.",
type: "ImportDeclaration",
line: 1,
column: 10,
endColumn: 13
}
]
},
Copy link
Member Author

Choose a reason for hiding this comment

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

This produces two errors: one for the whole import, and another specifically for the imported name bar. I think it's fine and technically correct.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Would it make sense to add some tests where importNames has more than one element?

@mdjermanovic
Copy link
Member Author

Overall LGTM. Would it make sense to add some tests where importNames has more than one element?

I've now added a couple of tests where importNames has more than one element.

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@nzakas nzakas merged commit 57089cb into main Jan 24, 2024
18 checks passed
@nzakas nzakas deleted the issue15261 branch January 24, 2024 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion breaking This change is backwards-incompatible feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
Status: Complete
2 participants