Merge pull request #3391 from Microsoft/port-3370
Port PR 3370 into release 1.5
This commit is contained in:
commit
363b7c85ab
8 changed files with 66 additions and 17 deletions
|
@ -5477,7 +5477,11 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||||
Debug.assert(!exportFunctionForFile);
|
Debug.assert(!exportFunctionForFile);
|
||||||
// make sure that name of 'exports' function does not conflict with existing identifiers
|
// make sure that name of 'exports' function does not conflict with existing identifiers
|
||||||
exportFunctionForFile = makeUniqueName("exports");
|
exportFunctionForFile = makeUniqueName("exports");
|
||||||
write("System.register([");
|
write("System.register(");
|
||||||
|
if (node.moduleName) {
|
||||||
|
write(`"${node.moduleName}", `);
|
||||||
|
}
|
||||||
|
write("[")
|
||||||
for (let i = 0; i < externalImports.length; ++i) {
|
for (let i = 0; i < externalImports.length; ++i) {
|
||||||
let text = getExternalModuleNameText(externalImports[i]);
|
let text = getExternalModuleNameText(externalImports[i]);
|
||||||
if (i !== 0) {
|
if (i !== 0) {
|
||||||
|
@ -5563,8 +5567,8 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||||
|
|
||||||
writeLine();
|
writeLine();
|
||||||
write("define(");
|
write("define(");
|
||||||
if (node.amdModuleName) {
|
if (node.moduleName) {
|
||||||
write("\"" + node.amdModuleName + "\", ");
|
write("\"" + node.moduleName + "\", ");
|
||||||
}
|
}
|
||||||
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
|
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
|
||||||
write(") {");
|
write(") {");
|
||||||
|
|
|
@ -4820,7 +4820,7 @@ module ts {
|
||||||
|
|
||||||
sourceFile.referencedFiles = referencedFiles;
|
sourceFile.referencedFiles = referencedFiles;
|
||||||
sourceFile.amdDependencies = amdDependencies;
|
sourceFile.amdDependencies = amdDependencies;
|
||||||
sourceFile.amdModuleName = amdModuleName;
|
sourceFile.moduleName = amdModuleName;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setExternalModuleIndicator(sourceFile: SourceFile) {
|
function setExternalModuleIndicator(sourceFile: SourceFile) {
|
||||||
|
|
|
@ -1001,7 +1001,7 @@ module ts {
|
||||||
text: string;
|
text: string;
|
||||||
|
|
||||||
amdDependencies: {path: string; name: string}[];
|
amdDependencies: {path: string; name: string}[];
|
||||||
amdModuleName: string;
|
moduleName: string;
|
||||||
referencedFiles: FileReference[];
|
referencedFiles: FileReference[];
|
||||||
|
|
||||||
hasNoDefaultLib: boolean;
|
hasNoDefaultLib: boolean;
|
||||||
|
|
|
@ -735,7 +735,7 @@ module ts {
|
||||||
public endOfFileToken: Node;
|
public endOfFileToken: Node;
|
||||||
|
|
||||||
public amdDependencies: { name: string; path: string }[];
|
public amdDependencies: { name: string; path: string }[];
|
||||||
public amdModuleName: string;
|
public moduleName: string;
|
||||||
public referencedFiles: FileReference[];
|
public referencedFiles: FileReference[];
|
||||||
|
|
||||||
public syntacticDiagnostics: Diagnostic[];
|
public syntacticDiagnostics: Diagnostic[];
|
||||||
|
@ -1766,7 +1766,7 @@ module ts {
|
||||||
* - noLib = true
|
* - noLib = true
|
||||||
* - noResolve = 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();
|
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
|
||||||
|
|
||||||
options.isolatedModules = true;
|
options.isolatedModules = true;
|
||||||
|
@ -1785,6 +1785,9 @@ module ts {
|
||||||
// Parse
|
// Parse
|
||||||
let inputFileName = fileName || "module.ts";
|
let inputFileName = fileName || "module.ts";
|
||||||
let sourceFile = createSourceFile(inputFileName, input, options.target);
|
let sourceFile = createSourceFile(inputFileName, input, options.target);
|
||||||
|
if (moduleName) {
|
||||||
|
sourceFile.moduleName = moduleName;
|
||||||
|
}
|
||||||
|
|
||||||
// Store syntactic diagnostics
|
// Store syntactic diagnostics
|
||||||
if (diagnostics && sourceFile.parseDiagnostics) {
|
if (diagnostics && sourceFile.parseDiagnostics) {
|
||||||
|
|
10
tests/baselines/reference/systemModule12.errors.txt
Normal file
10
tests/baselines/reference/systemModule12.errors.txt
Normal 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'.
|
||||||
|
|
14
tests/baselines/reference/systemModule12.js
Normal file
14
tests/baselines/reference/systemModule12.js
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
5
tests/cases/compiler/systemModule12.ts
Normal file
5
tests/cases/compiler/systemModule12.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
// @module: system
|
||||||
|
// @isolatedModules: true
|
||||||
|
|
||||||
|
///<amd-module name='NamedModule'/>
|
||||||
|
import n from 'file1'
|
|
@ -3,9 +3,9 @@
|
||||||
module ts {
|
module ts {
|
||||||
describe("Transpile", () => {
|
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 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++) {
|
for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
|
||||||
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);
|
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", () => {
|
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."
|
// 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", () => {
|
it("Generates no diagnostics with valid inputs", () => {
|
||||||
// No errors
|
// 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", () => {
|
it("Generates no diagnostics for missing file references", () => {
|
||||||
runTest(`/// <reference path="file2.ts" />
|
runTest(`/// <reference path="file2.ts" />
|
||||||
var x = 0;`,
|
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", () => {
|
it("Generates no diagnostics for missing module imports", () => {
|
||||||
runTest(`import {a} from "module2";`,
|
runTest(`import {a} from "module2";`,
|
||||||
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Generates expected syntactic diagnostics", () => {
|
it("Generates expected syntactic diagnostics", () => {
|
||||||
runTest(`a b`,
|
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", () => {
|
it("Does not generate semantic diagnostics", () => {
|
||||||
runTest(`var x: string = 0;`,
|
runTest(`var x: string = 0;`,
|
||||||
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Generates module output", () => {
|
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", () => {
|
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)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue