Merge pull request #140 from Microsoft/prologuedirectives

emit prologue directives as first lines in file, fixes #44
This commit is contained in:
Vladimir Matveev 2014-07-17 18:36:59 -07:00
commit 42234668ef
5 changed files with 95 additions and 10 deletions

View file

@ -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 && (<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);
}
}

View file

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

View file

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

View file

@ -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";
}

View file

@ -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"
}