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() { }