diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c5701087ebfd4..d119bd9e24563 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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; } diff --git a/tests/baselines/reference/invalidUnqualifiedEnum.js b/tests/baselines/reference/invalidUnqualifiedEnum.js new file mode 100644 index 0000000000000..14a794817a2c8 --- /dev/null +++ b/tests/baselines/reference/invalidUnqualifiedEnum.js @@ -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; +} diff --git a/tests/baselines/reference/invalidUnqualifiedEnum.symbols b/tests/baselines/reference/invalidUnqualifiedEnum.symbols new file mode 100644 index 0000000000000..de183d4f2951a --- /dev/null +++ b/tests/baselines/reference/invalidUnqualifiedEnum.symbols @@ -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)) +} + diff --git a/tests/baselines/reference/invalidUnqualifiedEnum.types b/tests/baselines/reference/invalidUnqualifiedEnum.types new file mode 100644 index 0000000000000..2689661a3dbb4 --- /dev/null +++ b/tests/baselines/reference/invalidUnqualifiedEnum.types @@ -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 +> : ^^^ +} + diff --git a/tests/cases/compiler/invalidUnqualifiedEnum.ts b/tests/cases/compiler/invalidUnqualifiedEnum.ts new file mode 100644 index 0000000000000..94686059f0b03 --- /dev/null +++ b/tests/cases/compiler/invalidUnqualifiedEnum.ts @@ -0,0 +1,8 @@ +// @declaration: true + +export enum Foo { + bar +} +export namespace Foo { + export const q = Foo.bar; +}