fix(types): make StaticImport/TypeImport imports optional - #362
Open
MFA-G wants to merge 1 commit into
Open
Conversation
Side effect imports such as `import "styles.css"` have no import clause, so the `imports` named capture group of ESM_STATIC_IMPORT_RE is undefined. The type declared it as a required `string`, so consumers had no indication they needed a guard and were free to call string methods on undefined. Mark `imports` optional on both interfaces, widen `clearImports` to accept `undefined` (its body already coerced falsy input to ""), guard the `findTypeImports` filter, and add a regression test. Fixes unjs#349
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
📝 WalkthroughWalkthroughThe import analysis types now allow missing import clauses for side-effect imports. Utility handling accepts undefined values, type-import filtering uses a safe fallback, and tests verify the parsed side-effect import shape. ChangesSide-effect import support
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #349
Problem
ESM_STATIC_IMPORT_REhas an optionalimportscapture group, because a side effect import has no import clause:So for a side effect import the property is simply absent at runtime:
But
StaticImport['imports'](andTypeImport['imports']) declared it as a requiredstring. TypeScript therefore told consumers the value was always present, and code likematch.imports.includes("type")type-checked cleanly while throwing at runtime.The existing suite has a side effect import case, but it only asserts the
specifier, so it never caught the mismatch.Fix
importsis nowimports?: stringonStaticImportandTypeImport, with a doc comment explaining when it isundefined.clearImportsacceptsstring | undefined. Its body already did(imports || ""), so this only makes the signature match the behaviour it already had —parseStaticImport/parseTypeImportkeep working unchanged on side effect imports.findTypeImportsguards the filter withmatch.imports ?? "", which was the one place that fed the possibly-undefined value straight intoRegExp.test(it previously relied onteststringifyingundefinedto"undefined").importsisundefinedand thatparseStaticImportstill yields emptynamedImports.Compatibility
This is a type-only change for a value that was already
undefinedat runtime. It is strictly more accurate, though downstream code that assumed a non-optionalstringunderstrictwill now surface the guard it was always missing — which is the point of the issue.Validation
vitest run— 199/199 pass (6 files)tsc --noEmit— cleaneslint src test— cleanprettier --check src test— cleanunbuild— builds successfullySummary by CodeRabbit
New Features
Bug Fixes
Tests