diff --git a/bin/tsc.js b/bin/tsc.js index aaba61dfe9405..0b8dd88271647 100644 --- a/bin/tsc.js +++ b/bin/tsc.js @@ -155,17 +155,16 @@ var ts; Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: 1 /* Error */, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: 1 /* Error */, key: "Cannot find global type '{0}'." }, Named_properties_0_of_types_1_and_2_are_not_identical: { code: 2319, category: 1 /* Error */, key: "Named properties '{0}' of types '{1}' and '{2}' are not identical." }, - Interface_0_cannot_simultaneously_extend_types_1_and_2_Colon: { code: 2320, category: 1 /* Error */, key: "Interface '{0}' cannot simultaneously extend types '{1}' and '{2}':" }, + Interface_0_cannot_simultaneously_extend_types_1_and_2: { code: 2320, category: 1 /* Error */, key: "Interface '{0}' cannot simultaneously extend types '{1}' and '{2}'." }, Excessive_stack_depth_comparing_types_0_and_1: { code: 2321, category: 1 /* Error */, key: "Excessive stack depth comparing types '{0}' and '{1}'." }, - Type_0_is_not_assignable_to_type_1_Colon: { code: 2322, category: 1 /* Error */, key: "Type '{0}' is not assignable to type '{1}':" }, Type_0_is_not_assignable_to_type_1: { code: 2323, category: 1 /* Error */, key: "Type '{0}' is not assignable to type '{1}'." }, Property_0_is_missing_in_type_1: { code: 2324, category: 1 /* Error */, key: "Property '{0}' is missing in type '{1}'." }, Property_0_is_private_in_type_1_but_not_in_type_2: { code: 2325, category: 1 /* Error */, key: "Property '{0}' is private in type '{1}' but not in type '{2}'." }, - Types_of_property_0_are_incompatible_Colon: { code: 2326, category: 1 /* Error */, key: "Types of property '{0}' are incompatible:" }, + Types_of_property_0_are_incompatible: { code: 2326, category: 1 /* Error */, key: "Types of property '{0}' are incompatible." }, Property_0_is_optional_in_type_1_but_required_in_type_2: { code: 2327, category: 1 /* Error */, key: "Property '{0}' is optional in type '{1}' but required in type '{2}'." }, - Types_of_parameters_0_and_1_are_incompatible_Colon: { code: 2328, category: 1 /* Error */, key: "Types of parameters '{0}' and '{1}' are incompatible:" }, + Types_of_parameters_0_and_1_are_incompatible: { code: 2328, category: 1 /* Error */, key: "Types of parameters '{0}' and '{1}' are incompatible." }, Index_signature_is_missing_in_type_0: { code: 2329, category: 1 /* Error */, key: "Index signature is missing in type '{0}'." }, - Index_signatures_are_incompatible_Colon: { code: 2330, category: 1 /* Error */, key: "Index signatures are incompatible:" }, + Index_signatures_are_incompatible: { code: 2330, category: 1 /* Error */, key: "Index signatures are incompatible." }, this_cannot_be_referenced_in_a_module_body: { code: 2331, category: 1 /* Error */, key: "'this' cannot be referenced in a module body." }, this_cannot_be_referenced_in_current_location: { code: 2332, category: 1 /* Error */, key: "'this' cannot be referenced in current location." }, this_cannot_be_referenced_in_constructor_arguments: { code: 2333, category: 1 /* Error */, key: "'this' cannot be referenced in constructor arguments." }, @@ -178,7 +177,6 @@ var ts; Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword: { code: 2340, category: 1 /* Error */, key: "Only public and protected methods of the base class are accessible via the 'super' keyword" }, Property_0_is_private_and_only_accessible_within_class_1: { code: 2341, category: 1 /* Error */, key: "Property '{0}' is private and only accessible within class '{1}'." }, An_index_expression_argument_must_be_of_type_string_number_or_any: { code: 2342, category: 1 /* Error */, key: "An index expression argument must be of type 'string', 'number', or 'any'." }, - Type_0_does_not_satisfy_the_constraint_1_Colon: { code: 2343, category: 1 /* Error */, key: "Type '{0}' does not satisfy the constraint '{1}':" }, Type_0_does_not_satisfy_the_constraint_1: { code: 2344, category: 1 /* Error */, key: "Type '{0}' does not satisfy the constraint '{1}'." }, Argument_of_type_0_is_not_assignable_to_parameter_of_type_1: { code: 2345, category: 1 /* Error */, key: "Argument of type '{0}' is not assignable to parameter of type '{1}'." }, Supplied_parameters_do_not_match_any_signature_of_call_target: { code: 2346, category: 1 /* Error */, key: "Supplied parameters do not match any signature of call target." }, @@ -188,7 +186,6 @@ var ts; Only_a_void_function_can_be_called_with_the_new_keyword: { code: 2350, category: 1 /* Error */, key: "Only a void function can be called with the 'new' keyword." }, Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature: { code: 2351, category: 1 /* Error */, key: "Cannot use 'new' with an expression whose type lacks a call or construct signature." }, Neither_type_0_nor_type_1_is_assignable_to_the_other: { code: 2352, category: 1 /* Error */, key: "Neither type '{0}' nor type '{1}' is assignable to the other." }, - Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon: { code: 2353, category: 1 /* Error */, key: "Neither type '{0}' nor type '{1}' is assignable to the other:" }, No_best_common_type_exists_among_return_expressions: { code: 2354, category: 1 /* Error */, key: "No best common type exists among return expressions." }, A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_or_consist_of_a_single_throw_statement: { code: 2355, category: 1 /* Error */, key: "A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement." }, An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type: { code: 2356, category: 1 /* Error */, key: "An arithmetic operand must be of type 'any', 'number' or an enum type." }, @@ -249,12 +246,9 @@ var ts; Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: 1 /* Error */, key: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, Class_name_cannot_be_0: { code: 2414, category: 1 /* Error */, key: "Class name cannot be '{0}'" }, Class_0_incorrectly_extends_base_class_1: { code: 2415, category: 1 /* Error */, key: "Class '{0}' incorrectly extends base class '{1}'." }, - Class_0_incorrectly_extends_base_class_1_Colon: { code: 2416, category: 1 /* Error */, key: "Class '{0}' incorrectly extends base class '{1}':" }, Class_static_side_0_incorrectly_extends_base_class_static_side_1: { code: 2417, category: 1 /* Error */, key: "Class static side '{0}' incorrectly extends base class static side '{1}'." }, - Class_static_side_0_incorrectly_extends_base_class_static_side_1_Colon: { code: 2418, category: 1 /* Error */, key: "Class static side '{0}' incorrectly extends base class static side '{1}':" }, Type_name_0_in_extends_clause_does_not_reference_constructor_function_for_0: { code: 2419, category: 1 /* Error */, key: "Type name '{0}' in extends clause does not reference constructor function for '{0}'." }, Class_0_incorrectly_implements_interface_1: { code: 2420, category: 1 /* Error */, key: "Class '{0}' incorrectly implements interface '{1}'." }, - Class_0_incorrectly_implements_interface_1_Colon: { code: 2421, category: 1 /* Error */, key: "Class '{0}' incorrectly implements interface '{1}':" }, A_class_may_only_implement_another_class_or_interface: { code: 2422, category: 1 /* Error */, key: "A class may only implement another class or interface." }, Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor: { code: 2423, category: 1 /* Error */, key: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member accessor." }, Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property: { code: 2424, category: 1 /* Error */, key: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member property." }, @@ -262,7 +256,6 @@ var ts; Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function: { code: 2426, category: 1 /* Error */, key: "Class '{0}' defines instance member accessor '{1}', but extended class '{2}' defines it as instance member function." }, Interface_name_cannot_be_0: { code: 2427, category: 1 /* Error */, key: "Interface name cannot be '{0}'" }, All_declarations_of_an_interface_must_have_identical_type_parameters: { code: 2428, category: 1 /* Error */, key: "All declarations of an interface must have identical type parameters." }, - Interface_0_incorrectly_extends_interface_1_Colon: { code: 2429, category: 1 /* Error */, key: "Interface '{0}' incorrectly extends interface '{1}':" }, Interface_0_incorrectly_extends_interface_1: { code: 2430, category: 1 /* Error */, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: 1 /* Error */, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: 1 /* Error */, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, @@ -286,7 +279,9 @@ var ts; Left_hand_side_of_assignment_expression_cannot_be_a_constant: { code: 2450, category: 1 /* Error */, key: "Left-hand side of assignment expression cannot be a constant.", isEarly: true }, Cannot_redeclare_block_scoped_variable_0: { code: 2451, category: 1 /* Error */, key: "Cannot redeclare block-scoped variable '{0}'.", isEarly: true }, An_enum_member_cannot_have_a_numeric_name: { code: 2452, category: 1 /* Error */, key: "An enum member cannot have a numeric name." }, - Type_alias_0_circularly_references_itself: { code: 2453, category: 1 /* Error */, key: "Type alias '{0}' circularly references itself." }, + The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly: { code: 2453, category: 1 /* Error */, key: "The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly." }, + Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0: { code: 2455, category: 1 /* Error */, key: "Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}'." }, + Type_alias_0_circularly_references_itself: { code: 2456, category: 1 /* Error */, key: "Type alias '{0}' circularly references itself." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: 1 /* Error */, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 4001, category: 1 /* Error */, key: "Type parameter '{0}' of exported class has or is using name '{1}' from private module '{2}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: 1 /* Error */, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, @@ -2130,6 +2125,12 @@ var ts; }; } ts.chainDiagnosticMessages = chainDiagnosticMessages; + function concatenateDiagnosticMessageChains(headChain, tailChain) { + Debug.assert(!headChain.next); + headChain.next = tailChain; + return headChain; + } + ts.concatenateDiagnosticMessageChains = concatenateDiagnosticMessageChains; function flattenDiagnosticChain(file, start, length, diagnosticChain, newLine) { Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); @@ -9344,6 +9345,7 @@ var ts; var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var inferenceFailureType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var anySignature = createSignature(undefined, undefined, emptyArray, anyType, 0, false, false); var unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, 0, false, false); var globals = {}; @@ -9517,42 +9519,14 @@ var ts; } } function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { - var errorLocation = location; var result; var lastLocation; - var memberWithInitializerThatReferencesIdentifierFromConstructor; - function returnResolvedSymbol(s) { - if (s && memberWithInitializerThatReferencesIdentifierFromConstructor) { - var propertyName = memberWithInitializerThatReferencesIdentifierFromConstructor.name; - error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.identifierToString(propertyName), nameArg); - return undefined; - } - if (!s && nameNotFoundMessage) { - error(errorLocation, nameNotFoundMessage, nameArg); - } - if (s && s.flags & 2 /* BlockScopedVariable */) { - var declaration = ts.forEach(s.declarations, function (d) { return d.flags & ts.NodeFlags.BlockScoped ? d : undefined; }); - ts.Debug.assert(declaration, "Block-scoped variable declaration is undefined"); - var declarationSourceFile = ts.getSourceFileOfNode(declaration); - var referenceSourceFile = ts.getSourceFileOfNode(errorLocation); - if (declarationSourceFile === referenceSourceFile) { - if (declaration.pos > errorLocation.pos) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); - } - } - else if (compilerOptions.out) { - var sourceFiles = program.getSourceFiles(); - if (sourceFiles.indexOf(referenceSourceFile) < sourceFiles.indexOf(declarationSourceFile)) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); - } - } - } - return s; - } - while (location) { + var propertyWithInvalidInitializer; + var errorLocation = location; + loop: while (location) { if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - return returnResolvedSymbol(result); + break loop; } } switch (location.kind) { @@ -9561,12 +9535,12 @@ var ts; break; case 181 /* ModuleDeclaration */: if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & ts.SymbolFlags.ModuleMember)) { - return returnResolvedSymbol(result); + break loop; } break; case 180 /* EnumDeclaration */: if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { - return returnResolvedSymbol(result); + break loop; } break; case 120 /* Property */: @@ -9574,7 +9548,7 @@ var ts; var ctor = findConstructorDeclaration(location.parent); if (ctor && ctor.locals) { if (getSymbol(ctor.locals, name, meaning & ts.SymbolFlags.Value)) { - memberWithInitializerThatReferencesIdentifierFromConstructor = location; + propertyWithInvalidInitializer = location; } } } @@ -9586,9 +9560,7 @@ var ts; error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); return undefined; } - else { - return returnResolvedSymbol(result); - } + break loop; } break; case 121 /* Method */: @@ -9598,32 +9570,66 @@ var ts; case 175 /* FunctionDeclaration */: case 145 /* ArrowFunction */: if (name === "arguments") { - return returnResolvedSymbol(argumentsSymbol); + result = argumentsSymbol; + break loop; } break; case 144 /* FunctionExpression */: if (name === "arguments") { - return returnResolvedSymbol(argumentsSymbol); + result = argumentsSymbol; + break loop; } var id = location.name; if (id && name === id.text) { - return returnResolvedSymbol(location.symbol); + result = location.symbol; + break loop; } break; case 171 /* CatchBlock */: var id = location.variable; if (name === id.text) { - return returnResolvedSymbol(location.symbol); + result = location.symbol; + break loop; } break; } lastLocation = location; location = location.parent; } - if (result = getSymbol(globals, name, meaning)) { - return returnResolvedSymbol(result); + if (!result) { + result = getSymbol(globals, name, meaning); + } + if (!result) { + if (nameNotFoundMessage) { + error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.identifierToString(nameArg)); + } + return undefined; + } + if (nameNotFoundMessage) { + if (propertyWithInvalidInitializer) { + var propertyName = propertyWithInvalidInitializer.name; + error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.identifierToString(propertyName), typeof nameArg === "string" ? nameArg : ts.identifierToString(nameArg)); + return undefined; + } + if (result.flags & 2 /* BlockScopedVariable */) { + var declaration = ts.forEach(result.declarations, function (d) { return d.flags & ts.NodeFlags.BlockScoped ? d : undefined; }); + ts.Debug.assert(declaration, "Block-scoped variable declaration is undefined"); + var declarationSourceFile = ts.getSourceFileOfNode(declaration); + var referenceSourceFile = ts.getSourceFileOfNode(errorLocation); + if (declarationSourceFile === referenceSourceFile) { + if (declaration.pos > errorLocation.pos) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); + } + } + else if (compilerOptions.out) { + var sourceFiles = program.getSourceFiles(); + if (sourceFiles.indexOf(referenceSourceFile) < sourceFiles.indexOf(declarationSourceFile)) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); + } + } + } } - return returnResolvedSymbol(undefined); + return result; } function resolveImport(symbol) { ts.Debug.assert((symbol.flags & 16777216 /* Import */) !== 0, "Should only get Imports here."); @@ -9665,7 +9671,7 @@ var ts; } function resolveEntityName(location, name, meaning) { if (name.kind === 59 /* Identifier */) { - var symbol = resolveName(location, name.text, meaning, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(name)); + var symbol = resolveName(location, name.text, meaning, ts.Diagnostics.Cannot_find_name_0, name); if (!symbol) { return; } @@ -9751,7 +9757,7 @@ var ts; } if (node.exportName.text) { var meaning = ts.SymbolFlags.Value | ts.SymbolFlags.Type | ts.SymbolFlags.Namespace; - var exportSymbol = resolveName(node, node.exportName.text, meaning, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(node.exportName)); + var exportSymbol = resolveName(node, node.exportName.text, meaning, ts.Diagnostics.Cannot_find_name_0, node.exportName); } } symbolLinks.exportAssignSymbol = exportSymbol || unknownSymbol; @@ -10022,10 +10028,9 @@ var ts; } function isImportDeclarationEntityNameReferenceDeclarationVisibile(entityName) { var firstIdentifier = getFirstIdentifier(entityName); - var firstIdentifierName = ts.identifierToString(firstIdentifier); - var symbolOfNameSpace = resolveName(entityName.parent, firstIdentifier.text, ts.SymbolFlags.Namespace, ts.Diagnostics.Cannot_find_name_0, firstIdentifierName); + var symbolOfNameSpace = resolveName(entityName.parent, firstIdentifier.text, ts.SymbolFlags.Namespace, ts.Diagnostics.Cannot_find_name_0, firstIdentifier); var hasNamespaceDeclarationsVisibile = hasVisibleDeclarations(symbolOfNameSpace); - return hasNamespaceDeclarationsVisibile ? { accessibility: 0 /* Accessible */, aliasesToMakeVisible: hasNamespaceDeclarationsVisibile.aliasesToMakeVisible } : { accessibility: 1 /* NotAccessible */, errorSymbolName: firstIdentifierName }; + return hasNamespaceDeclarationsVisibile ? { accessibility: 0 /* Accessible */, aliasesToMakeVisible: hasNamespaceDeclarationsVisibile.aliasesToMakeVisible } : { accessibility: 1 /* NotAccessible */, errorSymbolName: ts.identifierToString(firstIdentifier) }; } function releaseStringWriter(writer) { writer.clear(); @@ -11925,27 +11930,27 @@ var ts; var assignableRelation = {}; var identityRelation = {}; function isTypeIdenticalTo(source, target) { - return checkTypeRelatedTo(source, target, identityRelation, undefined, undefined, undefined); + return checkTypeRelatedTo(source, target, identityRelation, undefined); } function isTypeSubtypeOf(source, target) { - return checkTypeSubtypeOf(source, target, undefined, undefined, undefined); + return checkTypeSubtypeOf(source, target, undefined); } - function checkTypeSubtypeOf(source, target, errorNode, chainedMessage, terminalMessage) { - return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, chainedMessage, terminalMessage); + function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); } function isTypeAssignableTo(source, target) { - return checkTypeAssignableTo(source, target, undefined, undefined, undefined); + return checkTypeAssignableTo(source, target, undefined); } - function checkTypeAssignableTo(source, target, errorNode, chainedMessage, terminalMessage) { - return checkTypeRelatedTo(source, target, assignableRelation, errorNode, chainedMessage, terminalMessage); + function checkTypeAssignableTo(source, target, errorNode, headMessage) { + return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage); } function isTypeRelatedTo(source, target, relation) { - return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + return checkTypeRelatedTo(source, target, relation, undefined); } function isSignatureAssignableTo(source, target) { var sourceType = getOrCreateTypeFromSignature(source); var targetType = getOrCreateTypeFromSignature(target); - return checkTypeRelatedTo(sourceType, targetType, assignableRelation, undefined, undefined, undefined); + return checkTypeRelatedTo(sourceType, targetType, assignableRelation, undefined); } function isPropertyIdenticalTo(sourceProp, targetProp) { return isPropertyIdenticalToRecursive(sourceProp, targetProp, false, function (s, t, _reportErrors) { return isTypeIdenticalTo(s, t); }); @@ -11975,7 +11980,7 @@ var ts; var typeName1 = typeToString(existing.containingType); var typeName2 = typeToString(base); var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_properties_0_of_types_1_and_2_are_not_identical, prop.name, typeName1, typeName2); - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2_Colon, typeToString(type), typeName1, typeName2); + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); addDiagnostic(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo, program.getCompilerHost().getNewLine())); } } @@ -11999,7 +12004,7 @@ var ts; return isOptionalProperty(sourceProp) === isOptionalProperty(targetProp) && relate(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); } } - function checkTypeRelatedTo(source, target, relation, errorNode, chainedMessage, terminalMessage) { + function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { var errorInfo; var sourceStack; var targetStack; @@ -12007,11 +12012,14 @@ var ts; var depth = 0; var overflow = false; ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); - var result = isRelatedToWithCustomErrors(source, target, errorNode !== undefined, chainedMessage, terminalMessage); + var result = isRelatedToWithCustomErrors(source, target, errorNode !== undefined, headMessage); if (overflow) { error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); } else if (errorInfo) { + if (containingMessageChain) { + errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + } addDiagnostic(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo, program.getCompilerHost().getNewLine())); } return result; @@ -12019,9 +12027,9 @@ var ts; errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); } function isRelatedTo(source, target, reportErrors) { - return isRelatedToWithCustomErrors(source, target, reportErrors, undefined, undefined); + return isRelatedToWithCustomErrors(source, target, reportErrors, undefined); } - function isRelatedToWithCustomErrors(source, target, reportErrors, chainedMessage, terminalMessage) { + function isRelatedToWithCustomErrors(source, target, reportErrors, headMessage) { if (relation === identityRelation) { if (source === target) return true; @@ -12076,11 +12084,9 @@ var ts; } } if (reportErrors) { - chainedMessage = chainedMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1_Colon; - terminalMessage = terminalMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1; - var diagnosticKey = errorInfo ? chainedMessage : terminalMessage; - ts.Debug.assert(diagnosticKey); - reportError(diagnosticKey, typeToString(source), typeToString(target)); + headMessage = headMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1; + ts.Debug.assert(headMessage); + reportError(headMessage, typeToString(source), typeToString(target)); } return false; } @@ -12239,7 +12245,7 @@ var ts; } if (!isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors)) { if (reportErrors) { - reportError(ts.Diagnostics.Types_of_property_0_are_incompatible_Colon, symbolToString(targetProp)); + reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); } return false; } @@ -12332,7 +12338,7 @@ var ts; if (!isRelatedTo(s, t, reportErrors)) { if (!isRelatedTo(t, s, false)) { if (reportErrors) { - reportError(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible_Colon, source.parameters[i < sourceMax ? i : sourceMax].name, target.parameters[i < targetMax ? i : targetMax].name); + reportError(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, source.parameters[i < sourceMax ? i : sourceMax].name, target.parameters[i < targetMax ? i : targetMax].name); } return false; } @@ -12373,7 +12379,7 @@ var ts; } if (!isRelatedTo(sourceType, targetType, reportErrors)) { if (reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible_Colon); + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } return false; } @@ -12402,7 +12408,7 @@ var ts; } if (!compatible) { if (reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible_Colon); + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } return false; } @@ -12456,6 +12462,32 @@ var ts; function getCommonSupertype(types) { return ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); } + function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { + var bestSupertype; + var bestSupertypeDownfallType; + var bestSupertypeScore = 0; + for (var i = 0; i < types.length; i++) { + var score = 0; + var downfallType = undefined; + for (var j = 0; j < types.length; j++) { + if (isTypeSubtypeOf(types[j], types[i])) { + score++; + } + else if (!downfallType) { + downfallType = types[j]; + } + } + if (score > bestSupertypeScore) { + bestSupertype = types[i]; + bestSupertypeDownfallType = downfallType; + bestSupertypeScore = score; + } + if (bestSupertypeScore === types.length - 1) { + break; + } + } + checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); + } function isTypeOfObjectLiteral(type) { return (type.flags & 32768 /* Anonymous */) && type.symbol && (type.symbol.flags & 2048 /* ObjectLiteral */) ? true : false; } @@ -12694,27 +12726,28 @@ var ts; } } function getInferredType(context, index) { - var result = context.inferredTypes[index]; - if (!result) { + var inferredType = context.inferredTypes[index]; + if (!inferredType) { var inferences = context.inferences[index]; if (inferences.length) { var unionOrSuperType = context.inferUnionTypes ? getUnionType(inferences) : getCommonSupertype(inferences); - var inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : undefinedType; + inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : inferenceFailureType; } else { inferredType = emptyObjectType; } - var constraint = getConstraintOfTypeParameter(context.typeParameters[index]); - var result = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType; - context.inferredTypes[index] = result; + if (inferredType !== inferenceFailureType) { + var constraint = getConstraintOfTypeParameter(context.typeParameters[index]); + inferredType = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType; + } + context.inferredTypes[index] = inferredType; } - return result; + return inferredType; } function getInferredTypes(context) { for (var i = 0; i < context.inferredTypes.length; i++) { getInferredType(context, i); } - context.inferences = undefined; return context.inferredTypes; } function hasAncestor(node, kind) { @@ -12723,7 +12756,7 @@ var ts; function getResolvedSymbol(node) { var links = getNodeLinks(node); if (!links.resolvedSymbol) { - links.resolvedSymbol = resolveName(node, node.text, ts.SymbolFlags.Value | 2097152 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(node)) || unknownSymbol; + links.resolvedSymbol = resolveName(node, node.text, ts.SymbolFlags.Value | 2097152 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; } return links.resolvedSymbol; } @@ -13531,21 +13564,29 @@ var ts; } } var inferredTypes = getInferredTypes(context); - return ts.contains(inferredTypes, undefinedType) ? undefined : inferredTypes; + context.failedTypeParameterIndex = ts.indexOf(inferredTypes, inferenceFailureType); + for (var i = 0; i < inferredTypes.length; i++) { + if (inferredTypes[i] === inferenceFailureType) { + inferredTypes[i] = unknownType; + } + } + return context; } - function checkTypeArguments(signature, typeArguments) { + function checkTypeArguments(signature, typeArguments, typeArgumentResultTypes, reportErrors) { var typeParameters = signature.typeParameters; - var result = []; + var typeArgumentsAreAssignable = true; for (var i = 0; i < typeParameters.length; i++) { var typeArgNode = typeArguments[i]; var typeArgument = getTypeFromTypeNode(typeArgNode); - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint && fullTypeCheck) { - checkTypeAssignableTo(typeArgument, constraint, typeArgNode, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1_Colon, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + typeArgumentResultTypes[i] = typeArgument; + if (typeArgumentsAreAssignable) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, constraint, reportErrors ? typeArgNode : undefined, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + } } - result.push(typeArgument); } - return result; + return typeArgumentsAreAssignable; } function checkApplicableSignature(node, signature, relation, excludeArgument, reportErrors) { if (node.arguments) { @@ -13556,7 +13597,7 @@ var ts; } var paramType = getTypeAtPosition(signature, i); var argType = arg.kind === 7 /* StringLiteral */ && !reportErrors ? getStringLiteralType(arg) : checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); - var isValidArgument = checkTypeRelatedTo(argType, paramType, relation, reportErrors ? arg : undefined, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1); + var isValidArgument = checkTypeRelatedTo(argType, paramType, relation, reportErrors ? arg : undefined, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1); if (!isValidArgument) { return false; } @@ -13581,23 +13622,74 @@ var ts; excludeArgument[i] = true; } } - var relation = candidates.length === 1 ? assignableRelation : subtypeRelation; - var lastCandidate; - while (true) { + var candidateForArgumentError; + var candidateForTypeArgumentError; + var resultOfFailedInference; + var result; + if (candidates.length > 1) { + result = chooseOverload(candidates, subtypeRelation, excludeArgument); + } + if (!result) { + candidateForArgumentError = undefined; + candidateForTypeArgumentError = undefined; + resultOfFailedInference = undefined; + result = chooseOverload(candidates, assignableRelation, excludeArgument); + } + if (result) { + return result; + } + if (candidateForArgumentError) { + checkApplicableSignature(node, candidateForArgumentError, assignableRelation, undefined, true); + } + else if (candidateForTypeArgumentError) { + if (node.typeArguments) { + checkTypeArguments(candidateForTypeArgumentError, node.typeArguments, [], true); + } + else { + ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); + var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; + var inferenceCandidates = resultOfFailedInference.inferences[resultOfFailedInference.failedTypeParameterIndex]; + var diagnosticChainHead = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); + reportNoCommonSupertypeError(inferenceCandidates, node.func, diagnosticChainHead); + } + } + else { + error(node, ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + } + if (!fullTypeCheck) { + for (var i = 0, n = candidates.length; i < n; i++) { + if (signatureHasCorrectArity(node, candidates[i])) { + return candidates[i]; + } + } + } + return resolveErrorCall(node); + function chooseOverload(candidates, relation, excludeArgument) { for (var i = 0; i < candidates.length; i++) { if (!signatureHasCorrectArity(node, candidates[i])) { continue; } + var originalCandidate = candidates[i]; + var inferenceResult; while (true) { - var candidate = candidates[i]; + var candidate = originalCandidate; if (candidate.typeParameters) { - var typeArguments = node.typeArguments ? checkTypeArguments(candidate, node.typeArguments) : inferTypeArguments(candidate, args, excludeArgument); - if (!typeArguments) { + var typeArgumentTypes; + var typeArgumentsAreValid; + if (node.typeArguments) { + typeArgumentTypes = new Array(candidate.typeParameters.length); + typeArgumentsAreValid = checkTypeArguments(candidate, node.typeArguments, typeArgumentTypes, false); + } + else { + inferenceResult = inferTypeArguments(candidate, args, excludeArgument); + typeArgumentsAreValid = inferenceResult.failedTypeParameterIndex < 0; + typeArgumentTypes = inferenceResult.inferredTypes; + } + if (!typeArgumentsAreValid) { break; } - candidate = getSignatureInstantiation(candidate, typeArguments); + candidate = getSignatureInstantiation(candidate, typeArgumentTypes); } - lastCandidate = candidate; if (!checkApplicableSignature(node, candidate, relation, excludeArgument, false)) { break; } @@ -13607,26 +13699,25 @@ var ts; } excludeArgument[index] = false; } - } - if (relation === assignableRelation) { - break; - } - relation = assignableRelation; - } - if (lastCandidate) { - checkApplicableSignature(node, lastCandidate, relation, undefined, true); - } - else { - error(node, ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - } - if (!fullTypeCheck) { - for (var i = 0, n = candidates.length; i < n; i++) { - if (signatureHasCorrectArity(node, candidates[i])) { - return candidates[i]; + if (originalCandidate.typeParameters) { + var instantiatedCandidate = candidate; + if (typeArgumentsAreValid) { + candidateForArgumentError = instantiatedCandidate; + } + else { + candidateForTypeArgumentError = originalCandidate; + if (!node.typeArguments) { + resultOfFailedInference = inferenceResult; + } + } + } + else { + ts.Debug.assert(originalCandidate === candidate); + candidateForArgumentError = originalCandidate; } } + return undefined; } - return resolveErrorCall(node); function collectCandidates() { var result = candidates; var lastParent; @@ -13750,7 +13841,7 @@ var ts; if (fullTypeCheck && targetType !== unknownType) { var widenedType = getWidenedType(exprType, true); if (!(isTypeAssignableTo(targetType, widenedType))) { - checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); + checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } return targetType; @@ -13880,7 +13971,7 @@ var ts; else { var exprType = checkExpression(node.body); if (node.type) { - checkTypeAssignableTo(exprType, getTypeFromTypeNode(node.type), node.body, undefined, undefined); + checkTypeAssignableTo(exprType, getTypeFromTypeNode(node.type), node.body, undefined); } checkFunctionExpressionBodies(node.body); } @@ -14110,7 +14201,7 @@ var ts; if (fullTypeCheck && operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment) { var ok = checkReferenceExpression(node.left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant); if (ok) { - checkTypeAssignableTo(valueType, leftType, node.left, undefined, undefined); + checkTypeAssignableTo(valueType, leftType, node.left, undefined); } } } @@ -14414,7 +14505,7 @@ var ts; var constraint = getConstraintOfTypeParameter(type.target.typeParameters[i]); if (fullTypeCheck && constraint) { var typeArgument = type.typeArguments[i]; - checkTypeAssignableTo(typeArgument, constraint, node, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1_Colon, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + checkTypeAssignableTo(typeArgument, constraint, node, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } } } @@ -14852,7 +14943,7 @@ var ts; } if (node.initializer) { if (!(getNodeLinks(node.initializer).flags & 1 /* TypeChecked */)) { - checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, undefined, undefined); + checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, undefined); } checkCollisionWithConstDeclarations(node); } @@ -14933,7 +15024,7 @@ var ts; var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var checkAssignability = func.type || (func.kind === 123 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(getDeclarationOfKind(func.symbol, 124 /* SetAccessor */))); if (checkAssignability) { - checkTypeAssignableTo(checkExpression(node.expression), returnType, node.expression, undefined, undefined); + checkTypeAssignableTo(checkExpression(node.expression), returnType, node.expression, undefined); } else if (func.kind == 122 /* Constructor */) { if (!isTypeAssignableTo(checkExpression(node.expression), returnType)) { @@ -14954,7 +15045,7 @@ var ts; if (fullTypeCheck && clause.expression) { var caseType = checkExpression(clause.expression); if (!isTypeAssignableTo(expressionType, caseType)) { - checkTypeAssignableTo(caseType, expressionType, clause.expression, undefined, undefined); + checkTypeAssignableTo(caseType, expressionType, clause.expression, undefined); } } checkBlock(clause); @@ -15061,9 +15152,9 @@ var ts; if (type.baseTypes.length) { if (fullTypeCheck) { var baseType = type.baseTypes[0]; - checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1_Colon, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); var staticBaseType = getTypeOfSymbol(baseType.symbol); - checkTypeAssignableTo(staticType, getTypeWithoutConstructors(staticBaseType), node.name, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1_Colon, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + checkTypeAssignableTo(staticType, getTypeWithoutConstructors(staticBaseType), node.name, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); if (baseType.symbol !== resolveEntityName(node, node.baseType.typeName, ts.SymbolFlags.Value)) { error(node.baseType, ts.Diagnostics.Type_name_0_in_extends_clause_does_not_reference_constructor_function_for_0, typeToString(baseType)); } @@ -15079,7 +15170,7 @@ var ts; if (t !== unknownType) { var declaredType = (t.flags & 4096 /* Reference */) ? t.target : t; if (declaredType.flags & (1024 /* Class */ | 2048 /* Interface */)) { - checkTypeAssignableTo(type, t, node.name, ts.Diagnostics.Class_0_incorrectly_implements_interface_1_Colon, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); + checkTypeAssignableTo(type, t, node.name, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); } else { error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); @@ -15184,7 +15275,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); if (checkInheritedPropertiesAreIdentical(type, node.name)) { ts.forEach(type.baseTypes, function (baseType) { - checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1_Colon, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); }); checkIndexConstraints(type); } @@ -15229,7 +15320,7 @@ var ts; if (initializer) { autoValue = getConstantValueForExpression(initializer); if (autoValue === undefined && !ambient) { - checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, undefined, undefined); + checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, undefined); } } else if (ambient) { diff --git a/bin/typescriptServices.js b/bin/typescriptServices.js index 661017b48b70f..8aebc0f642a6f 100644 --- a/bin/typescriptServices.js +++ b/bin/typescriptServices.js @@ -155,17 +155,16 @@ var ts; Global_type_0_must_have_1_type_parameter_s: { code: 2317, category: 1 /* Error */, key: "Global type '{0}' must have {1} type parameter(s)." }, Cannot_find_global_type_0: { code: 2318, category: 1 /* Error */, key: "Cannot find global type '{0}'." }, Named_properties_0_of_types_1_and_2_are_not_identical: { code: 2319, category: 1 /* Error */, key: "Named properties '{0}' of types '{1}' and '{2}' are not identical." }, - Interface_0_cannot_simultaneously_extend_types_1_and_2_Colon: { code: 2320, category: 1 /* Error */, key: "Interface '{0}' cannot simultaneously extend types '{1}' and '{2}':" }, + Interface_0_cannot_simultaneously_extend_types_1_and_2: { code: 2320, category: 1 /* Error */, key: "Interface '{0}' cannot simultaneously extend types '{1}' and '{2}'." }, Excessive_stack_depth_comparing_types_0_and_1: { code: 2321, category: 1 /* Error */, key: "Excessive stack depth comparing types '{0}' and '{1}'." }, - Type_0_is_not_assignable_to_type_1_Colon: { code: 2322, category: 1 /* Error */, key: "Type '{0}' is not assignable to type '{1}':" }, Type_0_is_not_assignable_to_type_1: { code: 2323, category: 1 /* Error */, key: "Type '{0}' is not assignable to type '{1}'." }, Property_0_is_missing_in_type_1: { code: 2324, category: 1 /* Error */, key: "Property '{0}' is missing in type '{1}'." }, Property_0_is_private_in_type_1_but_not_in_type_2: { code: 2325, category: 1 /* Error */, key: "Property '{0}' is private in type '{1}' but not in type '{2}'." }, - Types_of_property_0_are_incompatible_Colon: { code: 2326, category: 1 /* Error */, key: "Types of property '{0}' are incompatible:" }, + Types_of_property_0_are_incompatible: { code: 2326, category: 1 /* Error */, key: "Types of property '{0}' are incompatible." }, Property_0_is_optional_in_type_1_but_required_in_type_2: { code: 2327, category: 1 /* Error */, key: "Property '{0}' is optional in type '{1}' but required in type '{2}'." }, - Types_of_parameters_0_and_1_are_incompatible_Colon: { code: 2328, category: 1 /* Error */, key: "Types of parameters '{0}' and '{1}' are incompatible:" }, + Types_of_parameters_0_and_1_are_incompatible: { code: 2328, category: 1 /* Error */, key: "Types of parameters '{0}' and '{1}' are incompatible." }, Index_signature_is_missing_in_type_0: { code: 2329, category: 1 /* Error */, key: "Index signature is missing in type '{0}'." }, - Index_signatures_are_incompatible_Colon: { code: 2330, category: 1 /* Error */, key: "Index signatures are incompatible:" }, + Index_signatures_are_incompatible: { code: 2330, category: 1 /* Error */, key: "Index signatures are incompatible." }, this_cannot_be_referenced_in_a_module_body: { code: 2331, category: 1 /* Error */, key: "'this' cannot be referenced in a module body." }, this_cannot_be_referenced_in_current_location: { code: 2332, category: 1 /* Error */, key: "'this' cannot be referenced in current location." }, this_cannot_be_referenced_in_constructor_arguments: { code: 2333, category: 1 /* Error */, key: "'this' cannot be referenced in constructor arguments." }, @@ -178,7 +177,6 @@ var ts; Only_public_and_protected_methods_of_the_base_class_are_accessible_via_the_super_keyword: { code: 2340, category: 1 /* Error */, key: "Only public and protected methods of the base class are accessible via the 'super' keyword" }, Property_0_is_private_and_only_accessible_within_class_1: { code: 2341, category: 1 /* Error */, key: "Property '{0}' is private and only accessible within class '{1}'." }, An_index_expression_argument_must_be_of_type_string_number_or_any: { code: 2342, category: 1 /* Error */, key: "An index expression argument must be of type 'string', 'number', or 'any'." }, - Type_0_does_not_satisfy_the_constraint_1_Colon: { code: 2343, category: 1 /* Error */, key: "Type '{0}' does not satisfy the constraint '{1}':" }, Type_0_does_not_satisfy_the_constraint_1: { code: 2344, category: 1 /* Error */, key: "Type '{0}' does not satisfy the constraint '{1}'." }, Argument_of_type_0_is_not_assignable_to_parameter_of_type_1: { code: 2345, category: 1 /* Error */, key: "Argument of type '{0}' is not assignable to parameter of type '{1}'." }, Supplied_parameters_do_not_match_any_signature_of_call_target: { code: 2346, category: 1 /* Error */, key: "Supplied parameters do not match any signature of call target." }, @@ -188,7 +186,6 @@ var ts; Only_a_void_function_can_be_called_with_the_new_keyword: { code: 2350, category: 1 /* Error */, key: "Only a void function can be called with the 'new' keyword." }, Cannot_use_new_with_an_expression_whose_type_lacks_a_call_or_construct_signature: { code: 2351, category: 1 /* Error */, key: "Cannot use 'new' with an expression whose type lacks a call or construct signature." }, Neither_type_0_nor_type_1_is_assignable_to_the_other: { code: 2352, category: 1 /* Error */, key: "Neither type '{0}' nor type '{1}' is assignable to the other." }, - Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon: { code: 2353, category: 1 /* Error */, key: "Neither type '{0}' nor type '{1}' is assignable to the other:" }, No_best_common_type_exists_among_return_expressions: { code: 2354, category: 1 /* Error */, key: "No best common type exists among return expressions." }, A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value_or_consist_of_a_single_throw_statement: { code: 2355, category: 1 /* Error */, key: "A function whose declared type is neither 'void' nor 'any' must return a value or consist of a single 'throw' statement." }, An_arithmetic_operand_must_be_of_type_any_number_or_an_enum_type: { code: 2356, category: 1 /* Error */, key: "An arithmetic operand must be of type 'any', 'number' or an enum type." }, @@ -249,12 +246,9 @@ var ts; Numeric_index_type_0_is_not_assignable_to_string_index_type_1: { code: 2413, category: 1 /* Error */, key: "Numeric index type '{0}' is not assignable to string index type '{1}'." }, Class_name_cannot_be_0: { code: 2414, category: 1 /* Error */, key: "Class name cannot be '{0}'" }, Class_0_incorrectly_extends_base_class_1: { code: 2415, category: 1 /* Error */, key: "Class '{0}' incorrectly extends base class '{1}'." }, - Class_0_incorrectly_extends_base_class_1_Colon: { code: 2416, category: 1 /* Error */, key: "Class '{0}' incorrectly extends base class '{1}':" }, Class_static_side_0_incorrectly_extends_base_class_static_side_1: { code: 2417, category: 1 /* Error */, key: "Class static side '{0}' incorrectly extends base class static side '{1}'." }, - Class_static_side_0_incorrectly_extends_base_class_static_side_1_Colon: { code: 2418, category: 1 /* Error */, key: "Class static side '{0}' incorrectly extends base class static side '{1}':" }, Type_name_0_in_extends_clause_does_not_reference_constructor_function_for_0: { code: 2419, category: 1 /* Error */, key: "Type name '{0}' in extends clause does not reference constructor function for '{0}'." }, Class_0_incorrectly_implements_interface_1: { code: 2420, category: 1 /* Error */, key: "Class '{0}' incorrectly implements interface '{1}'." }, - Class_0_incorrectly_implements_interface_1_Colon: { code: 2421, category: 1 /* Error */, key: "Class '{0}' incorrectly implements interface '{1}':" }, A_class_may_only_implement_another_class_or_interface: { code: 2422, category: 1 /* Error */, key: "A class may only implement another class or interface." }, Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_accessor: { code: 2423, category: 1 /* Error */, key: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member accessor." }, Class_0_defines_instance_member_function_1_but_extended_class_2_defines_it_as_instance_member_property: { code: 2424, category: 1 /* Error */, key: "Class '{0}' defines instance member function '{1}', but extended class '{2}' defines it as instance member property." }, @@ -262,7 +256,6 @@ var ts; Class_0_defines_instance_member_accessor_1_but_extended_class_2_defines_it_as_instance_member_function: { code: 2426, category: 1 /* Error */, key: "Class '{0}' defines instance member accessor '{1}', but extended class '{2}' defines it as instance member function." }, Interface_name_cannot_be_0: { code: 2427, category: 1 /* Error */, key: "Interface name cannot be '{0}'" }, All_declarations_of_an_interface_must_have_identical_type_parameters: { code: 2428, category: 1 /* Error */, key: "All declarations of an interface must have identical type parameters." }, - Interface_0_incorrectly_extends_interface_1_Colon: { code: 2429, category: 1 /* Error */, key: "Interface '{0}' incorrectly extends interface '{1}':" }, Interface_0_incorrectly_extends_interface_1: { code: 2430, category: 1 /* Error */, key: "Interface '{0}' incorrectly extends interface '{1}'." }, Enum_name_cannot_be_0: { code: 2431, category: 1 /* Error */, key: "Enum name cannot be '{0}'" }, In_an_enum_with_multiple_declarations_only_one_declaration_can_omit_an_initializer_for_its_first_enum_element: { code: 2432, category: 1 /* Error */, key: "In an enum with multiple declarations, only one declaration can omit an initializer for its first enum element." }, @@ -286,7 +279,9 @@ var ts; Left_hand_side_of_assignment_expression_cannot_be_a_constant: { code: 2450, category: 1 /* Error */, key: "Left-hand side of assignment expression cannot be a constant.", isEarly: true }, Cannot_redeclare_block_scoped_variable_0: { code: 2451, category: 1 /* Error */, key: "Cannot redeclare block-scoped variable '{0}'.", isEarly: true }, An_enum_member_cannot_have_a_numeric_name: { code: 2452, category: 1 /* Error */, key: "An enum member cannot have a numeric name." }, - Type_alias_0_circularly_references_itself: { code: 2453, category: 1 /* Error */, key: "Type alias '{0}' circularly references itself." }, + The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly: { code: 2453, category: 1 /* Error */, key: "The type argument for type parameter '{0}' cannot be inferred from the usage. Consider specifying the type arguments explicitly." }, + Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0: { code: 2455, category: 1 /* Error */, key: "Type argument candidate '{1}' is not a valid type argument because it is not a supertype of candidate '{0}'." }, + Type_alias_0_circularly_references_itself: { code: 2456, category: 1 /* Error */, key: "Type alias '{0}' circularly references itself." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: 1 /* Error */, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_name_1_from_private_module_2: { code: 4001, category: 1 /* Error */, key: "Type parameter '{0}' of exported class has or is using name '{1}' from private module '{2}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: 1 /* Error */, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, @@ -2130,6 +2125,12 @@ var ts; }; } ts.chainDiagnosticMessages = chainDiagnosticMessages; + function concatenateDiagnosticMessageChains(headChain, tailChain) { + Debug.assert(!headChain.next); + headChain.next = tailChain; + return headChain; + } + ts.concatenateDiagnosticMessageChains = concatenateDiagnosticMessageChains; function flattenDiagnosticChain(file, start, length, diagnosticChain, newLine) { Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); @@ -9149,6 +9150,7 @@ var ts; var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var inferenceFailureType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var anySignature = createSignature(undefined, undefined, emptyArray, anyType, 0, false, false); var unknownSignature = createSignature(undefined, undefined, emptyArray, unknownType, 0, false, false); var globals = {}; @@ -9322,42 +9324,14 @@ var ts; } } function resolveName(location, name, meaning, nameNotFoundMessage, nameArg) { - var errorLocation = location; var result; var lastLocation; - var memberWithInitializerThatReferencesIdentifierFromConstructor; - function returnResolvedSymbol(s) { - if (s && memberWithInitializerThatReferencesIdentifierFromConstructor) { - var propertyName = memberWithInitializerThatReferencesIdentifierFromConstructor.name; - error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.identifierToString(propertyName), nameArg); - return undefined; - } - if (!s && nameNotFoundMessage) { - error(errorLocation, nameNotFoundMessage, nameArg); - } - if (s && s.flags & 2 /* BlockScopedVariable */) { - var declaration = ts.forEach(s.declarations, function (d) { return d.flags & ts.NodeFlags.BlockScoped ? d : undefined; }); - ts.Debug.assert(declaration, "Block-scoped variable declaration is undefined"); - var declarationSourceFile = ts.getSourceFileOfNode(declaration); - var referenceSourceFile = ts.getSourceFileOfNode(errorLocation); - if (declarationSourceFile === referenceSourceFile) { - if (declaration.pos > errorLocation.pos) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); - } - } - else if (compilerOptions.out) { - var sourceFiles = program.getSourceFiles(); - if (sourceFiles.indexOf(referenceSourceFile) < sourceFiles.indexOf(declarationSourceFile)) { - error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); - } - } - } - return s; - } - while (location) { + var propertyWithInvalidInitializer; + var errorLocation = location; + loop: while (location) { if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - return returnResolvedSymbol(result); + break loop; } } switch (location.kind) { @@ -9366,12 +9340,12 @@ var ts; break; case 181 /* ModuleDeclaration */: if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & ts.SymbolFlags.ModuleMember)) { - return returnResolvedSymbol(result); + break loop; } break; case 180 /* EnumDeclaration */: if (result = getSymbol(getSymbolOfNode(location).exports, name, meaning & 8 /* EnumMember */)) { - return returnResolvedSymbol(result); + break loop; } break; case 120 /* Property */: @@ -9379,7 +9353,7 @@ var ts; var ctor = findConstructorDeclaration(location.parent); if (ctor && ctor.locals) { if (getSymbol(ctor.locals, name, meaning & ts.SymbolFlags.Value)) { - memberWithInitializerThatReferencesIdentifierFromConstructor = location; + propertyWithInvalidInitializer = location; } } } @@ -9391,9 +9365,7 @@ var ts; error(errorLocation, ts.Diagnostics.Static_members_cannot_reference_class_type_parameters); return undefined; } - else { - return returnResolvedSymbol(result); - } + break loop; } break; case 121 /* Method */: @@ -9403,32 +9375,66 @@ var ts; case 175 /* FunctionDeclaration */: case 145 /* ArrowFunction */: if (name === "arguments") { - return returnResolvedSymbol(argumentsSymbol); + result = argumentsSymbol; + break loop; } break; case 144 /* FunctionExpression */: if (name === "arguments") { - return returnResolvedSymbol(argumentsSymbol); + result = argumentsSymbol; + break loop; } var id = location.name; if (id && name === id.text) { - return returnResolvedSymbol(location.symbol); + result = location.symbol; + break loop; } break; case 171 /* CatchBlock */: var id = location.variable; if (name === id.text) { - return returnResolvedSymbol(location.symbol); + result = location.symbol; + break loop; } break; } lastLocation = location; location = location.parent; } - if (result = getSymbol(globals, name, meaning)) { - return returnResolvedSymbol(result); + if (!result) { + result = getSymbol(globals, name, meaning); + } + if (!result) { + if (nameNotFoundMessage) { + error(errorLocation, nameNotFoundMessage, typeof nameArg === "string" ? nameArg : ts.identifierToString(nameArg)); + } + return undefined; } - return returnResolvedSymbol(undefined); + if (nameNotFoundMessage) { + if (propertyWithInvalidInitializer) { + var propertyName = propertyWithInvalidInitializer.name; + error(errorLocation, ts.Diagnostics.Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor, ts.identifierToString(propertyName), typeof nameArg === "string" ? nameArg : ts.identifierToString(nameArg)); + return undefined; + } + if (result.flags & 2 /* BlockScopedVariable */) { + var declaration = ts.forEach(result.declarations, function (d) { return d.flags & ts.NodeFlags.BlockScoped ? d : undefined; }); + ts.Debug.assert(declaration, "Block-scoped variable declaration is undefined"); + var declarationSourceFile = ts.getSourceFileOfNode(declaration); + var referenceSourceFile = ts.getSourceFileOfNode(errorLocation); + if (declarationSourceFile === referenceSourceFile) { + if (declaration.pos > errorLocation.pos) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); + } + } + else if (compilerOptions.out) { + var sourceFiles = program.getSourceFiles(); + if (sourceFiles.indexOf(referenceSourceFile) < sourceFiles.indexOf(declarationSourceFile)) { + error(errorLocation, ts.Diagnostics.Block_scoped_variable_0_used_before_its_declaration, ts.identifierToString(declaration.name)); + } + } + } + } + return result; } function resolveImport(symbol) { ts.Debug.assert((symbol.flags & 16777216 /* Import */) !== 0, "Should only get Imports here."); @@ -9470,7 +9476,7 @@ var ts; } function resolveEntityName(location, name, meaning) { if (name.kind === 59 /* Identifier */) { - var symbol = resolveName(location, name.text, meaning, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(name)); + var symbol = resolveName(location, name.text, meaning, ts.Diagnostics.Cannot_find_name_0, name); if (!symbol) { return; } @@ -9556,7 +9562,7 @@ var ts; } if (node.exportName.text) { var meaning = ts.SymbolFlags.Value | ts.SymbolFlags.Type | ts.SymbolFlags.Namespace; - var exportSymbol = resolveName(node, node.exportName.text, meaning, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(node.exportName)); + var exportSymbol = resolveName(node, node.exportName.text, meaning, ts.Diagnostics.Cannot_find_name_0, node.exportName); } } symbolLinks.exportAssignSymbol = exportSymbol || unknownSymbol; @@ -9827,10 +9833,9 @@ var ts; } function isImportDeclarationEntityNameReferenceDeclarationVisibile(entityName) { var firstIdentifier = getFirstIdentifier(entityName); - var firstIdentifierName = ts.identifierToString(firstIdentifier); - var symbolOfNameSpace = resolveName(entityName.parent, firstIdentifier.text, ts.SymbolFlags.Namespace, ts.Diagnostics.Cannot_find_name_0, firstIdentifierName); + var symbolOfNameSpace = resolveName(entityName.parent, firstIdentifier.text, ts.SymbolFlags.Namespace, ts.Diagnostics.Cannot_find_name_0, firstIdentifier); var hasNamespaceDeclarationsVisibile = hasVisibleDeclarations(symbolOfNameSpace); - return hasNamespaceDeclarationsVisibile ? { accessibility: 0 /* Accessible */, aliasesToMakeVisible: hasNamespaceDeclarationsVisibile.aliasesToMakeVisible } : { accessibility: 1 /* NotAccessible */, errorSymbolName: firstIdentifierName }; + return hasNamespaceDeclarationsVisibile ? { accessibility: 0 /* Accessible */, aliasesToMakeVisible: hasNamespaceDeclarationsVisibile.aliasesToMakeVisible } : { accessibility: 1 /* NotAccessible */, errorSymbolName: ts.identifierToString(firstIdentifier) }; } function releaseStringWriter(writer) { writer.clear(); @@ -11730,27 +11735,27 @@ var ts; var assignableRelation = {}; var identityRelation = {}; function isTypeIdenticalTo(source, target) { - return checkTypeRelatedTo(source, target, identityRelation, undefined, undefined, undefined); + return checkTypeRelatedTo(source, target, identityRelation, undefined); } function isTypeSubtypeOf(source, target) { - return checkTypeSubtypeOf(source, target, undefined, undefined, undefined); + return checkTypeSubtypeOf(source, target, undefined); } - function checkTypeSubtypeOf(source, target, errorNode, chainedMessage, terminalMessage) { - return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, chainedMessage, terminalMessage); + function checkTypeSubtypeOf(source, target, errorNode, headMessage, containingMessageChain) { + return checkTypeRelatedTo(source, target, subtypeRelation, errorNode, headMessage, containingMessageChain); } function isTypeAssignableTo(source, target) { - return checkTypeAssignableTo(source, target, undefined, undefined, undefined); + return checkTypeAssignableTo(source, target, undefined); } - function checkTypeAssignableTo(source, target, errorNode, chainedMessage, terminalMessage) { - return checkTypeRelatedTo(source, target, assignableRelation, errorNode, chainedMessage, terminalMessage); + function checkTypeAssignableTo(source, target, errorNode, headMessage) { + return checkTypeRelatedTo(source, target, assignableRelation, errorNode, headMessage); } function isTypeRelatedTo(source, target, relation) { - return checkTypeRelatedTo(source, target, relation, undefined, undefined, undefined); + return checkTypeRelatedTo(source, target, relation, undefined); } function isSignatureAssignableTo(source, target) { var sourceType = getOrCreateTypeFromSignature(source); var targetType = getOrCreateTypeFromSignature(target); - return checkTypeRelatedTo(sourceType, targetType, assignableRelation, undefined, undefined, undefined); + return checkTypeRelatedTo(sourceType, targetType, assignableRelation, undefined); } function isPropertyIdenticalTo(sourceProp, targetProp) { return isPropertyIdenticalToRecursive(sourceProp, targetProp, false, function (s, t, _reportErrors) { return isTypeIdenticalTo(s, t); }); @@ -11780,7 +11785,7 @@ var ts; var typeName1 = typeToString(existing.containingType); var typeName2 = typeToString(base); var errorInfo = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.Named_properties_0_of_types_1_and_2_are_not_identical, prop.name, typeName1, typeName2); - errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2_Colon, typeToString(type), typeName1, typeName2); + errorInfo = ts.chainDiagnosticMessages(errorInfo, ts.Diagnostics.Interface_0_cannot_simultaneously_extend_types_1_and_2, typeToString(type), typeName1, typeName2); addDiagnostic(ts.createDiagnosticForNodeFromMessageChain(typeNode, errorInfo, program.getCompilerHost().getNewLine())); } } @@ -11804,7 +11809,7 @@ var ts; return isOptionalProperty(sourceProp) === isOptionalProperty(targetProp) && relate(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors); } } - function checkTypeRelatedTo(source, target, relation, errorNode, chainedMessage, terminalMessage) { + function checkTypeRelatedTo(source, target, relation, errorNode, headMessage, containingMessageChain) { var errorInfo; var sourceStack; var targetStack; @@ -11812,11 +11817,14 @@ var ts; var depth = 0; var overflow = false; ts.Debug.assert(relation !== identityRelation || !errorNode, "no error reporting in identity checking"); - var result = isRelatedToWithCustomErrors(source, target, errorNode !== undefined, chainedMessage, terminalMessage); + var result = isRelatedToWithCustomErrors(source, target, errorNode !== undefined, headMessage); if (overflow) { error(errorNode, ts.Diagnostics.Excessive_stack_depth_comparing_types_0_and_1, typeToString(source), typeToString(target)); } else if (errorInfo) { + if (containingMessageChain) { + errorInfo = ts.concatenateDiagnosticMessageChains(containingMessageChain, errorInfo); + } addDiagnostic(ts.createDiagnosticForNodeFromMessageChain(errorNode, errorInfo, program.getCompilerHost().getNewLine())); } return result; @@ -11824,9 +11832,9 @@ var ts; errorInfo = ts.chainDiagnosticMessages(errorInfo, message, arg0, arg1, arg2); } function isRelatedTo(source, target, reportErrors) { - return isRelatedToWithCustomErrors(source, target, reportErrors, undefined, undefined); + return isRelatedToWithCustomErrors(source, target, reportErrors, undefined); } - function isRelatedToWithCustomErrors(source, target, reportErrors, chainedMessage, terminalMessage) { + function isRelatedToWithCustomErrors(source, target, reportErrors, headMessage) { if (relation === identityRelation) { if (source === target) return true; @@ -11881,11 +11889,9 @@ var ts; } } if (reportErrors) { - chainedMessage = chainedMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1_Colon; - terminalMessage = terminalMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1; - var diagnosticKey = errorInfo ? chainedMessage : terminalMessage; - ts.Debug.assert(diagnosticKey); - reportError(diagnosticKey, typeToString(source), typeToString(target)); + headMessage = headMessage || ts.Diagnostics.Type_0_is_not_assignable_to_type_1; + ts.Debug.assert(headMessage); + reportError(headMessage, typeToString(source), typeToString(target)); } return false; } @@ -12044,7 +12050,7 @@ var ts; } if (!isRelatedTo(getTypeOfSymbol(sourceProp), getTypeOfSymbol(targetProp), reportErrors)) { if (reportErrors) { - reportError(ts.Diagnostics.Types_of_property_0_are_incompatible_Colon, symbolToString(targetProp)); + reportError(ts.Diagnostics.Types_of_property_0_are_incompatible, symbolToString(targetProp)); } return false; } @@ -12137,7 +12143,7 @@ var ts; if (!isRelatedTo(s, t, reportErrors)) { if (!isRelatedTo(t, s, false)) { if (reportErrors) { - reportError(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible_Colon, source.parameters[i < sourceMax ? i : sourceMax].name, target.parameters[i < targetMax ? i : targetMax].name); + reportError(ts.Diagnostics.Types_of_parameters_0_and_1_are_incompatible, source.parameters[i < sourceMax ? i : sourceMax].name, target.parameters[i < targetMax ? i : targetMax].name); } return false; } @@ -12178,7 +12184,7 @@ var ts; } if (!isRelatedTo(sourceType, targetType, reportErrors)) { if (reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible_Colon); + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } return false; } @@ -12207,7 +12213,7 @@ var ts; } if (!compatible) { if (reportErrors) { - reportError(ts.Diagnostics.Index_signatures_are_incompatible_Colon); + reportError(ts.Diagnostics.Index_signatures_are_incompatible); } return false; } @@ -12261,6 +12267,32 @@ var ts; function getCommonSupertype(types) { return ts.forEach(types, function (t) { return isSupertypeOfEach(t, types) ? t : undefined; }); } + function reportNoCommonSupertypeError(types, errorLocation, errorMessageChainHead) { + var bestSupertype; + var bestSupertypeDownfallType; + var bestSupertypeScore = 0; + for (var i = 0; i < types.length; i++) { + var score = 0; + var downfallType = undefined; + for (var j = 0; j < types.length; j++) { + if (isTypeSubtypeOf(types[j], types[i])) { + score++; + } + else if (!downfallType) { + downfallType = types[j]; + } + } + if (score > bestSupertypeScore) { + bestSupertype = types[i]; + bestSupertypeDownfallType = downfallType; + bestSupertypeScore = score; + } + if (bestSupertypeScore === types.length - 1) { + break; + } + } + checkTypeSubtypeOf(bestSupertypeDownfallType, bestSupertype, errorLocation, ts.Diagnostics.Type_argument_candidate_1_is_not_a_valid_type_argument_because_it_is_not_a_supertype_of_candidate_0, errorMessageChainHead); + } function isTypeOfObjectLiteral(type) { return (type.flags & 32768 /* Anonymous */) && type.symbol && (type.symbol.flags & 2048 /* ObjectLiteral */) ? true : false; } @@ -12499,27 +12531,28 @@ var ts; } } function getInferredType(context, index) { - var result = context.inferredTypes[index]; - if (!result) { + var inferredType = context.inferredTypes[index]; + if (!inferredType) { var inferences = context.inferences[index]; if (inferences.length) { var unionOrSuperType = context.inferUnionTypes ? getUnionType(inferences) : getCommonSupertype(inferences); - var inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : undefinedType; + inferredType = unionOrSuperType ? getWidenedType(unionOrSuperType) : inferenceFailureType; } else { inferredType = emptyObjectType; } - var constraint = getConstraintOfTypeParameter(context.typeParameters[index]); - var result = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType; - context.inferredTypes[index] = result; + if (inferredType !== inferenceFailureType) { + var constraint = getConstraintOfTypeParameter(context.typeParameters[index]); + inferredType = constraint && !isTypeAssignableTo(inferredType, constraint) ? constraint : inferredType; + } + context.inferredTypes[index] = inferredType; } - return result; + return inferredType; } function getInferredTypes(context) { for (var i = 0; i < context.inferredTypes.length; i++) { getInferredType(context, i); } - context.inferences = undefined; return context.inferredTypes; } function hasAncestor(node, kind) { @@ -12528,7 +12561,7 @@ var ts; function getResolvedSymbol(node) { var links = getNodeLinks(node); if (!links.resolvedSymbol) { - links.resolvedSymbol = resolveName(node, node.text, ts.SymbolFlags.Value | 2097152 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, ts.identifierToString(node)) || unknownSymbol; + links.resolvedSymbol = resolveName(node, node.text, ts.SymbolFlags.Value | 2097152 /* ExportValue */, ts.Diagnostics.Cannot_find_name_0, node) || unknownSymbol; } return links.resolvedSymbol; } @@ -13336,21 +13369,29 @@ var ts; } } var inferredTypes = getInferredTypes(context); - return ts.contains(inferredTypes, undefinedType) ? undefined : inferredTypes; + context.failedTypeParameterIndex = ts.indexOf(inferredTypes, inferenceFailureType); + for (var i = 0; i < inferredTypes.length; i++) { + if (inferredTypes[i] === inferenceFailureType) { + inferredTypes[i] = unknownType; + } + } + return context; } - function checkTypeArguments(signature, typeArguments) { + function checkTypeArguments(signature, typeArguments, typeArgumentResultTypes, reportErrors) { var typeParameters = signature.typeParameters; - var result = []; + var typeArgumentsAreAssignable = true; for (var i = 0; i < typeParameters.length; i++) { var typeArgNode = typeArguments[i]; var typeArgument = getTypeFromTypeNode(typeArgNode); - var constraint = getConstraintOfTypeParameter(typeParameters[i]); - if (constraint && fullTypeCheck) { - checkTypeAssignableTo(typeArgument, constraint, typeArgNode, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1_Colon, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + typeArgumentResultTypes[i] = typeArgument; + if (typeArgumentsAreAssignable) { + var constraint = getConstraintOfTypeParameter(typeParameters[i]); + if (constraint) { + typeArgumentsAreAssignable = checkTypeAssignableTo(typeArgument, constraint, reportErrors ? typeArgNode : undefined, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + } } - result.push(typeArgument); } - return result; + return typeArgumentsAreAssignable; } function checkApplicableSignature(node, signature, relation, excludeArgument, reportErrors) { if (node.arguments) { @@ -13361,7 +13402,7 @@ var ts; } var paramType = getTypeAtPosition(signature, i); var argType = arg.kind === 7 /* StringLiteral */ && !reportErrors ? getStringLiteralType(arg) : checkExpressionWithContextualType(arg, paramType, excludeArgument && excludeArgument[i] ? identityMapper : undefined); - var isValidArgument = checkTypeRelatedTo(argType, paramType, relation, reportErrors ? arg : undefined, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1); + var isValidArgument = checkTypeRelatedTo(argType, paramType, relation, reportErrors ? arg : undefined, ts.Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1); if (!isValidArgument) { return false; } @@ -13386,23 +13427,74 @@ var ts; excludeArgument[i] = true; } } - var relation = candidates.length === 1 ? assignableRelation : subtypeRelation; - var lastCandidate; - while (true) { + var candidateForArgumentError; + var candidateForTypeArgumentError; + var resultOfFailedInference; + var result; + if (candidates.length > 1) { + result = chooseOverload(candidates, subtypeRelation, excludeArgument); + } + if (!result) { + candidateForArgumentError = undefined; + candidateForTypeArgumentError = undefined; + resultOfFailedInference = undefined; + result = chooseOverload(candidates, assignableRelation, excludeArgument); + } + if (result) { + return result; + } + if (candidateForArgumentError) { + checkApplicableSignature(node, candidateForArgumentError, assignableRelation, undefined, true); + } + else if (candidateForTypeArgumentError) { + if (node.typeArguments) { + checkTypeArguments(candidateForTypeArgumentError, node.typeArguments, [], true); + } + else { + ts.Debug.assert(resultOfFailedInference.failedTypeParameterIndex >= 0); + var failedTypeParameter = candidateForTypeArgumentError.typeParameters[resultOfFailedInference.failedTypeParameterIndex]; + var inferenceCandidates = resultOfFailedInference.inferences[resultOfFailedInference.failedTypeParameterIndex]; + var diagnosticChainHead = ts.chainDiagnosticMessages(undefined, ts.Diagnostics.The_type_argument_for_type_parameter_0_cannot_be_inferred_from_the_usage_Consider_specifying_the_type_arguments_explicitly, typeToString(failedTypeParameter)); + reportNoCommonSupertypeError(inferenceCandidates, node.func, diagnosticChainHead); + } + } + else { + error(node, ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); + } + if (!fullTypeCheck) { + for (var i = 0, n = candidates.length; i < n; i++) { + if (signatureHasCorrectArity(node, candidates[i])) { + return candidates[i]; + } + } + } + return resolveErrorCall(node); + function chooseOverload(candidates, relation, excludeArgument) { for (var i = 0; i < candidates.length; i++) { if (!signatureHasCorrectArity(node, candidates[i])) { continue; } + var originalCandidate = candidates[i]; + var inferenceResult; while (true) { - var candidate = candidates[i]; + var candidate = originalCandidate; if (candidate.typeParameters) { - var typeArguments = node.typeArguments ? checkTypeArguments(candidate, node.typeArguments) : inferTypeArguments(candidate, args, excludeArgument); - if (!typeArguments) { + var typeArgumentTypes; + var typeArgumentsAreValid; + if (node.typeArguments) { + typeArgumentTypes = new Array(candidate.typeParameters.length); + typeArgumentsAreValid = checkTypeArguments(candidate, node.typeArguments, typeArgumentTypes, false); + } + else { + inferenceResult = inferTypeArguments(candidate, args, excludeArgument); + typeArgumentsAreValid = inferenceResult.failedTypeParameterIndex < 0; + typeArgumentTypes = inferenceResult.inferredTypes; + } + if (!typeArgumentsAreValid) { break; } - candidate = getSignatureInstantiation(candidate, typeArguments); + candidate = getSignatureInstantiation(candidate, typeArgumentTypes); } - lastCandidate = candidate; if (!checkApplicableSignature(node, candidate, relation, excludeArgument, false)) { break; } @@ -13412,26 +13504,25 @@ var ts; } excludeArgument[index] = false; } - } - if (relation === assignableRelation) { - break; - } - relation = assignableRelation; - } - if (lastCandidate) { - checkApplicableSignature(node, lastCandidate, relation, undefined, true); - } - else { - error(node, ts.Diagnostics.Supplied_parameters_do_not_match_any_signature_of_call_target); - } - if (!fullTypeCheck) { - for (var i = 0, n = candidates.length; i < n; i++) { - if (signatureHasCorrectArity(node, candidates[i])) { - return candidates[i]; + if (originalCandidate.typeParameters) { + var instantiatedCandidate = candidate; + if (typeArgumentsAreValid) { + candidateForArgumentError = instantiatedCandidate; + } + else { + candidateForTypeArgumentError = originalCandidate; + if (!node.typeArguments) { + resultOfFailedInference = inferenceResult; + } + } + } + else { + ts.Debug.assert(originalCandidate === candidate); + candidateForArgumentError = originalCandidate; } } + return undefined; } - return resolveErrorCall(node); function collectCandidates() { var result = candidates; var lastParent; @@ -13555,7 +13646,7 @@ var ts; if (fullTypeCheck && targetType !== unknownType) { var widenedType = getWidenedType(exprType, true); if (!(isTypeAssignableTo(targetType, widenedType))) { - checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other_Colon, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); + checkTypeAssignableTo(exprType, targetType, node, ts.Diagnostics.Neither_type_0_nor_type_1_is_assignable_to_the_other); } } return targetType; @@ -13685,7 +13776,7 @@ var ts; else { var exprType = checkExpression(node.body); if (node.type) { - checkTypeAssignableTo(exprType, getTypeFromTypeNode(node.type), node.body, undefined, undefined); + checkTypeAssignableTo(exprType, getTypeFromTypeNode(node.type), node.body, undefined); } checkFunctionExpressionBodies(node.body); } @@ -13915,7 +14006,7 @@ var ts; if (fullTypeCheck && operator >= ts.SyntaxKind.FirstAssignment && operator <= ts.SyntaxKind.LastAssignment) { var ok = checkReferenceExpression(node.left, ts.Diagnostics.Invalid_left_hand_side_of_assignment_expression, ts.Diagnostics.Left_hand_side_of_assignment_expression_cannot_be_a_constant); if (ok) { - checkTypeAssignableTo(valueType, leftType, node.left, undefined, undefined); + checkTypeAssignableTo(valueType, leftType, node.left, undefined); } } } @@ -14219,7 +14310,7 @@ var ts; var constraint = getConstraintOfTypeParameter(type.target.typeParameters[i]); if (fullTypeCheck && constraint) { var typeArgument = type.typeArguments[i]; - checkTypeAssignableTo(typeArgument, constraint, node, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1_Colon, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); + checkTypeAssignableTo(typeArgument, constraint, node, ts.Diagnostics.Type_0_does_not_satisfy_the_constraint_1); } } } @@ -14657,7 +14748,7 @@ var ts; } if (node.initializer) { if (!(getNodeLinks(node.initializer).flags & 1 /* TypeChecked */)) { - checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, undefined, undefined); + checkTypeAssignableTo(checkAndMarkExpression(node.initializer), type, node, undefined); } checkCollisionWithConstDeclarations(node); } @@ -14738,7 +14829,7 @@ var ts; var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var checkAssignability = func.type || (func.kind === 123 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(getDeclarationOfKind(func.symbol, 124 /* SetAccessor */))); if (checkAssignability) { - checkTypeAssignableTo(checkExpression(node.expression), returnType, node.expression, undefined, undefined); + checkTypeAssignableTo(checkExpression(node.expression), returnType, node.expression, undefined); } else if (func.kind == 122 /* Constructor */) { if (!isTypeAssignableTo(checkExpression(node.expression), returnType)) { @@ -14759,7 +14850,7 @@ var ts; if (fullTypeCheck && clause.expression) { var caseType = checkExpression(clause.expression); if (!isTypeAssignableTo(expressionType, caseType)) { - checkTypeAssignableTo(caseType, expressionType, clause.expression, undefined, undefined); + checkTypeAssignableTo(caseType, expressionType, clause.expression, undefined); } } checkBlock(clause); @@ -14866,9 +14957,9 @@ var ts; if (type.baseTypes.length) { if (fullTypeCheck) { var baseType = type.baseTypes[0]; - checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1_Colon, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); + checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Class_0_incorrectly_extends_base_class_1); var staticBaseType = getTypeOfSymbol(baseType.symbol); - checkTypeAssignableTo(staticType, getTypeWithoutConstructors(staticBaseType), node.name, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1_Colon, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); + checkTypeAssignableTo(staticType, getTypeWithoutConstructors(staticBaseType), node.name, ts.Diagnostics.Class_static_side_0_incorrectly_extends_base_class_static_side_1); if (baseType.symbol !== resolveEntityName(node, node.baseType.typeName, ts.SymbolFlags.Value)) { error(node.baseType, ts.Diagnostics.Type_name_0_in_extends_clause_does_not_reference_constructor_function_for_0, typeToString(baseType)); } @@ -14884,7 +14975,7 @@ var ts; if (t !== unknownType) { var declaredType = (t.flags & 4096 /* Reference */) ? t.target : t; if (declaredType.flags & (1024 /* Class */ | 2048 /* Interface */)) { - checkTypeAssignableTo(type, t, node.name, ts.Diagnostics.Class_0_incorrectly_implements_interface_1_Colon, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); + checkTypeAssignableTo(type, t, node.name, ts.Diagnostics.Class_0_incorrectly_implements_interface_1); } else { error(typeRefNode, ts.Diagnostics.A_class_may_only_implement_another_class_or_interface); @@ -14989,7 +15080,7 @@ var ts; var type = getDeclaredTypeOfSymbol(symbol); if (checkInheritedPropertiesAreIdentical(type, node.name)) { ts.forEach(type.baseTypes, function (baseType) { - checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1_Colon, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); + checkTypeAssignableTo(type, baseType, node.name, ts.Diagnostics.Interface_0_incorrectly_extends_interface_1); }); checkIndexConstraints(type); } @@ -15034,7 +15125,7 @@ var ts; if (initializer) { autoValue = getConstantValueForExpression(initializer); if (autoValue === undefined && !ambient) { - checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, undefined, undefined); + checkTypeAssignableTo(checkExpression(initializer), enumType, initializer, undefined); } } else if (ambient) { @@ -28155,9 +28246,6 @@ var ts; listItemIndex: 0 }; } - if (node.kind === 20 /* GreaterThanToken */ || node.kind === 12 /* CloseParenToken */ || node === parent.func) { - return undefined; - } return ts.findListItemInfo(node); } function getContainingArgumentInfo(node) { @@ -28282,6 +28370,9 @@ var ts; (function (ts) { function findListItemInfo(node) { var syntaxList = findContainingList(node); + if (!syntaxList) { + return undefined; + } var children = syntaxList.getChildren(); var index = ts.indexOf(children, node); return { @@ -28300,9 +28391,6 @@ var ts; return c; } }); - if (!syntaxList) { - ts.Debug.assert(findChildOfKind(node.parent, 188 /* SyntaxList */), "Node of kind " + ts.SyntaxKind[node.parent.kind] + " has no list children"); - } return syntaxList; } ts.findContainingList = findContainingList; @@ -28343,11 +28431,12 @@ var ts; var child = current.getChildAt(i); var start = allowPositionInLeadingTrivia ? child.getFullStart() : child.getStart(sourceFile); if (start <= position) { - if (position < child.getEnd()) { + var end = child.getEnd(); + if (position < end || (position === end && child.kind === 1 /* EndOfFileToken */)) { current = child; continue outer; } - else if (includeItemAtEndPosition && child.getEnd() === position) { + else if (includeItemAtEndPosition && end === position) { var previousToken = findPrecedingToken(position, sourceFile, child); if (previousToken && includeItemAtEndPosition(previousToken)) { return previousToken; @@ -28402,7 +28491,7 @@ var ts; for (var i = 0, len = children.length; i < len; ++i) { var child = children[i]; if (nodeHasTokens(child)) { - if (position < child.end) { + if (position <= child.end) { if (child.getStart(sourceFile) >= position) { var candidate = findRightmostChildNodeWithTokens(children, i); return candidate && findRightmostToken(candidate); @@ -30423,7 +30512,7 @@ var ts; SmartIndenter.getIndentation = getIndentation; function getActualIndentationForListItemBeforeComma(commaToken, sourceFile, options) { var commaItemInfo = ts.findListItemInfo(commaToken); - ts.Debug.assert(commaItemInfo.listItemIndex > 0); + ts.Debug.assert(commaItemInfo && commaItemInfo.listItemIndex > 0); return deriveActualIndentationFromList(commaItemInfo.list.getChildren(), commaItemInfo.listItemIndex - 1, sourceFile, options); } function getActualIndentationForNode(current, parent, currentLineAndChar, parentAndChildShareLine, sourceFile, options) { @@ -32078,6 +32167,9 @@ var ts; NodeObject.prototype.getFullText = function (sourceFile) { return (sourceFile || this.getSourceFile()).text.substring(this.pos, this.end); }; + NodeObject.prototype.getText = function (sourceFile) { + return (sourceFile || this.getSourceFile()).text.substring(this.getStart(), this.getEnd()); + }; NodeObject.prototype.addSyntheticNodes = function (nodes, pos, end) { scanner.setTextPos(pos); while (pos < end) { @@ -32473,15 +32565,9 @@ var ts; SourceFileObject.prototype.getPositionFromLineAndCharacter = function (line, character) { return -1; }; - SourceFileObject.prototype.getSourceUnit = function () { - return this.getSyntaxTree().sourceUnit(); - }; SourceFileObject.prototype.getScriptSnapshot = function () { return this.scriptSnapshot; }; - SourceFileObject.prototype.getLineMap = function () { - return this.getSyntaxTree().lineMap(); - }; SourceFileObject.prototype.getNamedDeclarations = function () { if (!this.namedDeclarations) { var sourceFile = this; @@ -32537,19 +32623,10 @@ var ts; } return this.namedDeclarations; }; - SourceFileObject.prototype.getSyntaxTree = function () { - if (!this.syntaxTree) { - var start = new Date().getTime(); - this.syntaxTree = TypeScript.Parser.parse(this.filename, TypeScript.SimpleText.fromScriptSnapshot(this.scriptSnapshot), this.languageVersion, this.isDeclareFile()); - var time = new Date().getTime() - start; - } - return this.syntaxTree; - }; SourceFileObject.prototype.isDeclareFile = function () { return TypeScript.isDTSFile(this.filename); }; SourceFileObject.prototype.update = function (scriptSnapshot, version, isOpen, textChangeRange) { - var oldSyntaxTree = this.syntaxTree; if (textChangeRange && ts.Debug.shouldAssert(1 /* Normal */)) { var oldText = this.scriptSnapshot; var newText = scriptSnapshot; @@ -32563,14 +32640,11 @@ var ts; TypeScript.Debug.assert(oldTextSuffix === newTextSuffix); } } - var text = TypeScript.SimpleText.fromScriptSnapshot(scriptSnapshot); - var newSyntaxTree = !textChangeRange || !oldSyntaxTree ? TypeScript.Parser.parse(this.filename, text, this.languageVersion, TypeScript.isDTSFile(this.filename)) : TypeScript.IncrementalParser.parse(oldSyntaxTree, textChangeRange, text); - return SourceFileObject.createSourceFileObject(this.filename, scriptSnapshot, this.languageVersion, version, isOpen, newSyntaxTree); + return SourceFileObject.createSourceFileObject(this.filename, scriptSnapshot, this.languageVersion, version, isOpen); }; - SourceFileObject.createSourceFileObject = function (filename, scriptSnapshot, languageVersion, version, isOpen, syntaxTree) { + SourceFileObject.createSourceFileObject = function (filename, scriptSnapshot, languageVersion, version, isOpen) { var newSourceFile = ts.createSourceFile(filename, scriptSnapshot.getText(0, scriptSnapshot.getLength()), languageVersion, version, isOpen); newSourceFile.scriptSnapshot = scriptSnapshot; - newSourceFile.syntaxTree = syntaxTree; return newSourceFile; }; return SourceFileObject; @@ -32992,22 +33066,36 @@ var ts; } SyntaxTreeCache.prototype.initialize = function (filename) { ts.Debug.assert(!!this.currentFileSyntaxTree === !!this.currentSourceFile); + var start = new Date().getTime(); this.hostCache = new HostCache(this.host); + this.host.log("SyntaxTreeCache.Initialize: new HostCache: " + (new Date().getTime() - start)); var version = this.hostCache.getVersion(filename); var syntaxTree = null; var sourceFile; if (this.currentFileSyntaxTree === null || this.currentFilename !== filename) { var scriptSnapshot = this.hostCache.getScriptSnapshot(filename); + var start = new Date().getTime(); syntaxTree = this.createSyntaxTree(filename, scriptSnapshot); + this.host.log("SyntaxTreeCache.Initialize: createSyntaxTree: " + (new Date().getTime() - start)); + var start = new Date().getTime(); sourceFile = createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, true); + this.host.log("SyntaxTreeCache.Initialize: createSourceFile: " + (new Date().getTime() - start)); + var start = new Date().getTime(); fixupParentReferences(sourceFile); + this.host.log("SyntaxTreeCache.Initialize: fixupParentRefs : " + (new Date().getTime() - start)); } else if (this.currentFileVersion !== version) { var scriptSnapshot = this.hostCache.getScriptSnapshot(filename); + var start = new Date().getTime(); syntaxTree = this.updateSyntaxTree(filename, scriptSnapshot, this.currentSourceFile.getScriptSnapshot(), this.currentFileSyntaxTree, this.currentFileVersion); + this.host.log("SyntaxTreeCache.Initialize: updateSyntaxTree: " + (new Date().getTime() - start)); var editRange = this.hostCache.getChangeRange(filename, this.currentFileVersion, this.currentSourceFile.getScriptSnapshot()); + var start = new Date().getTime(); sourceFile = !editRange ? createSourceFileFromScriptSnapshot(filename, scriptSnapshot, getDefaultCompilerOptions(), version, true) : this.currentSourceFile.update(scriptSnapshot, version, true, editRange); + this.host.log("SyntaxTreeCache.Initialize: updateSourceFile: " + (new Date().getTime() - start)); + var start = new Date().getTime(); fixupParentReferences(sourceFile); + this.host.log("SyntaxTreeCache.Initialize: fixupParentRefs : " + (new Date().getTime() - start)); } if (syntaxTree !== null) { ts.Debug.assert(sourceFile); @@ -33212,19 +33300,19 @@ var ts; return node.parent.kind === 117 /* QualifiedName */ && node.parent.right === node; } function isRightSideOfPropertyAccess(node) { - return node.parent.kind === 138 /* PropertyAccess */ && node.parent.right === node; + return node && node.parent && node.parent.kind === 138 /* PropertyAccess */ && node.parent.right === node; } function isCallExpressionTarget(node) { if (isRightSideOfPropertyAccess(node)) { node = node.parent; } - return node.parent.kind === 140 /* CallExpression */ && node.parent.func === node; + return node && node.parent && node.parent.kind === 140 /* CallExpression */ && node.parent.func === node; } function isNewExpressionTarget(node) { if (isRightSideOfPropertyAccess(node)) { node = node.parent; } - return node.parent.kind === 141 /* NewExpression */ && node.parent.func === node; + return node && node.parent && node.parent.kind === 141 /* NewExpression */ && node.parent.func === node; } function isNameOfModuleDeclaration(node) { return node.parent.kind === 181 /* ModuleDeclaration */ && node.parent.name === node; @@ -33255,6 +33343,27 @@ var ts; function isNameOfExternalModuleImportOrDeclaration(node) { return node.kind === 7 /* StringLiteral */ && (isNameOfModuleDeclaration(node) || (node.parent.kind === 183 /* ImportDeclaration */ && node.parent.externalModuleName === node)); } + function isInsideComment(sourceFile, token, position) { + return position <= token.getStart(sourceFile) && (isInsideCommentRange(ts.getTrailingCommentRanges(sourceFile.text, token.getFullStart())) || isInsideCommentRange(ts.getLeadingCommentRanges(sourceFile.text, token.getFullStart()))); + function isInsideCommentRange(comments) { + return ts.forEach(comments, function (comment) { + if (comment.pos < position && position < comment.end) { + return true; + } + else if (position === comment.end) { + var text = sourceFile.text; + var width = comment.end - comment.pos; + if (width <= 2 || text.charCodeAt(comment.pos + 1) === 47 /* slash */) { + return true; + } + else { + return !(text.charCodeAt(comment.end - 1) === 47 /* slash */ && text.charCodeAt(comment.end - 2) === 42 /* asterisk */); + } + } + return false; + }); + } + } var SemanticMeaning; (function (SemanticMeaning) { SemanticMeaning[SemanticMeaning["None"] = 0x0] = "None"; @@ -33456,193 +33565,59 @@ var ts; }; } function getCompletionsAtPosition(filename, position, isMemberCompletion) { - function getCompletionEntriesFromSymbols(symbols, session) { - ts.forEach(symbols, function (symbol) { - var entry = createCompletionEntry(symbol, session.typeChecker); - if (entry && !ts.lookUp(session.symbols, entry.name)) { - session.entries.push(entry); - session.symbols[entry.name] = symbol; - } - }); - } - function isCompletionListBlocker(sourceUnit, position) { - if (position < 0 || position > TypeScript.fullWidth(sourceUnit)) { - return true; - } - return TypeScript.Syntax.isEntirelyInsideComment(sourceUnit, position) || TypeScript.Syntax.isEntirelyInStringOrRegularExpressionLiteral(sourceUnit, position) || isIdentifierDefinitionLocation(sourceUnit, position) || isRightOfIllegalDot(sourceUnit, position); - } - function getContainingObjectLiteralApplicableForCompletion(sourceUnit, position) { - var previousToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position); - if (previousToken) { - var parent = previousToken.parent; - switch (previousToken.kind()) { - case 70 /* OpenBraceToken */: - case 79 /* CommaToken */: - if (parent && parent.kind() === 2 /* SeparatedList */) { - parent = parent.parent; - } - if (parent && parent.kind() === 216 /* ObjectLiteralExpression */) { - return parent; - } - break; - } - } - return undefined; - } - function isIdentifierDefinitionLocation(sourceUnit, position) { - var positionedToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position); - if (positionedToken) { - var containingNodeKind = TypeScript.Syntax.containingNode(positionedToken) && TypeScript.Syntax.containingNode(positionedToken).kind(); - switch (positionedToken.kind()) { - case 79 /* CommaToken */: - return containingNodeKind === 228 /* ParameterList */ || containingNodeKind === 225 /* VariableDeclaration */ || containingNodeKind === 133 /* EnumDeclaration */; - case 72 /* OpenParenToken */: - return containingNodeKind === 228 /* ParameterList */ || containingNodeKind === 237 /* CatchClause */; - case 70 /* OpenBraceToken */: - return containingNodeKind === 133 /* EnumDeclaration */; - case 57 /* PublicKeyword */: - case 55 /* PrivateKeyword */: - case 58 /* StaticKeyword */: - case 77 /* DotDotDotToken */: - return containingNodeKind === 243 /* Parameter */; - case 44 /* ClassKeyword */: - case 65 /* ModuleKeyword */: - case 46 /* EnumKeyword */: - case 52 /* InterfaceKeyword */: - case 27 /* FunctionKeyword */: - case 40 /* VarKeyword */: - case 64 /* GetKeyword */: - case 68 /* SetKeyword */: - return true; - } - switch (positionedToken.text()) { - case "class": - case "interface": - case "enum": - case "module": - return true; - } - } - return false; - } - function getNonIdentifierCompleteTokenOnLeft(sourceUnit, position) { - var positionedToken = TypeScript.Syntax.findCompleteTokenOnLeft(sourceUnit, position, true); - if (positionedToken && position === TypeScript.end(positionedToken) && positionedToken.kind() == 10 /* EndOfFileToken */) { - positionedToken = TypeScript.previousToken(positionedToken, true); - } - if (positionedToken && position === TypeScript.end(positionedToken) && positionedToken.kind() === 11 /* IdentifierName */) { - positionedToken = TypeScript.previousToken(positionedToken, true); - } - return positionedToken; - } - function isRightOfIllegalDot(sourceUnit, position) { - var positionedToken = getNonIdentifierCompleteTokenOnLeft(sourceUnit, position); - if (positionedToken) { - switch (positionedToken.kind()) { - case 76 /* DotToken */: - var leftOfDotPositionedToken = TypeScript.previousToken(positionedToken, true); - return leftOfDotPositionedToken && leftOfDotPositionedToken.kind() === 13 /* NumericLiteral */; - case 13 /* NumericLiteral */: - var text = positionedToken.text(); - return text.charAt(text.length - 1) === "."; - } - } - return false; - } - function isPunctuation(kind) { - return (ts.SyntaxKind.FirstPunctuation <= kind && kind <= ts.SyntaxKind.LastPunctuation); - } - function filterContextualMembersList(contextualMemberSymbols, existingMembers) { - if (!existingMembers || existingMembers.length === 0) { - return contextualMemberSymbols; - } - var existingMemberNames = {}; - ts.forEach(existingMembers, function (m) { - if (m.kind !== 137 /* PropertyAssignment */) { - return; - } - if (m.getStart() <= position && position <= m.getEnd()) { - return; - } - existingMemberNames[m.name.text] = true; - }); - var filteredMembers = []; - ts.forEach(contextualMemberSymbols, function (s) { - if (!existingMemberNames[s.name]) { - filteredMembers.push(s); - } - }); - return filteredMembers; - } synchronizeHostData(); filename = TypeScript.switchToForwardSlashes(filename); var sourceFile = getSourceFile(filename); - var sourceUnit = sourceFile.getSourceUnit(); - if (isCompletionListBlocker(sourceFile.getSyntaxTree().sourceUnit(), position)) { - host.log("Returning an empty list because completion was blocked."); - return null; - } - var node = TypeScript.ASTHelpers.getAstAtPosition(sourceUnit, position, true, true); - if (node && node.kind() === 11 /* IdentifierName */ && TypeScript.start(node) === TypeScript.end(node)) { - node = node.parent; - } - var isRightOfDot = false; - if (node && node.kind() === 213 /* MemberAccessExpression */ && TypeScript.end(node.expression) < position) { - isRightOfDot = true; - node = node.expression; + var currentToken = ts.getTokenAtPosition(sourceFile, position); + if (isInsideComment(sourceFile, currentToken, position)) { + host.log("Returning an empty list because completion was inside a comment."); + return undefined; } - else if (node && node.kind() === 121 /* QualifiedName */ && TypeScript.end(node.left) < position) { - isRightOfDot = true; - node = node.left; + var previousToken = ts.findPrecedingToken(position, sourceFile); + if (previousToken && position <= previousToken.end && previousToken.kind === 59 /* Identifier */) { + previousToken = ts.findPrecedingToken(previousToken.pos, sourceFile); } - else if (node && node.parent && node.kind() === 11 /* IdentifierName */ && node.parent.kind() === 213 /* MemberAccessExpression */ && node.parent.name === node) { - isRightOfDot = true; - node = node.parent.expression; + if (previousToken && isCompletionListBlocker(previousToken)) { + host.log("Returning an empty list because completion was requested in an invalid position."); + return undefined; } - else if (node && node.parent && node.kind() === 11 /* IdentifierName */ && node.parent.kind() === 121 /* QualifiedName */ && node.parent.right === node) { + var node; + var isRightOfDot; + if (previousToken && previousToken.kind === 15 /* DotToken */ && (previousToken.parent.kind === 138 /* PropertyAccess */ || previousToken.parent.kind === 117 /* QualifiedName */)) { + node = previousToken.parent.left; isRightOfDot = true; - node = node.parent.left; - } - var precedingToken = ts.findTokenOnLeftOfPosition(sourceFile, TypeScript.end(node)); - var mappedNode; - if (!precedingToken) { - mappedNode = sourceFile; - } - else if (isPunctuation(precedingToken.kind)) { - mappedNode = precedingToken.parent; } else { - mappedNode = precedingToken; + node = currentToken; + isRightOfDot = false; } - ts.Debug.assert(mappedNode, "Could not map a Fidelity node to an AST node"); activeCompletionSession = { filename: filename, position: position, entries: [], symbols: {}, - location: mappedNode, typeChecker: typeInfoResolver }; if (isRightOfDot) { var symbols = []; isMemberCompletion = true; - if (mappedNode.kind === 59 /* Identifier */ || mappedNode.kind === 117 /* QualifiedName */ || mappedNode.kind === 138 /* PropertyAccess */) { - var symbol = typeInfoResolver.getSymbolInfo(mappedNode); + if (node.kind === 59 /* Identifier */ || node.kind === 117 /* QualifiedName */ || node.kind === 138 /* PropertyAccess */) { + var symbol = typeInfoResolver.getSymbolInfo(node); if (symbol && symbol.flags & 16777216 /* Import */) { symbol = typeInfoResolver.getAliasedSymbol(symbol); } if (symbol && symbol.flags & ts.SymbolFlags.HasExports) { ts.forEachValue(symbol.exports, function (symbol) { - if (typeInfoResolver.isValidPropertyAccess((mappedNode.parent), symbol.name)) { + if (typeInfoResolver.isValidPropertyAccess((node.parent), symbol.name)) { symbols.push(symbol); } }); } } - var type = typeInfoResolver.getTypeOfNode(mappedNode); + var type = typeInfoResolver.getTypeOfNode(node); if (type) { ts.forEach(type.getApparentProperties(), function (symbol) { - if (typeInfoResolver.isValidPropertyAccess((mappedNode.parent), symbol.name)) { + if (typeInfoResolver.isValidPropertyAccess((node.parent), symbol.name)) { symbols.push(symbol); } }); @@ -33650,25 +33625,23 @@ var ts; getCompletionEntriesFromSymbols(symbols, activeCompletionSession); } else { - var containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(sourceFile.getSyntaxTree().sourceUnit(), position); + var containingObjectLiteral = getContainingObjectLiteralApplicableForCompletion(previousToken); if (containingObjectLiteral) { - var objectLiteral = (mappedNode.kind === 136 /* ObjectLiteral */ ? mappedNode : ts.getAncestor(mappedNode, 136 /* ObjectLiteral */)); - ts.Debug.assert(objectLiteral); isMemberCompletion = true; - var contextualType = typeInfoResolver.getContextualType(objectLiteral); + var contextualType = typeInfoResolver.getContextualType(containingObjectLiteral); if (!contextualType) { return undefined; } var contextualTypeMembers = typeInfoResolver.getPropertiesOfType(contextualType); if (contextualTypeMembers && contextualTypeMembers.length > 0) { - var filteredMembers = filterContextualMembersList(contextualTypeMembers, objectLiteral.properties); + var filteredMembers = filterContextualMembersList(contextualTypeMembers, containingObjectLiteral.properties); getCompletionEntriesFromSymbols(filteredMembers, activeCompletionSession); } } else { isMemberCompletion = false; var symbolMeanings = ts.SymbolFlags.Type | ts.SymbolFlags.Value | ts.SymbolFlags.Namespace | 16777216 /* Import */; - var symbols = typeInfoResolver.getSymbolsInScope(mappedNode, symbolMeanings); + var symbols = typeInfoResolver.getSymbolsInScope(node, symbolMeanings); getCompletionEntriesFromSymbols(symbols, activeCompletionSession); } } @@ -33679,9 +33652,138 @@ var ts; isMemberCompletion: isMemberCompletion, entries: activeCompletionSession.entries }; + function getCompletionEntriesFromSymbols(symbols, session) { + ts.forEach(symbols, function (symbol) { + var entry = createCompletionEntry(symbol, session.typeChecker); + if (entry && !ts.lookUp(session.symbols, entry.name)) { + session.entries.push(entry); + session.symbols[entry.name] = symbol; + } + }); + } + function isCompletionListBlocker(previousToken) { + return isInStringOrRegularExpressionLiteral(previousToken) || isIdentifierDefinitionLocation(previousToken) || isRightOfIllegalDot(previousToken); + } + function isInStringOrRegularExpressionLiteral(previousToken) { + if (previousToken.kind === 7 /* StringLiteral */) { + var start = previousToken.getStart(); + var end = previousToken.getEnd(); + if (start < position && position < end) { + return true; + } + else if (position === end) { + var width = end - start; + var text = previousToken.getSourceFile().text; + return width <= 1 || text.charCodeAt(start) !== text.charCodeAt(end - 1) || text.charCodeAt(end - 2) === 92 /* backslash */; + } + } + else if (previousToken.kind === 8 /* RegularExpressionLiteral */) { + return previousToken.getStart() < position && position < previousToken.getEnd(); + } + return false; + } + function getContainingObjectLiteralApplicableForCompletion(previousToken) { + if (previousToken) { + var parent = previousToken.parent; + switch (previousToken.kind) { + case 9 /* OpenBraceToken */: + case 18 /* CommaToken */: + if (parent && parent.kind === 136 /* ObjectLiteral */) { + return parent; + } + break; + } + } + return undefined; + } + function isFunction(kind) { + switch (kind) { + case 144 /* FunctionExpression */: + case 145 /* ArrowFunction */: + case 175 /* FunctionDeclaration */: + case 121 /* Method */: + case 122 /* Constructor */: + case 123 /* GetAccessor */: + case 124 /* SetAccessor */: + case 125 /* CallSignature */: + case 126 /* ConstructSignature */: + case 127 /* IndexSignature */: + return true; + } + return false; + } + function isIdentifierDefinitionLocation(previousToken) { + if (previousToken) { + var containingNodeKind = previousToken.parent.kind; + switch (previousToken.kind) { + case 18 /* CommaToken */: + return containingNodeKind === 174 /* VariableDeclaration */ || containingNodeKind === 152 /* VariableStatement */ || containingNodeKind === 180 /* EnumDeclaration */ || isFunction(containingNodeKind); + case 11 /* OpenParenToken */: + return containingNodeKind === 171 /* CatchBlock */ || isFunction(containingNodeKind); + case 9 /* OpenBraceToken */: + return containingNodeKind === 180 /* EnumDeclaration */ || containingNodeKind === 178 /* InterfaceDeclaration */; + case 17 /* SemicolonToken */: + return containingNodeKind === 120 /* Property */ && previousToken.parent.parent.kind === 178 /* InterfaceDeclaration */; + case 102 /* PublicKeyword */: + case 100 /* PrivateKeyword */: + case 103 /* StaticKeyword */: + case 16 /* DotDotDotToken */: + return containingNodeKind === 119 /* Parameter */; + case 63 /* ClassKeyword */: + case 110 /* ModuleKeyword */: + case 71 /* EnumKeyword */: + case 97 /* InterfaceKeyword */: + case 77 /* FunctionKeyword */: + case 92 /* VarKeyword */: + case 109 /* GetKeyword */: + case 113 /* SetKeyword */: + return true; + } + switch (previousToken.getText()) { + case "class": + case "interface": + case "enum": + case "module": + case "function": + case "var": + return true; + } + } + return false; + } + function isRightOfIllegalDot(previousToken) { + if (previousToken && previousToken.kind === 6 /* NumericLiteral */) { + var text = previousToken.getFullText(); + return text.charAt(text.length - 1) === "."; + } + return false; + } + function filterContextualMembersList(contextualMemberSymbols, existingMembers) { + if (!existingMembers || existingMembers.length === 0) { + return contextualMemberSymbols; + } + var existingMemberNames = {}; + ts.forEach(existingMembers, function (m) { + if (m.kind !== 137 /* PropertyAssignment */) { + return; + } + if (m.getStart() <= position && position <= m.getEnd()) { + return; + } + existingMemberNames[m.name.text] = true; + }); + var filteredMembers = []; + ts.forEach(contextualMemberSymbols, function (s) { + if (!existingMemberNames[s.name]) { + filteredMembers.push(s); + } + }); + return filteredMembers; + } } function getCompletionEntryDetails(filename, position, entryName) { filename = TypeScript.switchToForwardSlashes(filename); + var sourceFile = getSourceFile(filename); var session = activeCompletionSession; if (!session || session.filename !== filename || session.position !== position) { return undefined; @@ -33691,7 +33793,8 @@ var ts; var type = session.typeChecker.getTypeOfSymbol(symbol); ts.Debug.assert(type, "Could not find type for symbol"); var completionEntry = createCompletionEntry(symbol, session.typeChecker); - var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), session.location, session.typeChecker, session.location, SemanticMeaning.All); + var location = ts.getTouchingPropertyName(sourceFile, position); + var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), location, session.typeChecker, location, SemanticMeaning.All); return { name: entryName, kind: displayPartsDocumentationsAndSymbolKind.symbolKind, @@ -33871,12 +33974,20 @@ var ts; } var type = typeResolver.getTypeOfSymbol(symbol); if (type) { - if (isCallExpressionTarget(location) || isNewExpressionTarget(location)) { - var callExpression; - if (location.parent.kind === 138 /* PropertyAccess */ && location.parent.right === location) { + if (location.parent && location.parent.kind === 138 /* PropertyAccess */) { + var right = location.parent.right; + if (right === location || (right && right.kind === 116 /* Missing */)) { location = location.parent; } + } + var callExpression; + if (location.kind === 140 /* CallExpression */ || location.kind === 141 /* NewExpression */) { + callExpression = location; + } + else if (isCallExpressionTarget(location) || isNewExpressionTarget(location)) { callExpression = location.parent; + } + if (callExpression) { var candidateSignatures = []; signature = typeResolver.getResolvedSignature(callExpression, candidateSignatures); if (!signature && candidateSignatures.length) { @@ -34667,7 +34778,7 @@ var ts; if (symbol.getFlags() && (4 /* Property */ | 4096 /* Method */)) { var privateDeclaration = ts.forEach(symbol.getDeclarations(), function (d) { return (d.flags & 32 /* Private */) ? d : undefined; }); if (privateDeclaration) { - return privateDeclaration.parent; + return ts.getAncestor(privateDeclaration, 177 /* ClassDeclaration */); } } if (symbol.parent) { @@ -35546,9 +35657,14 @@ var ts; } function getIndentationAtPosition(filename, position, editorOptions) { filename = TypeScript.switchToForwardSlashes(filename); + var start = new Date().getTime(); var sourceFile = getCurrentSourceFile(filename); + host.log("getIndentationAtPosition: getCurrentSourceFile: " + (new Date().getTime() - start)); + var start = new Date().getTime(); var options = new TypeScript.FormattingOptions(!editorOptions.ConvertTabsToSpaces, editorOptions.TabSize, editorOptions.IndentSize, editorOptions.NewLineCharacter); - return ts.formatting.SmartIndenter.getIndentation(position, sourceFile, options); + var result = ts.formatting.SmartIndenter.getIndentation(position, sourceFile, options); + host.log("getIndentationAtPosition: computeIndentation : " + (new Date().getTime() - start)); + return result; } function getFormattingManager(filename, options) { if (formattingRulesProvider == null) { @@ -35603,10 +35719,7 @@ var ts; var preamble = matchArray[1]; var matchPosition = matchArray.index + preamble.length; var token = ts.getTokenAtPosition(sourceFile, matchPosition); - if (token.getStart() <= matchPosition && matchPosition < token.getEnd()) { - continue; - } - if (!getContainingComment(ts.getTrailingCommentRanges(fileContents, token.getFullStart()), matchPosition) && !getContainingComment(ts.getLeadingCommentRanges(fileContents, token.getFullStart()), matchPosition)) { + if (!isInsideComment(sourceFile, token, matchPosition)) { continue; } var descriptor = undefined; diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index f5eeca0f4cb85..647564694fe35 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -5475,31 +5475,29 @@ module ts { Debug.assert(!result.length); for (var i = 0; i < signatures.length; i++) { var signature = signatures[i]; - if (true) { - var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent = signature.declaration && signature.declaration.parent; - if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent === lastParent) { - pos++; - } - else { - lastParent = parent; - pos = cutoffPos; - } + var symbol = signature.declaration && getSymbolOfNode(signature.declaration); + var parent = signature.declaration && signature.declaration.parent; + if (!lastSymbol || symbol === lastSymbol) { + if (lastParent && parent === lastParent) { + pos++; } else { - // current declaration belongs to a different symbol - // set cutoffPos so re-orderings in the future won't change result set from 0 to cutoffPos - pos = cutoffPos = result.length; lastParent = parent; + pos = cutoffPos; } - lastSymbol = symbol; + } + else { + // current declaration belongs to a different symbol + // set cutoffPos so re-orderings in the future won't change result set from 0 to cutoffPos + pos = cutoffPos = result.length; + lastParent = parent; + } + lastSymbol = symbol; - for (var j = result.length; j > pos; j--) { - result[j] = result[j - 1]; - } - result[pos] = signature; + for (var j = result.length; j > pos; j--) { + result[j] = result[j - 1]; } + result[pos] = signature; } } } diff --git a/src/services/services.ts b/src/services/services.ts index d2e4360725da0..4d4a4f94ea58f 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1286,7 +1286,7 @@ module ts { return ""; } - interface DisplayPartsSymbolWriter extends SymbolWriter { + export interface DisplayPartsSymbolWriter extends SymbolWriter { displayParts(): SymbolDisplayPart[]; } diff --git a/src/services/syntax/scanner.ts b/src/services/syntax/scanner.ts index 29ced029f1cca..a30b8b7d4b2e1 100644 --- a/src/services/syntax/scanner.ts +++ b/src/services/syntax/scanner.ts @@ -1628,13 +1628,12 @@ module TypeScript.Scanner { var diagnostic = _tokenDiagnostics[tokenDiagnosticsLength - 1]; if (diagnostic.start() >= position) { tokenDiagnosticsLength--; + _tokenDiagnostics.pop(); } else { break; } } - - _tokenDiagnostics.length = tokenDiagnosticsLength; } function resetToPosition(absolutePosition: number): void {