@@ -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
0 commit comments