Skip to content

Commit 836007e

Browse files
committed
Merge pull request microsoft#5116 from weswigham/5059-prologue-location
Change prologue emit location to inside module IIFEs
2 parents 4ecf4f4 + 28475c3 commit 836007e

21 files changed

Lines changed: 180 additions & 10 deletions

src/compiler/emitter.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
446446
/** If removeComments is true, no leading-comments needed to be emitted **/
447447
let emitLeadingCommentsOfPosition = compilerOptions.removeComments ? function (pos: number) { } : emitLeadingCommentsOfPositionWorker;
448448

449-
let moduleEmitDelegates: Map<(node: SourceFile, startIndex: number) => void> = {
449+
let moduleEmitDelegates: Map<(node: SourceFile) => void> = {
450450
[ModuleKind.ES6]: emitES6Module,
451451
[ModuleKind.AMD]: emitAMDModule,
452452
[ModuleKind.System]: emitSystemModule,
@@ -6594,7 +6594,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
65946594
write("}"); // execute
65956595
}
65966596

6597-
function emitSystemModule(node: SourceFile, startIndex: number): void {
6597+
function emitSystemModule(node: SourceFile): void {
65986598
collectExternalModuleInfo(node);
65996599
// System modules has the following shape
66006600
// System.register(['dep-1', ... 'dep-n'], function(exports) {/* module body function */})
@@ -6639,6 +6639,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
66396639
write(`], function(${exportFunctionForFile}) {`);
66406640
writeLine();
66416641
increaseIndent();
6642+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
66426643
emitEmitHelpers(node);
66436644
emitCaptureThisForNodeIfNecessary(node);
66446645
emitSystemModuleBody(node, dependencyGroups, startIndex);
@@ -6732,7 +6733,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67326733
write(") {");
67336734
}
67346735

6735-
function emitAMDModule(node: SourceFile, startIndex: number) {
6736+
function emitAMDModule(node: SourceFile) {
67366737
emitEmitHelpers(node);
67376738
collectExternalModuleInfo(node);
67386739

@@ -6743,6 +6744,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67436744
}
67446745
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
67456746
increaseIndent();
6747+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
67466748
emitExportStarHelper();
67476749
emitCaptureThisForNodeIfNecessary(node);
67486750
emitLinesStartingAt(node.statements, startIndex);
@@ -6753,7 +6755,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67536755
write("});");
67546756
}
67556757

6756-
function emitCommonJSModule(node: SourceFile, startIndex: number) {
6758+
function emitCommonJSModule(node: SourceFile) {
6759+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
67576760
emitEmitHelpers(node);
67586761
collectExternalModuleInfo(node);
67596762
emitExportStarHelper();
@@ -6763,7 +6766,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67636766
emitExportEquals(/*emitAsReturn*/ false);
67646767
}
67656768

6766-
function emitUMDModule(node: SourceFile, startIndex: number) {
6769+
function emitUMDModule(node: SourceFile) {
67676770
emitEmitHelpers(node);
67686771
collectExternalModuleInfo(node);
67696772

@@ -6782,6 +6785,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67826785
})(`);
67836786
emitAMDFactoryHeader(dependencyNames);
67846787
increaseIndent();
6788+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ true);
67856789
emitExportStarHelper();
67866790
emitCaptureThisForNodeIfNecessary(node);
67876791
emitLinesStartingAt(node.statements, startIndex);
@@ -6792,11 +6796,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
67926796
write("});");
67936797
}
67946798

6795-
function emitES6Module(node: SourceFile, startIndex: number) {
6799+
function emitES6Module(node: SourceFile) {
67966800
externalImports = undefined;
67976801
exportSpecifiers = undefined;
67986802
exportEquals = undefined;
67996803
hasExportStars = false;
6804+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
68006805
emitEmitHelpers(node);
68016806
emitCaptureThisForNodeIfNecessary(node);
68026807
emitLinesStartingAt(node.statements, startIndex);
@@ -6985,14 +6990,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
69856990
emitShebang();
69866991
emitDetachedComments(node);
69876992

6988-
// emit prologue directives prior to __extends
6989-
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
6990-
69916993
if (isExternalModule(node) || compilerOptions.isolatedModules) {
69926994
let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS];
6993-
emitModule(node, startIndex);
6995+
emitModule(node);
69946996
}
69956997
else {
6998+
// emit prologue directives prior to __extends
6999+
let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false);
69967000
externalImports = undefined;
69977001
exportSpecifiers = undefined;
69987002
exportEquals = undefined;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [modulePrologueAMD.ts]
2+
"use strict";
3+
4+
export class Foo {}
5+
6+
//// [modulePrologueAMD.js]
7+
define(["require", "exports"], function (require, exports) {
8+
"use strict";
9+
var Foo = (function () {
10+
function Foo() {
11+
}
12+
return Foo;
13+
})();
14+
exports.Foo = Foo;
15+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/modulePrologueAMD.ts ===
2+
"use strict";
3+
4+
export class Foo {}
5+
>Foo : Symbol(Foo, Decl(modulePrologueAMD.ts, 0, 13))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/modulePrologueAMD.ts ===
2+
"use strict";
3+
>"use strict" : string
4+
5+
export class Foo {}
6+
>Foo : Foo
7+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [modulePrologueCommonjs.ts]
2+
"use strict";
3+
4+
export class Foo {}
5+
6+
//// [modulePrologueCommonjs.js]
7+
"use strict";
8+
var Foo = (function () {
9+
function Foo() {
10+
}
11+
return Foo;
12+
})();
13+
exports.Foo = Foo;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/modulePrologueCommonjs.ts ===
2+
"use strict";
3+
4+
export class Foo {}
5+
>Foo : Symbol(Foo, Decl(modulePrologueCommonjs.ts, 0, 13))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/modulePrologueCommonjs.ts ===
2+
"use strict";
3+
>"use strict" : string
4+
5+
export class Foo {}
6+
>Foo : Foo
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [modulePrologueES6.ts]
2+
"use strict";
3+
4+
export class Foo {}
5+
6+
//// [modulePrologueES6.js]
7+
"use strict";
8+
export class Foo {
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/modulePrologueES6.ts ===
2+
"use strict";
3+
4+
export class Foo {}
5+
>Foo : Symbol(Foo, Decl(modulePrologueES6.ts, 0, 13))
6+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/compiler/modulePrologueES6.ts ===
2+
"use strict";
3+
>"use strict" : string
4+
5+
export class Foo {}
6+
>Foo : Foo
7+

0 commit comments

Comments
 (0)