Skip to content

Commit 23ec44f

Browse files
authored
fix(3565): preserve trailing commas in declaration-emitted array binding patterns (#3574)
1 parent 3fef754 commit 23ec44f

4 files changed

Lines changed: 19 additions & 53 deletions

File tree

internal/transformers/declarations/transform.go

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type DeclarationTransformer struct {
6464
rawReferencedFiles []ReferencedFilePair
6565
rawTypeReferenceDirectives []*ast.FileReference
6666
rawLibReferenceDirectives []*ast.FileReference
67+
bindingNameVisitor *ast.NodeVisitor
6768
}
6869

6970
// TODO: Convert to transformers.TransformerFactory signature to allow more automatic composition with other transforms
@@ -94,6 +95,7 @@ func NewDeclarationTransformer(host DeclarationEmitHost, context *printer.EmitCo
9495
}
9596
}
9697
tx.NewTransformer(tx.visit, context)
98+
tx.bindingNameVisitor = tx.EmitContext().NewNodeVisitor(tx.visitBindingName)
9799
return tx
98100
}
99101

@@ -1943,7 +1945,7 @@ func (tx *DeclarationTransformer) ensureParameter(p *ast.ParameterDeclaration) *
19431945
p,
19441946
nil,
19451947
p.DotDotDotToken,
1946-
tx.filterBindingPatternInitializers(p.Name()),
1948+
tx.bindingNameVisitor.VisitNode(p.Name()),
19471949
questionToken,
19481950
tx.ensureType(p.AsNode(), true),
19491951
tx.ensureNoInitializer(p.AsNode()),
@@ -1963,35 +1965,19 @@ func (tx *DeclarationTransformer) ensureNoInitializer(node *ast.Node) *ast.Node
19631965
return nil
19641966
}
19651967

1966-
func (tx *DeclarationTransformer) filterBindingPatternInitializers(node *ast.Node) *ast.Node {
1967-
if node.Kind == ast.KindIdentifier {
1968+
func (tx *DeclarationTransformer) visitBindingName(node *ast.Node) *ast.Node {
1969+
switch node.Kind {
1970+
case ast.KindIdentifier, ast.KindOmittedExpression:
19681971
return node
1969-
} else {
1970-
// TODO: visitor to avoid always making new nodes?
1971-
elements := make([]*ast.Node, 0, len(node.Elements()))
1972-
for _, elem := range node.Elements() {
1973-
if elem.Kind == ast.KindOmittedExpression {
1974-
elements = append(elements, elem)
1975-
continue
1976-
}
1977-
if elem.PropertyName() != nil && ast.IsComputedPropertyName(elem.PropertyName()) && ast.IsEntityNameExpression(elem.PropertyName().Expression()) {
1978-
tx.checkEntityNameVisibility(elem.PropertyName().Expression(), tx.enclosingDeclaration)
1979-
}
1980-
if elem.Name() == nil {
1981-
elements = append(elements, elem)
1982-
continue
1983-
}
1984-
1985-
elements = append(elements, tx.Factory().UpdateBindingElement(
1986-
elem.AsBindingElement(),
1987-
elem.AsBindingElement().DotDotDotToken,
1988-
elem.PropertyName(),
1989-
tx.filterBindingPatternInitializers(elem.Name()),
1990-
nil,
1991-
))
1972+
case ast.KindArrayBindingPattern, ast.KindObjectBindingPattern:
1973+
return node.VisitEachChild(tx.bindingNameVisitor)
1974+
case ast.KindBindingElement:
1975+
if node.PropertyName() != nil && ast.IsComputedPropertyName(node.PropertyName()) && ast.IsEntityNameExpression(node.PropertyName().Expression()) {
1976+
tx.checkEntityNameVisibility(node.PropertyName().Expression(), tx.enclosingDeclaration)
19921977
}
1993-
elemList := tx.Factory().NewNodeList(elements)
1994-
return tx.Factory().UpdateBindingPattern(node.AsBindingPattern(), elemList)
1978+
return tx.Factory().UpdateBindingElement(node.AsBindingElement(), node.AsBindingElement().DotDotDotToken, node.PropertyName(), tx.bindingNameVisitor.VisitNode(node.Name()), nil /*initializer*/)
1979+
default:
1980+
return node
19951981
}
19961982
}
19971983

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ function bar2([, , z, , ,]) { }
1717

1818

1919
//// [declarationEmitDestructuring5.d.ts]
20-
declare function baz([, z, ]: [any, any, any?]): void;
21-
declare function foo([, b]: [any, any]): void;
22-
declare function bar([z, , ]: [any, any?, any?]): void;
23-
declare function bar1([z, , ]?: [number, number, number, number, number]): void;
24-
declare function bar2([, , z, , ]: [any, any, any, any?, any?]): void;
20+
declare function baz([, z, ,]: [any, any, any?]): void;
21+
declare function foo([, b,]: [any, any]): void;
22+
declare function bar([z, , ,]: [any, any?, any?]): void;
23+
declare function bar1([z, , ,]?: [number, number, number, number, number]): void;
24+
declare function bar2([, , z, , ,]: [any, any, any, any?, any?]): void;

testdata/baselines/reference/submoduleTriaged/compiler/declarationEmitDestructuring5.js.diff

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

testdata/submoduleTriaged.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,6 @@ conformance/definiteAssignmentAssertionsWithObjectShortHand.js.diff
232232
## https://github.com/microsoft/typescript-go/issues/3564
233233
conformance/genericFunctionParameters.js.diff
234234

235-
# Declaration emit trailing comma changes in destructuring parameters
236-
## https://github.com/microsoft/typescript-go/issues/3565
237-
compiler/declarationEmitDestructuring5.js.diff
238-
239235
# Late-bound computed property [prop] preserved instead of resolved to plain prop
240236
## https://github.com/microsoft/typescript-go/issues/3566
241237
compiler/lateBoundAssignmentCandidateJS2.js.diff

0 commit comments

Comments
 (0)