Make no-restricted-imports apply to dynamic imports
See original GitHub issueWhat rule do you want to change? no-restricted-imports
Does this change cause the rule to produce more or fewer warnings? More
How will the change be implemented? (New option, new default behavior, etc.)? New default behaviour would make sense, although could be an opt-in via option too.
Please provide some example code that this change will affect:
await import('restricted');
What does the rule currently do for this code? Nothing.
What will the rule do after it’s changed? Report an error just as if it was a sync import.
Are you willing to submit a pull request to implement this change? Maybe
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:14 (11 by maintainers)
Top Results From Across the Web
no-restricted-imports - ESLint - Pluggable JavaScript Linter
Rule Details. This rule allows you to specify imports that you don't want to use in your application. It applies to static imports...
Read more >no-restricted-imports | typescript-eslint
Disallow specified modules when loaded by import . Examples. This rule extends the base eslint/no-restricted-imports rule. How to Use.
Read more >Lint rule "no-restricted-imports" throw error when patterns ...
When I add group to pattern attribute to no-restricted-import ... message: "Please use the default import from 'lodash' instead." }]}]*/.
Read more >Minimizing bundle size - Material UI - MUI
If you are using Create React App, you will need to use a couple of projects that let you use .babelrc configuration, without...
Read more >Using ESLint to restrict where files can be imported from
To prevent importing certain npm packages, you could use ESLint's native rule no-restricted-imports . A useful example case is if you want ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If this enhancement doesn’t reach consensus, an alternative is to configure no-restricted-syntax rule.
For example, this disallows importing
foo
and anything frombar/
:Online demo link
New default behavior would be a breaking change, because the rule currently doesn’t check dynamic imports and ESLint already supports dynamic imports for over a year now.
Since this change results in ESLint reporting more linting errors, and it isn’t a bug fix, per our semver-policy we would have to either wait for the next major version, or add an option with a default value that retains the current behavior of this rule.
ecmaVersion >= 2020
is basically the same as not checkingecmaVersion
at all, because this syntax isn’t available inecmaVersion < 2020
.Sorry, I wasn’t clear enough! There are certainly valid use cases, like with static imports.
The problem is that, unlike with static imports, we often can’t know the import source. Dynamic imports have dynamic specifiers;
import()
syntax allows expressions inside()
:We could say that the new option applies only to dynamic imports with static specifiers (i.e. string literals, like
import("foo")
).Still, it might be confusing when combined with other configuration options. For example, user might expect an error on the dynamic import in the following case (assuming that the new option is enabled):
Also, the rule won’t be able to restrict just certain names from a module: