Skip to content

Emit downlevel parameter initializers unless we are certain they don't have any side effects. #2109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3899,7 +3899,9 @@ module ts {
emitSignatureParameters(node);
}

if (isSingleLineEmptyBlock(node.body) || !node.body) {
if (!node.body) {
// There can be no body when there are parse errors. Just emit an empty block
// in that case.
write(" { }");
}
else if (node.body.kind === SyntaxKind.Block) {
Expand Down Expand Up @@ -3979,15 +3981,57 @@ module ts {
}

function emitBlockFunctionBody(node: FunctionLikeDeclaration, body: Block) {
// If the body has no statements, and we know there's no code that would cause any
// prologue to be emitted, then just do a simple emit if the empty block.
if (body.statements.length === 0 && !anyParameterHasBindingPatternOrInitializer(node)) {
emitFunctionBodyWithNoStatements(node, body);
}
else {
emitFunctionBodyWithStatements(node, body);
}
}

function anyParameterHasBindingPatternOrInitializer(func: FunctionLikeDeclaration) {
return forEach(func.parameters, hasBindingPatternOrInitializer);
}

function hasBindingPatternOrInitializer(parameter: ParameterDeclaration) {
return parameter.initializer || isBindingPattern(parameter.name);
}

function emitFunctionBodyWithNoStatements(node: FunctionLikeDeclaration, body: Block) {
var singleLine = isSingleLineEmptyBlock(node.body);

write(" {");
if (singleLine) {
write(" ");
}
else {
increaseIndent();
writeLine();
}

emitLeadingCommentsOfPosition(body.statements.end);

if (!singleLine) {
decreaseIndent();
}

emitToken(SyntaxKind.CloseBraceToken, body.statements.end);
}

function emitFunctionBodyWithStatements(node: FunctionLikeDeclaration, body: Block) {
write(" {");
scopeEmitStart(node);

var outPos = writer.getTextPos();

increaseIndent();
emitDetachedComments(body.statements);
var startIndex = emitDirectivePrologues(body.statements, /*startWithNewLine*/ true);
emitFunctionBodyPreamble(node);
decreaseIndent();

var preambleEmitted = writer.getTextPos() !== outPos;

if (!preambleEmitted && nodeEndIsOnSameLineAsNodeStart(body, body)) {
Expand Down
8 changes: 6 additions & 2 deletions tests/baselines/reference/accessorWithInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ var C = (function () {
function C() {
}
Object.defineProperty(C.prototype, "X", {
set: function (v) { },
set: function (v) {
if (v === void 0) { v = 0; }
},
enumerable: true,
configurable: true
});
Object.defineProperty(C, "X", {
set: function (v2) { },
set: function (v2) {
if (v2 === void 0) { v2 = 0; }
},
enumerable: true,
configurable: true
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ b.b(1);

//// [callSignatureWithOptionalParameterAndInitializer.js]
// Optional parameters cannot also have initializer expressions, these are all errors
function foo(x) { }
var f = function foo(x) { };
var f2 = function (x, y) { };
function foo(x) {
if (x === void 0) { x = 1; }
}
var f = function foo(x) {
if (x === void 0) { x = 1; }
};
var f2 = function (x, y) {
if (y === void 0) { y = 1; }
};
foo(1);
foo();
f(1);
Expand All @@ -69,7 +75,9 @@ f2(1, 2);
var C = (function () {
function C() {
}
C.prototype.foo = function (x) { };
C.prototype.foo = function (x) {
if (x === void 0) { x = 1; }
};
return C;
})();
var c;
Expand All @@ -86,9 +94,15 @@ a(1);
a.foo();
a.foo(1);
var b = {
foo: function (x) { },
a: function foo(x, y) { },
b: function (x) { }
foo: function (x) {
if (x === void 0) { x = 1; }
},
a: function foo(x, y) {
if (y === void 0) { y = ''; }
},
b: function (x) {
if (x === void 0) { x = ''; }
}
};
b.foo();
b.foo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ b.b(1);

//// [callSignaturesWithParameterInitializers.js]
// Optional parameters allow initializers only in implementation signatures
function foo(x) { }
var f = function foo(x) { };
var f2 = function (x, y) { };
function foo(x) {
if (x === void 0) { x = 1; }
}
var f = function foo(x) {
if (x === void 0) { x = 1; }
};
var f2 = function (x, y) {
if (y === void 0) { y = 1; }
};
foo(1);
foo();
f(1);
Expand All @@ -71,7 +77,9 @@ f2(1, 2);
var C = (function () {
function C() {
}
C.prototype.foo = function (x) { };
C.prototype.foo = function (x) {
if (x === void 0) { x = 1; }
};
return C;
})();
var c;
Expand All @@ -89,9 +97,15 @@ a(1);
a.foo();
a.foo(1);
var b = {
foo: function (x) { },
a: function foo(x, y) { },
b: function (x) { }
foo: function (x) {
if (x === void 0) { x = 1; }
},
a: function foo(x, y) {
if (y === void 0) { y = 1; }
},
b: function (x) {
if (x === void 0) { x = 1; }
}
};
b.foo();
b.foo(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,29 @@ b.foo(1);
//// [callSignaturesWithParameterInitializers2.js]
// Optional parameters allow initializers only in implementation signatures
// All the below declarations are errors
function foo(x) { }
function foo(x) {
if (x === void 0) { x = 1; }
}
foo(1);
foo();
var C = (function () {
function C() {
}
C.prototype.foo = function (x) { };
C.prototype.foo = function (x) {
if (x === void 0) { x = 1; }
};
return C;
})();
var c;
c.foo();
c.foo(1);
var b = {
foo: function (x) { },
foo: function (x) { }
foo: function (x) {
if (x === void 0) { x = 1; }
},
foo: function (x) {
if (x === void 0) { x = 1; }
}
};
b.foo();
b.foo(1);
8 changes: 0 additions & 8 deletions tests/baselines/reference/callWithSpread.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ var __extends = this.__extends || function (d, b) {
d.prototype = new __();
};
function foo(x, y) {
var z = [];
for (var _i = 2; _i < arguments.length; _i++) {
z[_i - 2] = arguments[_i];
}
}
var a;
var z;
Expand Down Expand Up @@ -93,10 +89,6 @@ var C = (function () {
this.foo.apply(this, [x, y].concat(z));
}
C.prototype.foo = function (x, y) {
var z = [];
for (var _i = 2; _i < arguments.length; _i++) {
z[_i - 2] = arguments[_i];
}
};
return C;
})();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ module M {
var M;
(function (_M) {
_M.x = 3;
function fn(M, p) { }
function fn(M, p) {
if (p === void 0) { p = _M.x; }
}
})(M || (M = {}));
var M;
(function (_M_1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var M;
var c = (function () {
function c() {
}
c.prototype.fn = function (M, p) { };
c.prototype.fn = function (M, p) {
if (p === void 0) { p = _M.x; }
};
return c;
})();
})(M || (M = {}));
Expand Down
4 changes: 0 additions & 4 deletions tests/baselines/reference/collisionRestParameterFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ function f3NoError() {
var _i = 10; // no error
}
function f4(_i) {
var rest = [];
for (var _a = 1; _a < arguments.length; _a++) {
rest[_a - 1] = arguments[_a];
}
}
function f4NoError(_i) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ function foo() {
var _i = 10; // no error
}
function f4(_i) {
var rest = [];
for (var _a = 1; _a < arguments.length; _a++) {
rest[_a - 1] = arguments[_a];
}
}
function f4NoError(_i) {
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/contextualTyping.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions tests/baselines/reference/contextualTyping.sourcemap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2197,18 +2197,21 @@ sourceFile:contextualTyping.ts
2 > ^^^^
3 > ^
4 > ^
5 > ^^^^^
5 > ^^^^
6 > ^
1 >
>function
2 > c9t5
3 > (
4 > f: (n: number) => IFoo
5 > ) {}
5 > ) {
6 > }
1 >Emitted(87, 10) Source(146, 10) + SourceIndex(0)
2 >Emitted(87, 14) Source(146, 14) + SourceIndex(0)
3 >Emitted(87, 15) Source(146, 15) + SourceIndex(0)
4 >Emitted(87, 16) Source(146, 37) + SourceIndex(0)
5 >Emitted(87, 21) Source(146, 41) + SourceIndex(0)
5 >Emitted(87, 20) Source(146, 40) + SourceIndex(0)
6 >Emitted(87, 21) Source(146, 41) + SourceIndex(0)
---
>>>;
1 >
Expand Down
Loading