Merge pull request #3391 from Microsoft/port-3370

Port PR 3370 into release 1.5
This commit is contained in:
Jason Freeman 2015-06-10 16:13:58 -07:00
commit 363b7c85ab
8 changed files with 66 additions and 17 deletions

View file

@ -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(") {");

View file

@ -4820,7 +4820,7 @@ module ts {
sourceFile.referencedFiles = referencedFiles;
sourceFile.amdDependencies = amdDependencies;
sourceFile.amdModuleName = amdModuleName;
sourceFile.moduleName = amdModuleName;
}
function setExternalModuleIndicator(sourceFile: SourceFile) {

View file

@ -1001,7 +1001,7 @@ module ts {
text: string;
amdDependencies: {path: string; name: string}[];
amdModuleName: string;
moduleName: string;
referencedFiles: FileReference[];
hasNoDefaultLib: boolean;

View file

@ -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) {

View file

@ -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'.

View file

@ -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() {
}
}
});

View file

@ -0,0 +1,5 @@
// @module: system
// @isolatedModules: true
///<amd-module name='NamedModule'/>
import n from 'file1'

View file

@ -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(`/// <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)
});
});
}