From 4d03e4511cf30a31655c0a26de0a407ff296331b Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:59:07 -0800 Subject: [PATCH] Convert patterns in object rest bindings to assignment targets An object rest binding with a nested pattern, as in export const { ...{ object } } = source, is a syntax error, but emit runs anyway. CommonJS export emit turns the declaration into an assignment and left the nested binding pattern as a spread assignment's expression, so the printer panicked before emit could finish with the diagnostic. --- tsc/internal/transformers/utilities.go | 2 +- ...ExportNestedRestBinding(target=es2018).errors.txt | 9 +++++++++ ...commonjsExportNestedRestBinding(target=es2018).js | 12 ++++++++++++ ...njsExportNestedRestBinding(target=es2018).symbols | 10 ++++++++++ ...monjsExportNestedRestBinding(target=es2018).types | 11 +++++++++++ ...ExportNestedRestBinding(target=esnext).errors.txt | 9 +++++++++ ...commonjsExportNestedRestBinding(target=esnext).js | 12 ++++++++++++ ...njsExportNestedRestBinding(target=esnext).symbols | 10 ++++++++++ ...monjsExportNestedRestBinding(target=esnext).types | 11 +++++++++++ .../compiler/commonjsExportNestedRestBinding.ts | 5 +++++ 10 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).js create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).symbols create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).types create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).js create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).symbols create mode 100644 tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).types create mode 100644 tsc/testdata/tests/cases/compiler/commonjsExportNestedRestBinding.ts diff --git a/tsc/internal/transformers/utilities.go b/tsc/internal/transformers/utilities.go index af826b85a6c20..a3954a039c736 100644 --- a/tsc/internal/transformers/utilities.go +++ b/tsc/internal/transformers/utilities.go @@ -134,7 +134,7 @@ func convertBindingElementToArrayAssignmentElement(emitContext *printer.EmitCont func convertBindingElementToObjectAssignmentElement(emitContext *printer.EmitContext, element *ast.BindingElement) *ast.ObjectLiteralElement { if element.DotDotDotToken != nil { - spread := emitContext.Factory.NewSpreadAssignment(element.Name()) + spread := emitContext.Factory.NewSpreadAssignment(convertBindingNameToAssignmentElementTarget(emitContext, element.Name())) emitContext.SetOriginal(spread, element.AsNode()) emitContext.AssignCommentAndSourceMapRanges(spread, element.AsNode()) return spread diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).errors.txt b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).errors.txt new file mode 100644 index 0000000000000..0fa935fc77566 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).errors.txt @@ -0,0 +1,9 @@ +commonjsExportNestedRestBinding.ts(2,18): error TS1003: Identifier expected. + + +==== commonjsExportNestedRestBinding.ts (1 errors) ==== + declare const source: any; + export const {...{object}} = source; + ~ +!!! error TS1003: Identifier expected. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).js b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).js new file mode 100644 index 0000000000000..281d4a95c92a4 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +//// [commonjsExportNestedRestBinding.ts] +declare const source: any; +export const {...{object}} = source; + + +//// [commonjsExportNestedRestBinding.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.object = void 0; +({ ...{ object: exports.object } } = source); diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).symbols b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).symbols new file mode 100644 index 0000000000000..8857b19f93ad8 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).symbols @@ -0,0 +1,10 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +=== commonjsExportNestedRestBinding.ts === +declare const source: any; +>source : Symbol(source, Decl(commonjsExportNestedRestBinding.ts, 0, 13)) + +export const {...{object}} = source; +>object : Symbol(object, Decl(commonjsExportNestedRestBinding.ts, 1, 18)) +>source : Symbol(source, Decl(commonjsExportNestedRestBinding.ts, 0, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).types b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).types new file mode 100644 index 0000000000000..a869d0743426f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=es2018).types @@ -0,0 +1,11 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +=== commonjsExportNestedRestBinding.ts === +declare const source: any; +>source : any + +export const {...{object}} = source; +> : any +>object : any +>source : any + diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).errors.txt b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).errors.txt new file mode 100644 index 0000000000000..0fa935fc77566 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).errors.txt @@ -0,0 +1,9 @@ +commonjsExportNestedRestBinding.ts(2,18): error TS1003: Identifier expected. + + +==== commonjsExportNestedRestBinding.ts (1 errors) ==== + declare const source: any; + export const {...{object}} = source; + ~ +!!! error TS1003: Identifier expected. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).js b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).js new file mode 100644 index 0000000000000..281d4a95c92a4 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).js @@ -0,0 +1,12 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +//// [commonjsExportNestedRestBinding.ts] +declare const source: any; +export const {...{object}} = source; + + +//// [commonjsExportNestedRestBinding.js] +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.object = void 0; +({ ...{ object: exports.object } } = source); diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).symbols b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).symbols new file mode 100644 index 0000000000000..8857b19f93ad8 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).symbols @@ -0,0 +1,10 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +=== commonjsExportNestedRestBinding.ts === +declare const source: any; +>source : Symbol(source, Decl(commonjsExportNestedRestBinding.ts, 0, 13)) + +export const {...{object}} = source; +>object : Symbol(object, Decl(commonjsExportNestedRestBinding.ts, 1, 18)) +>source : Symbol(source, Decl(commonjsExportNestedRestBinding.ts, 0, 13)) + diff --git a/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).types b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).types new file mode 100644 index 0000000000000..a869d0743426f --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/commonjsExportNestedRestBinding(target=esnext).types @@ -0,0 +1,11 @@ +//// [tests/cases/compiler/commonjsExportNestedRestBinding.ts] //// + +=== commonjsExportNestedRestBinding.ts === +declare const source: any; +>source : any + +export const {...{object}} = source; +> : any +>object : any +>source : any + diff --git a/tsc/testdata/tests/cases/compiler/commonjsExportNestedRestBinding.ts b/tsc/testdata/tests/cases/compiler/commonjsExportNestedRestBinding.ts new file mode 100644 index 0000000000000..d99fc6f8ee73f --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/commonjsExportNestedRestBinding.ts @@ -0,0 +1,5 @@ +// @target: es2018, esnext +// @module: commonjs + +declare const source: any; +export const {...{object}} = source;