Skip to content

Commit 534fc14

Browse files
authored
fix(45850): Preserve const enums should keep import refs for exported const enums exported via named exports (#45858)
1 parent b2a6b49 commit 534fc14

9 files changed

+157
-1
lines changed

src/compiler/checker.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24477,7 +24477,19 @@ namespace ts {
2447724477
}
2447824478

2447924479
function isExportOrExportExpression(location: Node) {
24480-
return !!findAncestor(location, e => e.parent && isExportAssignment(e.parent) && e.parent.expression === e && isEntityNameExpression(e));
24480+
return !!findAncestor(location, n => {
24481+
const parent = n.parent;
24482+
if (parent === undefined) {
24483+
return "quit";
24484+
}
24485+
if (isExportAssignment(parent)) {
24486+
return parent.expression === n && isEntityNameExpression(n);
24487+
}
24488+
if (isExportSpecifier(parent)) {
24489+
return parent.name === n || parent.propertyName === n;
24490+
}
24491+
return false;
24492+
});
2448124493
}
2448224494

2448324495
function markAliasReferenced(symbol: Symbol, location: Node) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/constEnumPreserveEmitNamedExport1.ts] ////
2+
3+
//// [a.ts]
4+
const enum A {
5+
Foo
6+
};
7+
export { A };
8+
9+
//// [b.ts]
10+
import { A } from './a';
11+
export { A };
12+
13+
14+
//// [a.js]
15+
var A;
16+
(function (A) {
17+
A[A["Foo"] = 0] = "Foo";
18+
})(A || (A = {}));
19+
;
20+
export { A };
21+
//// [b.js]
22+
import { A } from './a';
23+
export { A };
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/a.ts ===
2+
const enum A {
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
Foo
6+
>Foo : Symbol(A.Foo, Decl(a.ts, 0, 14))
7+
8+
};
9+
export { A };
10+
>A : Symbol(A, Decl(a.ts, 3, 8))
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import { A } from './a';
14+
>A : Symbol(A, Decl(b.ts, 0, 8))
15+
16+
export { A };
17+
>A : Symbol(A, Decl(b.ts, 1, 8))
18+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/a.ts ===
2+
const enum A {
3+
>A : A
4+
5+
Foo
6+
>Foo : A.Foo
7+
8+
};
9+
export { A };
10+
>A : typeof A
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import { A } from './a';
14+
>A : typeof A
15+
16+
export { A };
17+
>A : typeof A
18+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/constEnumPreserveEmitNamedExport2.ts] ////
2+
3+
//// [a.ts]
4+
const enum A {
5+
Foo
6+
};
7+
export { A };
8+
9+
//// [b.ts]
10+
import { A } from './a';
11+
export { A as B };
12+
13+
14+
//// [a.js]
15+
var A;
16+
(function (A) {
17+
A[A["Foo"] = 0] = "Foo";
18+
})(A || (A = {}));
19+
;
20+
export { A };
21+
//// [b.js]
22+
import { A } from './a';
23+
export { A as B };
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/a.ts ===
2+
const enum A {
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
Foo
6+
>Foo : Symbol(A.Foo, Decl(a.ts, 0, 14))
7+
8+
};
9+
export { A };
10+
>A : Symbol(A, Decl(a.ts, 3, 8))
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import { A } from './a';
14+
>A : Symbol(A, Decl(b.ts, 0, 8))
15+
16+
export { A as B };
17+
>A : Symbol(A, Decl(b.ts, 0, 8))
18+
>B : Symbol(B, Decl(b.ts, 1, 8))
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/a.ts ===
2+
const enum A {
3+
>A : A
4+
5+
Foo
6+
>Foo : A.Foo
7+
8+
};
9+
export { A };
10+
>A : typeof A
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import { A } from './a';
14+
>A : typeof A
15+
16+
export { A as B };
17+
>A : typeof A
18+
>B : typeof A
19+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @preserveConstEnums: true
2+
// @target: esnext
3+
4+
// @filename: a.ts
5+
const enum A {
6+
Foo
7+
};
8+
export { A };
9+
10+
// @filename: b.ts
11+
import { A } from './a';
12+
export { A };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @preserveConstEnums: true
2+
// @target: esnext
3+
4+
// @filename: a.ts
5+
const enum A {
6+
Foo
7+
};
8+
export { A };
9+
10+
// @filename: b.ts
11+
import { A } from './a';
12+
export { A as B };

0 commit comments

Comments
 (0)