Merge pull request #157 from Microsoft/use_strict_in_functions

emit 'use strict' at the beginning of the function
This commit is contained in:
Vladimir Matveev 2014-07-18 22:20:23 -07:00
commit ea46b974e8
2 changed files with 26 additions and 19 deletions

View File

@ -1158,6 +1158,11 @@ module ts {
write(" {");
scopeEmitStart(node);
increaseIndent();
var startIndex = 0;
if (node.body.kind === SyntaxKind.FunctionBlock) {
startIndex = emitDirectivePrologues((<Block>node.body).statements, /*startWithNewLine*/ true);
}
var outPos = writer.getTextPos();
emitCaptureThisForNodeIfNecessary(node);
emitDefaultValueAssignments(node);
@ -1176,7 +1181,7 @@ module ts {
}
else {
if (node.body.kind === SyntaxKind.FunctionBlock) {
emitLines((<Block>node.body).statements);
emitLinesStartingAt((<Block>node.body).statements, startIndex);
}
else {
writeLine();
@ -1658,25 +1663,27 @@ module ts {
}
}
function isDirectivePrologue(n: Statement): boolean {
return n.kind === SyntaxKind.ExpressionStatement && (<ExpressionStatement>n).expression.kind === SyntaxKind.StringLiteral;
function emitDirectivePrologues(statements: Statement[], startWithNewLine: boolean): number {
for (var i = 0; i < statements.length; ++i) {
if (statements[i].kind === SyntaxKind.ExpressionStatement &&
(<ExpressionStatement>statements[i]).expression.kind === SyntaxKind.StringLiteral) {
if (startWithNewLine || i > 0) {
writeLine();
}
emit(statements[i]);
}
else {
// return index of the first non prologue directive
return i;
}
}
return statements.length;
}
function emitSourceFile(node: SourceFile) {
currentSourceFile = node;
var startIndex = 0;
for (; startIndex < node.statements.length; ++startIndex) {
// emit prologue directives prior to __extends
if (isDirectivePrologue(node.statements[startIndex])) {
if (startIndex > 0) {
writeLine();
}
emit(node.statements[startIndex]);
}
else {
break;
}
}
// emit prologue directives prior to __extends
var startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
if (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends) {
writeLine();
write("var __extends = this.__extends || function (d, b) {");

View File

@ -20,18 +20,18 @@ function bar(x: number = 10) {
//// [strictMode5.js]
function foo() {
"use strict";
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
"use strict";
}
var A = (function () {
function A() {
}
A.prototype.m = function () {
var _this = this;
"use strict";
var _this = this;
var v = function () {
return _this.n();
};
@ -41,6 +41,6 @@ var A = (function () {
return A;
})();
function bar(x) {
if (x === void 0) { x = 10; }
"use strict";
if (x === void 0) { x = 10; }
}