Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5539,7 +5539,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) {
Expand Down Expand Up @@ -5625,8 +5629,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(") {");
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4895,7 +4895,7 @@ module ts {

sourceFile.referencedFiles = referencedFiles;
sourceFile.amdDependencies = amdDependencies;
sourceFile.amdModuleName = amdModuleName;
sourceFile.moduleName = amdModuleName;
}

function setExternalModuleIndicator(sourceFile: SourceFile) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ module ts {
text: string;

amdDependencies: {path: string; name: string}[];
amdModuleName: string;
moduleName: string;
referencedFiles: FileReference[];

hasNoDefaultLib: boolean;
Expand Down
7 changes: 5 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/systemModule12.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tests/cases/compiler/systemModule12.ts(3,15): error TS2307: Cannot find module 'file1'.


==== tests/cases/compiler/systemModule12.ts (1 errors) ====

///<amd-module name='NamedModule'/>
import n from 'file1'
~~~~~~~
!!! error TS2307: Cannot find module 'file1'.

14 changes: 14 additions & 0 deletions tests/baselines/reference/systemModule12.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//// [systemModule12.ts]

///<amd-module name='NamedModule'/>
import n from 'file1'


//// [systemModule12.js]
System.register("NamedModule", [], function(exports_1) {
return {
setters:[],
execute: function() {
}
}
});
5 changes: 5 additions & 0 deletions tests/cases/compiler/systemModule12.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @module: system
// @isolatedModules: true

///<amd-module name='NamedModule'/>
import n from 'file1'
33 changes: 23 additions & 10 deletions tests/cases/unittests/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand All @@ -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(`/// <reference path="file2.ts" />
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)
});
});
}