feat(linter): support dynamic import() in no-restricted-imports#21486
feat(linter): support dynamic import() in no-restricted-imports#21486costajohnt wants to merge 6 commits into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
68581ef to
70b3964
Compare
Add handling for `import()` expressions with string literal arguments in the `no-restricted-imports` rule. When the source is a string literal, it is checked against the configured `paths` and `patterns` restrictions. Note: `importNames` filtering does not apply to dynamic imports since they have no named bindings — only path-level restrictions are checked, matching the behavior of the existing TSImportEqualsDeclaration handler. Closes oxc-project#21411
Fixes clippy::needless_raw_string_hashes lint warnings in dynamic import test strings.
685dfe7 to
9e9e138
Compare
|
@camc314 @costajohnt I made some changes here:
Also @camc314 just noting that this isn't technically a breaking change as we have defined it, since this is basically just adding support for "new" syntax which yields better analysis. https://oxc.rs/docs/guide/usage/linter/versioning.html#are-new-lint-errors-a-breaking-change |
|
Thanks for picking this up @camchenry, much appreciated. The destructure scope feels right to me. That pattern covers basically every real use of dynamic import I've seen, and chasing the dynamic key variants would have been a rabbit hole. |
|
Hey @camc314, when you have a minute could you take another look at this. Both of the points you raised back in April are in the diff now. The CI is green and the change is mergeable. If you've moved on from this and want to drop the changes-requested review, no worries either way. |
Summary
ImportExpressionhandling tono-restricted-importsso thatimport('source')with string literal arguments is checked against configuredpathsandpatternsimportNamesfiltering is skipped since dynamic imports have no named bindingsWhy
The rule currently only handles static
import/exportdeclarations andTSImportEqualsDeclaration. Dynamicimport()expressions bypass restrictions entirely, allowing restricted modules to be imported at runtime.Design
The implementation follows the existing
report_ts_import_equals_declaration_allowed()pattern, using the sameget_string_literal_result()method on bothRestrictedPathandRestrictedPattern. Non-string-literal arguments (variables, template literals with substitutions) are ignored since the source cannot be statically analyzed.Test cases
Pass (4): non-matching source, non-string argument,
importNamesdoesn't apply to dynamic import, template literalFail (5): simple string restriction, path with message, pattern with glob, pattern with group/message, regex pattern
Closes #21411