From 2cb7401a56d89bb80ed58c9d0ede7dfbf63c933b Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Mon, 11 Apr 2016 14:27:18 -0700 Subject: [PATCH 1/3] Do not emit ES6 import/export inside namespaces ES6 imports and exports are illegal inside namespaces. In order to emit syntactically legal code, skip emit for these incorrect statements. --- src/compiler/transformers/ts.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 0b8c11a66e2..90bac017d5a 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -157,7 +157,15 @@ namespace ts { * @param node The node to visit. */ function namespaceElementVisitorWorker(node: Node): VisitResult { - if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) { + if (node.kind === SyntaxKind.ExportDeclaration || + node.kind === SyntaxKind.ImportDeclaration || + node.kind === SyntaxKind.ImportClause || + (node.kind === SyntaxKind.ImportEqualsDeclaration && + (node).moduleReference.kind === SyntaxKind.ExternalModuleReference)) { + // do not emit ES6 imports and exports since they are illegal inside a namespace + return createNotEmittedStatement(node); + } + else if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) { // This node is explicitly marked as TypeScript, or is exported at the namespace // level, so we should transform the node. return visitTypeScript(node); @@ -2903,4 +2911,4 @@ namespace ts { && resolver.getNodeCheckFlags(getOriginalNode(currentSuperContainer)) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding); } } -} \ No newline at end of file +} From 221c0f36564729313900558c89a13342a43c12a6 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Tue, 12 Apr 2016 09:56:51 -0700 Subject: [PATCH 2/3] Update es5 module internal imports test and related baselines --- .../es5ModuleInternalNamedImports.errors.txt | 18 +++++++++++++++++- .../reference/es5ModuleInternalNamedImports.js | 5 ++++- .../reference/es6ModuleInternalNamedImports.js | 7 ------- .../es6ModuleInternalNamedImports2.js | 7 ------- .../exportDeclarationInInternalModule.js | 1 - .../compiler/es5ModuleInternalNamedImports.ts | 4 ++++ 6 files changed, 25 insertions(+), 17 deletions(-) diff --git a/tests/baselines/reference/es5ModuleInternalNamedImports.errors.txt b/tests/baselines/reference/es5ModuleInternalNamedImports.errors.txt index f51ff27a412..936f381628d 100644 --- a/tests/baselines/reference/es5ModuleInternalNamedImports.errors.txt +++ b/tests/baselines/reference/es5ModuleInternalNamedImports.errors.txt @@ -6,9 +6,13 @@ tests/cases/compiler/es5ModuleInternalNamedImports.ts(27,5): error TS1194: Expor tests/cases/compiler/es5ModuleInternalNamedImports.ts(28,5): error TS1194: Export declarations are not permitted in a namespace. tests/cases/compiler/es5ModuleInternalNamedImports.ts(29,5): error TS1194: Export declarations are not permitted in a namespace. tests/cases/compiler/es5ModuleInternalNamedImports.ts(30,5): error TS1194: Export declarations are not permitted in a namespace. +tests/cases/compiler/es5ModuleInternalNamedImports.ts(31,25): error TS1147: Import declarations in a namespace cannot reference a module. +tests/cases/compiler/es5ModuleInternalNamedImports.ts(32,20): error TS1147: Import declarations in a namespace cannot reference a module. +tests/cases/compiler/es5ModuleInternalNamedImports.ts(33,32): error TS1147: Import declarations in a namespace cannot reference a module. +tests/cases/compiler/es5ModuleInternalNamedImports.ts(35,16): error TS2307: Cannot find module 'M3'. -==== tests/cases/compiler/es5ModuleInternalNamedImports.ts (8 errors) ==== +==== tests/cases/compiler/es5ModuleInternalNamedImports.ts (12 errors) ==== export module M { // variable @@ -55,5 +59,17 @@ tests/cases/compiler/es5ModuleInternalNamedImports.ts(30,5): error TS1194: Expor export {M_A as a}; ~~~~~~~~~~~~~~~~~~ !!! error TS1194: Export declarations are not permitted in a namespace. + import * as M2 from "M2"; + ~~~~ +!!! error TS1147: Import declarations in a namespace cannot reference a module. + import M4 from "M4"; + ~~~~ +!!! error TS1147: Import declarations in a namespace cannot reference a module. + export import M5 = require("M5"); + ~~~~ +!!! error TS1147: Import declarations in a namespace cannot reference a module. } + import M3 from "M3"; + ~~~~ +!!! error TS2307: Cannot find module 'M3'. \ No newline at end of file diff --git a/tests/baselines/reference/es5ModuleInternalNamedImports.js b/tests/baselines/reference/es5ModuleInternalNamedImports.js index e8963b7db40..2fcbd5a53d8 100644 --- a/tests/baselines/reference/es5ModuleInternalNamedImports.js +++ b/tests/baselines/reference/es5ModuleInternalNamedImports.js @@ -29,7 +29,11 @@ export module M { export {M_F as f}; export {M_E as e}; export {M_A as a}; + import * as M2 from "M2"; + import M4 from "M4"; + export import M5 = require("M5"); } +import M3 from "M3"; //// [es5ModuleInternalNamedImports.js] @@ -60,6 +64,5 @@ define(["require", "exports"], function (require, exports) { var M_E = M.M_E; // alias M.M_A = M_M; - // Reexports })(M = exports.M || (exports.M = {})); }); diff --git a/tests/baselines/reference/es6ModuleInternalNamedImports.js b/tests/baselines/reference/es6ModuleInternalNamedImports.js index 98504601d02..22d15b9fd61 100644 --- a/tests/baselines/reference/es6ModuleInternalNamedImports.js +++ b/tests/baselines/reference/es6ModuleInternalNamedImports.js @@ -55,11 +55,4 @@ export var M; var M_E = M.M_E; // alias M.M_A = M_M; - // Reexports - export { M_V as v }; - export { M_C as c }; - export { M_M as m }; - export { M_F as f }; - export { M_E as e }; - export { M_A as a }; })(M || (M = {})); diff --git a/tests/baselines/reference/es6ModuleInternalNamedImports2.js b/tests/baselines/reference/es6ModuleInternalNamedImports2.js index 98226a1e52b..35eabc9a65e 100644 --- a/tests/baselines/reference/es6ModuleInternalNamedImports2.js +++ b/tests/baselines/reference/es6ModuleInternalNamedImports2.js @@ -59,11 +59,4 @@ export var M; M.M_A = M_M; })(M || (M = {})); (function (M) { - // Reexports - export { M_V as v }; - export { M_C as c }; - export { M_M as m }; - export { M_F as f }; - export { M_E as e }; - export { M_A as a }; })(M || (M = {})); diff --git a/tests/baselines/reference/exportDeclarationInInternalModule.js b/tests/baselines/reference/exportDeclarationInInternalModule.js index ecd9d5adf4d..a9ea4bf5eda 100644 --- a/tests/baselines/reference/exportDeclarationInInternalModule.js +++ b/tests/baselines/reference/exportDeclarationInInternalModule.js @@ -53,7 +53,6 @@ var Bbb; return SomeType; }()); Bbb.SomeType = SomeType; - // this line causes the nullref })(Bbb || (Bbb = {})); var a; diff --git a/tests/cases/compiler/es5ModuleInternalNamedImports.ts b/tests/cases/compiler/es5ModuleInternalNamedImports.ts index 05943d1c67e..62b976a7df7 100644 --- a/tests/cases/compiler/es5ModuleInternalNamedImports.ts +++ b/tests/cases/compiler/es5ModuleInternalNamedImports.ts @@ -30,4 +30,8 @@ export module M { export {M_F as f}; export {M_E as e}; export {M_A as a}; + import * as M2 from "M2"; + import M4 from "M4"; + export import M5 = require("M5"); } +import M3 from "M3"; From bdb76400f316813e290865941ff8bb2449354003 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders Date: Wed, 13 Apr 2016 11:38:21 -0700 Subject: [PATCH 3/3] Return undefined instead of createNotEmittedStatement --- src/compiler/transformers/ts.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/transformers/ts.ts b/src/compiler/transformers/ts.ts index 90bac017d5a..1cf93f050c9 100644 --- a/src/compiler/transformers/ts.ts +++ b/src/compiler/transformers/ts.ts @@ -163,7 +163,7 @@ namespace ts { (node.kind === SyntaxKind.ImportEqualsDeclaration && (node).moduleReference.kind === SyntaxKind.ExternalModuleReference)) { // do not emit ES6 imports and exports since they are illegal inside a namespace - return createNotEmittedStatement(node); + return undefined; } else if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) { // This node is explicitly marked as TypeScript, or is exported at the namespace