Skip to content

Commit 76eab54

Browse files
authored
Add error for using generalized expressions with export assignments in ambient contexts (microsoft#18444)
1 parent 2077835 commit 76eab54

12 files changed

Lines changed: 130 additions & 7 deletions

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22345,6 +22345,10 @@ namespace ts {
2234522345

2234622346
checkExternalModuleExports(container);
2234722347

22348+
if (isInAmbientContext(node) && !isEntityNameExpression(node.expression)) {
22349+
grammarErrorOnNode(node.expression, Diagnostics.The_expression_of_an_export_assignment_must_be_an_identifier_or_qualified_name_in_an_ambient_context);
22350+
}
22351+
2234822352
if (node.isExportEquals && !isInAmbientContext(node)) {
2234922353
if (modulekind >= ModuleKind.ES2015) {
2235022354
// export assignment is not supported in es6 modules

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,10 @@
22122212
"category": "Error",
22132213
"code": 2713
22142214
},
2215+
"The expression of an export assignment must be an identifier or qualified name in an ambient context.": {
2216+
"category": "Error",
2217+
"code": 2714
2218+
},
22152219

22162220
"Import declaration '{0}' is using private name '{1}'.": {
22172221
"category": "Error",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
tests/cases/compiler/foo.d.ts(1,16): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
2+
tests/cases/compiler/foo2.d.ts(1,10): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
3+
tests/cases/compiler/indirection.d.ts(3,20): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
4+
tests/cases/compiler/indirection2.d.ts(3,14): error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
5+
6+
7+
==== tests/cases/compiler/consumer.ts (0 errors) ====
8+
/// <reference path="./indirection.d.ts" />
9+
/// <reference path="./indirection2.d.ts" />
10+
import "indirect";
11+
import "foo";
12+
import "indirect2";
13+
import "foo2";
14+
==== tests/cases/compiler/foo.d.ts (1 errors) ====
15+
export default 2 + 2;
16+
~~~~~
17+
!!! error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
18+
export as namespace Foo;
19+
20+
==== tests/cases/compiler/foo2.d.ts (1 errors) ====
21+
export = 2 + 2;
22+
~~~~~
23+
!!! error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
24+
export as namespace Foo2;
25+
26+
==== tests/cases/compiler/indirection.d.ts (1 errors) ====
27+
/// <reference path="./foo.d.ts" />
28+
declare module "indirect" {
29+
export default typeof Foo.default;
30+
~~~~~~~~~~~~~~~~~~
31+
!!! error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
32+
}
33+
34+
==== tests/cases/compiler/indirection2.d.ts (1 errors) ====
35+
/// <reference path="./foo2.d.ts" />
36+
declare module "indirect2" {
37+
export = typeof Foo2;
38+
~~~~~~~~~~~
39+
!!! error TS2714: The expression of an export assignment must be an identifier or qualified name in an ambient context.
40+
}
41+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//// [tests/cases/compiler/ambientExportDefaultErrors.ts] ////
2+
3+
//// [foo.d.ts]
4+
export default 2 + 2;
5+
export as namespace Foo;
6+
7+
//// [foo2.d.ts]
8+
export = 2 + 2;
9+
export as namespace Foo2;
10+
11+
//// [indirection.d.ts]
12+
/// <reference path="./foo.d.ts" />
13+
declare module "indirect" {
14+
export default typeof Foo.default;
15+
}
16+
17+
//// [indirection2.d.ts]
18+
/// <reference path="./foo2.d.ts" />
19+
declare module "indirect2" {
20+
export = typeof Foo2;
21+
}
22+
23+
//// [consumer.ts]
24+
/// <reference path="./indirection.d.ts" />
25+
/// <reference path="./indirection2.d.ts" />
26+
import "indirect";
27+
import "foo";
28+
import "indirect2";
29+
import "foo2";
30+
31+
//// [consumer.js]
32+
"use strict";
33+
exports.__esModule = true;
34+
/// <reference path="./indirection.d.ts" />
35+
/// <reference path="./indirection2.d.ts" />
36+
require("indirect");
37+
require("foo");
38+
require("indirect2");
39+
require("foo2");
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== tests/cases/compiler/test.d.ts ===
2-
export default "test";
2+
export default undefined;
3+
>undefined : Symbol(default)
4+
35
export var __esModule;
46
>__esModule : Symbol(__esModule, Decl(test.d.ts, 1, 10))
57

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
=== tests/cases/compiler/test.d.ts ===
2-
export default "test";
2+
export default undefined;
3+
>undefined : undefined
4+
35
export var __esModule;
46
>__esModule : any
57

tests/baselines/reference/typeAliasExport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [typeAliasExport.ts]
22
declare module "a" {
3-
export default 0
3+
export default undefined
44
export var a;
55
export type a = typeof a;
66
}

tests/baselines/reference/typeAliasExport.symbols

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
=== tests/cases/compiler/typeAliasExport.ts ===
22
declare module "a" {
3-
export default 0
3+
export default undefined
4+
>undefined : Symbol(default)
5+
46
export var a;
57
>a : Symbol(a, Decl(typeAliasExport.ts, 2, 12), Decl(typeAliasExport.ts, 2, 15))
68

tests/baselines/reference/typeAliasExport.types

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
=== tests/cases/compiler/typeAliasExport.ts ===
22
declare module "a" {
3-
export default 0
3+
export default undefined
4+
>undefined : undefined
5+
46
export var a;
57
>a : any
68

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// @filename: foo.d.ts
2+
export default 2 + 2;
3+
export as namespace Foo;
4+
5+
// @filename: foo2.d.ts
6+
export = 2 + 2;
7+
export as namespace Foo2;
8+
9+
// @filename: indirection.d.ts
10+
/// <reference path="./foo.d.ts" />
11+
declare module "indirect" {
12+
export default typeof Foo.default;
13+
}
14+
15+
// @filename: indirection2.d.ts
16+
/// <reference path="./foo2.d.ts" />
17+
declare module "indirect2" {
18+
export = typeof Foo2;
19+
}
20+
21+
// @filename: consumer.ts
22+
/// <reference path="./indirection.d.ts" />
23+
/// <reference path="./indirection2.d.ts" />
24+
import "indirect";
25+
import "foo";
26+
import "indirect2";
27+
import "foo2";

0 commit comments

Comments
 (0)