Skip to content

Qualify enum member names when referencing in value position #62034

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5759,7 +5759,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
// If the symbol with this name is present it should refer to the symbol
if (symbolFromSymbolTable === symbol) {
// No need to qualify
// No need to qualify, unless this was from an enum and we're using it in a value context, e.g.
// enum Foo { bar }
// namespace Foo { export const q = Foo.bar }
// where we can't legally decl emit 'const q = bar'
if (symbol.flags & SymbolFlags.EnumMember) {
qualify = true;
}
return true;
}

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

//// [invalidUnqualifiedEnum.ts]
export enum Foo {
bar
}
export namespace Foo {
export const q = Foo.bar;
}


//// [invalidUnqualifiedEnum.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Foo = void 0;
var Foo;
(function (Foo) {
Foo[Foo["bar"] = 0] = "bar";
})(Foo || (exports.Foo = Foo = {}));
(function (Foo) {
Foo.q = Foo.bar;
})(Foo || (exports.Foo = Foo = {}));


//// [invalidUnqualifiedEnum.d.ts]
export declare enum Foo {
bar = 0
}
export declare namespace Foo {
const q = Foo.bar;
}
19 changes: 19 additions & 0 deletions tests/baselines/reference/invalidUnqualifiedEnum.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//// [tests/cases/compiler/invalidUnqualifiedEnum.ts] ////

=== invalidUnqualifiedEnum.ts ===
export enum Foo {
>Foo : Symbol(Foo, Decl(invalidUnqualifiedEnum.ts, 0, 0), Decl(invalidUnqualifiedEnum.ts, 2, 1))

bar
>bar : Symbol(Foo.bar, Decl(invalidUnqualifiedEnum.ts, 0, 17))
}
export namespace Foo {
>Foo : Symbol(Foo, Decl(invalidUnqualifiedEnum.ts, 0, 0), Decl(invalidUnqualifiedEnum.ts, 2, 1))

export const q = Foo.bar;
>q : Symbol(q, Decl(invalidUnqualifiedEnum.ts, 4, 16))
>Foo.bar : Symbol(Foo.bar, Decl(invalidUnqualifiedEnum.ts, 0, 17))
>Foo : Symbol(Foo, Decl(invalidUnqualifiedEnum.ts, 0, 0), Decl(invalidUnqualifiedEnum.ts, 2, 1))
>bar : Symbol(Foo.bar, Decl(invalidUnqualifiedEnum.ts, 0, 17))
}

26 changes: 26 additions & 0 deletions tests/baselines/reference/invalidUnqualifiedEnum.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//// [tests/cases/compiler/invalidUnqualifiedEnum.ts] ////

=== invalidUnqualifiedEnum.ts ===
export enum Foo {
>Foo : Foo
> : ^^^

bar
>bar : Foo.bar
> : ^^^^^^^
}
export namespace Foo {
>Foo : typeof Foo
> : ^^^^^^^^^^

export const q = Foo.bar;
>q : Foo.bar
> : ^^^^^^^
>Foo.bar : Foo
> : ^^^
>Foo : typeof Foo
> : ^^^^^^^^^^
>bar : Foo
> : ^^^
}

8 changes: 8 additions & 0 deletions tests/cases/compiler/invalidUnqualifiedEnum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @declaration: true

export enum Foo {
bar
}
export namespace Foo {
export const q = Foo.bar;
}