Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure enum aliases referenced in other enum members do not get marked as referenced #48770

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28942,10 +28942,17 @@ namespace ts {
prop = getPropertyOfType(apparentType, right.escapedText);
}
// In `Foo.Bar.Baz`, 'Foo' is not referenced if 'Bar' is a const enum or a module containing only const enums.
// `Foo` is also not referenced in `enum FooCopy { Bar = Foo.Bar }`, because the enum member value gets inlined
// here even if `Foo` is not a const enum.
//
// The exceptions are:
// 1. if 'isolatedModules' is enabled, because the const enum value will not be inlined, and
// 2. if 'preserveConstEnums' is enabled and the expression is itself an export, e.g. `export = Foo.Bar.Baz`.
if (isIdentifier(left) && parentSymbol && (compilerOptions.isolatedModules || !(prop && isConstEnumOrConstEnumOnlyModule(prop)) || shouldPreserveConstEnums(compilerOptions) && isExportOrExportExpression(node))) {
if (isIdentifier(left) && parentSymbol && (
compilerOptions.isolatedModules ||
!(prop && (isConstEnumOrConstEnumOnlyModule(prop) || prop.flags & SymbolFlags.EnumMember && node.parent.kind === SyntaxKind.EnumMember)) ||
shouldPreserveConstEnums(compilerOptions) && isExportOrExportExpression(node)
)) {
markAliasReferenced(parentSymbol, node);
}

Expand Down Expand Up @@ -40215,7 +40222,7 @@ namespace ts {

function isConstantMemberAccess(node: Expression): node is AccessExpression {
const type = getTypeOfExpression(node);
if(type === errorType) {
if (type === errorType) {
return false;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/baselines/reference/importElisionEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//// [tests/cases/compiler/importElisionEnum.ts] ////

//// [enum.ts]
export enum MyEnum {
a = 0,
b,
c,
d
}

//// [index.ts]
import { MyEnum as MyEnumFromModule } from "./enum";

enum MyEnum {
a = MyEnumFromModule.a
}

//// [enum.js]
"use strict";
exports.__esModule = true;
exports.MyEnum = void 0;
var MyEnum;
(function (MyEnum) {
MyEnum[MyEnum["a"] = 0] = "a";
MyEnum[MyEnum["b"] = 1] = "b";
MyEnum[MyEnum["c"] = 2] = "c";
MyEnum[MyEnum["d"] = 3] = "d";
})(MyEnum = exports.MyEnum || (exports.MyEnum = {}));
//// [index.js]
"use strict";
exports.__esModule = true;
var MyEnum;
(function (MyEnum) {
MyEnum[MyEnum["a"] = 0] = "a";
})(MyEnum || (MyEnum = {}));
31 changes: 31 additions & 0 deletions tests/baselines/reference/importElisionEnum.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
=== tests/cases/compiler/enum.ts ===
export enum MyEnum {
>MyEnum : Symbol(MyEnum, Decl(enum.ts, 0, 0))

a = 0,
>a : Symbol(MyEnum.a, Decl(enum.ts, 0, 20))

b,
>b : Symbol(MyEnum.b, Decl(enum.ts, 1, 8))

c,
>c : Symbol(MyEnum.c, Decl(enum.ts, 2, 4))

d
>d : Symbol(MyEnum.d, Decl(enum.ts, 3, 4))
}

=== tests/cases/compiler/index.ts ===
import { MyEnum as MyEnumFromModule } from "./enum";
>MyEnum : Symbol(MyEnumFromModule, Decl(enum.ts, 0, 0))
>MyEnumFromModule : Symbol(MyEnumFromModule, Decl(index.ts, 0, 8))

enum MyEnum {
>MyEnum : Symbol(MyEnum, Decl(index.ts, 0, 52))

a = MyEnumFromModule.a
>a : Symbol(MyEnum.a, Decl(index.ts, 2, 13))
>MyEnumFromModule.a : Symbol(MyEnumFromModule.a, Decl(enum.ts, 0, 20))
>MyEnumFromModule : Symbol(MyEnumFromModule, Decl(index.ts, 0, 8))
>a : Symbol(MyEnumFromModule.a, Decl(enum.ts, 0, 20))
}
32 changes: 32 additions & 0 deletions tests/baselines/reference/importElisionEnum.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/enum.ts ===
export enum MyEnum {
>MyEnum : MyEnum

a = 0,
>a : MyEnum.a
>0 : 0

b,
>b : MyEnum.b

c,
>c : MyEnum.c

d
>d : MyEnum.d
}

=== tests/cases/compiler/index.ts ===
import { MyEnum as MyEnumFromModule } from "./enum";
>MyEnum : typeof MyEnumFromModule
>MyEnumFromModule : typeof MyEnumFromModule

enum MyEnum {
>MyEnum : MyEnum

a = MyEnumFromModule.a
>a : MyEnum
>MyEnumFromModule.a : MyEnumFromModule.a
>MyEnumFromModule : typeof MyEnumFromModule
>a : MyEnumFromModule.a
}
14 changes: 14 additions & 0 deletions tests/cases/compiler/importElisionEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @Filename: enum.ts
export enum MyEnum {
a = 0,
b,
c,
d
}

// @Filename: index.ts
import { MyEnum as MyEnumFromModule } from "./enum";

enum MyEnum {
a = MyEnumFromModule.a
}