Skip to content

Commit 7b8919f

Browse files
Merge pull request microsoft#2138 from Microsoft/functionEmit
Remove optimization of eliding the preamble code for functions without statements.
2 parents 29e5983 + f721444 commit 7b8919f

File tree

60 files changed

+644
-169
lines changed

Some content is hidden

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

60 files changed

+644
-169
lines changed

src/compiler/emitter.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,6 +3088,8 @@ module ts {
30883088

30893089
write(tokenToString(node.operatorToken.kind));
30903090

3091+
// Check if the right expression is on a different line versus the operator itself. If so,
3092+
// we'll emit newline.
30913093
if (!nodeEndIsOnSameLineAsNodeStart(node.operatorToken, node.right)) {
30923094
increaseIndent();
30933095
writeLine();
@@ -3940,58 +3942,21 @@ module ts {
39403942
}
39413943

39423944
function emitBlockFunctionBody(node: FunctionLikeDeclaration, body: Block) {
3943-
// If the body has no statements, and we know there's no code that would cause any
3944-
// prologue to be emitted, then just do a simple emit if the empty block.
3945-
if (body.statements.length === 0 && !anyParameterHasBindingPatternOrInitializer(node)) {
3946-
emitFunctionBodyWithNoStatements(node, body);
3947-
}
3948-
else {
3949-
emitFunctionBodyWithStatements(node, body);
3950-
}
3951-
}
3952-
3953-
function anyParameterHasBindingPatternOrInitializer(func: FunctionLikeDeclaration) {
3954-
return forEach(func.parameters, hasBindingPatternOrInitializer);
3955-
}
3956-
3957-
function hasBindingPatternOrInitializer(parameter: ParameterDeclaration) {
3958-
return parameter.initializer || isBindingPattern(parameter.name);
3959-
}
3960-
3961-
function emitFunctionBodyWithNoStatements(node: FunctionLikeDeclaration, body: Block) {
3962-
var singleLine = isSingleLineEmptyBlock(node.body);
3963-
3964-
write(" {");
3965-
if (singleLine) {
3966-
write(" ");
3967-
}
3968-
else {
3969-
increaseIndent();
3970-
writeLine();
3971-
}
3972-
3973-
emitLeadingCommentsOfPosition(body.statements.end);
3974-
3975-
if (!singleLine) {
3976-
decreaseIndent();
3977-
}
3978-
3979-
emitToken(SyntaxKind.CloseBraceToken, body.statements.end);
3980-
}
3981-
3982-
function emitFunctionBodyWithStatements(node: FunctionLikeDeclaration, body: Block) {
39833945
write(" {");
39843946
scopeEmitStart(node);
39853947

3986-
var outPos = writer.getTextPos();
3948+
var initialTextPos = writer.getTextPos();
39873949

39883950
increaseIndent();
39893951
emitDetachedComments(body.statements);
3952+
3953+
// Emit all the directive prologues (like "use strict"). These have to come before
3954+
// any other preamble code we write (like parameter initializers).
39903955
var startIndex = emitDirectivePrologues(body.statements, /*startWithNewLine*/ true);
39913956
emitFunctionBodyPreamble(node);
39923957
decreaseIndent();
39933958

3994-
var preambleEmitted = writer.getTextPos() !== outPos;
3959+
var preambleEmitted = writer.getTextPos() !== initialTextPos;
39953960

39963961
if (!preambleEmitted && nodeEndIsOnSameLineAsNodeStart(body, body)) {
39973962
for (var i = 0, n = body.statements.length; i < n; i++) {

tests/baselines/reference/accessorWithRestParam.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ var C = (function () {
1010
function C() {
1111
}
1212
Object.defineProperty(C.prototype, "X", {
13-
set: function () { },
13+
set: function () {
14+
var v = [];
15+
for (var _i = 0; _i < arguments.length; _i++) {
16+
v[_i - 0] = arguments[_i];
17+
}
18+
},
1419
enumerable: true,
1520
configurable: true
1621
});
1722
Object.defineProperty(C, "X", {
18-
set: function () { },
23+
set: function () {
24+
var v2 = [];
25+
for (var _i = 0; _i < arguments.length; _i++) {
26+
v2[_i - 0] = arguments[_i];
27+
}
28+
},
1929
enumerable: true,
2030
configurable: true
2131
});

tests/baselines/reference/arrayLiteralInNonVarArgParameter.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ panic([], 'one', 'two');
55

66

77
//// [arrayLiteralInNonVarArgParameter.js]
8-
function panic(val) { }
8+
function panic(val) {
9+
var opt = [];
10+
for (var _i = 1; _i < arguments.length; _i++) {
11+
opt[_i - 1] = arguments[_i];
12+
}
13+
}
914
panic([], 'one', 'two');

tests/baselines/reference/baseTypeAfterDerivedType.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ interface Base2 {
2020
var Derived2 = (function () {
2121
function Derived2() {
2222
}
23-
Derived2.prototype.method = function () { };
23+
Derived2.prototype.method = function () {
24+
var args = [];
25+
for (var _i = 0; _i < arguments.length; _i++) {
26+
args[_i - 0] = arguments[_i];
27+
}
28+
};
2429
return Derived2;
2530
})();

tests/baselines/reference/callWithSpread.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ var __extends = this.__extends || function (d, b) {
6161
d.prototype = new __();
6262
};
6363
function foo(x, y) {
64+
var z = [];
65+
for (var _i = 2; _i < arguments.length; _i++) {
66+
z[_i - 2] = arguments[_i];
67+
}
6468
}
6569
var a;
6670
var z;
@@ -89,6 +93,10 @@ var C = (function () {
8993
this.foo.apply(this, [x, y].concat(z));
9094
}
9195
C.prototype.foo = function (x, y) {
96+
var z = [];
97+
for (var _i = 2; _i < arguments.length; _i++) {
98+
z[_i - 2] = arguments[_i];
99+
}
92100
};
93101
return C;
94102
})();

tests/baselines/reference/collisionRestParameterFunction.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ function f3NoError() {
5656
var _i = 10; // no error
5757
}
5858
function f4(_i) {
59+
var rest = [];
60+
for (var _a = 1; _a < arguments.length; _a++) {
61+
rest[_a - 1] = arguments[_a];
62+
}
5963
}
6064
function f4NoError(_i) {
6165
}

tests/baselines/reference/collisionRestParameterFunctionExpressions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function foo() {
4747
var _i = 10; // no error
4848
}
4949
function f4(_i) {
50+
var rest = [];
51+
for (var _a = 1; _a < arguments.length; _a++) {
52+
rest[_a - 1] = arguments[_a];
53+
}
5054
}
5155
function f4NoError(_i) {
5256
}

tests/baselines/reference/contextualTyping.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/contextualTyping.sourcemap.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,8 +2210,8 @@ sourceFile:contextualTyping.ts
22102210
2 >Emitted(87, 14) Source(146, 14) + SourceIndex(0)
22112211
3 >Emitted(87, 15) Source(146, 15) + SourceIndex(0)
22122212
4 >Emitted(87, 16) Source(146, 37) + SourceIndex(0)
2213-
5 >Emitted(87, 20) Source(146, 40) + SourceIndex(0)
2214-
6 >Emitted(87, 21) Source(146, 41) + SourceIndex(0)
2213+
5 >Emitted(87, 20) Source(146, 40) + SourceIndex(0) name (c9t5)
2214+
6 >Emitted(87, 21) Source(146, 41) + SourceIndex(0) name (c9t5)
22152215
---
22162216
>>>;
22172217
1 >

tests/baselines/reference/declFileRestParametersOfFunctionAndFunctionType.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ var f6 = () => { return [<any>10]; }
1111

1212

1313
//// [declFileRestParametersOfFunctionAndFunctionType.js]
14-
function f1() { }
14+
function f1() {
15+
var args = [];
16+
for (var _i = 0; _i < arguments.length; _i++) {
17+
args[_i - 0] = arguments[_i];
18+
}
19+
}
1520
function f2(x) { }
1621
function f3(x) { }
1722
function f4() { }

0 commit comments

Comments
 (0)