disallow incorrect literal property names in indexed access for const enums

This commit is contained in:
Vladimir Matveev 2015-01-12 10:58:21 -08:00
parent 799f907548
commit ecac4a519d
5 changed files with 28 additions and 4 deletions

View File

@ -5605,8 +5605,11 @@ module ts {
return unknownType;
}
if (isConstEnumObjectType(objectType) && node.argumentExpression && node.argumentExpression.kind !== SyntaxKind.StringLiteral) {
var isConstEnum = isConstEnumObjectType(objectType);
if (isConstEnum &&
(!node.argumentExpression || node.argumentExpression.kind !== SyntaxKind.StringLiteral)) {
error(node.argumentExpression, Diagnostics.Index_expression_arguments_in_const_enums_must_be_of_type_string);
return unknownType;
}
// TypeScript 1.0 spec (April 2014): 4.10 Property Access
@ -5627,6 +5630,10 @@ module ts {
getNodeLinks(node).resolvedSymbol = prop;
return getTypeOfSymbol(prop);
}
else if (isConstEnum) {
error(node.argumentExpression, Diagnostics.Property_0_does_not_exist_on_const_enum_1, name, symbolToString(objectType.symbol));
return unknownType;
}
}
}

View File

@ -369,9 +369,10 @@ module ts {
Enum_declarations_must_all_be_const_or_non_const: { code: 4082, category: DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." },
In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 4083, category: DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression.", isEarly: true },
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: 4084, 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." },
Index_expression_arguments_in_const_enums_must_be_of_type_string: { code: 4085, category: DiagnosticCategory.Error, key: "Index expression arguments in 'const' enums must be of type 'string'." },
Index_expression_arguments_in_const_enums_must_be_of_type_string: { code: 4085, category: DiagnosticCategory.Error, key: "Index expression arguments in 'const' enums must be of type 'string'.", isEarly: true },
const_enum_member_initializer_was_evaluated_to_a_non_finite_value: { code: 4086, 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: 4087, category: DiagnosticCategory.Error, key: "'const' enum member initializer was evaluated to disallowed value 'NaN'." },
Property_0_does_not_exist_on_const_enum_1: { code: 4088, category: DiagnosticCategory.Error, key: "Property '{0}' does not exist on 'const' enum '{1}'.", isEarly: true },
The_current_host_does_not_support_the_0_option: { code: 5001, category: DiagnosticCategory.Error, key: "The current host does not support the '{0}' option." },
Cannot_find_the_common_subdirectory_path_for_the_input_files: { code: 5009, category: DiagnosticCategory.Error, key: "Cannot find the common subdirectory path for the input files." },
Cannot_read_file_0_Colon_1: { code: 5012, category: DiagnosticCategory.Error, key: "Cannot read file '{0}': {1}" },
@ -419,7 +420,7 @@ module ts {
Unsupported_locale_0: { code: 6049, category: DiagnosticCategory.Error, key: "Unsupported locale '{0}'." },
Unable_to_open_file_0: { code: 6050, category: DiagnosticCategory.Error, key: "Unable to open file '{0}'." },
Corrupted_locale_file_0: { code: 6051, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
Raise_error_on_expressions_and_declarations_with_an_implied_any_type: { code: 6052, category: DiagnosticCategory.Message, key: "Warn on expressions and declarations with an implied 'any' type." },
Raise_error_on_expressions_and_declarations_with_an_implied_any_type: { code: 6052, category: DiagnosticCategory.Message, key: "Raise error on expressions and declarations with an implied 'any' type." },
File_0_not_found: { code: 6053, category: DiagnosticCategory.Error, key: "File '{0}' not found." },
File_0_must_have_extension_ts_or_d_ts: { code: 6054, category: DiagnosticCategory.Error, key: "File '{0}' must have extension '.ts' or '.d.ts'." },
Suppress_noImplicitAny_errors_for_indexing_objects_lacking_index_signatures: { code: 6055, category: DiagnosticCategory.Message, key: "Suppress noImplicitAny errors for indexing objects lacking index signatures." },

View File

@ -1574,7 +1574,8 @@
},
"Index expression arguments in 'const' enums must be of type 'string'.": {
"category": "Error",
"code": 4085
"code": 4085,
"isEarly": true
},
"'const' enum member initializer was evaluated to a non-finite value.": {
"category": "Error",
@ -1584,6 +1585,11 @@
"category": "Error",
"code": 4087
},
"Property '{0}' does not exist on 'const' enum '{1}'.": {
"category": "Error",
"code": 4088,
"isEarly": true
},
"The current host does not support the '{0}' option.": {
"category": "Error",
"code": 5001

View File

@ -0,0 +1,8 @@
tests/cases/compiler/constEnumBadPropertyNames.ts(2,11): error TS4088: Property 'B' does not exist on 'const' enum 'E'.
==== tests/cases/compiler/constEnumBadPropertyNames.ts (1 errors) ====
const enum E { A }
var x = E["B"]
~~~
!!! error TS4088: Property 'B' does not exist on 'const' enum 'E'.

View File

@ -0,0 +1,2 @@
const enum E { A }
var x = E["B"]