@@ -7,39 +7,38 @@ namespace ts {
7
7
const {
8
8
endLexicalEnvironment
9
9
} = context ;
10
- let currentSourceFile : SourceFile ;
11
10
return transformSourceFile ;
12
11
13
12
function transformSourceFile ( node : SourceFile ) {
14
- currentSourceFile = node ;
15
13
return visitEachChild ( node , visitor , context ) ;
16
14
}
17
15
18
16
function visitor ( node : Node ) : VisitResult < Node > {
19
- if ( node . transformFlags & TransformFlags . ESNext ) {
20
- return visitorWorker ( node ) ;
21
- }
22
- else if ( node . transformFlags & TransformFlags . ContainsESNext ) {
23
- return visitNodeContainingESNext ( node ) ;
24
- }
25
- else {
17
+ return visitorWorker ( node , /*noDestructuringValue*/ false ) ;
18
+ }
19
+
20
+ function visitorNoDestructuringValue ( node : Node ) : VisitResult < Node > {
21
+ return visitorWorker ( node , /*noDestructuringValue*/ true ) ;
22
+ }
23
+
24
+ function visitorWorker ( node : Node , noDestructuringValue : boolean ) : VisitResult < Node > {
25
+ if ( ( node . transformFlags & TransformFlags . ContainsESNext ) === 0 ) {
26
26
return node ;
27
27
}
28
- }
29
28
30
- function visitorWorker ( node : Node ) : VisitResult < Node > {
31
29
switch ( node . kind ) {
32
30
case SyntaxKind . ObjectLiteralExpression :
33
31
return visitObjectLiteralExpression ( node as ObjectLiteralExpression ) ;
34
32
case SyntaxKind . BinaryExpression :
35
- return visitBinaryExpression ( node as BinaryExpression , /*needsDestructuringValue*/ true ) ;
33
+ return visitBinaryExpression ( node as BinaryExpression , noDestructuringValue ) ;
36
34
case SyntaxKind . VariableDeclaration :
37
35
return visitVariableDeclaration ( node as VariableDeclaration ) ;
38
36
case SyntaxKind . ForOfStatement :
39
37
return visitForOfStatement ( node as ForOfStatement ) ;
40
- case SyntaxKind . ObjectBindingPattern :
41
- case SyntaxKind . ArrayBindingPattern :
42
- return node ;
38
+ case SyntaxKind . ForStatement :
39
+ return visitForStatement ( node as ForStatement ) ;
40
+ case SyntaxKind . VoidExpression :
41
+ return visitVoidExpression ( node as VoidExpression ) ;
43
42
case SyntaxKind . Constructor :
44
43
return visitConstructorDeclaration ( node as ConstructorDeclaration ) ;
45
44
case SyntaxKind . MethodDeclaration :
@@ -56,20 +55,13 @@ namespace ts {
56
55
return visitArrowFunction ( node as ArrowFunction ) ;
57
56
case SyntaxKind . Parameter :
58
57
return visitParameter ( node as ParameterDeclaration ) ;
59
- default :
60
- Debug . failBadSyntaxKind ( node ) ;
61
- return visitEachChild ( node , visitor , context ) ;
62
- }
63
- }
64
-
65
- function visitNodeContainingESNext ( node : Node ) {
66
- switch ( node . kind ) {
67
58
case SyntaxKind . ExpressionStatement :
68
59
return visitExpressionStatement ( node as ExpressionStatement ) ;
69
60
case SyntaxKind . ParenthesizedExpression :
70
- return visitParenthesizedExpression ( node as ParenthesizedExpression , /*needsDestructuringValue*/ true ) ;
61
+ return visitParenthesizedExpression ( node as ParenthesizedExpression , noDestructuringValue ) ;
62
+ default :
63
+ return visitEachChild ( node , visitor , context ) ;
71
64
}
72
- return visitEachChild ( node , visitor , context ) ;
73
65
}
74
66
75
67
function chunkObjectLiteralElements ( elements : ObjectLiteralElement [ ] ) : Expression [ ] {
@@ -105,56 +97,52 @@ namespace ts {
105
97
}
106
98
107
99
function visitObjectLiteralExpression ( node : ObjectLiteralExpression ) : Expression {
108
- // spread elements emit like so:
109
- // non-spread elements are chunked together into object literals, and then all are passed to __assign:
110
- // { a, ...o, b } => __assign({a}, o, {b});
111
- // If the first element is a spread element, then the first argument to __assign is {}:
112
- // { ...o, a, b, ...o2 } => __assign({}, o, {a, b}, o2)
113
- const objects = chunkObjectLiteralElements ( node . properties ) ;
114
- if ( objects . length && objects [ 0 ] . kind !== SyntaxKind . ObjectLiteralExpression ) {
115
- objects . unshift ( createObjectLiteral ( ) ) ;
100
+ if ( node . transformFlags & TransformFlags . ContainsObjectSpread ) {
101
+ // spread elements emit like so:
102
+ // non-spread elements are chunked together into object literals, and then all are passed to __assign:
103
+ // { a, ...o, b } => __assign({a}, o, {b});
104
+ // If the first element is a spread element, then the first argument to __assign is {}:
105
+ // { ...o, a, b, ...o2 } => __assign({}, o, {a, b}, o2)
106
+ const objects = chunkObjectLiteralElements ( node . properties ) ;
107
+ if ( objects . length && objects [ 0 ] . kind !== SyntaxKind . ObjectLiteralExpression ) {
108
+ objects . unshift ( createObjectLiteral ( ) ) ;
109
+ }
110
+ return createCall ( createIdentifier ( "__assign" ) , undefined , objects ) ;
116
111
}
117
- return createCall ( createIdentifier ( "__assign" ) , undefined , objects ) ;
112
+ return visitEachChild ( node , visitor , context ) ;
118
113
}
119
114
120
115
function visitExpressionStatement ( node : ExpressionStatement ) : ExpressionStatement {
121
- switch ( node . expression . kind ) {
122
- case SyntaxKind . ParenthesizedExpression :
123
- return updateStatement ( node , visitParenthesizedExpression ( < ParenthesizedExpression > node . expression , /*needsDestructuringValue*/ false ) ) ;
124
- case SyntaxKind . BinaryExpression :
125
- return updateStatement ( node , visitBinaryExpression ( < BinaryExpression > node . expression , /*needsDestructuringValue*/ false ) ) ;
126
- }
127
- return visitEachChild ( node , visitor , context ) ;
116
+ return visitEachChild ( node , visitorNoDestructuringValue , context ) ;
128
117
}
129
118
130
- function visitParenthesizedExpression ( node : ParenthesizedExpression , needsDestructuringValue : boolean ) : ParenthesizedExpression {
131
- if ( ! needsDestructuringValue ) {
132
- switch ( node . expression . kind ) {
133
- case SyntaxKind . ParenthesizedExpression :
134
- return updateParen ( node , visitParenthesizedExpression ( < ParenthesizedExpression > node . expression , /*needsDestructuringValue*/ false ) ) ;
135
- case SyntaxKind . BinaryExpression :
136
- return updateParen ( node , visitBinaryExpression ( < BinaryExpression > node . expression , /*needsDestructuringValue*/ false ) ) ;
137
- }
138
- }
139
- return visitEachChild ( node , visitor , context ) ;
119
+ function visitParenthesizedExpression ( node : ParenthesizedExpression , noDestructuringValue : boolean ) : ParenthesizedExpression {
120
+ return visitEachChild ( node , noDestructuringValue ? visitorNoDestructuringValue : visitor , context ) ;
140
121
}
141
122
142
123
/**
143
124
* Visits a BinaryExpression that contains a destructuring assignment.
144
125
*
145
126
* @param node A BinaryExpression node.
146
127
*/
147
- function visitBinaryExpression ( node : BinaryExpression , needsDestructuringValue : boolean ) : Expression {
128
+ function visitBinaryExpression ( node : BinaryExpression , noDestructuringValue : boolean ) : Expression {
148
129
if ( isDestructuringAssignment ( node ) && node . left . transformFlags & TransformFlags . ContainsObjectRest ) {
149
130
return flattenDestructuringAssignment (
150
131
context ,
151
132
node ,
152
- needsDestructuringValue ,
133
+ ! noDestructuringValue ,
153
134
FlattenLevel . ObjectRest ,
154
135
/*createAssignmentCallback*/ undefined ,
155
136
visitor
156
137
) ;
157
138
}
139
+ else if ( node . operatorToken . kind === SyntaxKind . CommaToken ) {
140
+ return updateBinary (
141
+ node ,
142
+ visitNode ( node . left , visitorNoDestructuringValue , isExpression ) ,
143
+ visitNode ( node . right , noDestructuringValue ? visitorNoDestructuringValue : visitor , isExpression )
144
+ ) ;
145
+ }
158
146
return visitEachChild ( node , visitor , context ) ;
159
147
}
160
148
@@ -178,6 +166,20 @@ namespace ts {
178
166
return visitEachChild ( node , visitor , context ) ;
179
167
}
180
168
169
+ function visitForStatement ( node : ForStatement ) : VisitResult < Statement > {
170
+ return updateFor (
171
+ node ,
172
+ visitNode ( node . initializer , visitorNoDestructuringValue , isForInitializer ) ,
173
+ visitNode ( node . condition , visitor , isExpression ) ,
174
+ visitNode ( node . incrementor , visitor , isExpression ) ,
175
+ visitNode ( node . statement , visitor , isStatement )
176
+ ) ;
177
+ }
178
+
179
+ function visitVoidExpression ( node : VoidExpression ) {
180
+ return visitEachChild ( node , visitorNoDestructuringValue , context ) ;
181
+ }
182
+
181
183
/**
182
184
* Visits a ForOfStatement and converts it into a ES2015-compatible ForOfStatement.
183
185
*
0 commit comments