Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Dedupe cjs export assignments - #4284

Merged
Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:dedupe-cjs-export-assignments
Jun 11, 2026
Merged

Wesley Wigham (weswigham) merged 4 commits into
microsoft:mainfrom
weswigham:dedupe-cjs-export-assignments

Conversation

@weswigham

Copy link
Copy Markdown
Member

Fixes #2533

This is weird, since you can end up needing to pull the type node from a different declaration than you actually encounter and emit. It's not insurmountable for other emitters, though (if they do some rudimentary binding...).

It's also just weird in that in TS, we'd have emitted every declaration the user wrote, so the error in the input (if multiple declarations have mismatched types) would persist into the output.

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

This PR addresses #2533 by deduplicating declaration emit output for CommonJS exports.* assignments, reducing repeated export declare var ... entries and aligning related type-serialization behavior used during .d.ts generation.

Changes:

  • Track and suppress repeated CommonJS export names during declaration transformation.
  • Improve type-node reuse for CommonJS export assignment declarations by selecting an annotated declaration when the current binary expression can’t provide a reusable pseudo-type.
  • Add a regression compiler test and update baselines to reflect the deduped .d.ts output (including affected submodule conformance baselines/diffs).

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
testdata/tests/cases/compiler/cjsExportsDuplication.ts New regression test covering repeated exports.foo assignments with and without JSDoc typing.
testdata/baselines/reference/submoduleAccepted/conformance/moduleExportDuplicateAlias3.js.diff Updates accepted-diff baseline to remove repeated emitted apply declarations.
testdata/baselines/reference/submoduleAccepted/conformance/moduleExportDuplicateAlias2.js.diff Updates accepted-diff baseline to reflect deduped apply export emission.
testdata/baselines/reference/submoduleAccepted/conformance/moduleExportDuplicateAlias.js.diff Updates accepted-diff baseline to reflect deduped apply export emission.
testdata/baselines/reference/submodule/conformance/moduleExportDuplicateAlias3.js Updates submodule reference baseline output to remove duplicate emitted declarations.
testdata/baselines/reference/submodule/conformance/moduleExportDuplicateAlias2.js Updates submodule reference baseline output to remove duplicate emitted declarations.
testdata/baselines/reference/submodule/conformance/moduleExportDuplicateAlias.js Updates submodule reference baseline output to remove duplicate emitted declarations.
testdata/baselines/reference/compiler/numericExportNameDeclaration.js Updates baseline to remove duplicate "1" export declarations and resulting duplicate-identifier errors.
testdata/baselines/reference/compiler/cjsExportsDuplication.types New baseline for types output of the new regression test.
testdata/baselines/reference/compiler/cjsExportsDuplication.symbols New baseline for symbols output of the new regression test.
testdata/baselines/reference/compiler/cjsExportsDuplication.js New baseline for emitted output (including .d.ts) of the new regression test.
testdata/baselines/reference/compiler/cjsExportsDuplication.errors.txt New baseline for diagnostics output of the new regression test.
internal/transformers/declarations/transform.go Adds per-source-file tracking to dedupe CommonJS export declaration emission.
internal/checker/nodebuilderimpl.go Adds fallback to reuse an annotated declaration’s type node for binary-expression declarations when needed.

tx.rawReferencedFiles = make([]ReferencedFilePair, 0)
tx.rawTypeReferenceDirectives = make([]*ast.FileReference, 0)
tx.rawLibReferenceDirectives = make([]*ast.FileReference, 0)
tx.witnessedCjsExports.Clear()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The transformer is fresh each time, so I don't think you need to clear it, the set auto inits for you

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Eh, we're in the habit of resetting everything source-file-specific on transformSourceFile call to support potential bundled emit modes (which we admittedly no longer support) - I see no reason to change habits now.

@weswigham
Wesley Wigham (weswigham) added this pull request to the merge queue Jun 11, 2026
Merged via the queue into microsoft:main with commit 45df665 Jun 11, 2026
21 checks passed
@weswigham
Wesley Wigham (weswigham) deleted the dedupe-cjs-export-assignments branch June 11, 2026 21:59
Jake Bailey (jakebailey) pushed a commit to jakebailey/TypeScript that referenced this pull request Aug 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple differences in declaration emit for CommonJS exports

3 participants