Skip to content

Commit 711d10a

Browse files
committed
Remove TrailingCommaBehavior in favor of two boolean parameters
1 parent d42c5ba commit 711d10a

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/compiler/parser.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,6 @@ module ts {
626626
Parameters, // Parameters in parameter list
627627
}
628628

629-
enum TrailingCommaBehavior {
630-
Disallow,
631-
Allow,
632-
Preserve
633-
}
634-
635629
// Tracks whether we nested (directly or indirectly) in a certain control block.
636630
// Used for validating break and continue statements.
637631
enum ControlBlockContext {
@@ -1203,7 +1197,7 @@ module ts {
12031197
}
12041198

12051199
// Parses a comma-delimited list of elements
1206-
function parseDelimitedList<T extends Node>(kind: ParsingContext, parseElement: () => T, trailingCommaBehavior: TrailingCommaBehavior): NodeArray<T> {
1200+
function parseDelimitedList<T extends Node>(kind: ParsingContext, parseElement: () => T, allowTrailingComma: boolean, preserveTrailingComma: boolean): NodeArray<T> {
12071201
var saveParsingContext = parsingContext;
12081202
parsingContext |= 1 << kind;
12091203
var result = <NodeArray<T>>[];
@@ -1228,13 +1222,13 @@ module ts {
12281222
else if (isListTerminator(kind)) {
12291223
// Check if the last token was a comma.
12301224
if (commaStart >= 0) {
1231-
if (trailingCommaBehavior === TrailingCommaBehavior.Disallow) {
1225+
if (!allowTrailingComma) {
12321226
if (file.syntacticErrors.length === errorCountBeforeParsingList) {
12331227
// Report a grammar error so we don't affect lookahead
12341228
grammarErrorAtPos(commaStart, scanner.getStartPos() - commaStart, Diagnostics.Trailing_comma_not_allowed);
12351229
}
12361230
}
1237-
else if (trailingCommaBehavior === TrailingCommaBehavior.Preserve) {
1231+
else if (preserveTrailingComma) {
12381232
result.push(<T>createNode(SyntaxKind.OmittedExpression));
12391233
}
12401234
}
@@ -1271,7 +1265,7 @@ module ts {
12711265

12721266
function parseBracketedList<T extends Node>(kind: ParsingContext, parseElement: () => T, startToken: SyntaxKind, endToken: SyntaxKind): NodeArray<T> {
12731267
if (parseExpected(startToken)) {
1274-
var result = parseDelimitedList(kind, parseElement, TrailingCommaBehavior.Disallow);
1268+
var result = parseDelimitedList(kind, parseElement, /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
12751269
parseExpected(endToken);
12761270
return result;
12771271
}
@@ -2307,7 +2301,8 @@ module ts {
23072301
else {
23082302
parseExpected(SyntaxKind.OpenParenToken);
23092303
}
2310-
callExpr.arguments = parseDelimitedList(ParsingContext.ArgumentExpressions, parseAssignmentExpression, TrailingCommaBehavior.Disallow);
2304+
callExpr.arguments = parseDelimitedList(ParsingContext.ArgumentExpressions,
2305+
parseAssignmentExpression, /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
23112306
parseExpected(SyntaxKind.CloseParenToken);
23122307
expr = finishNode(callExpr);
23132308
continue;
@@ -2384,7 +2379,8 @@ module ts {
23842379
var node = <ArrayLiteral>createNode(SyntaxKind.ArrayLiteral);
23852380
parseExpected(SyntaxKind.OpenBracketToken);
23862381
if (scanner.hasPrecedingLineBreak()) node.flags |= NodeFlags.MultiLine;
2387-
node.elements = parseDelimitedList(ParsingContext.ArrayLiteralMembers, parseArrayLiteralElement, TrailingCommaBehavior.Preserve);
2382+
node.elements = parseDelimitedList(ParsingContext.ArrayLiteralMembers,
2383+
parseArrayLiteralElement, /*allowTrailingComma*/ true, /*preserveTrailingComma*/ true);
23882384
parseExpected(SyntaxKind.CloseBracketToken);
23892385
return finishNode(node);
23902386
}
@@ -2427,9 +2423,9 @@ module ts {
24272423
}
24282424

24292425
// ES3 itself does not accept a trailing comma in an object literal, however, we'd like to preserve it in ES5.
2430-
var trailingCommaBehavior = languageVersion === ScriptTarget.ES3 ? TrailingCommaBehavior.Allow : TrailingCommaBehavior.Preserve;
2426+
var preserveTrailingComma = languageVersion !== ScriptTarget.ES3;
24312427

2432-
node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralMember, trailingCommaBehavior);
2428+
node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralMember, /*allowTrailingComma*/ true, preserveTrailingComma);
24332429
parseExpected(SyntaxKind.CloseBraceToken);
24342430

24352431
var seen: Map<SymbolFlags> = {};
@@ -2518,7 +2514,8 @@ module ts {
25182514
parseExpected(SyntaxKind.NewKeyword);
25192515
node.func = parseCallAndAccess(parsePrimaryExpression(), /* inNewExpression */ true);
25202516
if (parseOptional(SyntaxKind.OpenParenToken) || token === SyntaxKind.LessThanToken && (node.typeArguments = tryParse(parseTypeArgumentsAndOpenParen))) {
2521-
node.arguments = parseDelimitedList(ParsingContext.ArgumentExpressions, parseAssignmentExpression, TrailingCommaBehavior.Disallow);
2517+
node.arguments = parseDelimitedList(ParsingContext.ArgumentExpressions,
2518+
parseAssignmentExpression, /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
25222519
parseExpected(SyntaxKind.CloseParenToken);
25232520
}
25242521
return finishNode(node);
@@ -3087,7 +3084,8 @@ module ts {
30873084
}
30883085

30893086
function parseVariableDeclarationList(flags: NodeFlags, noIn?: boolean): NodeArray<VariableDeclaration> {
3090-
return parseDelimitedList(ParsingContext.VariableDeclarations, () => parseVariableDeclaration(flags, noIn), TrailingCommaBehavior.Disallow);
3087+
return parseDelimitedList(ParsingContext.VariableDeclarations,
3088+
() => parseVariableDeclaration(flags, noIn), /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
30913089
}
30923090

30933091
function parseVariableStatement(pos?: number, flags?: NodeFlags): VariableStatement {
@@ -3486,7 +3484,8 @@ module ts {
34863484
var implementsKeywordLength: number;
34873485
if (parseOptional(SyntaxKind.ImplementsKeyword)) {
34883486
implementsKeywordLength = scanner.getStartPos() - implementsKeywordStart;
3489-
node.implementedTypes = parseDelimitedList(ParsingContext.BaseTypeReferences, parseTypeReference, TrailingCommaBehavior.Disallow);
3487+
node.implementedTypes = parseDelimitedList(ParsingContext.BaseTypeReferences,
3488+
parseTypeReference, /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
34903489
}
34913490
var errorCountBeforeClassBody = file.syntacticErrors.length;
34923491
if (parseExpected(SyntaxKind.OpenBraceToken)) {
@@ -3514,7 +3513,8 @@ module ts {
35143513
var extendsKeywordLength: number;
35153514
if (parseOptional(SyntaxKind.ExtendsKeyword)) {
35163515
extendsKeywordLength = scanner.getStartPos() - extendsKeywordStart;
3517-
node.baseTypes = parseDelimitedList(ParsingContext.BaseTypeReferences, parseTypeReference, TrailingCommaBehavior.Disallow);
3516+
node.baseTypes = parseDelimitedList(ParsingContext.BaseTypeReferences,
3517+
parseTypeReference, /*allowTrailingComma*/ false, /*preserveTrailingComma*/ false);
35183518
}
35193519
var errorCountBeforeInterfaceBody = file.syntacticErrors.length;
35203520
node.members = parseTypeLiteral().members;
@@ -3578,7 +3578,8 @@ module ts {
35783578
parseExpected(SyntaxKind.EnumKeyword);
35793579
node.name = parseIdentifier();
35803580
if (parseExpected(SyntaxKind.OpenBraceToken)) {
3581-
node.members = parseDelimitedList(ParsingContext.EnumMembers, parseAndCheckEnumMember, TrailingCommaBehavior.Allow);
3581+
node.members = parseDelimitedList(ParsingContext.EnumMembers,
3582+
parseAndCheckEnumMember, /*allowTrailingComma*/ true, /*preserveTrailingComma*/ false);
35823583
parseExpected(SyntaxKind.CloseBraceToken);
35833584
}
35843585
else {

0 commit comments

Comments
 (0)