Skip to content

Fix CommonJS export destructuring emit#4404

Open
jakebailey wants to merge 3 commits into
mainfrom
jabaile/fix-4403
Open

Fix CommonJS export destructuring emit#4404
jakebailey wants to merge 3 commits into
mainfrom
jabaile/fix-4403

Conversation

@jakebailey

Copy link
Copy Markdown
Member

Fixes #4403

Copilot AI review requested due to automatic review settings June 23, 2026 04:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds regression coverage (and a small transformer fix) for a CommonJS emit bug where module-scope export const { … } = <importedValue> could lose the import namespace qualification on the RHS, leading to a runtime ReferenceError at module load (Fixes #4403).

Changes:

  • Fix CommonJS module transformer ancestor tracking when converting exported binding-pattern variable declarations into destructuring assignments.
  • Add a compiler repro test covering exported destructuring from imported values under --module commonjs (including array/object patterns, aliasing, default import, and default initializers).
  • Add an additional compiler test ensuring namespace-export destructuring preserves correct qualified references inside the namespace.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/transformers/moduletransforms/commonjsmodule.go Push/pop ancestor stack around synthesized destructuring assignment so identifier reference substitution (imports/exports) runs with the correct parent context.
testdata/tests/cases/compiler/commonjsExportDestructuringImportedValue.ts New regression test for CommonJS emit of export const {…} = importedValue (and related destructuring cases).
testdata/baselines/reference/compiler/commonjsExportDestructuringImportedValue.js Baseline validating RHS stays qualified (e.g. enum_1.CodePriceType) in emitted CommonJS output.
testdata/tests/cases/compiler/namespaceExportDestructuringReferences.ts New test ensuring namespace export destructuring uses the namespace-qualified references for computed names and default initializers.
testdata/baselines/reference/compiler/namespaceExportDestructuringReferences.js Baseline validating correct namespace-qualified emit (N.key, N.fallback, etc.).

@jakebailey jakebailey requested a review from weswigham June 23, 2026 17:14
@jakebailey jakebailey changed the title Add repro for CommonJS export destructuring emit Fix CommonJS export destructuring emit Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: CJS emit drops namespace qualifier on RHS of module-scope export const { … } = importedValue → ReferenceError

2 participants