Repository navigation
Dedupe cjs export assignments - #4284
Wesley Wigham (weswigham) merged 4 commits into
Conversation
There was a problem hiding this comment.
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.tsoutput (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() |
There was a problem hiding this comment.
The transformer is fresh each time, so I don't think you need to clear it, the set auto inits for you
There was a problem hiding this comment.
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.
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.