From 728a92ec1ab26a96175ccf5e2c8926101b624b8a Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 29 Mar 2017 13:08:13 -0700 Subject: [PATCH 1/2] Handle when namespace improt is malform and external module is undefined --- src/compiler/transformers/module/module.ts | 23 +++++++++++-------- src/compiler/transformers/module/system.ts | 26 ++++++++++++---------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/compiler/transformers/module/module.ts b/src/compiler/transformers/module/module.ts index 4ef9c674ad..81ed20bd4e 100644 --- a/src/compiler/transformers/module/module.ts +++ b/src/compiler/transformers/module/module.ts @@ -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); + } } } diff --git a/src/compiler/transformers/module/system.ts b/src/compiler/transformers/module/system.ts index 3daeee1e6d..56341f482f 100644 --- a/src/compiler/transformers/module/system.ts +++ b/src/compiler/transformers/module/system.ts @@ -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] + }); + } } } From 3c469c51a11081bd5d20cf80fab2adbb48dd37e0 Mon Sep 17 00:00:00 2001 From: Kanchalai Tanglertsampan Date: Wed, 29 Mar 2017 13:08:21 -0700 Subject: [PATCH 2/2] Add tests --- ...lidSyntaxNamespaceImportWithAMD.errors.txt | 22 +++++++++++ .../invalidSyntaxNamespaceImportWithAMD.js | 26 +++++++++++++ ...ntaxNamespaceImportWithCommonjs.errors.txt | 22 +++++++++++ ...nvalidSyntaxNamespaceImportWithCommonjs.js | 23 +++++++++++ ...SyntaxNamespaceImportWithSystem.errors.txt | 22 +++++++++++ .../invalidSyntaxNamespaceImportWithSystem.js | 38 +++++++++++++++++++ .../invalidSyntaxNamespaceImportWithAMD.ts | 6 +++ ...nvalidSyntaxNamespaceImportWithCommonjs.ts | 6 +++ .../invalidSyntaxNamespaceImportWithSystem.ts | 6 +++ 9 files changed, 171 insertions(+) create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.errors.txt create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.js create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.errors.txt create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.js create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.errors.txt create mode 100644 tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.js create mode 100644 tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts create mode 100644 tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts create mode 100644 tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.errors.txt b/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.errors.txt new file mode 100644 index 0000000000..97afe58604 --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.js b/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.js new file mode 100644 index 0000000000..b2adf37b50 --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithAMD.js @@ -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"; +}); diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.errors.txt b/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.errors.txt new file mode 100644 index 0000000000..97afe58604 --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.js b/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.js new file mode 100644 index 0000000000..b1ab6fc6aa --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithCommonjs.js @@ -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"; diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.errors.txt b/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.errors.txt new file mode 100644 index 0000000000..97afe58604 --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.errors.txt @@ -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. \ No newline at end of file diff --git a/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.js b/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.js new file mode 100644 index 0000000000..50e23aae57 --- /dev/null +++ b/tests/baselines/reference/invalidSyntaxNamespaceImportWithSystem.js @@ -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"; + } + }; +}); diff --git a/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts new file mode 100644 index 0000000000..4038feeb14 --- /dev/null +++ b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithAMD.ts @@ -0,0 +1,6 @@ +// @module: amd +// @filename: 0.ts +export class C { } + +// @filename: 1.ts +import * from Zero from "./0" \ No newline at end of file diff --git a/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts new file mode 100644 index 0000000000..f9362b04c5 --- /dev/null +++ b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithCommonjs.ts @@ -0,0 +1,6 @@ +// @module: commonjs +// @filename: 0.ts +export class C { } + +// @filename: 1.ts +import * from Zero from "./0" \ No newline at end of file diff --git a/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts new file mode 100644 index 0000000000..871871f8ef --- /dev/null +++ b/tests/cases/conformance/externalModules/invalidSyntaxNamespaceImportWithSystem.ts @@ -0,0 +1,6 @@ +// @module: system +// @filename: 0.ts +export class C { } + +// @filename: 1.ts +import * from Zero from "./0" \ No newline at end of file