From 3403de2b852872db954a18f6a4606bf0743d802f Mon Sep 17 00:00:00 2001 From: Titian Cernicova-Dragomir Date: Sat, 25 Nov 2023 00:35:55 +0000 Subject: [PATCH] Fixed errors in DTE when enums are accessed through a dotted identifier. Signed-off-by: Titian Cernicova-Dragomir --- src/compiler/checker.ts | 17 +- .../transformers/declarations/emitResolver.ts | 63 +-- src/compiler/utilities.ts | 17 + .../original/diff/constEnums.d.ts.diff | 49 -- .../original/dte/constEnums.d.ts | 504 ------------------ .../original/tsc/constEnums.d.ts | 501 ----------------- 6 files changed, 51 insertions(+), 1100 deletions(-) delete mode 100644 tests/baselines/reference/isolated-declarations/original/diff/constEnums.d.ts.diff delete mode 100644 tests/baselines/reference/isolated-declarations/original/dte/constEnums.d.ts delete mode 100644 tests/baselines/reference/isolated-declarations/original/tsc/constEnums.d.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c98f96209c15b..c374792c75072 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12,7 +12,6 @@ import { and, AnonymousType, AnyImportOrReExport, - AnyImportSyntax, append, appendIfUnique, ArrayBindingPattern, @@ -241,6 +240,7 @@ import { getAllJSDocTags, getAllowSyntheticDefaultImports, getAncestor, + getAnyImportSyntax, getAssignedExpandoInitializer, getAssignmentDeclarationKind, getAssignmentDeclarationPropertyAccessKind, @@ -3927,21 +3927,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { || (n === stopAt || isFunctionLike(n) && (!getImmediatelyInvokedFunctionExpression(n) || (getFunctionFlags(n) & FunctionFlags.AsyncGenerator)) ? "quit" : false)); } - function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined { - switch (node.kind) { - case SyntaxKind.ImportEqualsDeclaration: - return node as ImportEqualsDeclaration; - case SyntaxKind.ImportClause: - return (node as ImportClause).parent; - case SyntaxKind.NamespaceImport: - return (node as NamespaceImport).parent.parent; - case SyntaxKind.ImportSpecifier: - return (node as ImportSpecifier).parent.parent.parent; - default: - return undefined; - } - } - function getDeclarationOfAliasSymbol(symbol: Symbol): Declaration | undefined { return symbol.declarations && findLast(symbol.declarations, isAliasSymbolDeclaration); } diff --git a/src/compiler/transformers/declarations/emitResolver.ts b/src/compiler/transformers/declarations/emitResolver.ts index 015b391dfc0c0..bfe02c153c24d 100644 --- a/src/compiler/transformers/declarations/emitResolver.ts +++ b/src/compiler/transformers/declarations/emitResolver.ts @@ -1,5 +1,4 @@ import { - AnyImportSyntax, appendIfUnique, ComputedPropertyName, createEvaluator, @@ -19,6 +18,7 @@ import { findAncestor, FunctionDeclaration, FunctionLikeDeclaration, + getAnyImportSyntax, getFirstIdentifier, getNameOfDeclaration, getParseTreeNode, @@ -27,11 +27,7 @@ import { hasProperty, hasSyntacticModifier, Identifier, - ImportClause, - ImportEqualsDeclaration, - ImportSpecifier, isAccessor, - isArrowFunction, isBigIntLiteral, isBinaryExpression, isBindingElement, @@ -42,7 +38,7 @@ import { isEnumMember, isExpressionStatement, isFunctionDeclaration, - isFunctionExpression, + isFunctionExpressionOrArrowFunction, isFunctionLike, isGetAccessor, isGetAccessorDeclaration, @@ -54,6 +50,7 @@ import { isPartOfTypeNode, isPrefixUnaryExpression, isPropertyAccessExpression, + isPropertyName, isSetAccessor, isSetAccessorDeclaration, isStringLiteralLike, @@ -65,7 +62,6 @@ import { LateBoundDeclaration, LateVisibilityPaintedStatement, ModifierFlags, - NamespaceImport, Node, NodeArray, NodeFlags, @@ -122,13 +118,39 @@ export function createEmitDeclarationResolver(file: SourceFile): IsolatedEmitRes } return undefined; } + + function resolveEntityName(location: Node, node: Expression, meaning: SymbolFlags): EmitDeclarationSymbol | undefined { + if (isIdentifier(node)) { + return resolveName(location, node.escapedText, meaning); + } + else if (isPropertyAccessExpression(node) || isElementAccessExpression(node)) { + const symbol = resolveEntityName(location, node.expression, meaning); + if (symbol === undefined) return undefined; + + const name = isElementAccessExpression(node) ? node.argumentExpression : node.name; + if (!isPropertyName(name)) return; + + const memberSymbol = symbol.exports?.get(getMemberKey(name)); + if (!memberSymbol || !(memberSymbol.flags & meaning)) { + return undefined; + } + return memberSymbol; + } + else { + return undefined; + } + } + function isTargetEnumDeclaration(target: Expression, location: EnumDeclaration) { + const symbol = resolveEntityName(location, target, SymbolFlags.Namespace); + + return !!symbol?.declarations.some(d => d === location); + } const evaluate = createEvaluator({ evaluateElementAccessExpression(expr, location) { // We only resolve names in the current enum declaration if (!location || !isEnumDeclaration(location)) return undefined; if ( - isIdentifier(expr.expression) - && expr.expression.escapedText === location.name.escapedText + isTargetEnumDeclaration(expr.expression, location) && isStringLiteralLike(expr.argumentExpression) ) { return getEnumValueFromName(expr.argumentExpression, location); @@ -148,9 +170,8 @@ export function createEmitDeclarationResolver(file: SourceFile): IsolatedEmitRes return getEnumValueFromName(expr, location); } if ( - isPropertyAccessExpression(expr) - && isIdentifier(expr.expression) - && expr.expression.escapedText === location.name.escapedText + isEntityNameExpression(expr.expression) + && isTargetEnumDeclaration(expr.expression, location) ) { return getEnumValueFromName(expr.name, location); } @@ -640,24 +661,6 @@ export function createEmitDeclarationResolver(file: SourceFile): IsolatedEmitRes } } -function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined { - switch (node.kind) { - case SyntaxKind.ImportEqualsDeclaration: - return node as ImportEqualsDeclaration; - case SyntaxKind.ImportClause: - return (node as ImportClause).parent; - case SyntaxKind.NamespaceImport: - return (node as NamespaceImport).parent.parent; - case SyntaxKind.ImportSpecifier: - return (node as ImportSpecifier).parent.parent.parent; - default: - return undefined; - } -} -function isFunctionExpressionOrArrowFunction(node: Expression) { - return isFunctionExpression(node) || isArrowFunction(node); -} - function getParentScope(declaration: VariableDeclaration | FunctionDeclaration): | undefined | Node & { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 7f87221303f94..4e4664fa3b5c3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -10519,6 +10519,23 @@ export function getDeclarationContainer(node: Node): Node { })!.parent; } +/** @internal */ +export function getAnyImportSyntax(node: Node): AnyImportSyntax | undefined { + switch (node.kind) { + case SyntaxKind.ImportEqualsDeclaration: + return node as ImportEqualsDeclaration; + case SyntaxKind.ImportClause: + return (node as ImportClause).parent; + case SyntaxKind.NamespaceImport: + return (node as NamespaceImport).parent.parent; + case SyntaxKind.ImportSpecifier: + return (node as ImportSpecifier).parent.parent.parent; + default: + return undefined; + } +} + + /** @internal */ export function isGlobalSourceFile(node: Node) { return node.kind === SyntaxKind.SourceFile && !isExternalOrCommonJsModule(node as SourceFile); diff --git a/tests/baselines/reference/isolated-declarations/original/diff/constEnums.d.ts.diff b/tests/baselines/reference/isolated-declarations/original/diff/constEnums.d.ts.diff deleted file mode 100644 index 04f3e5e530902..0000000000000 --- a/tests/baselines/reference/isolated-declarations/original/diff/constEnums.d.ts.diff +++ /dev/null @@ -1,49 +0,0 @@ -// [[Reason: TODO Add support for nested namespace access for enums]] //// - -//// [tests/cases/compiler/constEnums.ts] //// - -=================================================================== ---- TSC declarations -+++ DTE declarations -@@ -48,9 +48,9 @@ - namespace B { - namespace C { - const enum E { - V1 = 1, -- V2 = 101 -+ V2 - } - } - } - } -@@ -103,17 +103,18 @@ - constEnums.ts(34,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(34,10): error TS2474: const enum member initializers must be constant expressions. - constEnums.ts(35,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(35,10): error TS2474: const enum member initializers must be constant expressions. -+constEnums.ts(55,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(65,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(65,22): error TS2474: const enum member initializers must be constant expressions. - constEnums.ts(66,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(66,22): error TS2474: const enum member initializers must be constant expressions. - constEnums.ts(124,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - constEnums.ts(166,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - - --==== constEnums.ts (12 errors) ==== -+==== constEnums.ts (13 errors) ==== - const enum Enum1 { - A0 = 100, - } - -@@ -179,8 +180,10 @@ - export module C { - export const enum E { - V1 = 1, - V2 = A.B.C.E.V1 | 100 -+ ~~ -+!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - } - } - } - } diff --git a/tests/baselines/reference/isolated-declarations/original/dte/constEnums.d.ts b/tests/baselines/reference/isolated-declarations/original/dte/constEnums.d.ts deleted file mode 100644 index a6807ef0c080d..0000000000000 --- a/tests/baselines/reference/isolated-declarations/original/dte/constEnums.d.ts +++ /dev/null @@ -1,504 +0,0 @@ -//// [tests/cases/compiler/constEnums.ts] //// - -//// [constEnums.ts] -const enum Enum1 { - A0 = 100, -} - -const enum Enum1 { - // correct cases - A, - B, - C = 10, - D = A | B, - E = A | 1, - F = 1 | A, - G = (1 & 1), - H = ~(A | B), - I = A >>> 1, - J = 1 & A, - K = ~(1 | 5), - L = ~D, - M = E << B, - N = E << 1, - O = E >> B, - P = E >> 1, - PQ = E ** 2, - Q = -D, - R = C & 5, - S = 5 & C, - T = C | D, - U = C | 1, - V = 10 | D, - W = Enum1.V, - - // correct cases: reference to the enum member from different enum declaration - W1 = A0, - W2 = Enum1.A0, - W3 = Enum1["A0"], - W4 = Enum1["W"], - W5 = Enum1[`V`], -} - -const enum Comments { - "//", - "/*", - "*/", - "///", - "#", - "", -} - -module A { - export module B { - export module C { - export const enum E { - V1 = 1, - V2 = A.B.C.E.V1 | 100 - } - } - } -} - -module A { - export module B { - export module C { - export const enum E { - V3 = A.B.C.E["V2"] & 200, - V4 = A.B.C.E[`V1`] << 1, - } - } - } -} - -module A1 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - } -} - -module A2 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - // module C will be classified as value - export module C { - var x = 1 - } - } -} - -import I = A.B.C.E; -import I1 = A1.B; -import I2 = A2.B; - -function foo0(e: I): void { - if (e === I.V1) { - } - else if (e === I.V2) { - } -} - -function foo1(e: I1.C.E): void { - if (e === I1.C.E.V1) { - } - else if (e === I1.C.E.V2) { - } -} - -function foo2(e: I2.C.E): void { - if (e === I2.C.E.V1) { - } - else if (e === I2.C.E.V2) { - } -} - - -function foo(x: Enum1) { - switch (x) { - case Enum1.A: - case Enum1.B: - case Enum1.C: - case Enum1.D: - case Enum1.E: - case Enum1.F: - case Enum1.G: - case Enum1.H: - case Enum1.I: - case Enum1.J: - case Enum1.K: - case Enum1.L: - case Enum1.M: - case Enum1.N: - case Enum1.O: - case Enum1.P: - case Enum1.PQ: - case Enum1.Q: - case Enum1.R: - case Enum1.S: - case Enum1["T"]: - case Enum1[`U`]: - case Enum1.V: - case Enum1.W: - case Enum1.W1: - case Enum1.W2: - case Enum1.W3: - case Enum1.W4: - break; - } -} - -function bar(e: A.B.C.E): number { - switch (e) { - case A.B.C.E.V1: return 1; - case A.B.C.E.V2: return 1; - case A.B.C.E.V3: return 1; - } -} - -function baz(c: Comments) { - switch (c) { - case Comments["//"]: - case Comments["/*"]: - case Comments["*/"]: - case Comments["///"]: - case Comments["#"]: - case Comments[""]: - break; - } -} - - -/// [Declarations] //// - - - -//// [constEnums.d.ts] -declare const enum Enum1 { - A0 = 100 -} -declare const enum Enum1 { - A = 0, - B = 1, - C = 10, - D = 1, - E = 1, - F = 1, - G = 1, - H = -2, - I = 0, - J = 0, - K = -6, - L = -2, - M = 2, - N = 2, - O = 0, - P = 0, - PQ = 1, - Q = -1, - R = 0, - S = 0, - T = 11, - U = 11, - V = 11, - W = 11, - W1, - W2, - W3, - W4 = 11, - W5 = 11 -} -declare const enum Comments { - "//" = 0, - "/*" = 1, - "*/" = 2, - "///" = 3, - "#" = 4, - "" = 6 -} -declare namespace A { - namespace B { - namespace C { - const enum E { - V1 = 1, - V2 - } - } - } -} -declare namespace A { - namespace B { - namespace C { - const enum E { - V3, - V4 - } - } - } -} -declare namespace A1 { - namespace B { - namespace C { - const enum E { - V1 = 10, - V2 = 110 - } - } - } -} -declare namespace A2 { - namespace B { - namespace C { - const enum E { - V1 = 10, - V2 = 110 - } - } - namespace C { - } - } -} -import I = A.B.C.E; -import I1 = A1.B; -import I2 = A2.B; -declare function foo0(e: I): void; -declare function foo1(e: I1.C.E): void; -declare function foo2(e: I2.C.E): void; -declare function foo(x: Enum1): invalid; -declare function bar(e: A.B.C.E): number; -declare function baz(c: Comments): invalid; - -/// [Errors] //// - -constEnums.ts(33,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(33,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(34,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(34,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(35,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(35,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(55,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(65,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(65,22): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(66,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(66,22): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(124,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(166,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - - -==== constEnums.ts (13 errors) ==== - const enum Enum1 { - A0 = 100, - } - - const enum Enum1 { - // correct cases - A, - B, - C = 10, - D = A | B, - E = A | 1, - F = 1 | A, - G = (1 & 1), - H = ~(A | B), - I = A >>> 1, - J = 1 & A, - K = ~(1 | 5), - L = ~D, - M = E << B, - N = E << 1, - O = E >> B, - P = E >> 1, - PQ = E ** 2, - Q = -D, - R = C & 5, - S = 5 & C, - T = C | D, - U = C | 1, - V = 10 | D, - W = Enum1.V, - - // correct cases: reference to the enum member from different enum declaration - W1 = A0, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W2 = Enum1.A0, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W3 = Enum1["A0"], - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W4 = Enum1["W"], - W5 = Enum1[`V`], - } - - const enum Comments { - "//", - "/*", - "*/", - "///", - "#", - "", - } - - module A { - export module B { - export module C { - export const enum E { - V1 = 1, - V2 = A.B.C.E.V1 | 100 - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - } - } - } - } - - module A { - export module B { - export module C { - export const enum E { - V3 = A.B.C.E["V2"] & 200, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - V4 = A.B.C.E[`V1`] << 1, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - } - } - } - } - - module A1 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - } - } - - module A2 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - // module C will be classified as value - export module C { - var x = 1 - } - } - } - - import I = A.B.C.E; - import I1 = A1.B; - import I2 = A2.B; - - function foo0(e: I): void { - if (e === I.V1) { - } - else if (e === I.V2) { - } - } - - function foo1(e: I1.C.E): void { - if (e === I1.C.E.V1) { - } - else if (e === I1.C.E.V2) { - } - } - - function foo2(e: I2.C.E): void { - if (e === I2.C.E.V1) { - } - else if (e === I2.C.E.V2) { - } - } - - - function foo(x: Enum1) { - ~~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - switch (x) { - case Enum1.A: - case Enum1.B: - case Enum1.C: - case Enum1.D: - case Enum1.E: - case Enum1.F: - case Enum1.G: - case Enum1.H: - case Enum1.I: - case Enum1.J: - case Enum1.K: - case Enum1.L: - case Enum1.M: - case Enum1.N: - case Enum1.O: - case Enum1.P: - case Enum1.PQ: - case Enum1.Q: - case Enum1.R: - case Enum1.S: - case Enum1["T"]: - case Enum1[`U`]: - case Enum1.V: - case Enum1.W: - case Enum1.W1: - case Enum1.W2: - case Enum1.W3: - case Enum1.W4: - break; - } - } - - function bar(e: A.B.C.E): number { - switch (e) { - case A.B.C.E.V1: return 1; - case A.B.C.E.V2: return 1; - case A.B.C.E.V3: return 1; - } - } - - function baz(c: Comments) { - ~~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - switch (c) { - case Comments["//"]: - case Comments["/*"]: - case Comments["*/"]: - case Comments["///"]: - case Comments["#"]: - case Comments[""]: - break; - } - } - \ No newline at end of file diff --git a/tests/baselines/reference/isolated-declarations/original/tsc/constEnums.d.ts b/tests/baselines/reference/isolated-declarations/original/tsc/constEnums.d.ts deleted file mode 100644 index ccb410b083f91..0000000000000 --- a/tests/baselines/reference/isolated-declarations/original/tsc/constEnums.d.ts +++ /dev/null @@ -1,501 +0,0 @@ -//// [tests/cases/compiler/constEnums.ts] //// - -//// [constEnums.ts] -const enum Enum1 { - A0 = 100, -} - -const enum Enum1 { - // correct cases - A, - B, - C = 10, - D = A | B, - E = A | 1, - F = 1 | A, - G = (1 & 1), - H = ~(A | B), - I = A >>> 1, - J = 1 & A, - K = ~(1 | 5), - L = ~D, - M = E << B, - N = E << 1, - O = E >> B, - P = E >> 1, - PQ = E ** 2, - Q = -D, - R = C & 5, - S = 5 & C, - T = C | D, - U = C | 1, - V = 10 | D, - W = Enum1.V, - - // correct cases: reference to the enum member from different enum declaration - W1 = A0, - W2 = Enum1.A0, - W3 = Enum1["A0"], - W4 = Enum1["W"], - W5 = Enum1[`V`], -} - -const enum Comments { - "//", - "/*", - "*/", - "///", - "#", - "", -} - -module A { - export module B { - export module C { - export const enum E { - V1 = 1, - V2 = A.B.C.E.V1 | 100 - } - } - } -} - -module A { - export module B { - export module C { - export const enum E { - V3 = A.B.C.E["V2"] & 200, - V4 = A.B.C.E[`V1`] << 1, - } - } - } -} - -module A1 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - } -} - -module A2 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - // module C will be classified as value - export module C { - var x = 1 - } - } -} - -import I = A.B.C.E; -import I1 = A1.B; -import I2 = A2.B; - -function foo0(e: I): void { - if (e === I.V1) { - } - else if (e === I.V2) { - } -} - -function foo1(e: I1.C.E): void { - if (e === I1.C.E.V1) { - } - else if (e === I1.C.E.V2) { - } -} - -function foo2(e: I2.C.E): void { - if (e === I2.C.E.V1) { - } - else if (e === I2.C.E.V2) { - } -} - - -function foo(x: Enum1) { - switch (x) { - case Enum1.A: - case Enum1.B: - case Enum1.C: - case Enum1.D: - case Enum1.E: - case Enum1.F: - case Enum1.G: - case Enum1.H: - case Enum1.I: - case Enum1.J: - case Enum1.K: - case Enum1.L: - case Enum1.M: - case Enum1.N: - case Enum1.O: - case Enum1.P: - case Enum1.PQ: - case Enum1.Q: - case Enum1.R: - case Enum1.S: - case Enum1["T"]: - case Enum1[`U`]: - case Enum1.V: - case Enum1.W: - case Enum1.W1: - case Enum1.W2: - case Enum1.W3: - case Enum1.W4: - break; - } -} - -function bar(e: A.B.C.E): number { - switch (e) { - case A.B.C.E.V1: return 1; - case A.B.C.E.V2: return 1; - case A.B.C.E.V3: return 1; - } -} - -function baz(c: Comments) { - switch (c) { - case Comments["//"]: - case Comments["/*"]: - case Comments["*/"]: - case Comments["///"]: - case Comments["#"]: - case Comments[""]: - break; - } -} - - -/// [Declarations] //// - - - -//// [constEnums.d.ts] -declare const enum Enum1 { - A0 = 100 -} -declare const enum Enum1 { - A = 0, - B = 1, - C = 10, - D = 1, - E = 1, - F = 1, - G = 1, - H = -2, - I = 0, - J = 0, - K = -6, - L = -2, - M = 2, - N = 2, - O = 0, - P = 0, - PQ = 1, - Q = -1, - R = 0, - S = 0, - T = 11, - U = 11, - V = 11, - W = 11, - W1, - W2, - W3, - W4 = 11, - W5 = 11 -} -declare const enum Comments { - "//" = 0, - "/*" = 1, - "*/" = 2, - "///" = 3, - "#" = 4, - "" = 6 -} -declare namespace A { - namespace B { - namespace C { - const enum E { - V1 = 1, - V2 = 101 - } - } - } -} -declare namespace A { - namespace B { - namespace C { - const enum E { - V3, - V4 - } - } - } -} -declare namespace A1 { - namespace B { - namespace C { - const enum E { - V1 = 10, - V2 = 110 - } - } - } -} -declare namespace A2 { - namespace B { - namespace C { - const enum E { - V1 = 10, - V2 = 110 - } - } - namespace C { - } - } -} -import I = A.B.C.E; -import I1 = A1.B; -import I2 = A2.B; -declare function foo0(e: I): void; -declare function foo1(e: I1.C.E): void; -declare function foo2(e: I2.C.E): void; -declare function foo(x: Enum1): invalid; -declare function bar(e: A.B.C.E): number; -declare function baz(c: Comments): invalid; - -/// [Errors] //// - -constEnums.ts(33,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(33,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(34,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(34,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(35,5): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(35,10): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(65,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(65,22): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(66,17): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(66,22): error TS2474: const enum member initializers must be constant expressions. -constEnums.ts(124,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. -constEnums.ts(166,10): error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - - -==== constEnums.ts (12 errors) ==== - const enum Enum1 { - A0 = 100, - } - - const enum Enum1 { - // correct cases - A, - B, - C = 10, - D = A | B, - E = A | 1, - F = 1 | A, - G = (1 & 1), - H = ~(A | B), - I = A >>> 1, - J = 1 & A, - K = ~(1 | 5), - L = ~D, - M = E << B, - N = E << 1, - O = E >> B, - P = E >> 1, - PQ = E ** 2, - Q = -D, - R = C & 5, - S = 5 & C, - T = C | D, - U = C | 1, - V = 10 | D, - W = Enum1.V, - - // correct cases: reference to the enum member from different enum declaration - W1 = A0, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W2 = Enum1.A0, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W3 = Enum1["A0"], - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - W4 = Enum1["W"], - W5 = Enum1[`V`], - } - - const enum Comments { - "//", - "/*", - "*/", - "///", - "#", - "", - } - - module A { - export module B { - export module C { - export const enum E { - V1 = 1, - V2 = A.B.C.E.V1 | 100 - } - } - } - } - - module A { - export module B { - export module C { - export const enum E { - V3 = A.B.C.E["V2"] & 200, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - V4 = A.B.C.E[`V1`] << 1, - ~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - ~~~~~~~~~~~~~~~~~~ -!!! error TS2474: const enum member initializers must be constant expressions. - } - } - } - } - - module A1 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - } - } - - module A2 { - export module B { - export module C { - export const enum E { - V1 = 10, - V2 = 110, - } - } - // module C will be classified as value - export module C { - var x = 1 - } - } - } - - import I = A.B.C.E; - import I1 = A1.B; - import I2 = A2.B; - - function foo0(e: I): void { - if (e === I.V1) { - } - else if (e === I.V2) { - } - } - - function foo1(e: I1.C.E): void { - if (e === I1.C.E.V1) { - } - else if (e === I1.C.E.V2) { - } - } - - function foo2(e: I2.C.E): void { - if (e === I2.C.E.V1) { - } - else if (e === I2.C.E.V2) { - } - } - - - function foo(x: Enum1) { - ~~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - switch (x) { - case Enum1.A: - case Enum1.B: - case Enum1.C: - case Enum1.D: - case Enum1.E: - case Enum1.F: - case Enum1.G: - case Enum1.H: - case Enum1.I: - case Enum1.J: - case Enum1.K: - case Enum1.L: - case Enum1.M: - case Enum1.N: - case Enum1.O: - case Enum1.P: - case Enum1.PQ: - case Enum1.Q: - case Enum1.R: - case Enum1.S: - case Enum1["T"]: - case Enum1[`U`]: - case Enum1.V: - case Enum1.W: - case Enum1.W1: - case Enum1.W2: - case Enum1.W3: - case Enum1.W4: - break; - } - } - - function bar(e: A.B.C.E): number { - switch (e) { - case A.B.C.E.V1: return 1; - case A.B.C.E.V2: return 1; - case A.B.C.E.V3: return 1; - } - } - - function baz(c: Comments) { - ~~~ -!!! error TS9007: Declaration emit for this file requires type resolution. An explicit type annotation may unblock declaration emit. - switch (c) { - case Comments["//"]: - case Comments["/*"]: - case Comments["*/"]: - case Comments["///"]: - case Comments["#"]: - case Comments[""]: - break; - } - } - \ No newline at end of file