-
-
Notifications
You must be signed in to change notification settings - Fork 739
fix(transformer/typescript): remove unused import equals declaration #16776
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
base: main
Are you sure you want to change the base?
fix(transformer/typescript): remove unused import equals declaration #16776
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
CodSpeed Performance ReportMerging #16776 will not alter performanceComparing Summary
Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR attempts to fix the removal of unused TypeScript import equals declarations by marking their references as type references before transformation. The implementation moves the reference marking logic from the declaration removal phase to an earlier enter_program phase and processes import chains in reverse order to handle dependencies correctly.
Key changes:
- Adds
mark_unused_import_equals_references_as_typemethod that processes import equals declarations before transformation - Processes imports in reverse order to correctly handle chains where one import references another
- Removes inline reference marking code from
transform_ts_import_equals
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
crates/oxc_transformer/src/typescript/module.rs |
Adds enter_program hook and two new helper methods for marking unused import equals references; removes inline reference marking from transformation logic |
crates/oxc_transformer/src/typescript/mod.rs |
Calls module.enter_program to mark unused import references before transformation |
tasks/transform_conformance/tests/.../remove-unused-import-equals/input.ts |
Test case with used/unused import chains and namespace with unused imports |
tasks/transform_conformance/tests/.../remove-unused-import-equals/output.js |
Expected output showing only used imports transformed and unused ones removed |
tasks/transform_conformance/snapshots/oxc.snap.md |
Test snapshot showing semantic mismatches indicating the test doesn't fully pass yet |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
...e/tests/babel-plugin-transform-typescript/test/fixtures/remove-unused-import-equals/input.ts
Show resolved
Hide resolved
...e/tests/babel-plugin-transform-typescript/test/fixtures/remove-unused-import-equals/input.ts
Show resolved
Hide resolved
...e/tests/babel-plugin-transform-typescript/test/fixtures/remove-unused-import-equals/input.ts
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new unused import = pre-pass is directionally good, but it currently only scans top-level statements and misses nested import = declarations inside namespaces/modules, which is a correctness issue (and likely related to the new snapshot mismatches). Additionally, moving reference-flag mutation to enter_program introduces ordering assumptions about when semantic references are finalized. Finally, the strict debug_assert_eq!(..., ReferenceFlags::Read) makes the transformer fragile to future semantic-flag changes.
Additional notes (1)
- Maintainability |
crates/oxc_transformer/src/typescript/mod.rs:81-85
The new pre-pass marks module-reference identifiers asTypeonly when the binding has all-type references. This assumes thatget_resolved_references(decl.id.symbol_id())is fully populated and stable atenter_programtime.
If any later traversal phase can create additional value references to that symbol (e.g., via desugaring, namespace transforms, or other TS passes that synthesize identifiers), this pre-pass can incorrectly flip a Read to Type too early, potentially causing an actually-used value import to be treated as type-only downstream.
Given you now call this at enter_program, it’s worth asserting/ensuring that no subsequent pass introduces new value references to TSImportEqualsDeclaration bindings (or that such passes run before this marking).
Summary of changes
What changed
- TypeScript transformer traversal order:
TypeScriptModule::enter_programis now invoked fromTypeScript::enter_program(crates/oxc_transformer/src/typescript/mod.rs). - Refactor of unused
import =handling: moved the logic that re-flags module-reference identifiers fromRead→Typeout oftransform_ts_import_equalsand into a dedicated pre-pass:- Added
TypeScriptModule::enter_programwhich callsmark_unused_import_equals_references_as_type. - Implemented reverse-order scanning of top-level statements to correctly handle chained
import x = y.zrelationships.
- Added
- Conformance coverage: added a new fixture
remove-unused-import-equalsand updated snapshot counts/results.
Files touched:
crates/oxc_transformer/src/typescript/mod.rscrates/oxc_transformer/src/typescript/module.rstasks/transform_conformance/snapshots/oxc.snap.mdtasks/transform_conformance/tests/.../remove-unused-import-equals/{input.ts,output.js}

close: #16628