Skip to content

Commit c663645

Browse files
authored
Inform getDeclarationSpaces about how an imported exportAssignment may merge (microsoft#23816)
1 parent ffc931c commit c663645

6 files changed

+100
-1
lines changed

src/compiler/checker.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -21611,7 +21611,8 @@ namespace ts {
2161121611
ExportType = 1 << 1,
2161221612
ExportNamespace = 1 << 2,
2161321613
}
21614-
function getDeclarationSpaces(d: Declaration): DeclarationSpaces {
21614+
function getDeclarationSpaces(decl: Declaration): DeclarationSpaces {
21615+
let d = decl as Node;
2161521616
switch (d.kind) {
2161621617
case SyntaxKind.InterfaceDeclaration:
2161721618
case SyntaxKind.TypeAliasDeclaration:
@@ -21627,6 +21628,13 @@ namespace ts {
2162721628
return DeclarationSpaces.ExportType | DeclarationSpaces.ExportValue;
2162821629
case SyntaxKind.SourceFile:
2162921630
return DeclarationSpaces.ExportType | DeclarationSpaces.ExportValue | DeclarationSpaces.ExportNamespace;
21631+
case SyntaxKind.ExportAssignment:
21632+
// Export assigned entity name expressions act as aliases and should fall through, otherwise they export values
21633+
if (!isEntityNameExpression((d as ExportAssignment).expression)) {
21634+
return DeclarationSpaces.ExportValue;
21635+
}
21636+
d = (d as ExportAssignment).expression;
21637+
/* falls through */
2163021638
// The below options all declare an Alias, which is allowed to merge with other values within the importing module
2163121639
case SyntaxKind.ImportEqualsDeclaration:
2163221640
case SyntaxKind.NamespaceImport:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
tests/cases/compiler/user.ts(1,8): error TS2395: Individual declarations in merged declaration 'Obj' must be all exported or all local.
2+
tests/cases/compiler/user.ts(1,8): error TS2440: Import declaration conflicts with local declaration of 'Obj'.
3+
tests/cases/compiler/user.ts(3,14): error TS2395: Individual declarations in merged declaration 'Obj' must be all exported or all local.
4+
tests/cases/compiler/user.ts(3,25): error TS2448: Block-scoped variable 'Obj' used before its declaration.
5+
6+
7+
==== tests/cases/compiler/assignment.ts (0 errors) ====
8+
export default {
9+
foo: 12
10+
};
11+
12+
==== tests/cases/compiler/user.ts (4 errors) ====
13+
import Obj from "./assignment";
14+
~~~
15+
!!! error TS2395: Individual declarations in merged declaration 'Obj' must be all exported or all local.
16+
~~~
17+
!!! error TS2440: Import declaration conflicts with local declaration of 'Obj'.
18+
19+
export const Obj = void Obj;
20+
~~~
21+
!!! error TS2395: Individual declarations in merged declaration 'Obj' must be all exported or all local.
22+
~~~
23+
!!! error TS2448: Block-scoped variable 'Obj' used before its declaration.
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/exportAssignmentImportMergeNoCrash.ts] ////
2+
3+
//// [assignment.ts]
4+
export default {
5+
foo: 12
6+
};
7+
8+
//// [user.ts]
9+
import Obj from "./assignment";
10+
11+
export const Obj = void Obj;
12+
13+
14+
//// [assignment.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
exports["default"] = {
18+
foo: 12
19+
};
20+
//// [user.js]
21+
"use strict";
22+
exports.__esModule = true;
23+
var assignment_1 = require("./assignment");
24+
exports.Obj = void exports.Obj;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/assignment.ts ===
2+
export default {
3+
foo: 12
4+
>foo : Symbol(foo, Decl(assignment.ts, 0, 16))
5+
6+
};
7+
8+
=== tests/cases/compiler/user.ts ===
9+
import Obj from "./assignment";
10+
>Obj : Symbol(Obj, Decl(user.ts, 0, 6), Decl(user.ts, 2, 12))
11+
12+
export const Obj = void Obj;
13+
>Obj : Symbol(Obj, Decl(user.ts, 2, 12))
14+
>Obj : Symbol(Obj, Decl(user.ts, 0, 6), Decl(user.ts, 2, 12))
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/assignment.ts ===
2+
export default {
3+
>{ foo: 12} : { foo: number; }
4+
5+
foo: 12
6+
>foo : number
7+
>12 : 12
8+
9+
};
10+
11+
=== tests/cases/compiler/user.ts ===
12+
import Obj from "./assignment";
13+
>Obj : { foo: number; }
14+
15+
export const Obj = void Obj;
16+
>Obj : any
17+
>void Obj : undefined
18+
>Obj : any
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @filename: assignment.ts
2+
export default {
3+
foo: 12
4+
};
5+
6+
// @filename: user.ts
7+
import Obj from "./assignment";
8+
9+
export const Obj = void Obj;

0 commit comments

Comments
 (0)