diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 86b2b78b3f4..c1e85fa09fe 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -552,7 +552,11 @@ module ts { } function emitLines(nodes: Node[]) { - for (var i = 0; i < nodes.length; i++) { + emitLinesStartingAt(nodes, /*startIndex*/ 0); + } + + function emitLinesStartingAt(nodes: Node[], startIndex: number): void { + for (var i = startIndex; i < nodes.length; i++) { writeLine(); emit(nodes[i]); } @@ -1594,7 +1598,7 @@ module ts { }); } - function emitAMDModule(node: SourceFile) { + function emitAMDModule(node: SourceFile, startIndex: number) { var imports = getExternalImportDeclarations(node); writeLine(); write("define([\"require\", \"exports\""); @@ -1615,7 +1619,7 @@ module ts { write(") {"); increaseIndent(); emitCaptureThisForNodeIfNecessary(node); - emitLines(node.statements); + emitLinesStartingAt(node.statements, startIndex); var exportName = resolver.getExportAssignmentName(node); if (exportName) { writeLine(); @@ -1633,9 +1637,9 @@ module ts { write("});"); } - function emitCommonJSModule(node: SourceFile) { + function emitCommonJSModule(node: SourceFile, startIndex: number) { emitCaptureThisForNodeIfNecessary(node); - emitLines(node.statements); + emitLinesStartingAt(node.statements, startIndex); var exportName = resolver.getExportAssignmentName(node); if (exportName) { writeLine(); @@ -1650,8 +1654,25 @@ module ts { } } + function isDirectivePrologue(n: Statement): boolean { + return n.kind === SyntaxKind.ExpressionStatement && (n).expression.kind === SyntaxKind.StringLiteral; + } + 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; + } + } if (!extendsEmitted && resolver.getNodeCheckFlags(node) & NodeCheckFlags.EmitExtends) { writeLine(); write("var __extends = this.__extends || function (d, b) {"); @@ -1671,15 +1692,15 @@ module ts { } if (node.flags & NodeFlags.ExternalModule) { if (compilerOptions.module === ModuleKind.AMD) { - emitAMDModule(node); + emitAMDModule(node, startIndex); } else { - emitCommonJSModule(node); + emitCommonJSModule(node, startIndex); } } else { emitCaptureThisForNodeIfNecessary(node); - emitLines(node.statements); + emitLinesStartingAt(node.statements, startIndex); } } diff --git a/tests/baselines/reference/strictMode1.js b/tests/baselines/reference/strictMode1.js index 14bba3aa4a3..f4c9db50ab7 100644 --- a/tests/baselines/reference/strictMode1.js +++ b/tests/baselines/reference/strictMode1.js @@ -4,13 +4,13 @@ class A {} class B extends A {} //// [strictMode1.js] +"use strict"; var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; -"use strict"; var A = (function () { function A() { } diff --git a/tests/baselines/reference/strictMode3.js b/tests/baselines/reference/strictMode3.js index 39d55dffd14..792ab931f53 100644 --- a/tests/baselines/reference/strictMode3.js +++ b/tests/baselines/reference/strictMode3.js @@ -12,13 +12,13 @@ function foo() { } //// [strictMode3.js] +"use strict"; var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; -"use strict"; var A = (function () { function A() { } diff --git a/tests/baselines/reference/strictMode5.js b/tests/baselines/reference/strictMode5.js new file mode 100644 index 00000000000..8a3ff76fe04 --- /dev/null +++ b/tests/baselines/reference/strictMode5.js @@ -0,0 +1,46 @@ +//// [strictMode5.ts] +function foo(...args) { + "use strict" +} + +class A { + m() { + "use strict" + + var v = () => { + return this.n(); + }; + } + n() {} +} + +function bar(x: number = 10) { + "use strict" +} + +//// [strictMode5.js] +function foo() { + 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 v = function () { + return _this.n(); + }; + }; + A.prototype.n = function () { + }; + return A; +})(); +function bar(x) { + if (x === void 0) { x = 10; } + "use strict"; +} diff --git a/tests/cases/compiler/strictMode5.ts b/tests/cases/compiler/strictMode5.ts new file mode 100644 index 00000000000..c980cf7c86d --- /dev/null +++ b/tests/cases/compiler/strictMode5.ts @@ -0,0 +1,18 @@ +function foo(...args) { + "use strict" +} + +class A { + m() { + "use strict" + + var v = () => { + return this.n(); + }; + } + n() {} +} + +function bar(x: number = 10) { + "use strict" +} \ No newline at end of file