Skip to content

Commit 45cf145

Browse files
committed
Merge pull request microsoft#3370 from Microsoft/named_modules
emit module name for system modules, add moduleName argument to 'tran…
2 parents 223d26f + 00e28ff commit 45cf145

File tree

8 files changed

+66
-17
lines changed

8 files changed

+66
-17
lines changed

src/compiler/emitter.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -5539,7 +5539,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
55395539
Debug.assert(!exportFunctionForFile);
55405540
// make sure that name of 'exports' function does not conflict with existing identifiers
55415541
exportFunctionForFile = makeUniqueName("exports");
5542-
write("System.register([");
5542+
write("System.register(");
5543+
if (node.moduleName) {
5544+
write(`"${node.moduleName}", `);
5545+
}
5546+
write("[")
55435547
for (let i = 0; i < externalImports.length; ++i) {
55445548
let text = getExternalModuleNameText(externalImports[i]);
55455549
if (i !== 0) {
@@ -5625,8 +5629,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
56255629

56265630
writeLine();
56275631
write("define(");
5628-
if (node.amdModuleName) {
5629-
write("\"" + node.amdModuleName + "\", ");
5632+
if (node.moduleName) {
5633+
write("\"" + node.moduleName + "\", ");
56305634
}
56315635
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
56325636
write(") {");

src/compiler/parser.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4895,7 +4895,7 @@ module ts {
48954895

48964896
sourceFile.referencedFiles = referencedFiles;
48974897
sourceFile.amdDependencies = amdDependencies;
4898-
sourceFile.amdModuleName = amdModuleName;
4898+
sourceFile.moduleName = amdModuleName;
48994899
}
49004900

49014901
function setExternalModuleIndicator(sourceFile: SourceFile) {

src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ module ts {
11421142
text: string;
11431143

11441144
amdDependencies: {path: string; name: string}[];
1145-
amdModuleName: string;
1145+
moduleName: string;
11461146
referencedFiles: FileReference[];
11471147

11481148
hasNoDefaultLib: boolean;

src/services/services.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ module ts {
735735
public endOfFileToken: Node;
736736

737737
public amdDependencies: { name: string; path: string }[];
738-
public amdModuleName: string;
738+
public moduleName: string;
739739
public referencedFiles: FileReference[];
740740

741741
public syntacticDiagnostics: Diagnostic[];
@@ -1766,7 +1766,7 @@ module ts {
17661766
* - noLib = true
17671767
* - noResolve = true
17681768
*/
1769-
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[]): string {
1769+
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string {
17701770
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
17711771

17721772
options.isolatedModules = true;
@@ -1785,6 +1785,9 @@ module ts {
17851785
// Parse
17861786
let inputFileName = fileName || "module.ts";
17871787
let sourceFile = createSourceFile(inputFileName, input, options.target);
1788+
if (moduleName) {
1789+
sourceFile.moduleName = moduleName;
1790+
}
17881791

17891792
// Store syntactic diagnostics
17901793
if (diagnostics && sourceFile.parseDiagnostics) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/systemModule12.ts(3,15): error TS2307: Cannot find module 'file1'.
2+
3+
4+
==== tests/cases/compiler/systemModule12.ts (1 errors) ====
5+
6+
///<amd-module name='NamedModule'/>
7+
import n from 'file1'
8+
~~~~~~~
9+
!!! error TS2307: Cannot find module 'file1'.
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [systemModule12.ts]
2+
3+
///<amd-module name='NamedModule'/>
4+
import n from 'file1'
5+
6+
7+
//// [systemModule12.js]
8+
System.register("NamedModule", [], function(exports_1) {
9+
return {
10+
setters:[],
11+
execute: function() {
12+
}
13+
}
14+
});
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @module: system
2+
// @isolatedModules: true
3+
4+
///<amd-module name='NamedModule'/>
5+
import n from 'file1'

tests/cases/unittests/transpile.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module ts {
44
describe("Transpile", () => {
55

6-
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
6+
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, moduleName?: string, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
77
let diagnostics: Diagnostic[] = [];
8-
let result = transpile(input, compilerOptions, "file.ts", diagnostics);
8+
let result = transpile(input, compilerOptions, "file.ts", diagnostics, moduleName);
99

1010
for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
1111
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);
@@ -19,41 +19,54 @@ module ts {
1919

2020
it("Generates correct compilerOptions diagnostics", () => {
2121
// Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher."
22-
runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
22+
runTest(`var x = 0;`, {}, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
2323
});
2424

2525
it("Generates no diagnostics with valid inputs", () => {
2626
// No errors
27-
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
27+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
2828
});
2929

3030
it("Generates no diagnostics for missing file references", () => {
3131
runTest(`/// <reference path="file2.ts" />
3232
var x = 0;`,
33-
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
33+
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
3434
});
3535

3636
it("Generates no diagnostics for missing module imports", () => {
3737
runTest(`import {a} from "module2";`,
38-
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
38+
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
3939
});
4040

4141
it("Generates expected syntactic diagnostics", () => {
4242
runTest(`a b`,
43-
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
43+
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
4444
});
4545

4646
it("Does not generate semantic diagnostics", () => {
4747
runTest(`var x: string = 0;`,
48-
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
48+
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
4949
});
5050

5151
it("Generates module output", () => {
52-
runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
52+
runTest(`var x = 0;`, { module: ModuleKind.AMD }, /*moduleName*/undefined, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
5353
});
5454

5555
it("Uses correct newLine character", () => {
56-
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
56+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, /*moduleName*/undefined, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
57+
});
58+
59+
it("Sets module name", () => {
60+
let output =
61+
`System.register("NamedModule", [], function(exports_1) {\n var x;\n` +
62+
` return {\n` +
63+
` setters:[],\n` +
64+
` execute: function() {\n` +
65+
` var x = 1;\n` +
66+
` }\n` +
67+
` }\n` +
68+
`});\n`;
69+
runTest("var x = 1;", { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, "NamedModule", output)
5770
});
5871
});
5972
}

0 commit comments

Comments
 (0)