Skip to content

Commit 22cf036

Browse files
committed
Clean up naming, add documentation, flatten (some) nested commas
1 parent f45f757 commit 22cf036

File tree

7 files changed

+64
-18
lines changed

7 files changed

+64
-18
lines changed

src/compiler/core.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,35 @@ namespace ts {
490490
return result;
491491
}
492492

493+
/**
494+
* Maps an array. If the mapped value is an array, it is spread into the result.
495+
* Avoids allocation if all elements map to themselves.
496+
*
497+
* @param array The array to map.
498+
* @param mapfn The callback used to map the result into one or more values.
499+
*/
500+
export function sameFlatMap<T>(array: T[], mapfn: (x: T, i: number) => T | T[]): T[] {
501+
let result: T[];
502+
if (array) {
503+
for (let i = 0; i < array.length; i++) {
504+
const item = array[i];
505+
const mapped = mapfn(item, i);
506+
if (result || item !== mapped || isArray(mapped)) {
507+
if (!result) {
508+
result = array.slice(0, i);
509+
}
510+
if (isArray(mapped)) {
511+
addRange(result, mapped);
512+
}
513+
else {
514+
result.push(mapped);
515+
}
516+
}
517+
}
518+
}
519+
return result || array;
520+
}
521+
493522
/**
494523
* Computes the first matching span of elements and returns a tuple of the first span
495524
* and the remaining elements.

src/compiler/emitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,8 @@ namespace ts {
734734
case SyntaxKind.PartiallyEmittedExpression:
735735
return emitPartiallyEmittedExpression(<PartiallyEmittedExpression>node);
736736

737-
case SyntaxKind.CommaList:
738-
return emitCommaList(<CommaList>node);
737+
case SyntaxKind.CommaListExpression:
738+
return emitCommaList(<CommaListExpression>node);
739739
}
740740
}
741741

@@ -2104,7 +2104,7 @@ namespace ts {
21042104
emitExpression(node.expression);
21052105
}
21062106

2107-
function emitCommaList(node: CommaList) {
2107+
function emitCommaList(node: CommaListExpression) {
21082108
emitExpressionList(node, node.elements, ListFormat.CommaListElements);
21092109
}
21102110

src/compiler/factory.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,13 +2077,25 @@ namespace ts {
20772077
return node;
20782078
}
20792079

2080+
function flattenCommaElements(node: Expression): Expression | Expression[] {
2081+
if (nodeIsSynthesized(node) && !isParseTreeNode(node) && !node.original && !node.emitNode && !node.id) {
2082+
if (node.kind === SyntaxKind.CommaListExpression) {
2083+
return (<CommaListExpression>node).elements;
2084+
}
2085+
if (isBinaryExpression(node) && node.operatorToken.kind === SyntaxKind.CommaToken) {
2086+
return [node.left, node.right];
2087+
}
2088+
}
2089+
return node;
2090+
}
2091+
20802092
export function createCommaList(elements: Expression[]) {
2081-
const node = <CommaList>createSynthesizedNode(SyntaxKind.CommaList);
2082-
node.elements = createNodeArray(elements);
2093+
const node = <CommaListExpression>createSynthesizedNode(SyntaxKind.CommaListExpression);
2094+
node.elements = createNodeArray(sameFlatMap(elements, flattenCommaElements));
20832095
return node;
20842096
}
20852097

2086-
export function updateCommaList(node: CommaList, elements: Expression[]) {
2098+
export function updateCommaList(node: CommaListExpression, elements: Expression[]) {
20872099
return node.elements !== elements
20882100
? updateNode(createCommaList(elements), node)
20892101
: node;
@@ -2877,6 +2889,8 @@ namespace ts {
28772889
}
28782890

28792891
export function inlineExpressions(expressions: Expression[]) {
2892+
// Avoid deeply nested comma expressions as traversing them during emit can result in "Maximum call
2893+
// stack size exceeded" errors.
28802894
return expressions.length > 10
28812895
? createCommaList(expressions)
28822896
: reduceLeft(expressions, createComma);

src/compiler/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,8 @@ namespace ts {
362362
return visitNode(cbNode, (<ExternalModuleReference>node).expression);
363363
case SyntaxKind.MissingDeclaration:
364364
return visitNodes(cbNodes, node.decorators);
365-
case SyntaxKind.CommaList:
366-
return visitNodes(cbNodes, (<CommaList>node).elements);
365+
case SyntaxKind.CommaListExpression:
366+
return visitNodes(cbNodes, (<CommaListExpression>node).elements);
367367

368368
case SyntaxKind.JsxElement:
369369
return visitNode(cbNode, (<JsxElement>node).openingElement) ||

src/compiler/types.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ namespace ts {
389389
// Transformation nodes
390390
NotEmittedStatement,
391391
PartiallyEmittedExpression,
392+
CommaListExpression,
392393
MergeDeclarationMarker,
393394
EndOfDeclarationMarker,
394-
CommaList,
395395

396396
// Enum value count
397397
Count,
@@ -1604,8 +1604,11 @@ namespace ts {
16041604
kind: SyntaxKind.EndOfDeclarationMarker;
16051605
}
16061606

1607-
export interface CommaList extends Expression {
1608-
kind: SyntaxKind.CommaList;
1607+
/**
1608+
* A list of comma-seperated expressions. This node is only created by transformations.
1609+
*/
1610+
export interface CommaListExpression extends Expression {
1611+
kind: SyntaxKind.CommaListExpression;
16091612
elements: NodeArray<Expression>;
16101613
}
16111614

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,7 @@ namespace ts {
23272327
case SyntaxKind.SpreadElement:
23282328
return 1;
23292329

2330-
case SyntaxKind.CommaList:
2330+
case SyntaxKind.CommaListExpression:
23312331
return 0;
23322332

23332333
default:
@@ -3918,7 +3918,7 @@ namespace ts {
39183918
|| kind === SyntaxKind.SpreadElement
39193919
|| kind === SyntaxKind.AsExpression
39203920
|| kind === SyntaxKind.OmittedExpression
3921-
|| kind === SyntaxKind.CommaList
3921+
|| kind === SyntaxKind.CommaListExpression
39223922
|| isUnaryExpressionKind(kind);
39233923
}
39243924

src/compiler/visitor.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -876,9 +876,9 @@ namespace ts {
876876
return updatePartiallyEmittedExpression(<PartiallyEmittedExpression>node,
877877
visitNode((<PartiallyEmittedExpression>node).expression, visitor, isExpression));
878878

879-
case SyntaxKind.CommaList:
880-
return updateCommaList(<CommaList>node,
881-
nodesVisitor((<CommaList>node).elements, visitor, isExpression));
879+
case SyntaxKind.CommaListExpression:
880+
return updateCommaList(<CommaListExpression>node,
881+
nodesVisitor((<CommaListExpression>node).elements, visitor, isExpression));
882882

883883
default:
884884
// No need to visit nodes with no children.
@@ -1393,8 +1393,8 @@ namespace ts {
13931393
result = reduceNode((<PartiallyEmittedExpression>node).expression, cbNode, result);
13941394
break;
13951395

1396-
case SyntaxKind.CommaList:
1397-
result = reduceNodes((<CommaList>node).elements, cbNodes, result);
1396+
case SyntaxKind.CommaListExpression:
1397+
result = reduceNodes((<CommaListExpression>node).elements, cbNodes, result);
13981398
break;
13991399

14001400
default:

0 commit comments

Comments
 (0)