Skip to content

Commit 911a705

Browse files
Merge pull request #1974 from Microsoft/multiLineEmitting
Preserve single line blocks when emitting.
2 parents 5a1c740 + 36b6f4e commit 911a705

File tree

846 files changed

+3496
-6798
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

846 files changed

+3496
-6798
lines changed

src/compiler/emitter.ts

+75-50
Original file line numberDiff line numberDiff line change
@@ -2713,7 +2713,21 @@ module ts {
27132713
emit(node.whenFalse);
27142714
}
27152715

2716+
function isSingleLineBlock(node: Node) {
2717+
if (node && node.kind === SyntaxKind.Block) {
2718+
var block = <Block>node;
2719+
return block.statements.length === 0 && nodeEndIsOnSameLineAsNodeStart(block, block);
2720+
}
2721+
}
2722+
27162723
function emitBlock(node: Block) {
2724+
if (isSingleLineBlock(node)) {
2725+
emitToken(SyntaxKind.OpenBraceToken, node.pos);
2726+
write(" ");
2727+
emitToken(SyntaxKind.CloseBraceToken, node.statements.end);
2728+
return;
2729+
}
2730+
27172731
emitToken(SyntaxKind.OpenBraceToken, node.pos);
27182732
increaseIndent();
27192733
scopeEmitStart(node.parent);
@@ -2886,6 +2900,11 @@ module ts {
28862900
getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos));
28872901
}
28882902

2903+
function nodeEndIsOnSameLineAsNodeStart(node1: Node, node2: Node) {
2904+
return getLineOfLocalPosition(currentSourceFile, node1.end) ===
2905+
getLineOfLocalPosition(currentSourceFile, skipTrivia(currentSourceFile.text, node2.pos));
2906+
}
2907+
28892908
function emitCaseOrDefaultClause(node: CaseOrDefaultClause) {
28902909
if (node.kind === SyntaxKind.CaseClause) {
28912910
write("case ");
@@ -3385,73 +3404,79 @@ module ts {
33853404
emitSignatureParameters(node);
33863405
}
33873406

3388-
write(" {");
3389-
scopeEmitStart(node);
3390-
3391-
if (!node.body) {
3392-
writeLine();
3393-
write("}");
3407+
if (isSingleLineBlock(node.body)) {
3408+
write(" { }");
33943409
}
33953410
else {
3396-
increaseIndent();
3397-
3398-
emitDetachedComments(node.body.kind === SyntaxKind.Block ? (<Block>node.body).statements : node.body);
3399-
3400-
var startIndex = 0;
3401-
if (node.body.kind === SyntaxKind.Block) {
3402-
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
3403-
}
3404-
var outPos = writer.getTextPos();
3405-
3406-
emitCaptureThisForNodeIfNecessary(node);
3407-
emitDefaultValueAssignments(node);
3408-
emitRestParameter(node);
3409-
if (node.body.kind !== SyntaxKind.Block && outPos === writer.getTextPos()) {
3410-
decreaseIndent();
3411-
write(" ");
3412-
emitStart(node.body);
3413-
write("return ");
3411+
write(" {");
3412+
scopeEmitStart(node);
34143413

3415-
// Don't emit comments on this body. We'll have already taken care of it above
3416-
// when we called emitDetachedComments.
3417-
emitNode(node.body, /*disableComments:*/ true);
3418-
emitEnd(node.body);
3419-
write(";");
3420-
emitTempDeclarations(/*newLine*/ false);
3421-
write(" ");
3422-
emitStart(node.body);
3414+
if (!node.body) {
3415+
writeLine();
34233416
write("}");
3424-
emitEnd(node.body);
34253417
}
34263418
else {
3419+
increaseIndent();
3420+
3421+
emitDetachedComments(node.body.kind === SyntaxKind.Block ? (<Block>node.body).statements : node.body);
3422+
3423+
var startIndex = 0;
34273424
if (node.body.kind === SyntaxKind.Block) {
3428-
emitLinesStartingAt((<Block>node.body).statements, startIndex);
3425+
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
34293426
}
3430-
else {
3431-
writeLine();
3432-
emitLeadingComments(node.body);
3427+
var outPos = writer.getTextPos();
3428+
3429+
emitCaptureThisForNodeIfNecessary(node);
3430+
emitDefaultValueAssignments(node);
3431+
emitRestParameter(node);
3432+
if (node.body.kind !== SyntaxKind.Block && outPos === writer.getTextPos()) {
3433+
decreaseIndent();
3434+
write(" ");
3435+
emitStart(node.body);
34333436
write("return ");
3434-
emit(node.body, /*disableComments:*/ true);
3437+
3438+
// Don't emit comments on this body. We'll have already taken care of it above
3439+
// when we called emitDetachedComments.
3440+
emitNode(node.body, /*disableComments:*/ true);
3441+
emitEnd(node.body);
34353442
write(";");
3436-
emitTrailingComments(node.body);
3437-
}
3438-
emitTempDeclarations(/*newLine*/ true);
3439-
writeLine();
3440-
if (node.body.kind === SyntaxKind.Block) {
3441-
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
3442-
decreaseIndent();
3443-
emitToken(SyntaxKind.CloseBraceToken, (<Block>node.body).statements.end);
3444-
}
3445-
else {
3446-
decreaseIndent();
3443+
emitTempDeclarations(/*newLine*/ false);
3444+
write(" ");
34473445
emitStart(node.body);
34483446
write("}");
34493447
emitEnd(node.body);
34503448
}
3449+
else {
3450+
if (node.body.kind === SyntaxKind.Block) {
3451+
emitLinesStartingAt((<Block>node.body).statements, startIndex);
3452+
}
3453+
else {
3454+
writeLine();
3455+
emitLeadingComments(node.body);
3456+
write("return ");
3457+
emit(node.body, /*disableComments:*/ true);
3458+
write(";");
3459+
emitTrailingComments(node.body);
3460+
}
3461+
emitTempDeclarations(/*newLine*/ true);
3462+
writeLine();
3463+
if (node.body.kind === SyntaxKind.Block) {
3464+
emitLeadingCommentsOfPosition((<Block>node.body).statements.end);
3465+
decreaseIndent();
3466+
emitToken(SyntaxKind.CloseBraceToken, (<Block>node.body).statements.end);
3467+
}
3468+
else {
3469+
decreaseIndent();
3470+
emitStart(node.body);
3471+
write("}");
3472+
emitEnd(node.body);
3473+
}
3474+
}
34513475
}
3476+
3477+
scopeEmitEnd();
34523478
}
34533479

3454-
scopeEmitEnd();
34553480
if (node.flags & NodeFlags.Export) {
34563481
writeLine();
34573482
emitStart(node);

tests/baselines/reference/ArrowFunctionExpression1.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = (public x: string) => { };
33

44
//// [ArrowFunctionExpression1.js]
5-
var v = function (x) {
6-
};
5+
var v = function (x) { };

tests/baselines/reference/ClassAndModuleThatMergeWithModuleMemberThatUsesClassTypeParameter.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ var clodule1 = (function () {
5858
})();
5959
var clodule1;
6060
(function (clodule1) {
61-
function f(x) {
62-
}
61+
function f(x) { }
6362
})(clodule1 || (clodule1 = {}));
6463
var clodule2 = (function () {
6564
function clodule2() {

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module clodule {
1919
var clodule = (function () {
2020
function clodule() {
2121
}
22-
clodule.fn = function (id) {
23-
};
22+
clodule.fn = function (id) { };
2423
return clodule;
2524
})();
2625
var clodule;

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ module clodule {
1919
var clodule = (function () {
2020
function clodule() {
2121
}
22-
clodule.fn = function (id) {
23-
};
22+
clodule.fn = function (id) { };
2423
return clodule;
2524
})();
2625
var clodule;

tests/baselines/reference/ClassDeclaration11.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class C {
88
var C = (function () {
99
function C() {
1010
}
11-
C.prototype.foo = function () {
12-
};
11+
C.prototype.foo = function () { };
1312
return C;
1413
})();

tests/baselines/reference/ClassDeclaration13.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class C {
88
var C = (function () {
99
function C() {
1010
}
11-
C.prototype.bar = function () {
12-
};
11+
C.prototype.bar = function () { };
1312
return C;
1413
})();

tests/baselines/reference/ClassDeclaration21.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class C {
88
var C = (function () {
99
function C() {
1010
}
11-
C.prototype[1] = function () {
12-
};
11+
C.prototype[1] = function () { };
1312
return C;
1413
})();

tests/baselines/reference/ClassDeclaration22.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ class C {
88
var C = (function () {
99
function C() {
1010
}
11-
C.prototype["bar"] = function () {
12-
};
11+
C.prototype["bar"] = function () { };
1312
return C;
1413
})();

tests/baselines/reference/FunctionDeclaration12_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = function * yield() { }
33

44
//// [FunctionDeclaration12_es6.js]
5-
var v = , yield = function () {
6-
};
5+
var v = , yield = function () { };

tests/baselines/reference/FunctionDeclaration4.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ function foo();
33
function bar() { }
44

55
//// [FunctionDeclaration4.js]
6-
function bar() {
7-
}
6+
function bar() { }

tests/baselines/reference/FunctionDeclaration6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@
66

77
//// [FunctionDeclaration6.js]
88
{
9-
function bar() {
10-
}
9+
function bar() { }
1110
}

tests/baselines/reference/FunctionExpression1_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = function * () { }
33

44
//// [FunctionExpression1_es6.js]
5-
var v = function () {
6-
};
5+
var v = function () { };

tests/baselines/reference/FunctionExpression2_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = function * foo() { }
33

44
//// [FunctionExpression2_es6.js]
5-
var v = function foo() {
6-
};
5+
var v = function foo() { };

tests/baselines/reference/FunctionPropertyAssignments1_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = { *foo() { } }
33

44
//// [FunctionPropertyAssignments1_es6.js]
5-
var v = { foo: function () {
6-
} };
5+
var v = { foo: function () { } };

tests/baselines/reference/FunctionPropertyAssignments2_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = { *() { } }
33

44
//// [FunctionPropertyAssignments2_es6.js]
5-
var v = { : function () {
6-
} };
5+
var v = { : function () { } };

tests/baselines/reference/FunctionPropertyAssignments3_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = { *{ } }
33

44
//// [FunctionPropertyAssignments3_es6.js]
5-
var v = { : function () {
6-
} };
5+
var v = { : function () { } };

tests/baselines/reference/FunctionPropertyAssignments5_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = { *[foo()]() { } }
33

44
//// [FunctionPropertyAssignments5_es6.js]
5-
var v = { [foo()]: function () {
6-
} };
5+
var v = { [foo()]: function () { } };

tests/baselines/reference/FunctionPropertyAssignments6_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22
var v = { *<T>() { } }
33

44
//// [FunctionPropertyAssignments6_es6.js]
5-
var v = { : function () {
6-
} };
5+
var v = { : function () { } };

tests/baselines/reference/MemberAccessorDeclaration15.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ var C = (function () {
88
function C() {
99
}
1010
Object.defineProperty(C.prototype, "Foo", {
11-
set: function (a) {
12-
},
11+
set: function (a) { },
1312
enumerable: true,
1413
configurable: true
1514
});

tests/baselines/reference/MemberFunctionDeclaration1_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype.foo = function () {
11-
};
10+
C.prototype.foo = function () { };
1211
return C;
1312
})();

tests/baselines/reference/MemberFunctionDeclaration2_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype.foo = function () {
11-
};
10+
C.prototype.foo = function () { };
1211
return C;
1312
})();

tests/baselines/reference/MemberFunctionDeclaration3_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype[foo] = function () {
11-
};
10+
C.prototype[foo] = function () { };
1211
return C;
1312
})();

tests/baselines/reference/MemberFunctionDeclaration4_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype. = function () {
11-
};
10+
C.prototype. = function () { };
1211
return C;
1312
})();

tests/baselines/reference/MemberFunctionDeclaration7_es6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype.foo = function () {
11-
};
10+
C.prototype.foo = function () { };
1211
return C;
1312
})();

tests/baselines/reference/Protected4.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype.m = function () {
11-
};
10+
C.prototype.m = function () { };
1211
return C;
1312
})();

tests/baselines/reference/Protected5.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.m = function () {
11-
};
10+
C.m = function () { };
1211
return C;
1312
})();

tests/baselines/reference/Protected6.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.m = function () {
11-
};
10+
C.m = function () { };
1211
return C;
1312
})();

tests/baselines/reference/Protected7.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class C {
77
var C = (function () {
88
function C() {
99
}
10-
C.prototype.m = function () {
11-
};
10+
C.prototype.m = function () { };
1211
return C;
1312
})();

0 commit comments

Comments
 (0)