Skip to content

Commit a79a1d2

Browse files
committed
Record trailing comma even for incorrectly terminated lists
1 parent ab3326f commit a79a1d2

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/compiler/parser.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,18 +1226,6 @@ module ts {
12261226
error(Diagnostics._0_expected, ",");
12271227
}
12281228
else if (isListTerminator(kind)) {
1229-
// Check if the last token was a comma.
1230-
if (commaStart >= 0) {
1231-
if (!allowTrailingComma) {
1232-
if (file.syntacticErrors.length === errorCountBeforeParsingList) {
1233-
// Report a grammar error so we don't affect lookahead
1234-
grammarErrorAtPos(commaStart, scanner.getStartPos() - commaStart, Diagnostics.Trailing_comma_not_allowed);
1235-
}
1236-
}
1237-
// Always preserve a trailing comma by marking it on the NodeArray
1238-
result.hasTrailingComma = true;
1239-
}
1240-
12411229
break;
12421230
}
12431231
else {
@@ -1248,6 +1236,23 @@ module ts {
12481236
nextToken();
12491237
}
12501238
}
1239+
1240+
// Recording the trailing comma is deliberately done after the previous
1241+
// loop, and not just if we see a list terminator. This is because the list
1242+
// may have ended incorrectly, but it is still important to know if there
1243+
// was a trailing comma.
1244+
// Check if the last token was a comma.
1245+
if (commaStart >= 0) {
1246+
if (!allowTrailingComma) {
1247+
if (file.syntacticErrors.length === errorCountBeforeParsingList) {
1248+
// Report a grammar error so we don't affect lookahead
1249+
grammarErrorAtPos(commaStart, scanner.getStartPos() - commaStart, Diagnostics.Trailing_comma_not_allowed);
1250+
}
1251+
}
1252+
// Always preserve a trailing comma by marking it on the NodeArray
1253+
result.hasTrailingComma = true;
1254+
}
1255+
12511256
result.end = getNodeEnd();
12521257
parsingContext = saveParsingContext;
12531258
return result;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////declare function f(s: string);
4+
////declare function f(n: number);
5+
////declare function f(s: string, b: boolean);
6+
////declare function f(n: number, b: boolean);
7+
////
8+
////f(1/**/ var
9+
10+
goTo.marker();
11+
verify.signatureHelpCountIs(4);
12+
verify.currentSignatureHelpIs("f(n: number): any");
13+
verify.currentParameterHelpArgumentNameIs("n");
14+
verify.currentParameterSpanIs("n: number");
15+
16+
edit.insert(", ");
17+
verify.signatureHelpCountIs(4);
18+
verify.currentSignatureHelpIs("f(n: number, b: boolean): any");
19+
verify.currentParameterHelpArgumentNameIs("b");
20+
verify.currentParameterSpanIs("b: boolean");

0 commit comments

Comments
 (0)