From 7c8a50336d6c1bb56d0daef91cf52d05fc12ff56 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 3 Jun 2015 14:55:42 -0700 Subject: [PATCH 1/2] emit module name for system modules, add moduleName argument to 'transpile' function Conflicts: src/services/services.ts --- src/compiler/emitter.ts | 10 +++++++--- src/compiler/parser.ts | 2 +- src/compiler/types.ts | 2 +- src/services/services.ts | 7 +++++-- .../baselines/reference/systemModule12.errors.txt | 10 ++++++++++ tests/baselines/reference/systemModule12.js | 14 ++++++++++++++ tests/cases/compiler/systemModule12.ts | 5 +++++ 7 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/systemModule12.errors.txt create mode 100644 tests/baselines/reference/systemModule12.js create mode 100644 tests/cases/compiler/systemModule12.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 165f0da5cf..7d0a29f9e2 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -5477,7 +5477,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { Debug.assert(!exportFunctionForFile); // make sure that name of 'exports' function does not conflict with existing identifiers exportFunctionForFile = makeUniqueName("exports"); - write("System.register(["); + write("System.register("); + if (node.moduleName) { + write(`"${node.moduleName}", `); + } + write("[") for (let i = 0; i < externalImports.length; ++i) { let text = getExternalModuleNameText(externalImports[i]); if (i !== 0) { @@ -5563,8 +5567,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { writeLine(); write("define("); - if (node.amdModuleName) { - write("\"" + node.amdModuleName + "\", "); + if (node.moduleName) { + write("\"" + node.moduleName + "\", "); } emitAMDDependencies(node, /*includeNonAmdDependencies*/ true); write(") {"); diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 055fc1b6f7..07da065c1a 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -4820,7 +4820,7 @@ module ts { sourceFile.referencedFiles = referencedFiles; sourceFile.amdDependencies = amdDependencies; - sourceFile.amdModuleName = amdModuleName; + sourceFile.moduleName = amdModuleName; } function setExternalModuleIndicator(sourceFile: SourceFile) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 1db5e57d29..1a72e9fc94 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1001,7 +1001,7 @@ module ts { text: string; amdDependencies: {path: string; name: string}[]; - amdModuleName: string; + moduleName: string; referencedFiles: FileReference[]; hasNoDefaultLib: boolean; diff --git a/src/services/services.ts b/src/services/services.ts index 6a497c8730..ece411ae1e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -735,7 +735,7 @@ module ts { public endOfFileToken: Node; public amdDependencies: { name: string; path: string }[]; - public amdModuleName: string; + public moduleName: string; public referencedFiles: FileReference[]; public syntacticDiagnostics: Diagnostic[]; @@ -1766,7 +1766,7 @@ module ts { * - noLib = true * - noResolve = true */ - export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[]): string { + export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string { let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions(); options.isolatedModules = true; @@ -1785,6 +1785,9 @@ module ts { // Parse let inputFileName = fileName || "module.ts"; let sourceFile = createSourceFile(inputFileName, input, options.target); + if (moduleName) { + sourceFile.moduleName = moduleName; + } // Store syntactic diagnostics if (diagnostics && sourceFile.parseDiagnostics) { diff --git a/tests/baselines/reference/systemModule12.errors.txt b/tests/baselines/reference/systemModule12.errors.txt new file mode 100644 index 0000000000..5c89e2e65d --- /dev/null +++ b/tests/baselines/reference/systemModule12.errors.txt @@ -0,0 +1,10 @@ +tests/cases/compiler/systemModule12.ts(3,15): error TS2307: Cannot find module 'file1'. + + +==== tests/cases/compiler/systemModule12.ts (1 errors) ==== + + /// + import n from 'file1' + ~~~~~~~ +!!! error TS2307: Cannot find module 'file1'. + \ No newline at end of file diff --git a/tests/baselines/reference/systemModule12.js b/tests/baselines/reference/systemModule12.js new file mode 100644 index 0000000000..0b5f5a3e85 --- /dev/null +++ b/tests/baselines/reference/systemModule12.js @@ -0,0 +1,14 @@ +//// [systemModule12.ts] + +/// +import n from 'file1' + + +//// [systemModule12.js] +System.register("NamedModule", [], function(exports_1) { + return { + setters:[], + execute: function() { + } + } +}); diff --git a/tests/cases/compiler/systemModule12.ts b/tests/cases/compiler/systemModule12.ts new file mode 100644 index 0000000000..690fabc0a2 --- /dev/null +++ b/tests/cases/compiler/systemModule12.ts @@ -0,0 +1,5 @@ +// @module: system +// @isolatedModules: true + +/// +import n from 'file1' From 3c630aa247e079af94a6dd045a7d07cad7cb2b26 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Thu, 4 Jun 2015 22:22:25 -0700 Subject: [PATCH 2/2] added tests --- tests/cases/unittests/transpile.ts | 33 +++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index c906c03a9e..7aa9106880 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -3,9 +3,9 @@ module ts { describe("Transpile", () => { - function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void { + function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, moduleName?: string, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void { let diagnostics: Diagnostic[] = []; - let result = transpile(input, compilerOptions, "file.ts", diagnostics); + let result = transpile(input, compilerOptions, "file.ts", diagnostics, moduleName); for (let i = 0; i < expectedDiagnosticCodes.length; i++) { assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`); @@ -19,41 +19,54 @@ module ts { it("Generates correct compilerOptions diagnostics", () => { // Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher." - runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]); + runTest(`var x = 0;`, {}, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]); }); it("Generates no diagnostics with valid inputs", () => { // No errors - runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); + runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); }); it("Generates no diagnostics for missing file references", () => { runTest(`/// var x = 0;`, - { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); + { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); }); it("Generates no diagnostics for missing module imports", () => { runTest(`import {a} from "module2";`, - { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); + { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); }); it("Generates expected syntactic diagnostics", () => { runTest(`a b`, - { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected + { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected }); it("Does not generate semantic diagnostics", () => { runTest(`var x: string = 0;`, - { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); + { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []); }); it("Generates module output", () => { - runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`); + runTest(`var x = 0;`, { module: ModuleKind.AMD }, /*moduleName*/undefined, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`); }); it("Uses correct newLine character", () => { - runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []); + runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, /*moduleName*/undefined, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []); + }); + + it("Sets module name", () => { + let output = + `System.register("NamedModule", [], function(exports_1) {\n var x;\n` + + ` return {\n` + + ` setters:[],\n` + + ` execute: function() {\n` + + ` var x = 1;\n` + + ` }\n` + + ` }\n` + + `});\n`; + runTest("var x = 1;", { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, "NamedModule", output) }); }); }