Skip to content

Commit 47cdfbe

Browse files
committed
Add support for array return values from visitors
1 parent 99e6ad8 commit 47cdfbe

8 files changed

Lines changed: 71 additions & 63 deletions

File tree

src/compiler/transformers/destructuring.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts {
1616
node: BinaryExpression,
1717
needsValue: boolean,
1818
recordTempVariable: (node: Identifier) => void,
19-
visitor?: (node: Node) => Node) {
19+
visitor?: (node: Node) => OneOrMany<Node>) {
2020

2121
if (isEmptyObjectLiteralOrArrayLiteral(node.left)) {
2222
return node.right;
@@ -79,7 +79,7 @@ namespace ts {
7979
* @param value The rhs value for the binding pattern.
8080
* @param visitor An optional visitor to use to visit expressions.
8181
*/
82-
export function flattenParameterDestructuring(node: ParameterDeclaration, value: Expression, visitor?: (node: Node) => Node) {
82+
export function flattenParameterDestructuring(node: ParameterDeclaration, value: Expression, visitor?: (node: Node) => OneOrMany<Node>) {
8383
const declarations: VariableDeclaration[] = [];
8484

8585
flattenDestructuring(node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
@@ -110,7 +110,7 @@ namespace ts {
110110
* @param value An optional rhs value for the binding pattern.
111111
* @param visitor An optional visitor to use to visit expressions.
112112
*/
113-
export function flattenVariableDestructuring(node: VariableDeclaration, value?: Expression, visitor?: (node: Node) => Node) {
113+
export function flattenVariableDestructuring(node: VariableDeclaration, value?: Expression, visitor?: (node: Node) => OneOrMany<Node>) {
114114
const declarations: VariableDeclaration[] = [];
115115

116116
flattenDestructuring(node, value, node, emitAssignment, emitTempVariableAssignment, visitor);
@@ -151,7 +151,7 @@ namespace ts {
151151
node: VariableDeclaration,
152152
recordTempVariable: (name: Identifier) => void,
153153
nameSubstitution?: (name: Identifier) => Expression,
154-
visitor?: (node: Node) => Node) {
154+
visitor?: (node: Node) => OneOrMany<Node>) {
155155

156156
const pendingAssignments: Expression[] = [];
157157

@@ -191,7 +191,7 @@ namespace ts {
191191
location: TextRange,
192192
emitAssignment: (name: Identifier, value: Expression, location: TextRange, original: Node) => void,
193193
emitTempVariableAssignment: (value: Expression, location: TextRange) => Identifier,
194-
visitor?: (node: Node) => Node) {
194+
visitor?: (node: Node) => OneOrMany<Node>) {
195195
if (value && visitor) {
196196
value = visitNode(value, visitor, isExpression);
197197
}

src/compiler/transformers/es6.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,26 @@ namespace ts {
5555
return visitEachChild(node, visitor, context);
5656
}
5757

58-
function visitor(node: Node): Node {
58+
function visitor(node: Node): OneOrMany<Node> {
5959
const savedContainingNonArrowFunction = containingNonArrowFunction;
6060
const savedCurrentParent = currentParent;
6161
const savedCurrentNode = currentNode;
6262
const savedEnclosingBlockScopeContainer = enclosingBlockScopeContainer;
6363
const savedEnclosingBlockScopeContainerParent = enclosingBlockScopeContainerParent;
6464

6565
onBeforeVisitNode(node);
66-
node = visitorWorker(node);
66+
67+
const visited = visitorWorker(node);
6768

6869
containingNonArrowFunction = savedContainingNonArrowFunction;
6970
currentParent = savedCurrentParent;
7071
currentNode = savedCurrentNode;
7172
enclosingBlockScopeContainer = savedEnclosingBlockScopeContainer;
7273
enclosingBlockScopeContainerParent = savedEnclosingBlockScopeContainerParent;
73-
return node;
74+
return visited;
7475
}
7576

76-
function visitorWorker(node: Node): Node {
77+
function visitorWorker(node: Node): OneOrMany<Node> {
7778
if (node.transformFlags & TransformFlags.ES6) {
7879
return visitJavaScript(node);
7980
}
@@ -85,7 +86,7 @@ namespace ts {
8586
}
8687
}
8788

88-
function visitJavaScript(node: Node): Node {
89+
function visitJavaScript(node: Node): OneOrMany<Node> {
8990
switch (node.kind) {
9091
case SyntaxKind.ClassDeclaration:
9192
return visitClassDeclaration(<ClassDeclaration>node);

src/compiler/transformers/es7.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts {
1212
return visitEachChild(node, visitor, context);
1313
}
1414

15-
function visitor(node: Node): Node {
15+
function visitor(node: Node): OneOrMany<Node> {
1616
if (node.transformFlags & TransformFlags.ES7) {
1717
return visitorWorker(node);
1818
}
@@ -24,7 +24,7 @@ namespace ts {
2424
}
2525
}
2626

27-
function visitorWorker(node: Node) {
27+
function visitorWorker(node: Node): OneOrMany<Node> {
2828
switch (node.kind) {
2929
case SyntaxKind.BinaryExpression:
3030
return visitBinaryExpression(<BinaryExpression>node);

src/compiler/transformers/jsx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ts {
1818
return visitEachChild(node, visitor, context);
1919
}
2020

21-
function visitor(node: Node): Node {
21+
function visitor(node: Node): OneOrMany<Node> {
2222
if (node.transformFlags & TransformFlags.Jsx) {
2323
return visitorWorker(node);
2424
}
@@ -30,7 +30,7 @@ namespace ts {
3030
}
3131
}
3232

33-
function visitorWorker(node: Node): Node {
33+
function visitorWorker(node: Node): OneOrMany<Node> {
3434
switch (node.kind) {
3535
case SyntaxKind.JsxElement:
3636
return visitJsxElement(<JsxElement>node);

src/compiler/transformers/module/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ namespace ts {
173173
*
174174
* @param node The node.
175175
*/
176-
function visitor(node: Node) {
176+
function visitor(node: Node): OneOrMany<Node> {
177177
switch (node.kind) {
178178
case SyntaxKind.ImportDeclaration:
179179
return visitImportDeclaration(<ImportDeclaration>node);

src/compiler/transformers/module/system.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ namespace ts {
408408
return createArrayLiteral(setters);
409409
}
410410

411-
function visitSourceElement(node: Node): Node {
411+
function visitSourceElement(node: Node): OneOrMany<Node> {
412412
switch (node.kind) {
413413
case SyntaxKind.ImportDeclaration:
414414
return visitImportDeclaration(<ImportDeclaration>node);
@@ -427,7 +427,7 @@ namespace ts {
427427
}
428428
}
429429

430-
function visitNestedNode(node: Node): Node {
430+
function visitNestedNode(node: Node): OneOrMany<Node> {
431431
switch (node.kind) {
432432
case SyntaxKind.VariableStatement:
433433
return visitVariableStatement(<VariableStatement>node);

src/compiler/transformers/ts.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ts {
9292
*
9393
* @param node The node to visit.
9494
*/
95-
function visitWithStack(node: Node, visitor: (node: Node) => Node): Node {
95+
function visitWithStack(node: Node, visitor: (node: Node) => OneOrMany<Node>): OneOrMany<Node> {
9696
// Save state
9797
const savedCurrentNamespace = currentNamespace;
9898
const savedCurrentScope = currentScope;
@@ -102,23 +102,23 @@ namespace ts {
102102
// Handle state changes before visiting a node.
103103
onBeforeVisitNode(node);
104104

105-
node = visitor(node);
105+
const visited = visitor(node);
106106

107107
// Restore state
108108
currentNamespace = savedCurrentNamespace;
109109
currentScope = savedCurrentScope;
110110
currentParent = savedCurrentParent;
111111
currentNode = savedCurrentNode;
112112

113-
return node;
113+
return visited;
114114
}
115115

116116
/**
117117
* General-purpose node visitor.
118118
*
119119
* @param node The node to visit.
120120
*/
121-
function visitor(node: Node): Node {
121+
function visitor(node: Node): OneOrMany<Node> {
122122
return visitWithStack(node, visitorWorker);
123123
}
124124

@@ -127,14 +127,14 @@ namespace ts {
127127
*
128128
* @param node The node to visit.
129129
*/
130-
function visitorWorker(node: Node): Node {
130+
function visitorWorker(node: Node): OneOrMany<Node> {
131131
if (node.transformFlags & TransformFlags.TypeScript) {
132132
// This node is explicitly marked as TypeScript, so we should transform the node.
133-
node = visitTypeScript(node);
133+
return visitTypeScript(node);
134134
}
135135
else if (node.transformFlags & TransformFlags.ContainsTypeScript) {
136136
// This node contains TypeScript, so we should visit its children.
137-
node = visitEachChild(node, visitor, context);
137+
return visitEachChild(node, visitor, context);
138138
}
139139

140140
return node;
@@ -145,7 +145,7 @@ namespace ts {
145145
*
146146
* @param node The node to visit.
147147
*/
148-
function namespaceElementVisitor(node: Node): Node {
148+
function namespaceElementVisitor(node: Node): OneOrMany<Node> {
149149
return visitWithStack(node, namespaceElementVisitorWorker);
150150
}
151151

@@ -154,15 +154,15 @@ namespace ts {
154154
*
155155
* @param node The node to visit.
156156
*/
157-
function namespaceElementVisitorWorker(node: Node): Node {
157+
function namespaceElementVisitorWorker(node: Node): OneOrMany<Node> {
158158
if (node.transformFlags & TransformFlags.TypeScript || isExported(node)) {
159159
// This node is explicitly marked as TypeScript, or is exported at the namespace
160160
// level, so we should transform the node.
161-
node = visitTypeScript(node);
161+
return visitTypeScript(node);
162162
}
163163
else if (node.transformFlags & TransformFlags.ContainsTypeScript) {
164164
// This node contains TypeScript, so we should visit its children.
165-
node = visitEachChild(node, visitor, context);
165+
return visitEachChild(node, visitor, context);
166166
}
167167

168168
return node;
@@ -173,7 +173,7 @@ namespace ts {
173173
*
174174
* @param node The node to visit.
175175
*/
176-
function classElementVisitor(node: Node) {
176+
function classElementVisitor(node: Node): OneOrMany<Node> {
177177
return visitWithStack(node, classElementVisitorWorker);
178178
}
179179

@@ -182,7 +182,7 @@ namespace ts {
182182
*
183183
* @param node The node to visit.
184184
*/
185-
function classElementVisitorWorker(node: Node) {
185+
function classElementVisitorWorker(node: Node): OneOrMany<Node> {
186186
switch (node.kind) {
187187
case SyntaxKind.Constructor:
188188
// TypeScript constructors are transformed in `transformClassDeclaration`.
@@ -212,7 +212,7 @@ namespace ts {
212212
*
213213
* @param node The node to visit.
214214
*/
215-
function visitTypeScript(node: Node): Node {
215+
function visitTypeScript(node: Node): OneOrMany<Node> {
216216
if (hasModifier(node, ModifierFlags.Ambient)) {
217217
// TypeScript ambient declarations are elided.
218218
return undefined;

0 commit comments

Comments
 (0)