Skip to content

Commit 24a17ac

Browse files
authored
Error on invalid uses of namespace export (microsoft#37715)
1 parent 57f9076 commit 24a17ac

10 files changed

+115
-10
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34018,7 +34018,7 @@ namespace ts {
3401834018
return false;
3401934019
}
3402034020
if (inAmbientExternalModule && isExternalModuleNameRelative(moduleName.text)) {
34021-
// we have already reported errors on top level imports\exports in external module augmentations in checkModuleDeclaration
34021+
// we have already reported errors on top level imports/exports in external module augmentations in checkModuleDeclaration
3402234022
// no need to do this again.
3402334023
if (!isTopLevelInExternalModuleAugmentation(node)) {
3402434024
// TypeScript 1.0 spec (April 2013): 12.1.6
@@ -34154,16 +34154,10 @@ namespace ts {
3415434154

3415534155
checkGrammarExportDeclaration(node);
3415634156
if (!node.moduleSpecifier || checkExternalImportOrExportDeclaration(node)) {
34157-
if (node.exportClause) {
34157+
if (node.exportClause && !isNamespaceExport(node.exportClause)) {
3415834158
// export { x, y }
3415934159
// export { x, y } from "foo"
34160-
if (isNamedExports(node.exportClause)) {
34161-
forEach(node.exportClause.elements, checkExportSpecifier);
34162-
}
34163-
else if(!isNamespaceExport(node.exportClause)) {
34164-
checkImportBinding(node.exportClause);
34165-
}
34166-
34160+
forEach(node.exportClause.elements, checkExportSpecifier);
3416734161
const inAmbientExternalModule = node.parent.kind === SyntaxKind.ModuleBlock && isAmbientModule(node.parent.parent);
3416834162
const inAmbientNamespaceDeclaration = !inAmbientExternalModule && node.parent.kind === SyntaxKind.ModuleBlock &&
3416934163
!node.moduleSpecifier && node.flags & NodeFlags.Ambient;
@@ -34177,7 +34171,9 @@ namespace ts {
3417734171
if (moduleSymbol && hasExportAssignmentSymbol(moduleSymbol)) {
3417834172
error(node.moduleSpecifier, Diagnostics.Module_0_uses_export_and_cannot_be_used_with_export_Asterisk, symbolToString(moduleSymbol));
3417934173
}
34180-
34174+
else if (node.exportClause) {
34175+
checkAliasSymbol(node.exportClause);
34176+
}
3418134177
if (moduleKind !== ModuleKind.System && moduleKind < ModuleKind.ES2015) {
3418234178
checkExternalEmitHelpers(node, ExternalEmitHelpers.ExportStar);
3418334179
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/es2020/modules/b.ts(1,21): error TS2498: Module '"tests/cases/conformance/es2020/modules/a"' uses 'export =' and cannot be used with 'export *'.
2+
3+
4+
==== tests/cases/conformance/es2020/modules/a.ts (0 errors) ====
5+
export = {}
6+
7+
==== tests/cases/conformance/es2020/modules/b.ts (1 errors) ====
8+
export * as ns from './a';
9+
~~~~~
10+
!!! error TS2498: Module '"tests/cases/conformance/es2020/modules/a"' uses 'export =' and cannot be used with 'export *'.
11+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/conformance/es2020/modules/exportAsNamespace_exportAssignment.ts] ////
2+
3+
//// [a.ts]
4+
export = {}
5+
6+
//// [b.ts]
7+
export * as ns from './a';
8+
9+
10+
//// [a.js]
11+
"use strict";
12+
module.exports = {};
13+
//// [b.js]
14+
"use strict";
15+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16+
if (k2 === undefined) k2 = k;
17+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
18+
}) : (function(o, m, k, k2) {
19+
if (k2 === undefined) k2 = k;
20+
o[k2] = m[k];
21+
}));
22+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
23+
Object.defineProperty(o, "default", { enumerable: true, value: v });
24+
}) : function(o, v) {
25+
o["default"] = v;
26+
});
27+
var __importStar = (this && this.__importStar) || function (mod) {
28+
if (mod && mod.__esModule) return mod;
29+
var result = {};
30+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
31+
__setModuleDefault(result, mod);
32+
return result;
33+
};
34+
exports.__esModule = true;
35+
exports.ns = __importStar(require("./a"));
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/conformance/es2020/modules/b.ts(1,1): error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
2+
3+
4+
==== tests/cases/conformance/es2020/modules/a.ts (0 errors) ====
5+
export {}
6+
7+
==== tests/cases/conformance/es2020/modules/b.ts (1 errors) ====
8+
export * as ns from './a'; // Error
9+
~~~~~~~~~~~~~~~~~~~~~~~~~~
10+
!!! error TS2354: This syntax requires an imported helper but module 'tslib' cannot be found.
11+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/conformance/es2020/modules/exportAsNamespace_missingEmitHelpers.ts] ////
2+
3+
//// [a.ts]
4+
export {}
5+
6+
//// [b.ts]
7+
export * as ns from './a'; // Error
8+
9+
10+
//// [a.js]
11+
"use strict";
12+
exports.__esModule = true;
13+
//// [b.js]
14+
"use strict";
15+
exports.__esModule = true;
16+
exports.ns = require("./a"); // Error
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/es2020/modules/exportAsNamespace_nonExistent.ts(1,21): error TS2307: Cannot find module './nonexistent' or its corresponding type declarations.
2+
3+
4+
==== tests/cases/conformance/es2020/modules/exportAsNamespace_nonExistent.ts (1 errors) ====
5+
export * as ns from './nonexistent'; // Error
6+
~~~~~~~~~~~~~~~
7+
!!! error TS2307: Cannot find module './nonexistent' or its corresponding type declarations.
8+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [exportAsNamespace_nonExistent.ts]
2+
export * as ns from './nonexistent'; // Error
3+
4+
5+
//// [exportAsNamespace_nonExistent.js]
6+
export * as ns from './nonexistent'; // Error
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @module: commonjs
2+
// @esModuleInterop: true
3+
// @noTypesAndSymbols: true
4+
5+
// @Filename: a.ts
6+
export = {}
7+
8+
// @Filename: b.ts
9+
export * as ns from './a';
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @module: commonjs
2+
// @importHelpers: true
3+
// @noTypesAndSymbols: true
4+
5+
// @Filename: a.ts
6+
export {}
7+
8+
// @Filename: b.ts
9+
export * as ns from './a'; // Error
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @module: esnext
2+
// @noTypesAndSymbols: true
3+
4+
export * as ns from './nonexistent'; // Error

0 commit comments

Comments
 (0)