From 36c22d10c7b9768c22e342cd7b927c64e79ddb2b Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Fri, 18 Jul 2014 16:59:52 -0700 Subject: [PATCH] emit 'use strict' at the beginning of the function --- src/compiler/emitter.ts | 39 ++++++++++++++---------- tests/baselines/reference/strictMode5.js | 6 ++-- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index eec80b72729..5642decf273 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -1158,6 +1158,11 @@ module ts { write(" {"); scopeEmitStart(node); increaseIndent(); + + var startIndex = 0; + if (node.body.kind === SyntaxKind.FunctionBlock) { + startIndex = emitDirectivePrologues((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((node.body).statements); + emitLinesStartingAt((node.body).statements, startIndex); } else { writeLine(); @@ -1658,25 +1663,27 @@ module ts { } } - function isDirectivePrologue(n: Statement): boolean { - return n.kind === SyntaxKind.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 && + (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) {"); diff --git a/tests/baselines/reference/strictMode5.js b/tests/baselines/reference/strictMode5.js index 8a3ff76fe04..c46de926dbb 100644 --- a/tests/baselines/reference/strictMode5.js +++ b/tests/baselines/reference/strictMode5.js @@ -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; } }