Change prologue emit location to inside module IIFEs

This commit is contained in:
Wesley Wigham 2015-10-05 11:22:16 -07:00
parent 4ecf4f4e71
commit 28475c345d
21 changed files with 180 additions and 10 deletions

View File

@ -446,7 +446,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
/** If removeComments is true, no leading-comments needed to be emitted **/
let emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos: number) { } : emitLeadingCommentsOfPositionWorker;
let moduleEmitDelegates: Map<(node: SourceFile, startIndex: number) => void> = {
let moduleEmitDelegates: Map<(node: SourceFile) => void> = {
[ModuleKind.ES6]: emitES6Module,
[ModuleKind.AMD]: emitAMDModule,
[ModuleKind.System]: emitSystemModule,
@ -6594,7 +6594,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write("}"); // execute
}
function emitSystemModule(node: SourceFile, startIndex: number): void {
function emitSystemModule(node: SourceFile): void {
collectExternalModuleInfo(node);
// System modules has the following shape
// System.register(['dep-1', ... 'dep-n'], function(exports) {/* module body function */})
@ -6639,6 +6639,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(`], function(${exportFunctionForFile}) {`);
writeLine();
increaseIndent();
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
emitEmitHelpers(node);
emitCaptureThisForNodeIfNecessary(node);
emitSystemModuleBody(node, dependencyGroups, startIndex);
@ -6732,7 +6733,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(") {");
}
function emitAMDModule(node: SourceFile, startIndex: number) {
function emitAMDModule(node: SourceFile) {
emitEmitHelpers(node);
collectExternalModuleInfo(node);
@ -6743,6 +6744,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
increaseIndent();
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
emitExportStarHelper();
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
@ -6753,7 +6755,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write("});");
}
function emitCommonJSModule(node: SourceFile, startIndex: number) {
function emitCommonJSModule(node: SourceFile) {
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
emitEmitHelpers(node);
collectExternalModuleInfo(node);
emitExportStarHelper();
@ -6763,7 +6766,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitExportEquals(/*emitAsReturn*/ false);
}
function emitUMDModule(node: SourceFile, startIndex: number) {
function emitUMDModule(node: SourceFile) {
emitEmitHelpers(node);
collectExternalModuleInfo(node);
@ -6782,6 +6785,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
})(`);
emitAMDFactoryHeader(dependencyNames);
increaseIndent();
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
emitExportStarHelper();
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
@ -6792,11 +6796,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write("});");
}
function emitES6Module(node: SourceFile, startIndex: number) {
function emitES6Module(node: SourceFile) {
externalImports = undefined;
exportSpecifiers = undefined;
exportEquals = undefined;
hasExportStars = false;
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
emitEmitHelpers(node);
emitCaptureThisForNodeIfNecessary(node);
emitLinesStartingAt(node.statements, startIndex);
@ -6985,14 +6990,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitShebang();
emitDetachedComments(node);
// emit prologue directives prior to __extends
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
if (isExternalModule(node) || compilerOptions.isolatedModules) {
let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS];
emitModule(node, startIndex);
emitModule(node);
}
else {
// emit prologue directives prior to __extends
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
externalImports = undefined;
exportSpecifiers = undefined;
exportEquals = undefined;

View File

@ -0,0 +1,15 @@
//// [modulePrologueAMD.ts]
"use strict";
export class Foo {}
//// [modulePrologueAMD.js]
define(["require", "exports"], function (require, exports) {
"use strict";
var Foo = (function () {
function Foo() {
}
return Foo;
})();
exports.Foo = Foo;
});

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/modulePrologueAMD.ts ===
"use strict";
export class Foo {}
>Foo : Symbol(Foo, Decl(modulePrologueAMD.ts, 0, 13))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/modulePrologueAMD.ts ===
"use strict";
>"use strict" : string
export class Foo {}
>Foo : Foo

View File

@ -0,0 +1,13 @@
//// [modulePrologueCommonjs.ts]
"use strict";
export class Foo {}
//// [modulePrologueCommonjs.js]
"use strict";
var Foo = (function () {
function Foo() {
}
return Foo;
})();
exports.Foo = Foo;

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/modulePrologueCommonjs.ts ===
"use strict";
export class Foo {}
>Foo : Symbol(Foo, Decl(modulePrologueCommonjs.ts, 0, 13))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/modulePrologueCommonjs.ts ===
"use strict";
>"use strict" : string
export class Foo {}
>Foo : Foo

View File

@ -0,0 +1,9 @@
//// [modulePrologueES6.ts]
"use strict";
export class Foo {}
//// [modulePrologueES6.js]
"use strict";
export class Foo {
}

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/modulePrologueES6.ts ===
"use strict";
export class Foo {}
>Foo : Symbol(Foo, Decl(modulePrologueES6.ts, 0, 13))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/modulePrologueES6.ts ===
"use strict";
>"use strict" : string
export class Foo {}
>Foo : Foo

View File

@ -0,0 +1,21 @@
//// [modulePrologueSystem.ts]
"use strict";
export class Foo {}
//// [modulePrologueSystem.js]
System.register([], function(exports_1) {
"use strict";
var Foo;
return {
setters:[],
execute: function() {
Foo = (function () {
function Foo() {
}
return Foo;
})();
exports_1("Foo", Foo);
}
}
});

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/modulePrologueSystem.ts ===
"use strict";
export class Foo {}
>Foo : Symbol(Foo, Decl(modulePrologueSystem.ts, 0, 13))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/modulePrologueSystem.ts ===
"use strict";
>"use strict" : string
export class Foo {}
>Foo : Foo

View File

@ -0,0 +1,22 @@
//// [modulePrologueUmd.ts]
"use strict";
export class Foo {}
//// [modulePrologueUmd.js]
(function (factory) {
if (typeof module === 'object' && typeof module.exports === 'object') {
var v = factory(require, exports); if (v !== undefined) module.exports = v;
}
else if (typeof define === 'function' && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
var Foo = (function () {
function Foo() {
}
return Foo;
})();
exports.Foo = Foo;
});

View File

@ -0,0 +1,6 @@
=== tests/cases/compiler/modulePrologueUmd.ts ===
"use strict";
export class Foo {}
>Foo : Symbol(Foo, Decl(modulePrologueUmd.ts, 0, 13))

View File

@ -0,0 +1,7 @@
=== tests/cases/compiler/modulePrologueUmd.ts ===
"use strict";
>"use strict" : string
export class Foo {}
>Foo : Foo

View File

@ -0,0 +1,4 @@
// @module: amd
"use strict";
export class Foo {}

View File

@ -0,0 +1,4 @@
// @module: commonjs
"use strict";
export class Foo {}

View File

@ -0,0 +1,5 @@
// @module: es6
// @target: es6
"use strict";
export class Foo {}

View File

@ -0,0 +1,4 @@
// @module: system
"use strict";
export class Foo {}

View File

@ -0,0 +1,4 @@
// @module: umd
"use strict";
export class Foo {}