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(lint/noRestrictedImports): add rule #1723

Merged
merged 3 commits into from
Feb 6, 2024

Conversation

anonrig
Copy link
Contributor

@anonrig anonrig commented Jan 31, 2024

Adds no-restricted-imports rule to Biome.

Copy link

netlify bot commented Jan 31, 2024

Deploy Preview for biomejs ready!

Name Link
🔨 Latest commit 38d464c
🔍 Latest deploy log https://app.netlify.com/sites/biomejs/deploys/65c24d3c55056e00087c0ea5
😎 Deploy Preview https://deploy-preview-1723--biomejs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 99 (🟢 up 4 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 93 (no change from production)
PWA: -
View the detailed breakdown and full score reports

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

@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter A-Website Area: website L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels Jan 31, 2024
Copy link

codspeed-hq bot commented Jan 31, 2024

CodSpeed Performance Report

Merging #1723 will not alter performance

Comparing anonrig:add-no-restricted-imports (38d464c) with main (7823cee)

Summary

✅ 93 untouched benchmarks

Copy link
Contributor

@arendjr arendjr left a comment

Choose a reason for hiding this comment

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

I like this rule, but would it be possible to merge this with useImportRestrictions? It means the logic for the rule should also be put behind a config option, but given the naming I think it would be too confusing to have them both. I think I would call the option for this rule something like bannedModules if they were combined.

I’m not sure how to put the config inside the rule documentation unfortunately…

@arendjr
Copy link
Contributor

arendjr commented Feb 1, 2024

Btw, we’ve had some discussion in the past about where we want to go with the useImportRestrictions rule, which can still be found over here: rome/tools#4678

Unfortunately most of the ideas got blocked on needing project-level analysis (on the roadmap for 2024 now), so only the package private logic ended up being implemented. And there’s no config yet, since we only have one option implemented :)

So if you’re okay with adding bannedModules to the configuration for useImportRestrictions, then we can put the existing logic behind the packagePrivate option we discussed there. It’s fine to make ”none” the default, and setting it to ”all” would enable the existing logic in that rule.

Comment on lines 20 to 32
/// ```json
/// {
/// "noRestrictedImports": {
/// "options": {
/// "paths": ["lodash"]
/// }
/// }
/// }
/// ```
///
/// ```js
/// const lodash = require("lodash");
/// ```
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, we don't support setting options in example. We could just document the option. Any idea @ematipico ?

Copy link
Member

Choose a reason for hiding this comment

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

It could be an excellent addition and maybe a good first issue. As for now, we only document the options.

@anonrig anonrig force-pushed the add-no-restricted-imports branch from 8b627ef to 16e04e9 Compare February 1, 2024 16:35
@anonrig anonrig requested review from arendjr and Conaclos February 1, 2024 16:36
@anonrig anonrig force-pushed the add-no-restricted-imports branch from 16e04e9 to e34a2c1 Compare February 1, 2024 16:46
@ematipico
Copy link
Member

ematipico commented Feb 2, 2024

This rule, without options, does nothing. This means that the options are mandatory for this rule, and it is in contrast with all the rules we have.

Which means that if I enabled this rule like this:

{
	"noRestrictedImports": "warn"
}

Biome is happy, but the rule is never triggered. I have mixed feelings about creating a rule that only works if options are passed. And our infrastructure/deserialization wasn't designed for this case.

I like @arendjr suggestion. We could merge the options/logic of this rule to useImportRestrictions, although the options of this rules have a different objective compared to useImportRestrictions. The latter prevents the use of sub-paths.

So here my proposal:

  1. Merge this rule
  2. Update useImportRestrictions to accept two options:
    • bannedModules: the options of this rule
    • allowPrivateImports: allows to turn off the main logic of useImportRestrictions
  3. Add the logic of this rule to useImportRestrictions
  4. Delete noRestrictedImports

@anonrig @arendjr @Conaclos, how does that sound?

@anonrig if you like the idea, would like to take charge of the proposal?

@arendjr
Copy link
Contributor

arendjr commented Feb 2, 2024

@ematipico Sounds like a plan! 👍

We may just need to bikeshed a bit about the naming for the allowPrivateImports option :) I do think it should at least remain an enum, so we can add a third option to support package private annotations when we have the infra to analyze annotations from imports.

@ematipico
Copy link
Member

Always open to bikeshed names! I'm bad at that

@anonrig
Copy link
Contributor Author

anonrig commented Feb 2, 2024

I personally think we should have 1-to-1 correlation when we are migrating existing eslint rules but I'm ok with merging it with another rule as well.

Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Looks good, just a change in the documentation and a nit about the code

@Conaclos
Copy link
Member

Conaclos commented Feb 2, 2024

This rule, without options, does nothing. This means that the options are mandatory for this rule, and it is in contrast with all the rules we have.

Which means that if I enabled this rule like this:

{
	"noRestrictedImports": "warn"
}

Biome is happy, but the rule is never triggered. I have mixed feelings about creating a rule that only works if options are passed. And our infrastructure/deserialization wasn't designed for this case.

I like @arendjr suggestion. We could merge the options/logic of this rule to useImportRestrictions, although the options of this rules have a different objective compared to useImportRestrictions. The latter prevents the use of sub-paths.

So here my proposal:

1. Merge this rule

2. Update `useImportRestrictions` to accept two options:
   
   * `bannedModules`: the options of **this** rule
   * `allowPrivateImports`: allows to turn **off** the main logic of `useImportRestrictions`

3. Add the logic of **this** rule to `useImportRestrictions`

4. Delete `noRestrictedImports`

@anonrig @arendjr @Conaclos, how does that sound?

@anonrig if you like the idea, would like to take charge of the proposal?

I have mixed feeling about merging the two rules together. Maybe the name useImportRetrictions is not enough informative? I don't remember the rationals behind useImportRetrictions. I need more time to think about that. In the meantime we could merge this PR. The rule is a nursery rule, we could still change it / merge it with useImportRetrictions before the 1.6 release or in a future release.

@anonrig anonrig force-pushed the add-no-restricted-imports branch from e34a2c1 to ee5a205 Compare February 5, 2024 17:11
@anonrig
Copy link
Contributor Author

anonrig commented Feb 5, 2024

I think this PR is ready to merge. Regarding merging with another rule, we can make that decision later since this is a Nursery linting rule

@anonrig anonrig requested a review from ematipico February 5, 2024 17:12
@ematipico ematipico merged commit ca3e3c1 into biomejs:main Feb 6, 2024
15 of 18 checks passed
@anonrig anonrig deleted the add-no-restricted-imports branch February 13, 2024 18:32
@yann-combarnous
Copy link

Nice, any plan to also support the patterns option from https://eslint.org/docs/latest/rules/no-restricted-imports?

@Conaclos
Copy link
Member

@yann-combarnous

Nice, any plan to also support the patterns option from https://eslint.org/docs/latest/rules/no-restricted-imports?

I plan to add this in a future version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project A-Website Area: website L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants