Skip to content

Commit 34ad57a

Browse files
committed
Merge pull request #8027 from Microsoft/transforms-skip-es6-imports-inside-namespace
[Transforms] Do not emit ES6 import/export inside namespaces
2 parents f1ec827 + bdb7640 commit 34ad57a

File tree

7 files changed

+35
-19
lines changed

7 files changed

+35
-19
lines changed

src/compiler/transformers/ts.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ namespace ts {
157157
* @param node The node to visit.
158158
*/
159159
function namespaceElementVisitorWorker(node: Node): VisitResult<Node> {
160-
if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) {
160+
if (node.kind === SyntaxKind.ExportDeclaration ||
161+
node.kind === SyntaxKind.ImportDeclaration ||
162+
node.kind === SyntaxKind.ImportClause ||
163+
(node.kind === SyntaxKind.ImportEqualsDeclaration &&
164+
(<ImportEqualsDeclaration>node).moduleReference.kind === SyntaxKind.ExternalModuleReference)) {
165+
// do not emit ES6 imports and exports since they are illegal inside a namespace
166+
return undefined;
167+
}
168+
else if (node.transformFlags & TransformFlags.TypeScript || hasModifier(node, ModifierFlags.Export)) {
161169
// This node is explicitly marked as TypeScript, or is exported at the namespace
162170
// level, so we should transform the node.
163171
return visitTypeScript(node);
@@ -2914,4 +2922,4 @@ namespace ts {
29142922
&& resolver.getNodeCheckFlags(currentSuperContainer) & (NodeCheckFlags.AsyncMethodWithSuper | NodeCheckFlags.AsyncMethodWithSuperBinding);
29152923
}
29162924
}
2917-
}
2925+
}

tests/baselines/reference/es5ModuleInternalNamedImports.errors.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ tests/cases/compiler/es5ModuleInternalNamedImports.ts(27,5): error TS1194: Expor
66
tests/cases/compiler/es5ModuleInternalNamedImports.ts(28,5): error TS1194: Export declarations are not permitted in a namespace.
77
tests/cases/compiler/es5ModuleInternalNamedImports.ts(29,5): error TS1194: Export declarations are not permitted in a namespace.
88
tests/cases/compiler/es5ModuleInternalNamedImports.ts(30,5): error TS1194: Export declarations are not permitted in a namespace.
9+
tests/cases/compiler/es5ModuleInternalNamedImports.ts(31,25): error TS1147: Import declarations in a namespace cannot reference a module.
10+
tests/cases/compiler/es5ModuleInternalNamedImports.ts(32,20): error TS1147: Import declarations in a namespace cannot reference a module.
11+
tests/cases/compiler/es5ModuleInternalNamedImports.ts(33,32): error TS1147: Import declarations in a namespace cannot reference a module.
12+
tests/cases/compiler/es5ModuleInternalNamedImports.ts(35,16): error TS2307: Cannot find module 'M3'.
913

1014

11-
==== tests/cases/compiler/es5ModuleInternalNamedImports.ts (8 errors) ====
15+
==== tests/cases/compiler/es5ModuleInternalNamedImports.ts (12 errors) ====
1216

1317
export module M {
1418
// variable
@@ -55,5 +59,17 @@ tests/cases/compiler/es5ModuleInternalNamedImports.ts(30,5): error TS1194: Expor
5559
export {M_A as a};
5660
~~~~~~~~~~~~~~~~~~
5761
!!! error TS1194: Export declarations are not permitted in a namespace.
62+
import * as M2 from "M2";
63+
~~~~
64+
!!! error TS1147: Import declarations in a namespace cannot reference a module.
65+
import M4 from "M4";
66+
~~~~
67+
!!! error TS1147: Import declarations in a namespace cannot reference a module.
68+
export import M5 = require("M5");
69+
~~~~
70+
!!! error TS1147: Import declarations in a namespace cannot reference a module.
5871
}
72+
import M3 from "M3";
73+
~~~~
74+
!!! error TS2307: Cannot find module 'M3'.
5975

tests/baselines/reference/es5ModuleInternalNamedImports.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ export module M {
2929
export {M_F as f};
3030
export {M_E as e};
3131
export {M_A as a};
32+
import * as M2 from "M2";
33+
import M4 from "M4";
34+
export import M5 = require("M5");
3235
}
36+
import M3 from "M3";
3337

3438

3539
//// [es5ModuleInternalNamedImports.js]
@@ -60,6 +64,5 @@ define(["require", "exports"], function (require, exports) {
6064
var M_E = M.M_E;
6165
// alias
6266
M.M_A = M_M;
63-
// Reexports
6467
})(M = exports.M || (exports.M = {}));
6568
});

tests/baselines/reference/es6ModuleInternalNamedImports.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,4 @@ export var M;
5555
var M_E = M.M_E;
5656
// alias
5757
M.M_A = M_M;
58-
// Reexports
59-
export { M_V as v };
60-
export { M_C as c };
61-
export { M_M as m };
62-
export { M_F as f };
63-
export { M_E as e };
64-
export { M_A as a };
6558
})(M || (M = {}));

tests/baselines/reference/es6ModuleInternalNamedImports2.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,4 @@ export var M;
5959
M.M_A = M_M;
6060
})(M || (M = {}));
6161
(function (M) {
62-
// Reexports
63-
export { M_V as v };
64-
export { M_C as c };
65-
export { M_M as m };
66-
export { M_F as f };
67-
export { M_E as e };
68-
export { M_A as a };
6962
})(M || (M = {}));

tests/baselines/reference/exportDeclarationInInternalModule.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ var Bbb;
5353
return SomeType;
5454
}());
5555
Bbb.SomeType = SomeType;
56-
// this line causes the nullref
5756
})(Bbb || (Bbb = {}));
5857
var a;
5958

tests/cases/compiler/es5ModuleInternalNamedImports.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@ export module M {
3030
export {M_F as f};
3131
export {M_E as e};
3232
export {M_A as a};
33+
import * as M2 from "M2";
34+
import M4 from "M4";
35+
export import M5 = require("M5");
3336
}
37+
import M3 from "M3";

0 commit comments

Comments
 (0)