Skip to content

Commit 22f38b7

Browse files
Merge pull request #16704 from charlespierce/namespace_export_default
Diagnostics for default class / function export in namespace
2 parents c4319e3 + 8418d67 commit 22f38b7

13 files changed

+222
-2
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23666,7 +23666,14 @@ namespace ts {
2366623666
}
2366723667
flags |= ModifierFlags.Export;
2366823668
break;
23669+
case SyntaxKind.DefaultKeyword:
23670+
const container = node.parent.kind === SyntaxKind.SourceFile ? node.parent : node.parent.parent;
23671+
if (container.kind === SyntaxKind.ModuleDeclaration && !isAmbientModule(container)) {
23672+
return grammarErrorOnNode(modifier, Diagnostics.A_default_export_can_only_be_used_in_an_ECMAScript_style_module);
23673+
}
2366923674

23675+
flags |= ModifierFlags.Default;
23676+
break;
2367023677
case SyntaxKind.DeclareKeyword:
2367123678
if (flags & ModifierFlags.Ambient) {
2367223679
return grammarErrorOnNode(modifier, Diagnostics._0_modifier_already_seen, "declare");

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3158,7 +3158,7 @@ namespace ts {
31583158
getExternalModuleOrNamespaceExportName(currentNamespaceContainerName, node, /*allowComments*/ false, /*allowSourceMaps*/ true),
31593159
getLocalName(node)
31603160
);
3161-
setSourceMapRange(expression, createRange(node.name.pos, node.end));
3161+
setSourceMapRange(expression, createRange(node.name ? node.name.pos : node.pos, node.end));
31623162

31633163
const statement = createStatement(expression);
31643164
setSourceMapRange(statement, createRange(-1, node.end));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/exportDefaultClassInNamespace.ts(2,12): error TS1319: A default export can only be used in an ECMAScript-style module.
2+
tests/cases/compiler/exportDefaultClassInNamespace.ts(6,12): error TS1319: A default export can only be used in an ECMAScript-style module.
3+
4+
5+
==== tests/cases/compiler/exportDefaultClassInNamespace.ts (2 errors) ====
6+
namespace ns_class {
7+
export default class {}
8+
~~~~~~~
9+
!!! error TS1319: A default export can only be used in an ECMAScript-style module.
10+
}
11+
12+
namespace ns_abstract_class {
13+
export default abstract class {}
14+
~~~~~~~
15+
!!! error TS1319: A default export can only be used in an ECMAScript-style module.
16+
}
17+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//// [exportDefaultClassInNamespace.ts]
2+
namespace ns_class {
3+
export default class {}
4+
}
5+
6+
namespace ns_abstract_class {
7+
export default abstract class {}
8+
}
9+
10+
11+
//// [exportDefaultClassInNamespace.js]
12+
var ns_class;
13+
(function (ns_class) {
14+
var default_1 = (function () {
15+
function default_1() {
16+
}
17+
return default_1;
18+
}());
19+
ns_class.default_1 = default_1;
20+
})(ns_class || (ns_class = {}));
21+
var ns_abstract_class;
22+
(function (ns_abstract_class) {
23+
var default_2 = (function () {
24+
function default_2() {
25+
}
26+
return default_2;
27+
}());
28+
ns_abstract_class.default_2 = default_2;
29+
})(ns_abstract_class || (ns_abstract_class = {}));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/exportDefaultClassInNamespace.ts ===
2+
namespace ns_class {
3+
>ns_class : Symbol(ns_class, Decl(exportDefaultClassInNamespace.ts, 0, 0))
4+
5+
export default class {}
6+
}
7+
8+
namespace ns_abstract_class {
9+
>ns_abstract_class : Symbol(ns_abstract_class, Decl(exportDefaultClassInNamespace.ts, 2, 1))
10+
11+
export default abstract class {}
12+
}
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/exportDefaultClassInNamespace.ts ===
2+
namespace ns_class {
3+
>ns_class : typeof ns_class
4+
5+
export default class {}
6+
}
7+
8+
namespace ns_abstract_class {
9+
>ns_abstract_class : typeof ns_abstract_class
10+
11+
export default abstract class {}
12+
}
13+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/exportDefaultFunctionInNamespace.ts(2,12): error TS1319: A default export can only be used in an ECMAScript-style module.
2+
tests/cases/compiler/exportDefaultFunctionInNamespace.ts(6,12): error TS1319: A default export can only be used in an ECMAScript-style module.
3+
4+
5+
==== tests/cases/compiler/exportDefaultFunctionInNamespace.ts (2 errors) ====
6+
namespace ns_function {
7+
export default function () {}
8+
~~~~~~~
9+
!!! error TS1319: A default export can only be used in an ECMAScript-style module.
10+
}
11+
12+
namespace ns_async_function {
13+
export default async function () {}
14+
~~~~~~~
15+
!!! error TS1319: A default export can only be used in an ECMAScript-style module.
16+
}
17+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//// [exportDefaultFunctionInNamespace.ts]
2+
namespace ns_function {
3+
export default function () {}
4+
}
5+
6+
namespace ns_async_function {
7+
export default async function () {}
8+
}
9+
10+
11+
//// [exportDefaultFunctionInNamespace.js]
12+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13+
return new (P || (P = Promise))(function (resolve, reject) {
14+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
17+
step((generator = generator.apply(thisArg, _arguments || [])).next());
18+
});
19+
};
20+
var __generator = (this && this.__generator) || function (thisArg, body) {
21+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
22+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
23+
function verb(n) { return function (v) { return step([n, v]); }; }
24+
function step(op) {
25+
if (f) throw new TypeError("Generator is already executing.");
26+
while (_) try {
27+
if (f = 1, y && (t = y[op[0] & 2 ? "return" : op[0] ? "throw" : "next"]) && !(t = t.call(y, op[1])).done) return t;
28+
if (y = 0, t) op = [0, t.value];
29+
switch (op[0]) {
30+
case 0: case 1: t = op; break;
31+
case 4: _.label++; return { value: op[1], done: false };
32+
case 5: _.label++; y = op[1]; op = [0]; continue;
33+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
34+
default:
35+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
36+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
37+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
38+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
39+
if (t[2]) _.ops.pop();
40+
_.trys.pop(); continue;
41+
}
42+
op = body.call(thisArg, _);
43+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
44+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
45+
}
46+
};
47+
var ns_function;
48+
(function (ns_function) {
49+
default function () { }
50+
ns_function.default_1 = default_1;
51+
})(ns_function || (ns_function = {}));
52+
var ns_async_function;
53+
(function (ns_async_function) {
54+
default function () {
55+
return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
56+
return [2 /*return*/];
57+
}); });
58+
}
59+
ns_async_function.default_2 = default_2;
60+
})(ns_async_function || (ns_async_function = {}));
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/exportDefaultFunctionInNamespace.ts ===
2+
namespace ns_function {
3+
>ns_function : Symbol(ns_function, Decl(exportDefaultFunctionInNamespace.ts, 0, 0))
4+
5+
export default function () {}
6+
}
7+
8+
namespace ns_async_function {
9+
>ns_async_function : Symbol(ns_async_function, Decl(exportDefaultFunctionInNamespace.ts, 2, 1))
10+
11+
export default async function () {}
12+
}
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/exportDefaultFunctionInNamespace.ts ===
2+
namespace ns_function {
3+
>ns_function : typeof ns_function
4+
5+
export default function () {}
6+
}
7+
8+
namespace ns_async_function {
9+
>ns_async_function : typeof ns_async_function
10+
11+
export default async function () {}
12+
}
13+

0 commit comments

Comments
 (0)