From fa3d9f3997db907bf3cce66674bfe5ad1a0d4de0 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 23 Sep 2015 21:30:27 -0700 Subject: [PATCH] align behavior of constant expressions in initializers of ambient enum members with spec --- src/compiler/checker.ts | 67 +++++++------------ src/compiler/diagnosticMessages.json | 2 +- .../reference/ambientEnum1.errors.txt | 11 ++- .../ambientEnumDeclaration1.errors.txt | 24 ------- .../reference/ambientEnumDeclaration1.symbols | 23 +++++++ .../reference/ambientEnumDeclaration1.types | 35 ++++++++++ .../ambientEnumElementInitializer3.errors.txt | 9 --- .../ambientEnumElementInitializer3.symbols | 7 ++ .../ambientEnumElementInitializer3.types | 8 +++ .../reference/ambientErrors.errors.txt | 11 ++- .../reference/enumConstantMembers.errors.txt | 28 -------- .../reference/enumConstantMembers.symbols | 42 ++++++++++++ .../reference/enumConstantMembers.types | 50 ++++++++++++++ .../enumInitializersWithExponents.errors.txt | 18 ----- .../enumInitializersWithExponents.symbols | 23 +++++++ .../enumInitializersWithExponents.types | 29 ++++++++ ...tInitializerAfterComputedMember.errors.txt | 11 --- ...houtInitializerAfterComputedMember.symbols | 14 ++++ ...ithoutInitializerAfterComputedMember.types | 14 ++++ .../reference/initializersInAmbientEnums.js | 8 +++ .../initializersInAmbientEnums.symbols | 14 ++++ .../initializersInAmbientEnums.types | 20 ++++++ .../parserComputedPropertyName30.errors.txt | 8 ++- .../parserEnumDeclaration6.errors.txt | 12 ---- .../reference/parserEnumDeclaration6.symbols | 16 +++++ .../reference/parserEnumDeclaration6.types | 20 ++++++ .../compiler/initializersInAmbientEnums.ts | 5 ++ 27 files changed, 368 insertions(+), 161 deletions(-) delete mode 100644 tests/baselines/reference/ambientEnumDeclaration1.errors.txt create mode 100644 tests/baselines/reference/ambientEnumDeclaration1.symbols create mode 100644 tests/baselines/reference/ambientEnumDeclaration1.types delete mode 100644 tests/baselines/reference/ambientEnumElementInitializer3.errors.txt create mode 100644 tests/baselines/reference/ambientEnumElementInitializer3.symbols create mode 100644 tests/baselines/reference/ambientEnumElementInitializer3.types delete mode 100644 tests/baselines/reference/enumConstantMembers.errors.txt create mode 100644 tests/baselines/reference/enumConstantMembers.symbols create mode 100644 tests/baselines/reference/enumConstantMembers.types delete mode 100644 tests/baselines/reference/enumInitializersWithExponents.errors.txt create mode 100644 tests/baselines/reference/enumInitializersWithExponents.symbols create mode 100644 tests/baselines/reference/enumInitializersWithExponents.types delete mode 100644 tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt create mode 100644 tests/baselines/reference/enumWithoutInitializerAfterComputedMember.symbols create mode 100644 tests/baselines/reference/enumWithoutInitializerAfterComputedMember.types create mode 100644 tests/baselines/reference/initializersInAmbientEnums.js create mode 100644 tests/baselines/reference/initializersInAmbientEnums.symbols create mode 100644 tests/baselines/reference/initializersInAmbientEnums.types delete mode 100644 tests/baselines/reference/parserEnumDeclaration6.errors.txt create mode 100644 tests/baselines/reference/parserEnumDeclaration6.symbols create mode 100644 tests/baselines/reference/parserEnumDeclaration6.types create mode 100644 tests/cases/compiler/initializersInAmbientEnums.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2aee597092dd9..d3a016c3eb26b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12940,26 +12940,41 @@ namespace ts { if (!(nodeLinks.flags & NodeCheckFlags.EnumValuesComputed)) { let enumSymbol = getSymbolOfNode(node); let enumType = getDeclaredTypeOfSymbol(enumSymbol); - let autoValue = 0; + let autoValue = 0; // set to undefined when enum member is non-constant let ambient = isInAmbientContext(node); let enumIsConst = isConst(node); - forEach(node.members, member => { - if (member.name.kind !== SyntaxKind.ComputedPropertyName && isNumericLiteralName((member.name).text)) { + for (const member of node.members) { + if (member.name.kind === SyntaxKind.ComputedPropertyName) { + error(member.name, Diagnostics.Computed_property_names_are_not_allowed_in_enums); + } + else if (isNumericLiteralName((member.name).text)) { error(member.name, Diagnostics.An_enum_member_cannot_have_a_numeric_name); } + + const previousEnumMemberIsNonConstant = autoValue === undefined; + let initializer = member.initializer; if (initializer) { autoValue = computeConstantValueForEnumMemberInitializer(initializer, enumType, enumIsConst, ambient); } else if (ambient && !enumIsConst) { + // In ambient enum declarations that specify no const modifier, enum member declarations + // that omit a value are considered computed members (as opposed to having auto-incremented values assigned). autoValue = undefined; } + else if (previousEnumMemberIsNonConstant) { + // If the member declaration specifies no value, the member is considered a constant enum member. + // If the member is the first member in the enum declaration, it is assigned the value zero. + // Otherwise, it is assigned the value of the immediately preceding member plus one, + // and an error occurs if the immediately preceding member is not a constant enum member + error(member.name, Diagnostics.Enum_member_must_have_initializer); + } if (autoValue !== undefined) { getNodeLinks(member).enumMemberValue = autoValue++; } - }); + } nodeLinks.flags |= NodeCheckFlags.EnumValuesComputed; } @@ -12975,11 +12990,11 @@ namespace ts { if (enumIsConst) { error(initializer, Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); } - else if (!ambient) { + else if (ambient) { + error(initializer, Diagnostics.In_ambient_enum_declarations_member_initializer_must_be_constant_expression); + } + else { // Only here do we need to check that the initializer is assignable to the enum type. - // If it is a constant value (not undefined), it is syntactically constrained to be a number. - // Also, we do not need to check this for ambients because there is already - // a syntax error if it is not a constant. checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); } } @@ -13119,7 +13134,7 @@ namespace ts { } // Grammar checking - checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarEnumDeclaration(node); + checkGrammarDecorators(node) || checkGrammarModifiers(node); checkTypeNameIsReserved(node.name, Diagnostics.Enum_name_cannot_be_0); checkCollisionWithCapturedThisVariable(node, node.name); @@ -15619,40 +15634,6 @@ namespace ts { return false; } - function checkGrammarEnumDeclaration(enumDecl: EnumDeclaration): boolean { - let enumIsConst = (enumDecl.flags & NodeFlags.Const) !== 0; - - let hasError = false; - - // skip checks below for const enums - they allow arbitrary initializers as long as they can be evaluated to constant expressions. - // since all values are known in compile time - it is not necessary to check that constant enum section precedes computed enum members. - if (!enumIsConst) { - let inConstantEnumMemberSection = true; - let inAmbientContext = isInAmbientContext(enumDecl); - for (let node of enumDecl.members) { - // Do not use hasDynamicName here, because that returns false for well known symbols. - // We want to perform checkComputedPropertyName for all computed properties, including - // well known symbols. - if (node.name.kind === SyntaxKind.ComputedPropertyName) { - hasError = grammarErrorOnNode(node.name, Diagnostics.Computed_property_names_are_not_allowed_in_enums); - } - else if (inAmbientContext) { - if (node.initializer && !isIntegerLiteral(node.initializer)) { - hasError = grammarErrorOnNode(node.name, Diagnostics.Ambient_enum_elements_can_only_have_integer_literal_initializers) || hasError; - } - } - else if (node.initializer) { - inConstantEnumMemberSection = isIntegerLiteral(node.initializer); - } - else if (!inConstantEnumMemberSection) { - hasError = grammarErrorOnNode(node.name, Diagnostics.Enum_member_must_have_initializer) || hasError; - } - } - } - - return hasError; - } - function hasParseDiagnostics(sourceFile: SourceFile): boolean { return sourceFile.parseDiagnostics.length > 0; } diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e6ece87b5a9bc..1fb8a3523c221 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -195,7 +195,7 @@ "category": "Error", "code": 1063 }, - "Ambient enum elements can only have integer literal initializers.": { + "In ambient enum declarations member initializer must be constant expression.": { "category": "Error", "code": 1066 }, diff --git a/tests/baselines/reference/ambientEnum1.errors.txt b/tests/baselines/reference/ambientEnum1.errors.txt index b4bb534f5dd1e..be8a19b34bad6 100644 --- a/tests/baselines/reference/ambientEnum1.errors.txt +++ b/tests/baselines/reference/ambientEnum1.errors.txt @@ -1,17 +1,14 @@ -tests/cases/compiler/ambientEnum1.ts(2,9): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/compiler/ambientEnum1.ts(7,9): error TS1066: Ambient enum elements can only have integer literal initializers. +tests/cases/compiler/ambientEnum1.ts(7,13): error TS1066: In ambient enum declarations member initializer must be constant expression. -==== tests/cases/compiler/ambientEnum1.ts (2 errors) ==== +==== tests/cases/compiler/ambientEnum1.ts (1 errors) ==== declare enum E1 { y = 4.23 - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. } // Ambient enum with computer member declare enum E2 { x = 'foo'.length - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. + ~~~~~~~~~~~~ +!!! error TS1066: In ambient enum declarations member initializer must be constant expression. } \ No newline at end of file diff --git a/tests/baselines/reference/ambientEnumDeclaration1.errors.txt b/tests/baselines/reference/ambientEnumDeclaration1.errors.txt deleted file mode 100644 index 419f7e184ac77..0000000000000 --- a/tests/baselines/reference/ambientEnumDeclaration1.errors.txt +++ /dev/null @@ -1,24 +0,0 @@ -tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(5,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(6,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(7,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/conformance/ambient/ambientEnumDeclaration1.ts(8,5): error TS1066: Ambient enum elements can only have integer literal initializers. - - -==== tests/cases/conformance/ambient/ambientEnumDeclaration1.ts (4 errors) ==== - // In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions. - - declare enum E { - a = 10, - b = 10 + 1, - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - c = b, - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - d = (c) + 1, - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - e = 10 << 2 * 8, - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - } \ No newline at end of file diff --git a/tests/baselines/reference/ambientEnumDeclaration1.symbols b/tests/baselines/reference/ambientEnumDeclaration1.symbols new file mode 100644 index 0000000000000..62e6250cf95ae --- /dev/null +++ b/tests/baselines/reference/ambientEnumDeclaration1.symbols @@ -0,0 +1,23 @@ +=== tests/cases/conformance/ambient/ambientEnumDeclaration1.ts === +// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions. + +declare enum E { +>E : Symbol(E, Decl(ambientEnumDeclaration1.ts, 0, 0)) + + a = 10, +>a : Symbol(E.a, Decl(ambientEnumDeclaration1.ts, 2, 16)) + + b = 10 + 1, +>b : Symbol(E.b, Decl(ambientEnumDeclaration1.ts, 3, 11)) + + c = b, +>c : Symbol(E.c, Decl(ambientEnumDeclaration1.ts, 4, 15)) +>b : Symbol(E.b, Decl(ambientEnumDeclaration1.ts, 3, 11)) + + d = (c) + 1, +>d : Symbol(E.d, Decl(ambientEnumDeclaration1.ts, 5, 10)) +>c : Symbol(E.c, Decl(ambientEnumDeclaration1.ts, 4, 15)) + + e = 10 << 2 * 8, +>e : Symbol(E.e, Decl(ambientEnumDeclaration1.ts, 6, 16)) +} diff --git a/tests/baselines/reference/ambientEnumDeclaration1.types b/tests/baselines/reference/ambientEnumDeclaration1.types new file mode 100644 index 0000000000000..3ef30e458da95 --- /dev/null +++ b/tests/baselines/reference/ambientEnumDeclaration1.types @@ -0,0 +1,35 @@ +=== tests/cases/conformance/ambient/ambientEnumDeclaration1.ts === +// In ambient enum declarations, all values specified in enum member declarations must be classified as constant enum expressions. + +declare enum E { +>E : E + + a = 10, +>a : E +>10 : number + + b = 10 + 1, +>b : E +>10 + 1 : number +>10 : number +>1 : number + + c = b, +>c : E +>b : E + + d = (c) + 1, +>d : E +>(c) + 1 : number +>(c) : E +>c : E +>1 : number + + e = 10 << 2 * 8, +>e : E +>10 << 2 * 8 : number +>10 : number +>2 * 8 : number +>2 : number +>8 : number +} diff --git a/tests/baselines/reference/ambientEnumElementInitializer3.errors.txt b/tests/baselines/reference/ambientEnumElementInitializer3.errors.txt deleted file mode 100644 index 56058210c16ed..0000000000000 --- a/tests/baselines/reference/ambientEnumElementInitializer3.errors.txt +++ /dev/null @@ -1,9 +0,0 @@ -tests/cases/compiler/ambientEnumElementInitializer3.ts(2,2): error TS1066: Ambient enum elements can only have integer literal initializers. - - -==== tests/cases/compiler/ambientEnumElementInitializer3.ts (1 errors) ==== - declare enum E { - e = 3.3 // Decimal - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - } \ No newline at end of file diff --git a/tests/baselines/reference/ambientEnumElementInitializer3.symbols b/tests/baselines/reference/ambientEnumElementInitializer3.symbols new file mode 100644 index 0000000000000..ee0d5e4daeac6 --- /dev/null +++ b/tests/baselines/reference/ambientEnumElementInitializer3.symbols @@ -0,0 +1,7 @@ +=== tests/cases/compiler/ambientEnumElementInitializer3.ts === +declare enum E { +>E : Symbol(E, Decl(ambientEnumElementInitializer3.ts, 0, 0)) + + e = 3.3 // Decimal +>e : Symbol(E.e, Decl(ambientEnumElementInitializer3.ts, 0, 16)) +} diff --git a/tests/baselines/reference/ambientEnumElementInitializer3.types b/tests/baselines/reference/ambientEnumElementInitializer3.types new file mode 100644 index 0000000000000..f3d3b8f3ae2ae --- /dev/null +++ b/tests/baselines/reference/ambientEnumElementInitializer3.types @@ -0,0 +1,8 @@ +=== tests/cases/compiler/ambientEnumElementInitializer3.ts === +declare enum E { +>E : E + + e = 3.3 // Decimal +>e : E +>3.3 : number +} diff --git a/tests/baselines/reference/ambientErrors.errors.txt b/tests/baselines/reference/ambientErrors.errors.txt index 578bd44c8ec9d..a0ded21299cb6 100644 --- a/tests/baselines/reference/ambientErrors.errors.txt +++ b/tests/baselines/reference/ambientErrors.errors.txt @@ -2,8 +2,7 @@ tests/cases/conformance/ambient/ambientErrors.ts(2,15): error TS1039: Initialize tests/cases/conformance/ambient/ambientErrors.ts(6,18): error TS2382: Specialized overload signature is not assignable to any non-specialized signature. tests/cases/conformance/ambient/ambientErrors.ts(17,22): error TS2371: A parameter initializer is only allowed in a function or constructor implementation. tests/cases/conformance/ambient/ambientErrors.ts(20,24): error TS1183: An implementation cannot be declared in ambient contexts. -tests/cases/conformance/ambient/ambientErrors.ts(24,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/conformance/ambient/ambientErrors.ts(29,5): error TS1066: Ambient enum elements can only have integer literal initializers. +tests/cases/conformance/ambient/ambientErrors.ts(29,9): error TS1066: In ambient enum declarations member initializer must be constant expression. tests/cases/conformance/ambient/ambientErrors.ts(34,11): error TS1039: Initializers are not allowed in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(35,19): error TS1183: An implementation cannot be declared in ambient contexts. tests/cases/conformance/ambient/ambientErrors.ts(37,20): error TS1039: Initializers are not allowed in ambient contexts. @@ -16,7 +15,7 @@ tests/cases/conformance/ambient/ambientErrors.ts(51,16): error TS2436: Ambient m tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export assignment cannot be used in a module with other exported elements. -==== tests/cases/conformance/ambient/ambientErrors.ts (16 errors) ==== +==== tests/cases/conformance/ambient/ambientErrors.ts (15 errors) ==== // Ambient variable with an initializer declare var x = 4; ~ @@ -49,15 +48,13 @@ tests/cases/conformance/ambient/ambientErrors.ts(57,5): error TS2309: An export // Ambient enum with non - integer literal constant member declare enum E1 { y = 4.23 - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. } // Ambient enum with computer member declare enum E2 { x = 'foo'.length - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. + ~~~~~~~~~~~~ +!!! error TS1066: In ambient enum declarations member initializer must be constant expression. } // Ambient module with initializers for values, bodies for functions / classes diff --git a/tests/baselines/reference/enumConstantMembers.errors.txt b/tests/baselines/reference/enumConstantMembers.errors.txt deleted file mode 100644 index d425b7a6f4b16..0000000000000 --- a/tests/baselines/reference/enumConstantMembers.errors.txt +++ /dev/null @@ -1,28 +0,0 @@ -tests/cases/conformance/enums/enumConstantMembers.ts(12,5): error TS1061: Enum member must have initializer. -tests/cases/conformance/enums/enumConstantMembers.ts(18,5): error TS1066: Ambient enum elements can only have integer literal initializers. - - -==== tests/cases/conformance/enums/enumConstantMembers.ts (2 errors) ==== - // Constant members allow negatives, but not decimals. Also hex literals are allowed - enum E1 { - a = 1, - b - } - enum E2 { - a = - 1, - b - } - enum E3 { - a = 0.1, - b // Error because 0.1 is not a constant - ~ -!!! error TS1061: Enum member must have initializer. - } - - declare enum E4 { - a = 1, - b = -1, - c = 0.1 // Not a constant - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - } \ No newline at end of file diff --git a/tests/baselines/reference/enumConstantMembers.symbols b/tests/baselines/reference/enumConstantMembers.symbols new file mode 100644 index 0000000000000..6bf516cceefbe --- /dev/null +++ b/tests/baselines/reference/enumConstantMembers.symbols @@ -0,0 +1,42 @@ +=== tests/cases/conformance/enums/enumConstantMembers.ts === +// Constant members allow negatives, but not decimals. Also hex literals are allowed +enum E1 { +>E1 : Symbol(E1, Decl(enumConstantMembers.ts, 0, 0)) + + a = 1, +>a : Symbol(E1.a, Decl(enumConstantMembers.ts, 1, 9)) + + b +>b : Symbol(E1.b, Decl(enumConstantMembers.ts, 2, 10)) +} +enum E2 { +>E2 : Symbol(E2, Decl(enumConstantMembers.ts, 4, 1)) + + a = - 1, +>a : Symbol(E2.a, Decl(enumConstantMembers.ts, 5, 9)) + + b +>b : Symbol(E2.b, Decl(enumConstantMembers.ts, 6, 12)) +} +enum E3 { +>E3 : Symbol(E3, Decl(enumConstantMembers.ts, 8, 1)) + + a = 0.1, +>a : Symbol(E3.a, Decl(enumConstantMembers.ts, 9, 9)) + + b // Error because 0.1 is not a constant +>b : Symbol(E3.b, Decl(enumConstantMembers.ts, 10, 12)) +} + +declare enum E4 { +>E4 : Symbol(E4, Decl(enumConstantMembers.ts, 12, 1)) + + a = 1, +>a : Symbol(E4.a, Decl(enumConstantMembers.ts, 14, 17)) + + b = -1, +>b : Symbol(E4.b, Decl(enumConstantMembers.ts, 15, 10)) + + c = 0.1 // Not a constant +>c : Symbol(E4.c, Decl(enumConstantMembers.ts, 16, 11)) +} diff --git a/tests/baselines/reference/enumConstantMembers.types b/tests/baselines/reference/enumConstantMembers.types new file mode 100644 index 0000000000000..c99b5eeb62f1c --- /dev/null +++ b/tests/baselines/reference/enumConstantMembers.types @@ -0,0 +1,50 @@ +=== tests/cases/conformance/enums/enumConstantMembers.ts === +// Constant members allow negatives, but not decimals. Also hex literals are allowed +enum E1 { +>E1 : E1 + + a = 1, +>a : E1 +>1 : number + + b +>b : E1 +} +enum E2 { +>E2 : E2 + + a = - 1, +>a : E2 +>- 1 : number +>1 : number + + b +>b : E2 +} +enum E3 { +>E3 : E3 + + a = 0.1, +>a : E3 +>0.1 : number + + b // Error because 0.1 is not a constant +>b : E3 +} + +declare enum E4 { +>E4 : E4 + + a = 1, +>a : E4 +>1 : number + + b = -1, +>b : E4 +>-1 : number +>1 : number + + c = 0.1 // Not a constant +>c : E4 +>0.1 : number +} diff --git a/tests/baselines/reference/enumInitializersWithExponents.errors.txt b/tests/baselines/reference/enumInitializersWithExponents.errors.txt deleted file mode 100644 index c4338cdf78b36..0000000000000 --- a/tests/baselines/reference/enumInitializersWithExponents.errors.txt +++ /dev/null @@ -1,18 +0,0 @@ -tests/cases/compiler/enumInitializersWithExponents.ts(5,5): error TS1066: Ambient enum elements can only have integer literal initializers. -tests/cases/compiler/enumInitializersWithExponents.ts(6,5): error TS1066: Ambient enum elements can only have integer literal initializers. - - -==== tests/cases/compiler/enumInitializersWithExponents.ts (2 errors) ==== - // Must be integer literals. - declare enum E { - a = 1e3, // ok - b = 1e25, // ok - c = 1e-3, // error - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - d = 1e-9, // error - ~ -!!! error TS1066: Ambient enum elements can only have integer literal initializers. - e = 1e0, // ok - f = 1e+25 // ok - } \ No newline at end of file diff --git a/tests/baselines/reference/enumInitializersWithExponents.symbols b/tests/baselines/reference/enumInitializersWithExponents.symbols new file mode 100644 index 0000000000000..13f7141a32227 --- /dev/null +++ b/tests/baselines/reference/enumInitializersWithExponents.symbols @@ -0,0 +1,23 @@ +=== tests/cases/compiler/enumInitializersWithExponents.ts === +// Must be integer literals. +declare enum E { +>E : Symbol(E, Decl(enumInitializersWithExponents.ts, 0, 0)) + + a = 1e3, // ok +>a : Symbol(E.a, Decl(enumInitializersWithExponents.ts, 1, 16)) + + b = 1e25, // ok +>b : Symbol(E.b, Decl(enumInitializersWithExponents.ts, 2, 12)) + + c = 1e-3, // error +>c : Symbol(E.c, Decl(enumInitializersWithExponents.ts, 3, 13)) + + d = 1e-9, // error +>d : Symbol(E.d, Decl(enumInitializersWithExponents.ts, 4, 13)) + + e = 1e0, // ok +>e : Symbol(E.e, Decl(enumInitializersWithExponents.ts, 5, 13)) + + f = 1e+25 // ok +>f : Symbol(E.f, Decl(enumInitializersWithExponents.ts, 6, 12)) +} diff --git a/tests/baselines/reference/enumInitializersWithExponents.types b/tests/baselines/reference/enumInitializersWithExponents.types new file mode 100644 index 0000000000000..8da208e5f871f --- /dev/null +++ b/tests/baselines/reference/enumInitializersWithExponents.types @@ -0,0 +1,29 @@ +=== tests/cases/compiler/enumInitializersWithExponents.ts === +// Must be integer literals. +declare enum E { +>E : E + + a = 1e3, // ok +>a : E +>1e3 : number + + b = 1e25, // ok +>b : E +>1e25 : number + + c = 1e-3, // error +>c : E +>1e-3 : number + + d = 1e-9, // error +>d : E +>1e-9 : number + + e = 1e0, // ok +>e : E +>1e0 : number + + f = 1e+25 // ok +>f : E +>1e+25 : number +} diff --git a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt deleted file mode 100644 index 876c04a03229c..0000000000000 --- a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt +++ /dev/null @@ -1,11 +0,0 @@ -tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts(4,5): error TS1061: Enum member must have initializer. - - -==== tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts (1 errors) ==== - enum E { - a, - b = a, - c - ~ -!!! error TS1061: Enum member must have initializer. - } \ No newline at end of file diff --git a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.symbols b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.symbols new file mode 100644 index 0000000000000..54449b293999f --- /dev/null +++ b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.symbols @@ -0,0 +1,14 @@ +=== tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts === +enum E { +>E : Symbol(E, Decl(enumWithoutInitializerAfterComputedMember.ts, 0, 0)) + + a, +>a : Symbol(E.a, Decl(enumWithoutInitializerAfterComputedMember.ts, 0, 8)) + + b = a, +>b : Symbol(E.b, Decl(enumWithoutInitializerAfterComputedMember.ts, 1, 6)) +>a : Symbol(E.a, Decl(enumWithoutInitializerAfterComputedMember.ts, 0, 8)) + + c +>c : Symbol(E.c, Decl(enumWithoutInitializerAfterComputedMember.ts, 2, 10)) +} diff --git a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.types b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.types new file mode 100644 index 0000000000000..0ece7b76f1e3a --- /dev/null +++ b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.types @@ -0,0 +1,14 @@ +=== tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts === +enum E { +>E : E + + a, +>a : E + + b = a, +>b : E +>a : E + + c +>c : E +} diff --git a/tests/baselines/reference/initializersInAmbientEnums.js b/tests/baselines/reference/initializersInAmbientEnums.js new file mode 100644 index 0000000000000..21e13ade9c45c --- /dev/null +++ b/tests/baselines/reference/initializersInAmbientEnums.js @@ -0,0 +1,8 @@ +//// [initializersInAmbientEnums.ts] +declare enum E { + a = 10, + b = a, + e = 10 << 2 * 8, +} + +//// [initializersInAmbientEnums.js] diff --git a/tests/baselines/reference/initializersInAmbientEnums.symbols b/tests/baselines/reference/initializersInAmbientEnums.symbols new file mode 100644 index 0000000000000..fc4f01b909697 --- /dev/null +++ b/tests/baselines/reference/initializersInAmbientEnums.symbols @@ -0,0 +1,14 @@ +=== tests/cases/compiler/initializersInAmbientEnums.ts === +declare enum E { +>E : Symbol(E, Decl(initializersInAmbientEnums.ts, 0, 0)) + + a = 10, +>a : Symbol(E.a, Decl(initializersInAmbientEnums.ts, 0, 16)) + + b = a, +>b : Symbol(E.b, Decl(initializersInAmbientEnums.ts, 1, 11)) +>a : Symbol(E.a, Decl(initializersInAmbientEnums.ts, 0, 16)) + + e = 10 << 2 * 8, +>e : Symbol(E.e, Decl(initializersInAmbientEnums.ts, 2, 10)) +} diff --git a/tests/baselines/reference/initializersInAmbientEnums.types b/tests/baselines/reference/initializersInAmbientEnums.types new file mode 100644 index 0000000000000..40996e9b40afb --- /dev/null +++ b/tests/baselines/reference/initializersInAmbientEnums.types @@ -0,0 +1,20 @@ +=== tests/cases/compiler/initializersInAmbientEnums.ts === +declare enum E { +>E : E + + a = 10, +>a : E +>10 : number + + b = a, +>b : E +>a : E + + e = 10 << 2 * 8, +>e : E +>10 << 2 * 8 : number +>10 : number +>2 * 8 : number +>2 : number +>8 : number +} diff --git a/tests/baselines/reference/parserComputedPropertyName30.errors.txt b/tests/baselines/reference/parserComputedPropertyName30.errors.txt index de5bc8d7ddeed..94ab013c8be88 100644 --- a/tests/baselines/reference/parserComputedPropertyName30.errors.txt +++ b/tests/baselines/reference/parserComputedPropertyName30.errors.txt @@ -1,14 +1,20 @@ +tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(3,5): error TS1164: Computed property names are not allowed in enums. tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(3,11): error TS2304: Cannot find name 'id'. tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(4,5): error TS1005: ',' expected. +tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts(4,5): error TS1164: Computed property names are not allowed in enums. -==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts (4 errors) ==== enum E { // no ASI, comma expected [e] = id++ + ~~~ +!!! error TS1164: Computed property names are not allowed in enums. ~~ !!! error TS2304: Cannot find name 'id'. [e2] = 1 ~ !!! error TS1005: ',' expected. + ~~~~ +!!! error TS1164: Computed property names are not allowed in enums. } \ No newline at end of file diff --git a/tests/baselines/reference/parserEnumDeclaration6.errors.txt b/tests/baselines/reference/parserEnumDeclaration6.errors.txt deleted file mode 100644 index b9d547c07f37a..0000000000000 --- a/tests/baselines/reference/parserEnumDeclaration6.errors.txt +++ /dev/null @@ -1,12 +0,0 @@ -tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration6.ts(5,5): error TS1061: Enum member must have initializer. - - -==== tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration6.ts (1 errors) ==== - enum E { - A = 1, - B, - C = 1 << 1, - D, - ~ -!!! error TS1061: Enum member must have initializer. - } \ No newline at end of file diff --git a/tests/baselines/reference/parserEnumDeclaration6.symbols b/tests/baselines/reference/parserEnumDeclaration6.symbols new file mode 100644 index 0000000000000..325b236fd7e05 --- /dev/null +++ b/tests/baselines/reference/parserEnumDeclaration6.symbols @@ -0,0 +1,16 @@ +=== tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration6.ts === +enum E { +>E : Symbol(E, Decl(parserEnumDeclaration6.ts, 0, 0)) + + A = 1, +>A : Symbol(E.A, Decl(parserEnumDeclaration6.ts, 0, 8)) + + B, +>B : Symbol(E.B, Decl(parserEnumDeclaration6.ts, 1, 10)) + + C = 1 << 1, +>C : Symbol(E.C, Decl(parserEnumDeclaration6.ts, 2, 6)) + + D, +>D : Symbol(E.D, Decl(parserEnumDeclaration6.ts, 3, 15)) +} diff --git a/tests/baselines/reference/parserEnumDeclaration6.types b/tests/baselines/reference/parserEnumDeclaration6.types new file mode 100644 index 0000000000000..c13d66db41c95 --- /dev/null +++ b/tests/baselines/reference/parserEnumDeclaration6.types @@ -0,0 +1,20 @@ +=== tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration6.ts === +enum E { +>E : E + + A = 1, +>A : E +>1 : number + + B, +>B : E + + C = 1 << 1, +>C : E +>1 << 1 : number +>1 : number +>1 : number + + D, +>D : E +} diff --git a/tests/cases/compiler/initializersInAmbientEnums.ts b/tests/cases/compiler/initializersInAmbientEnums.ts new file mode 100644 index 0000000000000..728fc88190372 --- /dev/null +++ b/tests/cases/compiler/initializersInAmbientEnums.ts @@ -0,0 +1,5 @@ +declare enum E { + a = 10, + b = a, + e = 10 << 2 * 8, +} \ No newline at end of file