Skip to content

Commit 363b7c8

Browse files
committed
Merge pull request #3391 from Microsoft/port-3370
Port PR 3370 into release 1.5
2 parents 6f45524 + 3c630aa commit 363b7c8

File tree

8 files changed

+66
-17
lines changed

8 files changed

+66
-17
lines changed

src/compiler/emitter.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5477,7 +5477,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
54775477
Debug.assert(!exportFunctionForFile);
54785478
// make sure that name of 'exports' function does not conflict with existing identifiers
54795479
exportFunctionForFile = makeUniqueName("exports");
5480-
write("System.register([");
5480+
write("System.register(");
5481+
if (node.moduleName) {
5482+
write(`"${node.moduleName}", `);
5483+
}
5484+
write("[")
54815485
for (let i = 0; i < externalImports.length; ++i) {
54825486
let text = getExternalModuleNameText(externalImports[i]);
54835487
if (i !== 0) {
@@ -5563,8 +5567,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
55635567

55645568
writeLine();
55655569
write("define(");
5566-
if (node.amdModuleName) {
5567-
write("\"" + node.amdModuleName + "\", ");
5570+
if (node.moduleName) {
5571+
write("\"" + node.moduleName + "\", ");
55685572
}
55695573
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
55705574
write(") {");

src/compiler/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4820,7 +4820,7 @@ module ts {
48204820

48214821
sourceFile.referencedFiles = referencedFiles;
48224822
sourceFile.amdDependencies = amdDependencies;
4823-
sourceFile.amdModuleName = amdModuleName;
4823+
sourceFile.moduleName = amdModuleName;
48244824
}
48254825

48264826
function setExternalModuleIndicator(sourceFile: SourceFile) {

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ module ts {
10011001
text: string;
10021002

10031003
amdDependencies: {path: string; name: string}[];
1004-
amdModuleName: string;
1004+
moduleName: string;
10051005
referencedFiles: FileReference[];
10061006

10071007
hasNoDefaultLib: boolean;

src/services/services.ts

Lines changed: 5 additions & 2 deletions
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) {
Lines changed: 10 additions & 0 deletions
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+
Lines changed: 14 additions & 0 deletions
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+
});
Lines changed: 5 additions & 0 deletions
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

Lines changed: 23 additions & 10 deletions
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)