Skip to content

Commit 3dc6860

Browse files
committed
Address PR feedback for trailing commas
1 parent de2ac51 commit 3dc6860

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

Jakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var servicesSources = [
5555
"services.ts",
5656
"shims.ts",
5757
"signatureHelp.ts",
58-
"utilities.ts"
58+
"utilities.ts"
5959
].map(function (f) {
6060
return path.join(servicesDirectory, f);
6161
}));

src/compiler/emitter.ts

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -700,23 +700,46 @@ module ts {
700700
}
701701
}
702702

703-
function emitCommaList(nodes: Node[], count?: number) {
704-
if (!(count >= 0)) count = nodes.length;
703+
function emitTrailingCommaIfPresent(nodeList: NodeArray<Node>, isMultiline: boolean): void {
704+
if (nodeList.hasTrailingComma) {
705+
write(",");
706+
if (isMultiline) {
707+
writeLine();
708+
}
709+
}
710+
}
711+
712+
function emitCommaList(nodes: NodeArray<Node>, includeTrailingComma: boolean, count?: number) {
713+
if (!(count >= 0)) {
714+
count = nodes.length;
715+
}
705716
if (nodes) {
706717
for (var i = 0; i < count; i++) {
707-
if (i) write(", ");
718+
if (i) {
719+
write(", ");
720+
}
708721
emit(nodes[i]);
709722
}
723+
724+
if (includeTrailingComma) {
725+
emitTrailingCommaIfPresent(nodes, /*isMultiline*/ false);
726+
}
710727
}
711728
}
712729

713-
function emitMultiLineList(nodes: Node[]) {
730+
function emitMultiLineList(nodes: NodeArray<Node>, includeTrailingComma: boolean) {
714731
if (nodes) {
715732
for (var i = 0; i < nodes.length; i++) {
716-
if (i) write(",");
733+
if (i) {
734+
write(",");
735+
}
717736
writeLine();
718737
emit(nodes[i]);
719738
}
739+
740+
if (includeTrailingComma) {
741+
emitTrailingCommaIfPresent(nodes, /*isMultiline*/ true);
742+
}
720743
}
721744
}
722745

@@ -824,29 +847,18 @@ module ts {
824847
}
825848
}
826849

827-
function emitTrailingCommaIfPresent(nodeList: NodeArray<Node>, isMultiline: boolean): void {
828-
if (nodeList.hasTrailingComma) {
829-
write(",");
830-
if (isMultiline) {
831-
writeLine();
832-
}
833-
}
834-
}
835-
836850
function emitArrayLiteral(node: ArrayLiteral) {
837851
if (node.flags & NodeFlags.MultiLine) {
838852
write("[");
839853
increaseIndent();
840-
emitMultiLineList(node.elements);
841-
emitTrailingCommaIfPresent(node.elements, /*isMultiline*/ true);
854+
emitMultiLineList(node.elements, /*includeTrailingComma*/ true);
842855
decreaseIndent();
843856
writeLine();
844857
write("]");
845858
}
846859
else {
847860
write("[");
848-
emitCommaList(node.elements);
849-
emitTrailingCommaIfPresent(node.elements, /*isMultiline*/ false);
861+
emitCommaList(node.elements, /*includeTrailingComma*/ true);
850862
write("]");
851863
}
852864
}
@@ -858,24 +870,14 @@ module ts {
858870
else if (node.flags & NodeFlags.MultiLine) {
859871
write("{");
860872
increaseIndent();
861-
emitMultiLineList(node.properties);
862-
863-
if (compilerOptions.target === ScriptTarget.ES5) {
864-
emitTrailingCommaIfPresent(node.properties, /*isMultiline*/ true);
865-
}
866-
873+
emitMultiLineList(node.properties, /*includeTrailingComma*/ compilerOptions.target >= ScriptTarget.ES5);
867874
decreaseIndent();
868875
writeLine();
869876
write("}");
870877
}
871878
else {
872879
write("{ ");
873-
emitCommaList(node.properties);
874-
875-
if (compilerOptions.target === ScriptTarget.ES5) {
876-
emitTrailingCommaIfPresent(node.properties, /*isMultiline*/ false);
877-
}
878-
880+
emitCommaList(node.properties, /*includeTrailingComma*/ compilerOptions.target >= ScriptTarget.ES5);
879881
write(" }");
880882
}
881883
}
@@ -921,13 +923,13 @@ module ts {
921923
emitThis(node.func);
922924
if (node.arguments.length) {
923925
write(", ");
924-
emitCommaList(node.arguments);
926+
emitCommaList(node.arguments, /*includeTrailingComma*/ false);
925927
}
926928
write(")");
927929
}
928930
else {
929931
write("(");
930-
emitCommaList(node.arguments);
932+
emitCommaList(node.arguments, /*includeTrailingComma*/ false);
931933
write(")");
932934
}
933935
}
@@ -937,7 +939,7 @@ module ts {
937939
emit(node.func);
938940
if (node.arguments) {
939941
write("(");
940-
emitCommaList(node.arguments);
942+
emitCommaList(node.arguments, /*includeTrailingComma*/ false);
941943
write(")");
942944
}
943945
}
@@ -1110,7 +1112,7 @@ module ts {
11101112
if (node.declarations) {
11111113
emitToken(SyntaxKind.VarKeyword, endPos);
11121114
write(" ");
1113-
emitCommaList(node.declarations);
1115+
emitCommaList(node.declarations, /*includeTrailingComma*/ false);
11141116
}
11151117
if (node.initializer) {
11161118
emit(node.initializer);
@@ -1258,7 +1260,7 @@ module ts {
12581260
function emitVariableStatement(node: VariableStatement) {
12591261
emitLeadingComments(node);
12601262
if (!(node.flags & NodeFlags.Export)) write("var ");
1261-
emitCommaList(node.declarations);
1263+
emitCommaList(node.declarations, /*includeTrailingComma*/ false);
12621264
write(";");
12631265
emitTrailingComments(node);
12641266
}
@@ -1367,7 +1369,7 @@ module ts {
13671369
increaseIndent();
13681370
write("(");
13691371
if (node) {
1370-
emitCommaList(node.parameters, node.parameters.length - (hasRestParameters(node) ? 1 : 0));
1372+
emitCommaList(node.parameters, /*includeTrailingComma*/ false, node.parameters.length - (hasRestParameters(node) ? 1 : 0));
13711373
}
13721374
write(")");
13731375
decreaseIndent();

0 commit comments

Comments
 (0)