11/// <reference path="../factory.ts" />
22/// <reference path="../visitor.ts" />
3+ /// <reference path="./destructuring.ts" />
34
45/*@internal */
56namespace ts {
@@ -404,6 +405,9 @@ namespace ts {
404405 case SyntaxKind . YieldExpression :
405406 return visitYieldExpression ( < YieldExpression > node ) ;
406407
408+ case SyntaxKind . SpreadElementExpression :
409+ return visitSpreadElementExpression ( < SpreadElementExpression > node ) ;
410+
407411 case SyntaxKind . SuperKeyword :
408412 return visitSuperKeyword ( ) ;
409413
@@ -1129,7 +1133,7 @@ namespace ts {
11291133 createVariableStatement (
11301134 /*modifiers*/ undefined ,
11311135 createVariableDeclarationList (
1132- flattenParameterDestructuring ( parameter , temp , visitor )
1136+ flattenDestructuring ( parameter , temp , /*recordTempVariable*/ undefined , visitor )
11331137 )
11341138 ) ,
11351139 EmitFlags . CustomPrologue
@@ -1660,18 +1664,21 @@ namespace ts {
16601664 */
16611665 function visitParenthesizedExpression ( node : ParenthesizedExpression , needsDestructuringValue : boolean ) : ParenthesizedExpression {
16621666 // If we are here it is most likely because our expression is a destructuring assignment.
1663- if ( needsDestructuringValue ) {
1667+ if ( ! needsDestructuringValue ) {
1668+ // By default we always emit the RHS at the end of a flattened destructuring
1669+ // expression. If we are in a state where we do not need the destructuring value,
1670+ // we pass that information along to the children that care about it.
16641671 switch ( node . expression . kind ) {
16651672 case SyntaxKind . ParenthesizedExpression :
1666- return createParen (
1667- visitParenthesizedExpression ( < ParenthesizedExpression > node . expression , /*needsDestructuringValue*/ true ) ,
1668- /*location */ node
1673+ return updateParen (
1674+ node ,
1675+ visitParenthesizedExpression ( < ParenthesizedExpression > node . expression , /*needsDestructuringValue */ false )
16691676 ) ;
16701677
16711678 case SyntaxKind . BinaryExpression :
1672- return createParen (
1673- visitBinaryExpression ( < BinaryExpression > node . expression , /*needsDestructuringValue*/ true ) ,
1674- /*location */ node
1679+ return updateParen (
1680+ node ,
1681+ visitBinaryExpression ( < BinaryExpression > node . expression , /*needsDestructuringValue */ false )
16751682 ) ;
16761683 }
16771684 }
@@ -1689,7 +1696,13 @@ namespace ts {
16891696 function visitBinaryExpression ( node : BinaryExpression , needsDestructuringValue : boolean ) : Expression {
16901697 // If we are here it is because this is a destructuring assignment.
16911698 Debug . assert ( isDestructuringAssignment ( node ) ) ;
1692- return flattenDestructuringAssignment ( context , node , needsDestructuringValue , hoistVariableDeclaration , visitor ) ;
1699+ return flattenDestructuringToExpression (
1700+ < DestructuringAssignment > node ,
1701+ needsDestructuringValue ,
1702+ createAssignment ,
1703+ hoistVariableDeclaration ,
1704+ visitor
1705+ ) ;
16931706 }
16941707
16951708 function visitVariableStatement ( node : VariableStatement ) : Statement {
@@ -1701,15 +1714,17 @@ namespace ts {
17011714 if ( decl . initializer ) {
17021715 let assignment : Expression ;
17031716 if ( isBindingPattern ( decl . name ) ) {
1704- assignment = flattenVariableDestructuringToExpression ( decl , hoistVariableDeclaration , /*createAssignmentCallback */ undefined , visitor ) ;
1717+ assignment = flattenDestructuringToExpression ( decl , /*needsValue */ false , createAssignment , hoistVariableDeclaration , visitor ) ;
17051718 }
17061719 else {
17071720 assignment = createBinary ( < Identifier > decl . name , SyntaxKind . EqualsToken , visitNode ( decl . initializer , visitor , isExpression ) ) ;
17081721 }
1709- ( assignments || ( assignments = [ ] ) ) . push ( assignment ) ;
1722+
1723+ assignments = append ( assignments , assignment ) ;
17101724 }
17111725 }
17121726 if ( assignments ) {
1727+ // TODO(rbuckton): use inlineExpressions.
17131728 return createStatement ( reduceLeft ( assignments , ( acc , v ) => createBinary ( v , SyntaxKind . CommaToken , acc ) ) , node ) ;
17141729 }
17151730 else {
@@ -1854,8 +1869,11 @@ namespace ts {
18541869 if ( isBindingPattern ( node . name ) ) {
18551870 const recordTempVariablesInLine = ! enclosingVariableStatement
18561871 || ! hasModifier ( enclosingVariableStatement , ModifierFlags . Export ) ;
1857- return flattenVariableDestructuring ( node , /*value*/ undefined , visitor ,
1858- recordTempVariablesInLine ? undefined : hoistVariableDeclaration ) ;
1872+ return flattenDestructuring (
1873+ node ,
1874+ /*value*/ undefined ,
1875+ recordTempVariablesInLine ? undefined : hoistVariableDeclaration ,
1876+ visitor ) ;
18591877 }
18601878
18611879 return visitEachChild ( node , visitor , context ) ;
@@ -1956,9 +1974,10 @@ namespace ts {
19561974 if ( firstOriginalDeclaration && isBindingPattern ( firstOriginalDeclaration . name ) ) {
19571975 // This works whether the declaration is a var, let, or const.
19581976 // It will use rhsIterationValue _a[_i] as the initializer.
1959- const declarations = flattenVariableDestructuring (
1977+ const declarations = flattenDestructuring (
19601978 firstOriginalDeclaration ,
19611979 createElementAccess ( rhsReference , counter ) ,
1980+ /*recordTempVariable*/ undefined ,
19621981 visitor
19631982 ) ;
19641983
@@ -2007,10 +2026,10 @@ namespace ts {
20072026 // This is a destructuring pattern, so we flatten the destructuring instead.
20082027 statements . push (
20092028 createStatement (
2010- flattenDestructuringAssignment (
2011- context ,
2029+ flattenDestructuringToExpression (
20122030 assignment ,
20132031 /*needsValue*/ false ,
2032+ createAssignment ,
20142033 hoistVariableDeclaration ,
20152034 visitor
20162035 )
@@ -2840,7 +2859,7 @@ namespace ts {
28402859 }
28412860
28422861 function visitSpanOfSpreadElements ( chunk : Expression [ ] ) : VisitResult < Expression > {
2843- return map ( chunk , visitExpressionOfSpreadElement ) ;
2862+ return map ( chunk , visitSpreadElementExpression ) ;
28442863 }
28452864
28462865 function visitSpanOfNonSpreadElements ( chunk : Expression [ ] , multiLine : boolean , hasTrailingComma : boolean ) : VisitResult < Expression > {
@@ -2856,7 +2875,7 @@ namespace ts {
28562875 *
28572876 * @param node A SpreadElementExpression node.
28582877 */
2859- function visitExpressionOfSpreadElement ( node : SpreadElementExpression ) {
2878+ function visitSpreadElementExpression ( node : SpreadElementExpression ) {
28602879 return visitNode ( node . expression , visitor , isExpression ) ;
28612880 }
28622881
0 commit comments