diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 8536f24a7e2c5..18a37d0bb1707 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -68,6 +68,7 @@ module ts { getFullyQualifiedName, getResolvedSignature, getConstantValue, + getTypeOfExpression, isValidPropertyAccess, getSignatureFromDeclaration, isImplementationOfOverload, @@ -6388,7 +6389,7 @@ module ts { let isConstEnum = isConstEnumObjectType(objectType); if (isConstEnum && (!node.argumentExpression || node.argumentExpression.kind !== SyntaxKind.StringLiteral)) { - error(node.argumentExpression, Diagnostics.A_const_enum_member_can_only_be_accessed_using_a_string_literal); + error(node.argumentExpression, Diagnostics.const_enum_member_can_only_be_accessed_using_a_string_literal); return unknownType; } @@ -8067,7 +8068,7 @@ module ts { ((node.kind === SyntaxKind.Identifier || node.kind === SyntaxKind.QualifiedName) && isInRightSideOfImportOrExportAssignment(node)); if (!ok) { - error(node, Diagnostics.const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); + error(node, Diagnostics.const_enum_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment); } } return type; @@ -10255,6 +10256,7 @@ module ts { let enumSymbol = getSymbolOfNode(node); let enumType = getDeclaredTypeOfSymbol(enumSymbol); let autoValue = 0; + let initValue: number; let ambient = isInAmbientContext(node); let enumIsConst = isConst(node); @@ -10264,10 +10266,10 @@ module ts { } let initializer = member.initializer; if (initializer) { - autoValue = getConstantValueForEnumMemberInitializer(initializer); - if (autoValue === undefined) { + initValue = getConstantValueForEnumMemberInitializer(initializer); + if (initValue === undefined) { if (enumIsConst) { - error(initializer, Diagnostics.In_const_enum_declarations_member_initializer_must_be_constant_expression); + error(initializer, Diagnostics.const_enum_initializer_must_be_a_constant_expression); } else if (!ambient) { // Only here do we need to check that the initializer is assignable to the enum type. @@ -10276,19 +10278,18 @@ module ts { // a syntax error if it is not a constant. checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, /*headMessage*/ undefined); } + return; } else if (enumIsConst) { - if (isNaN(autoValue)) { - error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); + if (isNaN(initValue)) { + return error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN); } - else if (!isFinite(autoValue)) { - error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); + else if (!isFinite(initValue)) { + return error(initializer, Diagnostics.const_enum_member_initializer_was_evaluated_to_a_non_finite_value); } + } - - } - else if (ambient && !enumIsConst) { - autoValue = undefined; + autoValue = initValue; } if (autoValue !== undefined) { @@ -10423,6 +10424,8 @@ module ts { return; } + computeEnumMemberValues(node); + // Grammar checking checkGrammarDeclarationNameInStrictMode(node) || checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarEnumDeclaration(node); @@ -10431,8 +10434,6 @@ module ts { checkCollisionWithRequireExportsInGeneratedCode(node, node.name); checkExportsOnMergedDeclarations(node); - computeEnumMemberValues(node); - let enumIsConst = isConst(node); if (compilerOptions.separateCompilation && enumIsConst && isInAmbientContext(node)) { error(node.name, Diagnostics.Ambient_const_enums_are_not_allowed_when_the_separateCompilation_flag_is_provided); @@ -11984,6 +11985,7 @@ module ts { isSymbolAccessible, isEntityNameVisible, getConstantValue, + getTypeOfExpression, resolvesToSomeValue, collectLinkedAliases, getBlockScopedVariableId, @@ -12978,25 +12980,6 @@ module ts { } } - function isIntegerLiteral(expression: Expression): boolean { - if (expression.kind === SyntaxKind.PrefixUnaryExpression) { - let unaryExpression = expression; - if (unaryExpression.operator === SyntaxKind.PlusToken || unaryExpression.operator === SyntaxKind.MinusToken) { - expression = unaryExpression.operand; - } - } - if (expression.kind === SyntaxKind.NumericLiteral) { - // Allows for scientific notation since literalExpression.text was formed by - // coercing a number to a string. Sometimes this coercion can yield a string - // in scientific notation. - // We also don't need special logic for hex because a hex integer is converted - // to decimal when it is coerced. - return /^[0-9]+([eE]\+?[0-9]+)?$/.test((expression).text); - } - - return false; - } - function checkGrammarEnumDeclaration(enumDecl: EnumDeclaration): boolean { let enumIsConst = (enumDecl.flags & NodeFlags.Const) !== 0; @@ -13005,7 +12988,6 @@ module ts { // 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. @@ -13013,18 +12995,11 @@ module ts { // 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 (inAmbientContext) { + if (node.initializer && getEnumMemberValue(node) === undefined) { + hasError = grammarErrorOnNode(node.initializer, Diagnostics.Ambient_enum_initializer_must_be_a_constant_expression) || hasError; } } - else if (node.initializer) { - inConstantEnumMemberSection = isIntegerLiteral(node.initializer); - } - else if (!inConstantEnumMemberSection) { - hasError = grammarErrorOnNode(node.name, Diagnostics.Enum_member_must_have_initializer) || hasError; - } } } diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 52718c6ddc403..9eb1e682e6a49 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -45,9 +45,8 @@ module ts { A_set_accessor_cannot_have_rest_parameter: { code: 1053, category: DiagnosticCategory.Error, key: "A 'set' accessor cannot have rest parameter." }, A_get_accessor_cannot_have_parameters: { code: 1054, category: DiagnosticCategory.Error, key: "A 'get' accessor cannot have parameters." }, Accessors_are_only_available_when_targeting_ECMAScript_5_and_higher: { code: 1056, category: DiagnosticCategory.Error, key: "Accessors are only available when targeting ECMAScript 5 and higher." }, - Enum_member_must_have_initializer: { code: 1061, category: DiagnosticCategory.Error, key: "Enum member must have initializer." }, An_export_assignment_cannot_be_used_in_a_namespace: { code: 1063, category: DiagnosticCategory.Error, key: "An export assignment cannot be used in a namespace." }, - Ambient_enum_elements_can_only_have_integer_literal_initializers: { code: 1066, category: DiagnosticCategory.Error, key: "Ambient enum elements can only have integer literal initializers." }, + Ambient_enum_initializer_must_be_a_constant_expression: { code: 1066, category: DiagnosticCategory.Error, key: "Ambient enum initializer must be a constant expression." }, Unexpected_token_A_constructor_method_accessor_or_property_was_expected: { code: 1068, category: DiagnosticCategory.Error, key: "Unexpected token. A constructor, method, accessor, or property was expected." }, A_declare_modifier_cannot_be_used_with_an_import_declaration: { code: 1079, category: DiagnosticCategory.Error, key: "A 'declare' modifier cannot be used with an import declaration." }, Invalid_reference_directive_syntax: { code: 1084, category: DiagnosticCategory.Error, key: "Invalid 'reference' directive syntax." }, @@ -336,9 +335,9 @@ module ts { A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." }, Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." }, Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." }, - In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." }, - const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, - A_const_enum_member_can_only_be_accessed_using_a_string_literal: { code: 2476, category: DiagnosticCategory.Error, key: "A const enum member can only be accessed using a string literal." }, + const_enum_initializer_must_be_a_constant_expression: { code: 2474, category: DiagnosticCategory.Error, key: "'const' enum initializer must be a constant expression." }, + const_enum_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: DiagnosticCategory.Error, key: "'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, + const_enum_member_can_only_be_accessed_using_a_string_literal: { code: 2476, category: DiagnosticCategory.Error, key: "'const' enum member can only be accessed using a string literal." }, const_enum_member_initializer_was_evaluated_to_a_non_finite_value: { code: 2477, category: DiagnosticCategory.Error, key: "'const' enum member initializer was evaluated to a non-finite value." }, const_enum_member_initializer_was_evaluated_to_disallowed_value_NaN: { code: 2478, category: DiagnosticCategory.Error, key: "'const' enum member initializer was evaluated to disallowed value 'NaN'." }, Property_0_does_not_exist_on_const_enum_1: { code: 2479, category: DiagnosticCategory.Error, key: "Property '{0}' does not exist on 'const' enum '{1}'." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index d7446907d232c..dfc8a7e672160 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -167,15 +167,11 @@ "category": "Error", "code": 1056 }, - "Enum member must have initializer.": { - "category": "Error", - "code": 1061 - }, "An export assignment cannot be used in a namespace.": { "category": "Error", "code": 1063 }, - "Ambient enum elements can only have integer literal initializers.": { + "Ambient enum initializer must be a constant expression.": { "category": "Error", "code": 1066 }, @@ -1332,15 +1328,15 @@ "category": "Error", "code": 2473 }, - "In 'const' enum declarations member initializer must be constant expression.": { + "'const' enum initializer must be a constant expression.": { "category": "Error", "code": 2474 }, - "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.": { + "'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment.": { "category": "Error", "code": 2475 }, - "A const enum member can only be accessed using a string literal.": { + "'const' enum member can only be accessed using a string literal.": { "category": "Error", "code": 2476 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 6d5146a680172..f81f06311a005 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4418,12 +4418,26 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { } } + function isExpressionMemberOf(expr: Expression, parent: Node) { + if (expr) { + let memberType = resolver.getTypeOfExpression(expr); + return memberType.symbol === parent.symbol; + } + return false; + } + function emitEnumMember(node: EnumMember) { - let enumParent = node.parent; + let enumName = getGeneratedNameForNode(node.parent); emitStart(node); - write(getGeneratedNameForNode(enumParent)); + + // If referencing member from same type/enum, emit a reference + if (isExpressionMemberOf(node.initializer, node.parent)) { + return emitEnumMemberReference(enumName, node, node.initializer) + } + + write(enumName); write("["); - write(getGeneratedNameForNode(enumParent)); + write(enumName); write("["); emitExpressionForPropertyName(node.name); write("] = "); @@ -4434,6 +4448,27 @@ var __param = (this && this.__param) || function (paramIndex, decorator) { write(";"); } + function emitEnumMemberReference(enumName: string, node: EnumMember, refNode: Expression) { + write(enumName); + write("["); + emitExpressionForPropertyName(node.name); + write("] = "); + emitEnumExpression(enumName, refNode); + emitEnd(node); + write(";"); + } + + function emitEnumExpression(enumName: string, node: Node) { + switch (node.kind) { + case SyntaxKind.PropertyAccessExpression: + return emitPropertyAccess(node); + case SyntaxKind.ElementAccessExpression: + return emitIndexedAccess(node); + case SyntaxKind.Identifier: + return write(enumName + '.' + (node).text); + } + } + function writeEnumMemberDeclarationValue(member: EnumMember) { let value = resolver.getConstantValue(member); if (value !== undefined) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 86e680ca8b047..f90bfabf865bb 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1278,6 +1278,7 @@ module ts { isEntityNameVisible(entityName: EntityName | Expression, enclosingDeclaration: Node): SymbolVisibilityResult; // Returns the constant value this property access resolves to, or 'undefined' for a non-constant getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number; + getTypeOfExpression(expr: Expression): Type; resolvesToSomeValue(location: Node, name: string): boolean; getBlockScopedVariableId(node: Identifier): number; getReferencedValueDeclaration(reference: Identifier): Declaration; diff --git a/tests/baselines/reference/ambientEnum1.errors.txt b/tests/baselines/reference/ambientEnum1.errors.txt index b4bb534f5dd1e..e795f466d4014 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: Ambient enum initializer must be a 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: Ambient enum initializer must be a 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..1325c80b7677c --- /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..200ac60df67fa --- /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..d318141b43a78 --- /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..541d52eb367ff --- /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 16ce79a05ddc3..e70c1c4b8efa9 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 TS1184: 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: Ambient enum initializer must be a 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 TS1184: 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: Ambient enum initializer must be a constant expression. } // Ambient module with initializers for values, bodies for functions / classes diff --git a/tests/baselines/reference/collisionCodeGenEnumWithEnumMemberConflict.js b/tests/baselines/reference/collisionCodeGenEnumWithEnumMemberConflict.js index 1e56eb8bd9f3d..52d84c3928c9b 100644 --- a/tests/baselines/reference/collisionCodeGenEnumWithEnumMemberConflict.js +++ b/tests/baselines/reference/collisionCodeGenEnumWithEnumMemberConflict.js @@ -8,5 +8,5 @@ enum Color { var Color; (function (Color) { Color[Color["Color"] = 0] = "Color"; - Color[Color["Thing"] = 0] = "Thing"; + Color["Thing"] = Color.Color; })(Color || (Color = {})); diff --git a/tests/baselines/reference/collisionCodeGenModuleWithEnumMemberConflict.js b/tests/baselines/reference/collisionCodeGenModuleWithEnumMemberConflict.js index 3662f5d6ddd44..b71245108b0e1 100644 --- a/tests/baselines/reference/collisionCodeGenModuleWithEnumMemberConflict.js +++ b/tests/baselines/reference/collisionCodeGenModuleWithEnumMemberConflict.js @@ -12,6 +12,6 @@ var m1; var e; (function (e) { e[e["m1"] = 0] = "m1"; - e[e["m2"] = 0] = "m2"; + e["m2"] = e.m1; })(e || (e = {})); })(m1 || (m1 = {})); diff --git a/tests/baselines/reference/constEnum2.errors.txt b/tests/baselines/reference/constEnum2.errors.txt index f0cac0c2f5d27..fbcd524ff4c97 100644 --- a/tests/baselines/reference/constEnum2.errors.txt +++ b/tests/baselines/reference/constEnum2.errors.txt @@ -1,7 +1,7 @@ -tests/cases/conformance/constEnums/constEnum2.ts(11,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. -tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. +tests/cases/conformance/constEnums/constEnum2.ts(11,9): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/conformance/constEnums/constEnum2.ts(12,9): error TS2474: 'const' enum initializer must be a constant expression. tests/cases/conformance/constEnums/constEnum2.ts(13,5): error TS1005: ',' expected. -tests/cases/conformance/constEnums/constEnum2.ts(13,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. +tests/cases/conformance/constEnums/constEnum2.ts(13,9): error TS2474: 'const' enum initializer must be a constant expression. ==== tests/cases/conformance/constEnums/constEnum2.ts (4 errors) ==== @@ -17,13 +17,13 @@ tests/cases/conformance/constEnums/constEnum2.ts(13,9): error TS2474: In 'const' d = 10, e = 199 * Math.floor(Math.random() * 1000), ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. f = d - (100 * Math.floor(Math.random() % 8)) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. g = CONST, ~ !!! error TS1005: ',' expected. ~~~~~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. } \ No newline at end of file diff --git a/tests/baselines/reference/constEnumErrors.errors.txt b/tests/baselines/reference/constEnumErrors.errors.txt index be7dad54eeb52..c52b298290a75 100644 --- a/tests/baselines/reference/constEnumErrors.errors.txt +++ b/tests/baselines/reference/constEnumErrors.errors.txt @@ -1,13 +1,13 @@ tests/cases/compiler/constEnumErrors.ts(1,12): error TS2300: Duplicate identifier 'E'. tests/cases/compiler/constEnumErrors.ts(5,8): error TS2300: Duplicate identifier 'E'. -tests/cases/compiler/constEnumErrors.ts(12,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. -tests/cases/compiler/constEnumErrors.ts(14,9): error TS2474: In 'const' enum declarations member initializer must be constant expression. -tests/cases/compiler/constEnumErrors.ts(15,10): error TS2474: In 'const' enum declarations member initializer must be constant expression. -tests/cases/compiler/constEnumErrors.ts(22,13): error TS2476: A const enum member can only be accessed using a string literal. -tests/cases/compiler/constEnumErrors.ts(24,13): error TS2476: A const enum member can only be accessed using a string literal. -tests/cases/compiler/constEnumErrors.ts(26,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. -tests/cases/compiler/constEnumErrors.ts(27,10): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. -tests/cases/compiler/constEnumErrors.ts(32,5): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +tests/cases/compiler/constEnumErrors.ts(12,9): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/compiler/constEnumErrors.ts(14,9): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/compiler/constEnumErrors.ts(15,10): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/compiler/constEnumErrors.ts(22,13): error TS2476: 'const' enum member can only be accessed using a string literal. +tests/cases/compiler/constEnumErrors.ts(24,13): error TS2476: 'const' enum member can only be accessed using a string literal. +tests/cases/compiler/constEnumErrors.ts(26,9): error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +tests/cases/compiler/constEnumErrors.ts(27,10): error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +tests/cases/compiler/constEnumErrors.ts(32,5): error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. tests/cases/compiler/constEnumErrors.ts(40,9): error TS2477: 'const' enum member initializer was evaluated to a non-finite value. tests/cases/compiler/constEnumErrors.ts(41,9): error TS2477: 'const' enum member initializer was evaluated to a non-finite value. tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member initializer was evaluated to disallowed value 'NaN'. @@ -31,14 +31,14 @@ tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member // forward reference to the element of the same enum X = Y, ~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. // forward reference to the element of the same enum Y = E1.Z, ~~~~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. Y1 = E1["Z"] ~~~~~~~ -!!! error TS2474: In 'const' enum declarations member initializer must be constant expression. +!!! error TS2474: 'const' enum initializer must be a constant expression. } const enum E2 { @@ -47,25 +47,25 @@ tests/cases/compiler/constEnumErrors.ts(42,9): error TS2478: 'const' enum member var y0 = E2[1] ~ -!!! error TS2476: A const enum member can only be accessed using a string literal. +!!! error TS2476: 'const' enum member can only be accessed using a string literal. var name = "A"; var y1 = E2[name]; ~~~~ -!!! error TS2476: A const enum member can only be accessed using a string literal. +!!! error TS2476: 'const' enum member can only be accessed using a string literal. var x = E2; ~~ -!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +!!! error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. var y = [E2]; ~~ -!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +!!! error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. function foo(t: any): void { } foo(E2); ~~ -!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +!!! error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. const enum NaNOrInfinity { A = 9007199254740992, diff --git a/tests/baselines/reference/constEnumPropertyAccess2.errors.txt b/tests/baselines/reference/constEnumPropertyAccess2.errors.txt index 19b273faffa00..2b496256221b3 100644 --- a/tests/baselines/reference/constEnumPropertyAccess2.errors.txt +++ b/tests/baselines/reference/constEnumPropertyAccess2.errors.txt @@ -1,5 +1,5 @@ -tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(14,9): error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. -tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(15,12): error TS2476: A const enum member can only be accessed using a string literal. +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(14,9): error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(15,12): error TS2476: 'const' enum member can only be accessed using a string literal. tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(17,1): error TS2322: Type 'string' is not assignable to type 'G'. tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(19,1): error TS2364: Invalid left-hand side of assignment expression. @@ -20,10 +20,10 @@ tests/cases/conformance/constEnums/constEnumPropertyAccess2.ts(19,1): error TS23 // Error from referring constant enum in any other context than a property access var z = G; ~ -!!! error TS2475: 'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. +!!! error TS2475: 'const' enum can only be used in property or index access expressions or the right hand side of an import declaration or export assignment. var z1 = G[G.A]; ~~~ -!!! error TS2476: A const enum member can only be accessed using a string literal. +!!! error TS2476: 'const' enum member can only be accessed using a string literal. var g: G; g = "string"; ~ diff --git a/tests/baselines/reference/enumBasics.js b/tests/baselines/reference/enumBasics.js index 25aec5a72d84d..bcfd52bea6f7f 100644 --- a/tests/baselines/reference/enumBasics.js +++ b/tests/baselines/reference/enumBasics.js @@ -145,7 +145,7 @@ var E8; var E9; (function (E9) { E9[E9["A"] = 0] = "A"; - E9[E9["B"] = 0] = "B"; + E9["B"] = E9.A; })(E9 || (E9 = {})); // (refer to .js to validate) // Enum constant members are propagated diff --git a/tests/baselines/reference/enumConstDeclaration.errors.txt b/tests/baselines/reference/enumConstDeclaration.errors.txt new file mode 100644 index 0000000000000..98bef9962a374 --- /dev/null +++ b/tests/baselines/reference/enumConstDeclaration.errors.txt @@ -0,0 +1,52 @@ +tests/cases/compiler/enumConstDeclaration.ts(10,9): error TS1066: Ambient enum initializer must be a constant expression. +tests/cases/compiler/enumConstDeclaration.ts(12,9): error TS1066: Ambient enum initializer must be a constant expression. +tests/cases/compiler/enumConstDeclaration.ts(13,9): error TS1066: Ambient enum initializer must be a constant expression. +tests/cases/compiler/enumConstDeclaration.ts(24,9): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/compiler/enumConstDeclaration.ts(26,9): error TS2474: 'const' enum initializer must be a constant expression. +tests/cases/compiler/enumConstDeclaration.ts(27,9): error TS2474: 'const' enum initializer must be a constant expression. + + +==== tests/cases/compiler/enumConstDeclaration.ts (6 errors) ==== + + var val1 = 1; + const val2 = 2; + + declare enum test { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + ~~~~~~~ +!!! error TS1066: Ambient enum initializer must be a constant expression. + f, + g = val1, + ~~~~ +!!! error TS1066: Ambient enum initializer must be a constant expression. + h = val2, + ~~~~ +!!! error TS1066: Ambient enum initializer must be a constant expression. + } + + test.a; + test.f; + + const enum test2 { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + ~~~~~~~ +!!! error TS2474: 'const' enum initializer must be a constant expression. + f, + g = val1, + ~~~~ +!!! error TS2474: 'const' enum initializer must be a constant expression. + h = val2, + ~~~~ +!!! error TS2474: 'const' enum initializer must be a constant expression. + } + + test2.a; + test2.f; \ No newline at end of file diff --git a/tests/baselines/reference/enumConstDeclaration.js b/tests/baselines/reference/enumConstDeclaration.js new file mode 100644 index 0000000000000..b946bdf169af0 --- /dev/null +++ b/tests/baselines/reference/enumConstDeclaration.js @@ -0,0 +1,40 @@ +//// [enumConstDeclaration.ts] + +var val1 = 1; +const val2 = 2; + +declare enum test { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + f, + g = val1, + h = val2, +} + +test.a; +test.f; + +const enum test2 { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + f, + g = val1, + h = val2, +} + +test2.a; +test2.f; + +//// [enumConstDeclaration.js] +var val1 = 1; +var val2 = 2; +test.a; +test.f; +10 /* a */; +2.4 /* f */; diff --git a/tests/baselines/reference/enumConstantMembers.errors.txt b/tests/baselines/reference/enumConstantMembers.errors.txt index d425b7a6f4b16..32b86da29484c 100644 --- a/tests/baselines/reference/enumConstantMembers.errors.txt +++ b/tests/baselines/reference/enumConstantMembers.errors.txt @@ -1,9 +1,9 @@ -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(20,9): error TS1066: Ambient enum initializer must be a constant expression. +tests/cases/conformance/enums/enumConstantMembers.ts(29,9): error TS2474: 'const' enum initializer must be a constant expression. ==== tests/cases/conformance/enums/enumConstantMembers.ts (2 errors) ==== - // Constant members allow negatives, but not decimals. Also hex literals are allowed + // Enum members with no initilizer increment from last valid constant value. enum E1 { a = 1, b @@ -14,15 +14,28 @@ tests/cases/conformance/enums/enumConstantMembers.ts(18,5): error TS1066: Ambien } enum E3 { a = 0.1, - b // Error because 0.1 is not a constant - ~ -!!! error TS1061: Enum member must have initializer. + b // b is 1.1 } 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 + c = 0.1, + d, // d is 1.1 + e = invalid, + ~~~~~~~ +!!! error TS1066: Ambient enum initializer must be a constant expression. + f // f is 2.1 + } + + const enum E5 { + a = 1, + b = -1, + c = 0.1, + d, // d is 1.1 + e = invalid, + ~~~~~~~ +!!! error TS2474: 'const' enum initializer must be a constant expression. + f // f is 2.1 + } + \ No newline at end of file diff --git a/tests/baselines/reference/enumConstantMembers.js b/tests/baselines/reference/enumConstantMembers.js index f58c7d253af08..ca4ec0decdfaa 100644 --- a/tests/baselines/reference/enumConstantMembers.js +++ b/tests/baselines/reference/enumConstantMembers.js @@ -1,5 +1,5 @@ //// [enumConstantMembers.ts] -// Constant members allow negatives, but not decimals. Also hex literals are allowed +// Enum members with no initilizer increment from last valid constant value. enum E1 { a = 1, b @@ -10,17 +10,30 @@ enum E2 { } enum E3 { a = 0.1, - b // Error because 0.1 is not a constant + b // b is 1.1 } declare enum E4 { a = 1, b = -1, - c = 0.1 // Not a constant -} + c = 0.1, + d, // d is 1.1 + e = invalid, + f // f is 2.1 +} + +const enum E5 { + a = 1, + b = -1, + c = 0.1, + d, // d is 1.1 + e = invalid, + f // f is 2.1 +} + //// [enumConstantMembers.js] -// Constant members allow negatives, but not decimals. Also hex literals are allowed +// Enum members with no initilizer increment from last valid constant value. var E1; (function (E1) { E1[E1["a"] = 1] = "a"; @@ -34,5 +47,5 @@ var E2; var E3; (function (E3) { E3[E3["a"] = 0.1] = "a"; - E3[E3["b"] = 1.1] = "b"; // Error because 0.1 is not a constant + E3[E3["b"] = 1.1] = "b"; // b is 1.1 })(E3 || (E3 = {})); diff --git a/tests/baselines/reference/enumDecl1.js b/tests/baselines/reference/enumDecl1.js index fb1c00a19a69d..837630e942a06 100644 --- a/tests/baselines/reference/enumDecl1.js +++ b/tests/baselines/reference/enumDecl1.js @@ -15,8 +15,8 @@ declare module mAmbient { //// [enumDecl1.d.ts] declare module mAmbient { enum e { - x, - y, - z, + x = 0, + y = 1, + z = 2, } } diff --git a/tests/baselines/reference/enumErrors.js b/tests/baselines/reference/enumErrors.js index a8ecdf2470a4c..08b5388092e12 100644 --- a/tests/baselines/reference/enumErrors.js +++ b/tests/baselines/reference/enumErrors.js @@ -53,7 +53,7 @@ var E5; var E9; (function (E9) { E9[E9["A"] = 0] = "A"; - E9[E9["B"] = 0] = "B"; + E9["B"] = E9.A; })(E9 || (E9 = {})); //Enum with computed member intializer of different enum type // Bug 707850: This should be allowed 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..9898497477b57 --- /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..c966e95d22c47 --- /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 index 876c04a03229c..94f18ea2f4790 100644 --- a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt +++ b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.errors.txt @@ -1,11 +1,22 @@ -tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts(4,5): error TS1061: Enum member must have initializer. +tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts(9,9): error TS2304: Cannot find name 'bad'. ==== tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts (1 errors) ==== + const z = 6; + enum E { a, b = a, - c - ~ -!!! error TS1061: Enum member must have initializer. + c, + d = f, + f, + g = bad, + ~~~ +!!! error TS2304: Cannot find name 'bad'. + h = E.b, + i = E["b"], + j = z, + k = 3, + l, + m = (Math.random() > 0.5 ? 1 : 2), } \ No newline at end of file diff --git a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.js b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.js index 8d203e9fd71ae..f802edc83c663 100644 --- a/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.js +++ b/tests/baselines/reference/enumWithoutInitializerAfterComputedMember.js @@ -1,14 +1,35 @@ //// [enumWithoutInitializerAfterComputedMember.ts] +const z = 6; + enum E { a, b = a, - c + c, + d = f, + f, + g = bad, + h = E.b, + i = E["b"], + j = z, + k = 3, + l, + m = (Math.random() > 0.5 ? 1 : 2), } //// [enumWithoutInitializerAfterComputedMember.js] +var z = 6; var E; (function (E) { E[E["a"] = 0] = "a"; - E[E["b"] = 0] = "b"; + E["b"] = E.a; E[E["c"] = 1] = "c"; + E["d"] = E.f; + E[E["f"] = 2] = "f"; + E[E["g"] = bad] = "g"; + E["h"] = E.b; + E["i"] = E["b"]; + E[E["j"] = z] = "j"; + E[E["k"] = 3] = "k"; + E[E["l"] = 4] = "l"; + E[E["m"] = (Math.random() > 0.5 ? 1 : 2)] = "m"; })(E || (E = {})); diff --git a/tests/baselines/reference/mergedDeclarations2.js b/tests/baselines/reference/mergedDeclarations2.js index 962346541cc6c..f965c9f994cd5 100644 --- a/tests/baselines/reference/mergedDeclarations2.js +++ b/tests/baselines/reference/mergedDeclarations2.js @@ -17,7 +17,7 @@ var Foo; })(Foo || (Foo = {})); var Foo; (function (Foo) { - Foo[Foo["a"] = 0] = "a"; + Foo["a"] = Foo.b; })(Foo || (Foo = {})); var Foo; (function (Foo) { diff --git a/tests/baselines/reference/mergedEnumDeclarationCodeGen.js b/tests/baselines/reference/mergedEnumDeclarationCodeGen.js index 57b6e8051f8e8..a1a616e2c03b3 100644 --- a/tests/baselines/reference/mergedEnumDeclarationCodeGen.js +++ b/tests/baselines/reference/mergedEnumDeclarationCodeGen.js @@ -11,9 +11,9 @@ enum E { var E; (function (E) { E[E["a"] = 0] = "a"; - E[E["b"] = 0] = "b"; + E["b"] = E.a; })(E || (E = {})); var E; (function (E) { - E[E["c"] = 0] = "c"; + E["c"] = E.a; })(E || (E = {})); diff --git a/tests/baselines/reference/moduledecl.js b/tests/baselines/reference/moduledecl.js index 4a27e0fbe1f49..cfcf790b47433 100644 --- a/tests/baselines/reference/moduledecl.js +++ b/tests/baselines/reference/moduledecl.js @@ -479,9 +479,9 @@ declare module mAmbient { y: C; } enum e { - x, - y, - z, + x = 0, + y = 1, + z = 2, } module m3 { class C { @@ -494,9 +494,9 @@ declare module mAmbient { y: C; } enum e { - x, - y, - z, + x = 0, + y = 1, + z = 2, } } } 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..eebbb7fe90500 --- /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..7028d55286a49 --- /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/baselines/reference/parserRealSource10.js b/tests/baselines/reference/parserRealSource10.js index 4c241abba0e74..e80c8f2ca9ef3 100644 --- a/tests/baselines/reference/parserRealSource10.js +++ b/tests/baselines/reference/parserRealSource10.js @@ -583,8 +583,8 @@ var TypeScript; TokenID[TokenID["Whitespace"] = 110] = "Whitespace"; TokenID[TokenID["Comment"] = 111] = "Comment"; TokenID[TokenID["Lim"] = 112] = "Lim"; - TokenID[TokenID["LimFixed"] = 105] = "LimFixed"; - TokenID[TokenID["LimKeyword"] = 53] = "LimKeyword"; + TokenID["LimFixed"] = TokenID.EqualsGreaterThan; + TokenID["LimKeyword"] = TokenID.Yield; })(TypeScript.TokenID || (TypeScript.TokenID = {})); var TokenID = TypeScript.TokenID; TypeScript.tokenTable = new TokenInfo[]; diff --git a/tests/baselines/reference/parserRealSource3.js b/tests/baselines/reference/parserRealSource3.js index 38e9028297bc9..bc52d5d3a8886 100644 --- a/tests/baselines/reference/parserRealSource3.js +++ b/tests/baselines/reference/parserRealSource3.js @@ -234,8 +234,8 @@ var TypeScript; NodeType[NodeType["Error"] = 104] = "Error"; NodeType[NodeType["Comment"] = 105] = "Comment"; NodeType[NodeType["Debugger"] = 106] = "Debugger"; - NodeType[NodeType["GeneralNode"] = 71] = "GeneralNode"; - NodeType[NodeType["LastAsg"] = 41] = "LastAsg"; + NodeType["GeneralNode"] = NodeType.FuncDecl; + NodeType["LastAsg"] = NodeType.AsgRs2; })(TypeScript.NodeType || (TypeScript.NodeType = {})); var NodeType = TypeScript.NodeType; })(TypeScript || (TypeScript = {})); diff --git a/tests/baselines/reference/preserveConstEnums.js b/tests/baselines/reference/preserveConstEnums.js index 3f27825283fe0..58933f8cd57c9 100644 --- a/tests/baselines/reference/preserveConstEnums.js +++ b/tests/baselines/reference/preserveConstEnums.js @@ -7,5 +7,5 @@ const enum E { var E; (function (E) { E[E["Value"] = 1] = "Value"; - E[E["Value2"] = 1] = "Value2"; + E["Value2"] = E.Value; })(E || (E = {})); diff --git a/tests/cases/compiler/enumConstDeclaration.ts b/tests/cases/compiler/enumConstDeclaration.ts new file mode 100644 index 0000000000000..7fa321e8b2cee --- /dev/null +++ b/tests/cases/compiler/enumConstDeclaration.ts @@ -0,0 +1,31 @@ + +var val1 = 1; +const val2 = 2; + +declare enum test { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + f, + g = val1, + h = val2, +} + +test.a; +test.f; + +const enum test2 { + a = 10, + b = a, + c = 10 << 2 * 8, + d = 1.4, + e = Math.PI, + f, + g = val1, + h = val2, +} + +test2.a; +test2.f; \ No newline at end of file diff --git a/tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts b/tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts index cf4fd515b834d..2591c19591271 100644 --- a/tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts +++ b/tests/cases/compiler/enumWithoutInitializerAfterComputedMember.ts @@ -1,5 +1,16 @@ +const z = 6; + enum E { a, b = a, - c + c, + d = f, + f, + g = bad, + h = E.b, + i = E["b"], + j = z, + k = 3, + l, + m = (Math.random() > 0.5 ? 1 : 2), } \ No newline at end of file diff --git a/tests/cases/conformance/enums/enumConstantMembers.ts b/tests/cases/conformance/enums/enumConstantMembers.ts index e56828db06409..ba8fb3d2c18d9 100644 --- a/tests/cases/conformance/enums/enumConstantMembers.ts +++ b/tests/cases/conformance/enums/enumConstantMembers.ts @@ -1,4 +1,4 @@ -// Constant members allow negatives, but not decimals. Also hex literals are allowed +// Enum members with no initilizer increment from last valid constant value. enum E1 { a = 1, b @@ -9,11 +9,23 @@ enum E2 { } enum E3 { a = 0.1, - b // Error because 0.1 is not a constant + b // b is 1.1 } declare enum E4 { a = 1, b = -1, - c = 0.1 // Not a constant -} \ No newline at end of file + c = 0.1, + d, // d is 1.1 + e = invalid, + f // f is 2.1 +} + +const enum E5 { + a = 1, + b = -1, + c = 0.1, + d, // d is 1.1 + e = invalid, + f // f is 2.1 +}