Merge pull request #14915 from Microsoft/master-fix14870

[Master] exception when emitting with malformed namespace import
This commit is contained in:
Yui 2017-03-29 14:37:19 -07:00 committed by GitHub
commit bc1bda542c
11 changed files with 199 additions and 21 deletions

View file

@ -359,15 +359,20 @@ namespace ts {
// Find the name of the module alias, if there is one
const importAliasName = getLocalNameForExternalImport(importNode, currentSourceFile);
if (includeNonAmdDependencies && importAliasName) {
// Set emitFlags on the name of the classDeclaration
// This is so that when printer will not substitute the identifier
setEmitFlags(importAliasName, EmitFlags.NoSubstitution);
aliasedModuleNames.push(externalModuleName);
importAliasNames.push(createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, importAliasName));
}
else {
unaliasedModuleNames.push(externalModuleName);
// It is possible that externalModuleName is undefined if it is not string literal.
// This can happen in the invalid import syntax.
// E.g : "import * from alias from 'someLib';"
if (externalModuleName) {
if (includeNonAmdDependencies && importAliasName) {
// Set emitFlags on the name of the classDeclaration
// This is so that when printer will not substitute the identifier
setEmitFlags(importAliasName, EmitFlags.NoSubstitution);
aliasedModuleNames.push(externalModuleName);
importAliasNames.push(createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, importAliasName));
}
else {
unaliasedModuleNames.push(externalModuleName);
}
}
}

View file

@ -151,18 +151,20 @@ namespace ts {
for (let i = 0; i < externalImports.length; i++) {
const externalImport = externalImports[i];
const externalModuleName = getExternalModuleNameLiteral(externalImport, currentSourceFile, host, resolver, compilerOptions);
const text = externalModuleName.text;
const groupIndex = groupIndices.get(text);
if (groupIndex !== undefined) {
// deduplicate/group entries in dependency list by the dependency name
dependencyGroups[groupIndex].externalImports.push(externalImport);
}
else {
groupIndices.set(text, dependencyGroups.length);
dependencyGroups.push({
name: externalModuleName,
externalImports: [externalImport]
});
if (externalModuleName) {
const text = externalModuleName.text;
const groupIndex = groupIndices.get(text);
if (groupIndex !== undefined) {
// deduplicate/group entries in dependency list by the dependency name
dependencyGroups[groupIndex].externalImports.push(externalImport);
}
else {
groupIndices.set(text, dependencyGroups.length);
dependencyGroups.push({
name: externalModuleName,
externalImports: [externalImport]
});
}
}
}

View file

@ -0,0 +1,22 @@
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
export class C { }
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
import * from Zero from "./0"
~~~~
!!! error TS1005: 'as' expected.
~~~~
!!! error TS1005: 'from' expected.
~~~~
!!! error TS1141: String literal expected.
~~~~
!!! error TS1005: ';' expected.
~~~~~
!!! error TS1005: ';' expected.

View file

@ -0,0 +1,26 @@
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts] ////
//// [0.ts]
export class C { }
//// [1.ts]
import * from Zero from "./0"
//// [0.js]
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
});
//// [1.js]
define(["require", "exports"], function (require, exports) {
"use strict";
exports.__esModule = true;
from;
"./0";
});

View file

@ -0,0 +1,22 @@
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
export class C { }
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
import * from Zero from "./0"
~~~~
!!! error TS1005: 'as' expected.
~~~~
!!! error TS1005: 'from' expected.
~~~~
!!! error TS1141: String literal expected.
~~~~
!!! error TS1005: ';' expected.
~~~~~
!!! error TS1005: ';' expected.

View file

@ -0,0 +1,23 @@
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts] ////
//// [0.ts]
export class C { }
//// [1.ts]
import * from Zero from "./0"
//// [0.js]
"use strict";
exports.__esModule = true;
var C = (function () {
function C() {
}
return C;
}());
exports.C = C;
//// [1.js]
"use strict";
exports.__esModule = true;
var from = require();
from;
"./0";

View file

@ -0,0 +1,22 @@
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
export class C { }
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
import * from Zero from "./0"
~~~~
!!! error TS1005: 'as' expected.
~~~~
!!! error TS1005: 'from' expected.
~~~~
!!! error TS1141: String literal expected.
~~~~
!!! error TS1005: ';' expected.
~~~~~
!!! error TS1005: ';' expected.

View file

@ -0,0 +1,38 @@
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts] ////
//// [0.ts]
export class C { }
//// [1.ts]
import * from Zero from "./0"
//// [0.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var C;
return {
setters: [],
execute: function () {
C = (function () {
function C() {
}
return C;
}());
exports_1("C", C);
}
};
});
//// [1.js]
System.register([], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var from;
return {
setters: [],
execute: function () {
from;
"./0";
}
};
});

View file

@ -0,0 +1,6 @@
// @module: amd
// @filename: 0.ts
export class C { }
// @filename: 1.ts
import * from Zero from "./0"

View file

@ -0,0 +1,6 @@
// @module: commonjs
// @filename: 0.ts
export class C { }
// @filename: 1.ts
import * from Zero from "./0"

View file

@ -0,0 +1,6 @@
// @module: system
// @filename: 0.ts
export class C { }
// @filename: 1.ts
import * from Zero from "./0"