Skip to content

Commit bc1bda5

Browse files
authored
Merge pull request #14915 from Microsoft/master-fix14870
[Master] exception when emitting with malformed namespace import
2 parents e5a0f60 + 3c469c5 commit bc1bda5

11 files changed

+199
-21
lines changed

src/compiler/transformers/module/module.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,20 @@ namespace ts {
359359

360360
// Find the name of the module alias, if there is one
361361
const importAliasName = getLocalNameForExternalImport(importNode, currentSourceFile);
362-
if (includeNonAmdDependencies && importAliasName) {
363-
// Set emitFlags on the name of the classDeclaration
364-
// This is so that when printer will not substitute the identifier
365-
setEmitFlags(importAliasName, EmitFlags.NoSubstitution);
366-
aliasedModuleNames.push(externalModuleName);
367-
importAliasNames.push(createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, importAliasName));
368-
}
369-
else {
370-
unaliasedModuleNames.push(externalModuleName);
362+
// It is possible that externalModuleName is undefined if it is not string literal.
363+
// This can happen in the invalid import syntax.
364+
// E.g : "import * from alias from 'someLib';"
365+
if (externalModuleName) {
366+
if (includeNonAmdDependencies && importAliasName) {
367+
// Set emitFlags on the name of the classDeclaration
368+
// This is so that when printer will not substitute the identifier
369+
setEmitFlags(importAliasName, EmitFlags.NoSubstitution);
370+
aliasedModuleNames.push(externalModuleName);
371+
importAliasNames.push(createParameter(/*decorators*/ undefined, /*modifiers*/ undefined, /*dotDotDotToken*/ undefined, importAliasName));
372+
}
373+
else {
374+
unaliasedModuleNames.push(externalModuleName);
375+
}
371376
}
372377
}
373378

src/compiler/transformers/module/system.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,20 @@ namespace ts {
151151
for (let i = 0; i < externalImports.length; i++) {
152152
const externalImport = externalImports[i];
153153
const externalModuleName = getExternalModuleNameLiteral(externalImport, currentSourceFile, host, resolver, compilerOptions);
154-
const text = externalModuleName.text;
155-
const groupIndex = groupIndices.get(text);
156-
if (groupIndex !== undefined) {
157-
// deduplicate/group entries in dependency list by the dependency name
158-
dependencyGroups[groupIndex].externalImports.push(externalImport);
159-
}
160-
else {
161-
groupIndices.set(text, dependencyGroups.length);
162-
dependencyGroups.push({
163-
name: externalModuleName,
164-
externalImports: [externalImport]
165-
});
154+
if (externalModuleName) {
155+
const text = externalModuleName.text;
156+
const groupIndex = groupIndices.get(text);
157+
if (groupIndex !== undefined) {
158+
// deduplicate/group entries in dependency list by the dependency name
159+
dependencyGroups[groupIndex].externalImports.push(externalImport);
160+
}
161+
else {
162+
groupIndices.set(text, dependencyGroups.length);
163+
dependencyGroups.push({
164+
name: externalModuleName,
165+
externalImports: [externalImport]
166+
});
167+
}
166168
}
167169
}
168170

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
2+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
3+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
4+
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
5+
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
6+
7+
8+
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
9+
export class C { }
10+
11+
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
12+
import * from Zero from "./0"
13+
~~~~
14+
!!! error TS1005: 'as' expected.
15+
~~~~
16+
!!! error TS1005: 'from' expected.
17+
~~~~
18+
!!! error TS1141: String literal expected.
19+
~~~~
20+
!!! error TS1005: ';' expected.
21+
~~~~~
22+
!!! error TS1005: ';' expected.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts] ////
2+
3+
//// [0.ts]
4+
export class C { }
5+
6+
//// [1.ts]
7+
import * from Zero from "./0"
8+
9+
//// [0.js]
10+
define(["require", "exports"], function (require, exports) {
11+
"use strict";
12+
exports.__esModule = true;
13+
var C = (function () {
14+
function C() {
15+
}
16+
return C;
17+
}());
18+
exports.C = C;
19+
});
20+
//// [1.js]
21+
define(["require", "exports"], function (require, exports) {
22+
"use strict";
23+
exports.__esModule = true;
24+
from;
25+
"./0";
26+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
2+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
3+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
4+
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
5+
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
6+
7+
8+
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
9+
export class C { }
10+
11+
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
12+
import * from Zero from "./0"
13+
~~~~
14+
!!! error TS1005: 'as' expected.
15+
~~~~
16+
!!! error TS1005: 'from' expected.
17+
~~~~
18+
!!! error TS1141: String literal expected.
19+
~~~~
20+
!!! error TS1005: ';' expected.
21+
~~~~~
22+
!!! error TS1005: ';' expected.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts] ////
2+
3+
//// [0.ts]
4+
export class C { }
5+
6+
//// [1.ts]
7+
import * from Zero from "./0"
8+
9+
//// [0.js]
10+
"use strict";
11+
exports.__esModule = true;
12+
var C = (function () {
13+
function C() {
14+
}
15+
return C;
16+
}());
17+
exports.C = C;
18+
//// [1.js]
19+
"use strict";
20+
exports.__esModule = true;
21+
var from = require();
22+
from;
23+
"./0";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
tests/cases/conformance/externalModules/1.ts(1,10): error TS1005: 'as' expected.
2+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1005: 'from' expected.
3+
tests/cases/conformance/externalModules/1.ts(1,15): error TS1141: String literal expected.
4+
tests/cases/conformance/externalModules/1.ts(1,20): error TS1005: ';' expected.
5+
tests/cases/conformance/externalModules/1.ts(1,25): error TS1005: ';' expected.
6+
7+
8+
==== tests/cases/conformance/externalModules/0.ts (0 errors) ====
9+
export class C { }
10+
11+
==== tests/cases/conformance/externalModules/1.ts (5 errors) ====
12+
import * from Zero from "./0"
13+
~~~~
14+
!!! error TS1005: 'as' expected.
15+
~~~~
16+
!!! error TS1005: 'from' expected.
17+
~~~~
18+
!!! error TS1141: String literal expected.
19+
~~~~
20+
!!! error TS1005: ';' expected.
21+
~~~~~
22+
!!! error TS1005: ';' expected.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//// [tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts] ////
2+
3+
//// [0.ts]
4+
export class C { }
5+
6+
//// [1.ts]
7+
import * from Zero from "./0"
8+
9+
//// [0.js]
10+
System.register([], function (exports_1, context_1) {
11+
"use strict";
12+
var __moduleName = context_1 && context_1.id;
13+
var C;
14+
return {
15+
setters: [],
16+
execute: function () {
17+
C = (function () {
18+
function C() {
19+
}
20+
return C;
21+
}());
22+
exports_1("C", C);
23+
}
24+
};
25+
});
26+
//// [1.js]
27+
System.register([], function (exports_1, context_1) {
28+
"use strict";
29+
var __moduleName = context_1 && context_1.id;
30+
var from;
31+
return {
32+
setters: [],
33+
execute: function () {
34+
from;
35+
"./0";
36+
}
37+
};
38+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @module: amd
2+
// @filename: 0.ts
3+
export class C { }
4+
5+
// @filename: 1.ts
6+
import * from Zero from "./0"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @module: commonjs
2+
// @filename: 0.ts
3+
export class C { }
4+
5+
// @filename: 1.ts
6+
import * from Zero from "./0"

0 commit comments

Comments
 (0)