Skip to content

Fix CommonJS declaration emit crashes - #65498

Merged
Jake Bailey (jakebailey) merged 3 commits into
microsoft:mainfrom
jakebailey:compiler-fixes/commonjs-declaration-names
Nov 19, 2026
Merged

Jake Bailey (jakebailey) merged 3 commits into
microsoft:mainfrom
jakebailey:compiler-fixes/commonjs-declaration-names

Conversation

@jakebailey

@jakebailey Jake Bailey (jakebailey) commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Declaration emit for CommonJS files could build import and export specifiers with invalid module export names, which crashes the printer: destructured require bindings with numeric or computed keys ({ 0: x }, { [k]: x }), and exports named by template literals (exports[`class`]). Literal keys are normalized to identifiers or strings, dynamic keys become typed variables, and template names become string literals.

Copilot AI balanced review requested due to automatic review settings November 14, 2026 04:15
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 14, 2026
@typescript-automation typescript-automation Bot added Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug labels Nov 14, 2026

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.

🟡 Changes recommended

The fallback loses class type bindings and produces circular declarations, while signed-zero handling imports the wrong property for computed numeric keys.

2 open issues
What changed in this PR

This PR addresses CommonJS declaration-emission crashes in the native TypeScript compiler when JavaScript bindings or export names cannot be represented directly as imports or exports.

Changes:

  • Falls back to typed declarations for require destructuring with dynamic keys.
  • Normalizes numeric and computed import names and template-literal export names.
  • Adds three regression tests with declaration, type, and symbol baselines.
File Description
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitCommonjsTemplateExportName.ts Tests template-literal export names.
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitCommonjsRequireNumericKey.ts Tests numeric and literal computed require keys.
tsc/​testdata/​tests/​cases/​compiler/​declarationEmitCommonjsRequireComputedKey.ts Tests dynamic keys and mixed destructuring.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsTemplateExportName.types Records template-export types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsTemplateExportName.symbols Records template-export symbols.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsTemplateExportName.js Records normalized export declarations.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNumericKey.types Records numeric-key binding types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNumericKey.symbols Records numeric-key binding symbols.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireNumericKey.js Records normalized named imports.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireComputedKey.types Records mixed-destructuring types.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireComputedKey.symbols Records mixed-destructuring symbols.
tsc/​testdata/​baselines/​reference/​compiler/​declarationEmitCommonjsRequireComputedKey.js Records fallback declarations and sibling import.
tsc/​internal/​transformers/​declarations/​transform.go Adds conversion guards and name normalization.

Review effort: Balanced

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.

🟡 Changes recommended

The partial-import transformation bypasses visibility and stripInternal checks, potentially emitting unused imports from unpublished private modules and causing module-resolution errors for consumers.

1 open issue
2 resolved since last review

Review effort: Balanced

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.

🟢 Approval recommended

No active review issues remain for the CommonJS declaration-emission fixes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Declaration emit for CommonJS files can build import and export
specifiers with invalid module export names: destructured require
bindings with numeric or computed keys, and exports named by
no-substitution template literals with keyword text other than
`default` or non-identifier text. The printer rejects these names and
panics before producing a baseline.

Add the cases ahead of the fixes; their baselines are added with the
fixes.
Declaration emit turns a destructured CommonJS require into a named
import, passing each binding's property name straight through as the
import name. Numeric keys and computed keys are invalid module export
names, so the printer panicked on the resulting import specifiers.

Normalize numeric and literal computed keys to an identifier or a
string literal, using the string form of signed numeric keys so [-0]
names property "0". Dynamic keys cannot be expressed as an import, so
declare those bindings, along with any nested or rest bindings in the
same pattern, as typed variables. Import the remaining static bindings,
so names such as an imported class stay usable as types.
A CommonJS export named by a no-substitution template literal, such as
exports[`class`] or Object.defineProperty(exports, `a b`, ...), kept
its template literal node when its text was a keyword other than
`default` or non-identifier text. Template literals are invalid module
export names, so the printer panicked on the resulting export
specifier, and duplicate exports of the same name went undetected.

Convert any remaining no-substitution template literal name to a
string literal.
@github-project-automation github-project-automation Bot moved this from Not started to Needs merge in PR Backlog Nov 19, 2026
@jakebailey
Jake Bailey (jakebailey) added this pull request to the merge queue Nov 19, 2026
Merged via the queue into microsoft:main with commit 20448db Nov 19, 2026
34 checks passed
@jakebailey
Jake Bailey (jakebailey) deleted the compiler-fixes/commonjs-declaration-names branch November 19, 2026 17:54
@github-project-automation github-project-automation Bot moved this from Needs merge to Done in PR Backlog Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants