fix(45850): Preserve const enums should keep import refs for exported const enums exported via named exports (#45858)

This commit is contained in:
Oleksandr T
2021-09-16 20:15:41 +03:00
committed by GitHub
parent b2a6b49201
commit 534fc14c66
9 changed files with 157 additions and 1 deletions

View File

@@ -24477,7 +24477,19 @@ namespace ts {
}
function isExportOrExportExpression(location: Node) {
return !!findAncestor(location, e => e.parent && isExportAssignment(e.parent) && e.parent.expression === e && isEntityNameExpression(e));
return !!findAncestor(location, n => {
const parent = n.parent;
if (parent === undefined) {
return "quit";
}
if (isExportAssignment(parent)) {
return parent.expression === n && isEntityNameExpression(n);
}
if (isExportSpecifier(parent)) {
return parent.name === n || parent.propertyName === n;
}
return false;
});
}
function markAliasReferenced(symbol: Symbol, location: Node) {

View File

@@ -0,0 +1,23 @@
//// [tests/cases/compiler/constEnumPreserveEmitNamedExport1.ts] ////
//// [a.ts]
const enum A {
Foo
};
export { A };
//// [b.ts]
import { A } from './a';
export { A };
//// [a.js]
var A;
(function (A) {
A[A["Foo"] = 0] = "Foo";
})(A || (A = {}));
;
export { A };
//// [b.js]
import { A } from './a';
export { A };

View File

@@ -0,0 +1,18 @@
=== tests/cases/compiler/a.ts ===
const enum A {
>A : Symbol(A, Decl(a.ts, 0, 0))
Foo
>Foo : Symbol(A.Foo, Decl(a.ts, 0, 14))
};
export { A };
>A : Symbol(A, Decl(a.ts, 3, 8))
=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : Symbol(A, Decl(b.ts, 0, 8))
export { A };
>A : Symbol(A, Decl(b.ts, 1, 8))

View File

@@ -0,0 +1,18 @@
=== tests/cases/compiler/a.ts ===
const enum A {
>A : A
Foo
>Foo : A.Foo
};
export { A };
>A : typeof A
=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : typeof A
export { A };
>A : typeof A

View File

@@ -0,0 +1,23 @@
//// [tests/cases/compiler/constEnumPreserveEmitNamedExport2.ts] ////
//// [a.ts]
const enum A {
Foo
};
export { A };
//// [b.ts]
import { A } from './a';
export { A as B };
//// [a.js]
var A;
(function (A) {
A[A["Foo"] = 0] = "Foo";
})(A || (A = {}));
;
export { A };
//// [b.js]
import { A } from './a';
export { A as B };

View File

@@ -0,0 +1,19 @@
=== tests/cases/compiler/a.ts ===
const enum A {
>A : Symbol(A, Decl(a.ts, 0, 0))
Foo
>Foo : Symbol(A.Foo, Decl(a.ts, 0, 14))
};
export { A };
>A : Symbol(A, Decl(a.ts, 3, 8))
=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : Symbol(A, Decl(b.ts, 0, 8))
export { A as B };
>A : Symbol(A, Decl(b.ts, 0, 8))
>B : Symbol(B, Decl(b.ts, 1, 8))

View File

@@ -0,0 +1,19 @@
=== tests/cases/compiler/a.ts ===
const enum A {
>A : A
Foo
>Foo : A.Foo
};
export { A };
>A : typeof A
=== tests/cases/compiler/b.ts ===
import { A } from './a';
>A : typeof A
export { A as B };
>A : typeof A
>B : typeof A

View File

@@ -0,0 +1,12 @@
// @preserveConstEnums: true
// @target: esnext
// @filename: a.ts
const enum A {
Foo
};
export { A };
// @filename: b.ts
import { A } from './a';
export { A };

View File

@@ -0,0 +1,12 @@
// @preserveConstEnums: true
// @target: esnext
// @filename: a.ts
const enum A {
Foo
};
export { A };
// @filename: b.ts
import { A } from './a';
export { A as B };