Skip to content

Commit 5358304

Browse files
Copilotjakebailey
andcommitted
Port TS#63043: Implement walkBindingPattern for declaration emit of destructured parameter properties (#2751)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent 87b0e74 commit 5358304

5 files changed

Lines changed: 40 additions & 57 deletions

File tree

internal/transformers/declarations/transform.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl
14301430
parameterProperties = append(parameterProperties, updated)
14311431
} else {
14321432
// Pattern - this is currently an error, but we emit declarations for it somewhat correctly
1433-
// !!! is this worth reimplementing? We never made it not-an-error
1433+
parameterProperties = append(parameterProperties, tx.walkBindingPattern(param.Name().AsBindingPattern(), param)...)
14341434
}
14351435
}
14361436
tx.state.getSymbolAccessibilityDiagnostic = oldDiag
@@ -1542,6 +1542,27 @@ func (tx *DeclarationTransformer) transformClassDeclaration(input *ast.ClassDecl
15421542
)
15431543
}
15441544

1545+
func (tx *DeclarationTransformer) walkBindingPattern(pattern *ast.BindingPattern, param *ast.Node) []*ast.Node {
1546+
var elems []*ast.Node
1547+
for _, elem := range pattern.Elements.Nodes {
1548+
if ast.IsOmittedExpression(elem) {
1549+
continue
1550+
}
1551+
if ast.IsBindingPattern(elem.Name()) {
1552+
elems = append(elems, tx.walkBindingPattern(elem.Name().AsBindingPattern(), param)...)
1553+
continue
1554+
}
1555+
elems = append(elems, tx.Factory().NewPropertyDeclaration(
1556+
tx.ensureModifiers(param),
1557+
elem.Name(),
1558+
nil, /*questionOrExclamationToken*/
1559+
tx.ensureType(elem, false),
1560+
nil, /*initializer*/
1561+
))
1562+
}
1563+
return elems
1564+
}
1565+
15451566
func (tx *DeclarationTransformer) transformVariableStatement(input *ast.VariableStatement) *ast.Node {
15461567
visible := false
15471568
for _, decl := range input.DeclarationList.AsVariableDeclarationList().Declarations.Nodes {

testdata/baselines/reference/submodule/compiler/declarationEmitDestructuringParameterProperties.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ class C3 {
3636

3737
//// [declarationEmitDestructuringParameterProperties.d.ts]
3838
declare class C1 {
39+
x: string;
40+
y: string;
41+
z: string;
3942
constructor([x, y, z]: string[]);
4043
}
4144
type TupleType1 = [string, number, boolean];
4245
declare class C2 {
46+
x: string;
47+
y: number;
48+
z: boolean;
4349
constructor([x, y, z]: TupleType1);
4450
}
4551
type ObjType1 = {
@@ -48,5 +54,8 @@ type ObjType1 = {
4854
z: boolean;
4955
};
5056
declare class C3 {
57+
x: number;
58+
y: string;
59+
z: boolean;
5160
constructor({ x, y, z }: ObjType1);
5261
}

testdata/baselines/reference/submodule/compiler/declarationEmitDestructuringParameterProperties.js.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/declarationEmitDestructuringParameterProperties2.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ class C3 {
3636

3737
//// [declarationEmitDestructuringParameterProperties2.d.ts]
3838
declare class C1 {
39+
x: string;
40+
y: string;
41+
z: string;
3942
constructor([x, y, z]: string[]);
4043
}
4144
type TupleType1 = [string, number, boolean];
4245
declare class C2 {
46+
x: string;
47+
y: number;
48+
z: boolean;
4349
constructor([x, y, z]: TupleType1);
4450
}
4551
type ObjType1 = {
@@ -48,5 +54,8 @@ type ObjType1 = {
4854
z: boolean;
4955
};
5056
declare class C3 {
57+
x: number;
58+
y: string;
59+
z: boolean;
5160
constructor({ x, y, z }: ObjType1);
5261
}

testdata/baselines/reference/submodule/compiler/declarationEmitDestructuringParameterProperties2.js.diff

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)