From 6ef162dbbcc4e4795ff16525548676311ad1f9de Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Mon, 1 Jun 2015 17:40:44 -0700 Subject: [PATCH] Update LKG --- bin/lib.core.es6.d.ts | 13 +- bin/lib.d.ts | 16 +- bin/lib.dom.d.ts | 16 +- bin/lib.es6.d.ts | 29 +- bin/lib.webworker.d.ts | 16 +- bin/tsc.js | 2162 ++++++++++++++++++++-------- bin/tsserver.js | 2287 +++++++++++++++++++++--------- bin/typescript.d.ts | 116 +- bin/typescript.js | 2653 +++++++++++++++++++++++++---------- bin/typescriptServices.d.ts | 116 +- bin/typescriptServices.js | 2653 +++++++++++++++++++++++++---------- 11 files changed, 7336 insertions(+), 2741 deletions(-) diff --git a/bin/lib.core.es6.d.ts b/bin/lib.core.es6.d.ts index 86e2fa3ad70..1213967b972 100644 --- a/bin/lib.core.es6.d.ts +++ b/bin/lib.core.es6.d.ts @@ -1502,6 +1502,11 @@ interface Array { copyWithin(target: number, start: number, end?: number): T[]; } +interface IArguments { + /** Iterator */ + [Symbol.iterator](): IterableIterator; +} + interface ArrayConstructor { /** * Creates an array from an array-like object. @@ -1686,14 +1691,6 @@ interface GeneratorFunctionConstructor { } declare var GeneratorFunction: GeneratorFunctionConstructor; -interface Generator extends IterableIterator { - next(value?: any): IteratorResult; - throw(exception: any): IteratorResult; - return(value: T): IteratorResult; - [Symbol.iterator](): Generator; - [Symbol.toStringTag]: string; -} - interface Math { /** * Returns the number of leading zero bits in the 32-bit binary representation of a number. diff --git a/bin/lib.d.ts b/bin/lib.d.ts index e583f1ac783..7497c306161 100644 --- a/bin/lib.d.ts +++ b/bin/lib.d.ts @@ -3552,10 +3552,10 @@ declare module Intl { resolvedOptions(): ResolvedNumberFormatOptions; } var NumberFormat: { - new (locales?: string[], options?: NumberFormatOptions): Collator; - new (locale?: string, options?: NumberFormatOptions): Collator; - (locales?: string[], options?: NumberFormatOptions): Collator; - (locale?: string, options?: NumberFormatOptions): Collator; + new (locales?: string[], options?: NumberFormatOptions): NumberFormat; + new (locale?: string, options?: NumberFormatOptions): NumberFormat; + (locales?: string[], options?: NumberFormatOptions): NumberFormat; + (locale?: string, options?: NumberFormatOptions): NumberFormat; supportedLocalesOf(locales: string[], options?: NumberFormatOptions): string[]; supportedLocalesOf(locale: string, options?: NumberFormatOptions): string[]; } @@ -3597,10 +3597,10 @@ declare module Intl { resolvedOptions(): ResolvedDateTimeFormatOptions; } var DateTimeFormat: { - new (locales?: string[], options?: DateTimeFormatOptions): Collator; - new (locale?: string, options?: DateTimeFormatOptions): Collator; - (locales?: string[], options?: DateTimeFormatOptions): Collator; - (locale?: string, options?: DateTimeFormatOptions): Collator; + new (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; + (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; supportedLocalesOf(locales: string[], options?: DateTimeFormatOptions): string[]; supportedLocalesOf(locale: string, options?: DateTimeFormatOptions): string[]; } diff --git a/bin/lib.dom.d.ts b/bin/lib.dom.d.ts index 142486a1662..1df0e2f736c 100644 --- a/bin/lib.dom.d.ts +++ b/bin/lib.dom.d.ts @@ -2382,10 +2382,10 @@ declare module Intl { resolvedOptions(): ResolvedNumberFormatOptions; } var NumberFormat: { - new (locales?: string[], options?: NumberFormatOptions): Collator; - new (locale?: string, options?: NumberFormatOptions): Collator; - (locales?: string[], options?: NumberFormatOptions): Collator; - (locale?: string, options?: NumberFormatOptions): Collator; + new (locales?: string[], options?: NumberFormatOptions): NumberFormat; + new (locale?: string, options?: NumberFormatOptions): NumberFormat; + (locales?: string[], options?: NumberFormatOptions): NumberFormat; + (locale?: string, options?: NumberFormatOptions): NumberFormat; supportedLocalesOf(locales: string[], options?: NumberFormatOptions): string[]; supportedLocalesOf(locale: string, options?: NumberFormatOptions): string[]; } @@ -2427,10 +2427,10 @@ declare module Intl { resolvedOptions(): ResolvedDateTimeFormatOptions; } var DateTimeFormat: { - new (locales?: string[], options?: DateTimeFormatOptions): Collator; - new (locale?: string, options?: DateTimeFormatOptions): Collator; - (locales?: string[], options?: DateTimeFormatOptions): Collator; - (locale?: string, options?: DateTimeFormatOptions): Collator; + new (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; + (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; supportedLocalesOf(locales: string[], options?: DateTimeFormatOptions): string[]; supportedLocalesOf(locale: string, options?: DateTimeFormatOptions): string[]; } diff --git a/bin/lib.es6.d.ts b/bin/lib.es6.d.ts index d6ca7245140..b91bd34cf5d 100644 --- a/bin/lib.es6.d.ts +++ b/bin/lib.es6.d.ts @@ -1502,6 +1502,11 @@ interface Array { copyWithin(target: number, start: number, end?: number): T[]; } +interface IArguments { + /** Iterator */ + [Symbol.iterator](): IterableIterator; +} + interface ArrayConstructor { /** * Creates an array from an array-like object. @@ -1686,14 +1691,6 @@ interface GeneratorFunctionConstructor { } declare var GeneratorFunction: GeneratorFunctionConstructor; -interface Generator extends IterableIterator { - next(value?: any): IteratorResult; - throw(exception: any): IteratorResult; - return(value: T): IteratorResult; - [Symbol.iterator](): Generator; - [Symbol.toStringTag]: string; -} - interface Math { /** * Returns the number of leading zero bits in the 32-bit binary representation of a number. @@ -4933,10 +4930,10 @@ declare module Intl { resolvedOptions(): ResolvedNumberFormatOptions; } var NumberFormat: { - new (locales?: string[], options?: NumberFormatOptions): Collator; - new (locale?: string, options?: NumberFormatOptions): Collator; - (locales?: string[], options?: NumberFormatOptions): Collator; - (locale?: string, options?: NumberFormatOptions): Collator; + new (locales?: string[], options?: NumberFormatOptions): NumberFormat; + new (locale?: string, options?: NumberFormatOptions): NumberFormat; + (locales?: string[], options?: NumberFormatOptions): NumberFormat; + (locale?: string, options?: NumberFormatOptions): NumberFormat; supportedLocalesOf(locales: string[], options?: NumberFormatOptions): string[]; supportedLocalesOf(locale: string, options?: NumberFormatOptions): string[]; } @@ -4978,10 +4975,10 @@ declare module Intl { resolvedOptions(): ResolvedDateTimeFormatOptions; } var DateTimeFormat: { - new (locales?: string[], options?: DateTimeFormatOptions): Collator; - new (locale?: string, options?: DateTimeFormatOptions): Collator; - (locales?: string[], options?: DateTimeFormatOptions): Collator; - (locale?: string, options?: DateTimeFormatOptions): Collator; + new (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; + (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; supportedLocalesOf(locales: string[], options?: DateTimeFormatOptions): string[]; supportedLocalesOf(locale: string, options?: DateTimeFormatOptions): string[]; } diff --git a/bin/lib.webworker.d.ts b/bin/lib.webworker.d.ts index 1704e07f75a..79eac80a8fd 100644 --- a/bin/lib.webworker.d.ts +++ b/bin/lib.webworker.d.ts @@ -2382,10 +2382,10 @@ declare module Intl { resolvedOptions(): ResolvedNumberFormatOptions; } var NumberFormat: { - new (locales?: string[], options?: NumberFormatOptions): Collator; - new (locale?: string, options?: NumberFormatOptions): Collator; - (locales?: string[], options?: NumberFormatOptions): Collator; - (locale?: string, options?: NumberFormatOptions): Collator; + new (locales?: string[], options?: NumberFormatOptions): NumberFormat; + new (locale?: string, options?: NumberFormatOptions): NumberFormat; + (locales?: string[], options?: NumberFormatOptions): NumberFormat; + (locale?: string, options?: NumberFormatOptions): NumberFormat; supportedLocalesOf(locales: string[], options?: NumberFormatOptions): string[]; supportedLocalesOf(locale: string, options?: NumberFormatOptions): string[]; } @@ -2427,10 +2427,10 @@ declare module Intl { resolvedOptions(): ResolvedDateTimeFormatOptions; } var DateTimeFormat: { - new (locales?: string[], options?: DateTimeFormatOptions): Collator; - new (locale?: string, options?: DateTimeFormatOptions): Collator; - (locales?: string[], options?: DateTimeFormatOptions): Collator; - (locale?: string, options?: DateTimeFormatOptions): Collator; + new (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; + (locales?: string[], options?: DateTimeFormatOptions): DateTimeFormat; + (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; supportedLocalesOf(locales: string[], options?: DateTimeFormatOptions): string[]; supportedLocalesOf(locale: string, options?: DateTimeFormatOptions): string[]; } diff --git a/bin/tsc.js b/bin/tsc.js index a73a566ef56..3e08d6f7723 100644 --- a/bin/tsc.js +++ b/bin/tsc.js @@ -145,6 +145,16 @@ var ts; } } ts.addRange = addRange; + function rangeEquals(array1, array2, pos, end) { + while (pos < end) { + if (array1[pos] !== array2[pos]) { + return false; + } + pos++; + } + return true; + } + ts.rangeEquals = rangeEquals; function lastOrUndefined(array) { if (array.length === 0) { return undefined; @@ -301,8 +311,10 @@ var ts; var end = start + length; Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); - Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); - Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + if (file) { + Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); + Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + } var text = getLocaleSpecificMessage(message.key); if (arguments.length > 4) { text = formatStringFromArgs(text, arguments, 4); @@ -837,7 +849,7 @@ var ts; for (var _i = 0; _i < files.length; _i++) { var current = files[_i]; var name = ts.combinePaths(path, current); - var stat = _fs.lstatSync(name); + var stat = _fs.statSync(name); if (stat.isFile()) { if (!extension || ts.fileExtensionIs(name, extension)) { result.push(name); @@ -1039,7 +1051,7 @@ var ts; Unterminated_template_literal: { code: 1160, category: ts.DiagnosticCategory.Error, key: "Unterminated template literal." }, Unterminated_regular_expression_literal: { code: 1161, category: ts.DiagnosticCategory.Error, key: "Unterminated regular expression literal." }, An_object_member_cannot_be_declared_optional: { code: 1162, category: ts.DiagnosticCategory.Error, key: "An object member cannot be declared optional." }, - yield_expression_must_be_contained_within_a_generator_declaration: { code: 1163, category: ts.DiagnosticCategory.Error, key: "'yield' expression must be contained_within a generator declaration." }, + A_yield_expression_is_only_allowed_in_a_generator_body: { code: 1163, category: ts.DiagnosticCategory.Error, key: "A 'yield' expression is only allowed in a generator body." }, Computed_property_names_are_not_allowed_in_enums: { code: 1164, category: ts.DiagnosticCategory.Error, key: "Computed property names are not allowed in enums." }, A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol: { code: 1165, category: ts.DiagnosticCategory.Error, key: "A computed property name in an ambient context must directly refer to a built-in symbol." }, A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol: { code: 1166, category: ts.DiagnosticCategory.Error, key: "A computed property name in a class property declaration must directly refer to a built-in symbol." }, @@ -1094,6 +1106,10 @@ var ts; Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: ts.DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." }, Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: ts.DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." }, Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Specify_experimentalDecorators_to_remove_this_warning: { code: 1219, category: ts.DiagnosticCategory.Error, key: "Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning." }, + Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1220, category: ts.DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." }, + Generators_are_not_allowed_in_an_ambient_context: { code: 1221, category: ts.DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." }, + An_overload_signature_cannot_be_declared_as_a_generator: { code: 1222, category: ts.DiagnosticCategory.Error, key: "An overload signature cannot be declared as a generator." }, + _0_tag_already_specified: { code: 1223, category: ts.DiagnosticCategory.Error, key: "'{0}' tag already specified." }, Duplicate_identifier_0: { code: 2300, category: ts.DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: ts.DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: ts.DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, @@ -1254,7 +1270,7 @@ var ts; The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: ts.DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." }, Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: ts.DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." }, A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: ts.DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." }, - Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." }, + Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher." }, Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: ts.DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." }, In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: ts.DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." }, const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: ts.DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, @@ -1285,6 +1301,8 @@ var ts; A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: ts.DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." }, _0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: ts.DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." }, Cannot_find_namespace_0: { code: 2503, category: ts.DiagnosticCategory.Error, key: "Cannot find namespace '{0}'." }, + No_best_common_type_exists_among_yield_expressions: { code: 2504, category: ts.DiagnosticCategory.Error, key: "No best common type exists among yield expressions." }, + A_generator_cannot_have_a_void_type_annotation: { code: 2505, category: ts.DiagnosticCategory.Error, key: "A generator cannot have a 'void' type annotation." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: ts.DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." }, @@ -1445,6 +1463,7 @@ var ts; _0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer: { code: 7022, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer." }, _0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7023, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7024, category: ts.DiagnosticCategory.Error, key: "Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, + Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type: { code: 7025, category: ts.DiagnosticCategory.Error, key: "Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "'import ... =' can only be used in a .ts file." }, @@ -1458,16 +1477,12 @@ var ts; types_can_only_be_used_in_a_ts_file: { code: 8010, category: ts.DiagnosticCategory.Error, key: "'types' can only be used in a .ts file." }, type_arguments_can_only_be_used_in_a_ts_file: { code: 8011, category: ts.DiagnosticCategory.Error, key: "'type arguments' can only be used in a .ts file." }, parameter_modifiers_can_only_be_used_in_a_ts_file: { code: 8012, category: ts.DiagnosticCategory.Error, key: "'parameter modifiers' can only be used in a .ts file." }, - can_only_be_used_in_a_ts_file: { code: 8013, category: ts.DiagnosticCategory.Error, key: "'?' can only be used in a .ts file." }, property_declarations_can_only_be_used_in_a_ts_file: { code: 8014, category: ts.DiagnosticCategory.Error, key: "'property declarations' can only be used in a .ts file." }, enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: ts.DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." }, type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: ts.DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." }, decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: ts.DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." }, - yield_expressions_are_not_currently_supported: { code: 9000, category: ts.DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." }, - Generators_are_not_currently_supported: { code: 9001, category: ts.DiagnosticCategory.Error, key: "Generators are not currently supported." }, Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: ts.DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." }, - class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." }, - class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: ts.DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." } + class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." } }; })(ts || (ts = {})); /// @@ -3140,17 +3155,17 @@ var ts; bindBlockScopedDeclaration(node, 32, 899583); break; case 203: - bindDeclaration(node, 64, 792992, false); + bindBlockScopedDeclaration(node, 64, 792992); break; case 204: - bindDeclaration(node, 524288, 793056, false); + bindBlockScopedDeclaration(node, 524288, 793056); break; case 205: if (ts.isConst(node)) { - bindDeclaration(node, 128, 899967, false); + bindBlockScopedDeclaration(node, 128, 899967); } else { - bindDeclaration(node, 256, 899327, false); + bindBlockScopedDeclaration(node, 256, 899327); } break; case 206: @@ -3281,17 +3296,17 @@ var ts; ts.getFullWidth = getFullWidth; function containsParseError(node) { aggregateChildData(node); - return (node.parserContextFlags & 64) !== 0; + return (node.parserContextFlags & 128) !== 0; } ts.containsParseError = containsParseError; function aggregateChildData(node) { - if (!(node.parserContextFlags & 128)) { + if (!(node.parserContextFlags & 256)) { var thisNodeOrAnySubNodesHasError = ((node.parserContextFlags & 32) !== 0) || ts.forEachChild(node, containsParseError); if (thisNodeOrAnySubNodesHasError) { - node.parserContextFlags |= 64; + node.parserContextFlags |= 128; } - node.parserContextFlags |= 128; + node.parserContextFlags |= 256; } } function getSourceFileOfNode(node) { @@ -3534,6 +3549,75 @@ var ts; } ts.getJsDocComments = getJsDocComments; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; + function isTypeNode(node) { + if (142 <= node.kind && node.kind <= 150) { + return true; + } + switch (node.kind) { + case 112: + case 120: + case 122: + case 113: + case 123: + return true; + case 99: + return node.parent.kind !== 167; + case 8: + return node.parent.kind === 130; + case 177: + return true; + case 65: + if (node.parent.kind === 127 && node.parent.right === node) { + node = node.parent; + } + else if (node.parent.kind === 156 && node.parent.name === node) { + node = node.parent; + } + case 127: + case 156: + ts.Debug.assert(node.kind === 65 || node.kind === 127 || node.kind === 156, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); + var parent_1 = node.parent; + if (parent_1.kind === 145) { + return false; + } + if (142 <= parent_1.kind && parent_1.kind <= 150) { + return true; + } + switch (parent_1.kind) { + case 177: + return true; + case 129: + return node === parent_1.constraint; + case 133: + case 132: + case 130: + case 199: + return node === parent_1.type; + case 201: + case 163: + case 164: + case 136: + case 135: + case 134: + case 137: + case 138: + return node === parent_1.type; + case 139: + case 140: + case 141: + return node === parent_1.type; + case 161: + return node === parent_1.type; + case 158: + case 159: + return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + case 160: + return false; + } + } + return false; + } + ts.isTypeNode = isTypeNode; function forEachReturnStatement(body, visitor) { return traverse(body); function traverse(node) { @@ -3560,6 +3644,37 @@ var ts; } } ts.forEachReturnStatement = forEachReturnStatement; + function forEachYieldExpression(body, visitor) { + return traverse(body); + function traverse(node) { + switch (node.kind) { + case 173: + visitor(node); + var operand = node.expression; + if (operand) { + traverse(operand); + } + case 205: + case 203: + case 206: + case 204: + case 202: + return; + default: + if (isFunctionLike(node)) { + var name_3 = node.name; + if (name_3 && name_3.kind === 128) { + traverse(name_3.expression); + return; + } + } + else if (!isTypeNode(node)) { + ts.forEachChild(node, traverse); + } + } + } + } + ts.forEachYieldExpression = forEachYieldExpression; function isVariableLike(node) { if (node) { switch (node.kind) { @@ -3588,6 +3703,12 @@ var ts; return false; } ts.isAccessor = isAccessor; + function isClassLike(node) { + if (node) { + return node.kind === 202 || node.kind === 175; + } + } + ts.isClassLike = isClassLike; function isFunctionLike(node) { if (node) { switch (node.kind) { @@ -3804,6 +3925,7 @@ var ts; case 172: case 10: case 176: + case 173: return true; case 127: while (node.parent.kind === 127) { @@ -3816,8 +3938,8 @@ var ts; } case 7: case 8: - var parent_1 = node.parent; - switch (parent_1.kind) { + var parent_2 = node.parent; + switch (parent_2.kind) { case 199: case 130: case 133: @@ -3825,7 +3947,7 @@ var ts; case 227: case 225: case 153: - return parent_1.initializer === node; + return parent_2.initializer === node; case 183: case 184: case 185: @@ -3836,27 +3958,27 @@ var ts; case 221: case 196: case 194: - return parent_1.expression === node; + return parent_2.expression === node; case 187: - var forStatement = parent_1; + var forStatement = parent_2; return (forStatement.initializer === node && forStatement.initializer.kind !== 200) || forStatement.condition === node || forStatement.incrementor === node; case 188: case 189: - var forInStatement = parent_1; + var forInStatement = parent_2; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 200) || forInStatement.expression === node; case 161: - return node === parent_1.expression; + return node === parent_2.expression; case 178: - return node === parent_1.expression; + return node === parent_2.expression; case 128: - return node === parent_1.expression; + return node === parent_2.expression; case 131: return true; default: - if (isExpression(parent_1)) { + if (isExpression(parent_2)) { return true; } } @@ -3924,6 +4046,72 @@ var ts; return s.parameters.length > 0 && ts.lastOrUndefined(s.parameters).dotDotDotToken !== undefined; } ts.hasRestParameters = hasRestParameters; + function isJSDocConstructSignature(node) { + return node.kind === 241 && + node.parameters.length > 0 && + node.parameters[0].type.kind === 243; + } + ts.isJSDocConstructSignature = isJSDocConstructSignature; + function getJSDocTag(node, kind) { + if (node && node.jsDocComment) { + for (var _i = 0, _a = node.jsDocComment.tags; _i < _a.length; _i++) { + var tag = _a[_i]; + if (tag.kind === kind) { + return tag; + } + } + } + } + function getJSDocTypeTag(node) { + return getJSDocTag(node, 249); + } + ts.getJSDocTypeTag = getJSDocTypeTag; + function getJSDocReturnTag(node) { + return getJSDocTag(node, 248); + } + ts.getJSDocReturnTag = getJSDocReturnTag; + function getJSDocTemplateTag(node) { + return getJSDocTag(node, 250); + } + ts.getJSDocTemplateTag = getJSDocTemplateTag; + function getCorrespondingJSDocParameterTag(parameter) { + if (parameter.name && parameter.name.kind === 65) { + var parameterName = parameter.name.text; + var docComment = parameter.parent.jsDocComment; + if (docComment) { + return ts.forEach(docComment.tags, function (t) { + if (t.kind === 247) { + var parameterTag = t; + var name_4 = parameterTag.preParameterName || parameterTag.postParameterName; + if (name_4.text === parameterName) { + return t; + } + } + }); + } + } + } + ts.getCorrespondingJSDocParameterTag = getCorrespondingJSDocParameterTag; + function hasRestParameter(s) { + return isRestParameter(ts.lastOrUndefined(s.parameters)); + } + ts.hasRestParameter = hasRestParameter; + function isRestParameter(node) { + if (node) { + if (node.parserContextFlags & 64) { + if (node.type && node.type.kind === 242) { + return true; + } + var paramTag = getCorrespondingJSDocParameterTag(node); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 242; + } + } + return node.dotDotDotToken !== undefined; + } + return false; + } + ts.isRestParameter = isRestParameter; function isLiteralKind(kind) { return 7 <= kind && kind <= 10; } @@ -4647,6 +4835,10 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 256) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; + function isJavaScript(fileName) { + return ts.fileExtensionIs(fileName, ".js"); + } + ts.isJavaScript = isJavaScript; function getExpandedCharCodes(input) { var output = []; var length = input.length; @@ -4817,12 +5009,23 @@ var ts; return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), newEndN - oldStartN); } ts.collapseTextChangeRangesAcrossMultipleVersions = collapseTextChangeRangesAcrossMultipleVersions; + function getTypeParameterOwner(d) { + if (d && d.kind === 129) { + for (var current = d; current; current = current.parent) { + if (ts.isFunctionLike(current) || ts.isClassLike(current) || current.kind === 203) { + return current; + } + } + } + } + ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); /// /// var ts; (function (ts) { - var nodeConstructors = new Array(230); + ts.throwOnJSDocErrors = false; + var nodeConstructors = new Array(252); ts.parseTime = 0; function getNodeConstructor(kind) { return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); @@ -5125,6 +5328,49 @@ var ts; return visitNode(cbNode, node.expression); case 219: return visitNodes(cbNodes, node.decorators); + case 229: + return visitNode(cbNode, node.type); + case 233: + return visitNodes(cbNodes, node.types); + case 234: + return visitNodes(cbNodes, node.types); + case 232: + return visitNode(cbNode, node.elementType); + case 236: + return visitNode(cbNode, node.type); + case 235: + return visitNode(cbNode, node.type); + case 237: + return visitNodes(cbNodes, node.members); + case 239: + return visitNode(cbNode, node.name) || + visitNodes(cbNodes, node.typeArguments); + case 240: + return visitNode(cbNode, node.type); + case 241: + return visitNodes(cbNodes, node.parameters) || + visitNode(cbNode, node.type); + case 242: + return visitNode(cbNode, node.type); + case 243: + return visitNode(cbNode, node.type); + case 244: + return visitNode(cbNode, node.type); + case 238: + return visitNode(cbNode, node.name) || + visitNode(cbNode, node.type); + case 245: + return visitNodes(cbNodes, node.tags); + case 247: + return visitNode(cbNode, node.preParameterName) || + visitNode(cbNode, node.typeExpression) || + visitNode(cbNode, node.postParameterName); + case 248: + return visitNode(cbNode, node.typeExpression); + case 249: + return visitNode(cbNode, node.typeExpression); + case 250: + return visitNodes(cbNodes, node.typeParameters); } } ts.forEachChild = forEachChild; @@ -5140,11 +5386,20 @@ var ts; return IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks); } ts.updateSourceFile = updateSourceFile; + function parseIsolatedJSDocComment(content, start, length) { + return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + } + ts.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocTypeExpressionForTests(content, start, length) { + return Parser.JSDocParser.parseJSDocTypeExpressionForTests(content, start, length); + } + ts.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; var Parser; (function (Parser) { var scanner = ts.createScanner(2, true); var disallowInAndDecoratorContext = 2 | 16; var sourceFile; + var parseDiagnostics; var syntaxCursor; var token; var sourceText; @@ -5152,21 +5407,40 @@ var ts; var identifiers; var identifierCount; var parsingContext; - var contextFlags = 0; + var contextFlags; var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { + initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); + clearState(); + return result; + } + Parser.parseSourceFile = parseSourceFile; + function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { sourceText = _sourceText; syntaxCursor = _syntaxCursor; + parseDiagnostics = []; parsingContext = 0; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = 0; + contextFlags = ts.isJavaScript(fileName) ? 64 : 0; parseErrorBeforeNextFinishedNode = false; - createSourceFile(fileName, languageVersion); scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); + } + function clearState() { + scanner.setText(""); + scanner.setOnError(undefined); + parseDiagnostics = undefined; + sourceFile = undefined; + identifiers = undefined; + syntaxCursor = undefined; + sourceText = undefined; + } + function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { + sourceFile = createSourceFile(fileName, languageVersion); token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0, true, parseSourceElement); @@ -5176,20 +5450,40 @@ var ts; sourceFile.nodeCount = nodeCount; sourceFile.identifierCount = identifierCount; sourceFile.identifiers = identifiers; + sourceFile.parseDiagnostics = parseDiagnostics; if (setParentNodes) { fixupParentReferences(sourceFile); } - syntaxCursor = undefined; - scanner.setText(""); - scanner.setOnError(undefined); - var result = sourceFile; - sourceFile = undefined; - identifiers = undefined; - syntaxCursor = undefined; - sourceText = undefined; - return result; + if (ts.isJavaScript(fileName)) { + addJSDocComments(); + } + return sourceFile; + } + function addJSDocComments() { + forEachChild(sourceFile, visit); + return; + function visit(node) { + switch (node.kind) { + case 181: + case 201: + case 130: + addJSDocComment(node); + } + forEachChild(node, visit); + } + } + function addJSDocComment(node) { + var comments = ts.getLeadingCommentRangesOfNode(node, sourceFile); + if (comments) { + for (var _i = 0; _i < comments.length; _i++) { + var comment = comments[_i]; + var jsDocComment = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); + if (jsDocComment) { + node.jsDocComment = jsDocComment; + } + } + } } - Parser.parseSourceFile = parseSourceFile; function fixupParentReferences(sourceFile) { // normally parent references are set during binding. However, for clients that only need // a syntax tree, and no semantic features, then the binding process is an unnecessary @@ -5208,16 +5502,17 @@ var ts; } } } + Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - sourceFile = createNode(228, 0); + var sourceFile = createNode(228, 0); sourceFile.pos = 0; sourceFile.end = sourceText.length; sourceFile.text = sourceText; - sourceFile.parseDiagnostics = []; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 2048 : 0; + return sourceFile; } function setContextFlag(val, flag) { if (val) { @@ -5318,9 +5613,9 @@ var ts; parseErrorAtPosition(start, length, message, arg0); } function parseErrorAtPosition(start, length, message, arg0) { - var lastError = ts.lastOrUndefined(sourceFile.parseDiagnostics); + var lastError = ts.lastOrUndefined(parseDiagnostics); if (!lastError || start !== lastError.start) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); } parseErrorBeforeNextFinishedNode = true; } @@ -5351,7 +5646,7 @@ var ts; } function speculationHelper(callback, isLookAhead) { var saveToken = token; - var saveParseDiagnosticsLength = sourceFile.parseDiagnostics.length; + var saveParseDiagnosticsLength = parseDiagnostics.length; var saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; var saveContextFlags = contextFlags; var result = isLookAhead @@ -5360,7 +5655,7 @@ var ts; ts.Debug.assert(saveContextFlags === contextFlags); if (!result || isLookAhead) { token = saveToken; - sourceFile.parseDiagnostics.length = saveParseDiagnosticsLength; + parseDiagnostics.length = saveParseDiagnosticsLength; parseErrorBeforeNextFinishedNode = saveParseErrorBeforeNextFinishedNode; } return result; @@ -5442,8 +5737,8 @@ var ts; node.end = pos; return node; } - function finishNode(node) { - node.end = scanner.getStartPos(); + function finishNode(node, end) { + node.end = end === undefined ? scanner.getStartPos() : end; if (contextFlags) { node.parserContextFlags = contextFlags; } @@ -5492,15 +5787,24 @@ var ts; token === 8 || token === 7; } - function parsePropertyName() { + function parsePropertyNameWorker(allowComputedPropertyNames) { if (token === 8 || token === 7) { return parseLiteralNode(true); } - if (token === 18) { + if (allowComputedPropertyNames && token === 18) { return parseComputedPropertyName(); } return parseIdentifierName(); } + function parsePropertyName() { + return parsePropertyNameWorker(true); + } + function parseSimplePropertyName() { + return parsePropertyNameWorker(false); + } + function isSimplePropertyName() { + return token === 8 || token === 7 || isIdentifierOrKeyword(); + } function parseComputedPropertyName() { var node = createNode(128); parseExpected(18); @@ -5556,10 +5860,10 @@ var ts; switch (parsingContext) { case 0: case 1: - return isSourceElement(inErrorRecovery); + return !(token === 22 && inErrorRecovery) && isStartOfModuleElement(); case 2: case 4: - return isStartOfStatement(inErrorRecovery); + return !(token === 22 && inErrorRecovery) && isStartOfStatement(); case 3: return token === 67 || token === 73; case 5: @@ -5600,6 +5904,12 @@ var ts; return isHeritageClause(); case 20: return isIdentifierOrKeyword(); + case 21: + case 22: + case 24: + return JSDocParser.isJSDocType(); + case 23: + return isSimplePropertyName(); } ts.Debug.fail("Non-exhaustive case in 'isListElement'."); } @@ -5661,6 +5971,14 @@ var ts; return token === 25 || token === 16; case 19: return token === 14 || token === 15; + case 21: + return token === 17 || token === 51 || token === 15; + case 22: + return token === 25 || token === 15; + case 24: + return token === 19 || token === 15; + case 23: + return token === 15; } } function isVariableDeclaratorListTerminator() { @@ -5676,7 +5994,7 @@ var ts; return false; } function isInSomeParsingContext() { - for (var kind = 0; kind < 21; kind++) { + for (var kind = 0; kind < 25; kind++) { if (parsingContext & (1 << kind)) { if (isListElement(kind, true) || isListTerminator(kind)) { return true; @@ -5697,7 +6015,7 @@ var ts; result.push(element); if (checkForStrictMode && !inStrictModeContext()) { if (ts.isPrologueDirective(element)) { - if (isUseStrictPrologueDirective(sourceFile, element)) { + if (isUseStrictPrologueDirective(element)) { setStrictModeContext(true); checkForStrictMode = false; } @@ -5717,9 +6035,9 @@ var ts; parsingContext = saveParsingContext; return result; } - function isUseStrictPrologueDirective(sourceFile, node) { + function isUseStrictPrologueDirective(node) { ts.Debug.assert(ts.isPrologueDirective(node)); - var nodeText = ts.getSourceTextOfNodeFromSourceFile(sourceFile, node.expression); + var nodeText = ts.getTextOfNodeFromSourceText(sourceText, node.expression); return nodeText === '"use strict"' || nodeText === "'use strict'"; } function parseListElement(parsingContext, parseElement) { @@ -5920,6 +6238,10 @@ var ts; case 18: return ts.Diagnostics.Type_expected; case 19: return ts.Diagnostics.Unexpected_token_expected; case 20: return ts.Diagnostics.Identifier_expected; + case 21: return ts.Diagnostics.Parameter_declaration_expected; + case 22: return ts.Diagnostics.Type_argument_expected; + case 24: return ts.Diagnostics.Type_expected; + case 23: return ts.Diagnostics.Property_assignment_expected; } } ; @@ -6568,11 +6890,6 @@ var ts; nextToken(); return !scanner.hasPrecedingLineBreak() && isIdentifier(); } - function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { - nextToken(); - return !scanner.hasPrecedingLineBreak() && - (isIdentifier() || token === 14 || token === 18); - } function parseYieldExpression() { var node = createNode(173); nextToken(); @@ -6681,10 +6998,11 @@ var ts; if (token === 14) { return parseFunctionBlock(false, false); } - if (isStartOfStatement(true) && - !isStartOfExpressionStatement() && + if (token !== 22 && token !== 83 && - token !== 69) { + token !== 69 && + isStartOfStatement() && + !isStartOfExpressionStatement()) { return parseFunctionBlock(false, true); } return parseAssignmentExpressionOrHigher(); @@ -7327,21 +7645,68 @@ var ts; return finishNode(expressionStatement); } } - function isStartOfStatement(inErrorRecovery) { - if (ts.isModifier(token)) { - var result = lookAhead(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return true; + function isIdentifierOrKeyword() { + return token >= 65; + } + function nextTokenIsIdentifierOrKeywordOnSameLine() { + nextToken(); + return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + } + function parseDeclarationFlags() { + while (true) { + switch (token) { + case 98: + case 104: + case 70: + case 83: + case 69: + case 77: + return 1; + case 103: + case 124: + nextToken(); + return isIdentifierOrKeyword() ? 1 : 0; + case 117: + case 118: + nextToken(); + return isIdentifierOrKeyword() || token === 8 ? 2 : 0; + case 85: + nextToken(); + return token === 8 || token === 35 || + token === 14 || isIdentifierOrKeyword() ? + 2 : 0; + case 78: + nextToken(); + if (token === 53 || token === 35 || + token === 14 || token === 73) { + return 2; + } + continue; + case 115: + case 108: + case 106: + case 107: + case 109: + nextToken(); + continue; + default: + return 0; } } + } + function getDeclarationFlags() { + return lookAhead(parseDeclarationFlags); + } + function getStatementFlags() { switch (token) { + case 52: case 22: - return !inErrorRecovery; case 14: case 98: case 104: case 83: case 69: + case 77: case 84: case 75: case 100: @@ -7356,50 +7721,67 @@ var ts; case 72: case 68: case 81: - return true; + return 1; case 70: - var isConstEnum = lookAhead(nextTokenIsEnumKeyword); - return !isConstEnum; + case 78: + case 85: + return getDeclarationFlags(); + case 115: case 103: case 117: case 118: - case 77: case 124: - if (isDeclarationStart()) { - return false; - } + return getDeclarationFlags() || 1; case 108: case 106: case 107: case 109: - if (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine)) { - return false; - } + return getDeclarationFlags() || + (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine) ? 0 : 1); default: - return isStartOfExpression(); + return isStartOfExpression() ? 1 : 0; } } - function nextTokenIsEnumKeyword() { - nextToken(); - return token === 77; + function isStartOfStatement() { + return (getStatementFlags() & 1) !== 0; } - function nextTokenIsIdentifierOrKeywordOnSameLine() { + function isStartOfModuleElement() { + return (getStatementFlags() & 3) !== 0; + } + function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { nextToken(); - return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + return !scanner.hasPrecedingLineBreak() && + (isIdentifier() || token === 14 || token === 18); + } + function isLetDeclaration() { + return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); } function parseStatement() { + return parseModuleElementOfKind(1); + } + function parseModuleElement() { + return parseModuleElementOfKind(3); + } + function parseSourceElement() { + return parseModuleElementOfKind(3); + } + function parseModuleElementOfKind(flags) { switch (token) { + case 22: + return parseEmptyStatement(); case 14: return parseBlock(false, false); case 98: - case 70: return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 104: + if (isLetDeclaration()) { + return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + } + break; case 83: return parseFunctionDeclaration(scanner.getStartPos(), undefined, undefined); case 69: return parseClassDeclaration(scanner.getStartPos(), undefined, undefined); - case 22: - return parseEmptyStatement(); case 84: return parseIfStatement(); case 75: @@ -7426,44 +7808,66 @@ var ts; return parseTryStatement(); case 72: return parseDebuggerStatement(); - case 104: - if (isLetDeclaration()) { - return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 52: + return parseDeclaration(); + case 70: + case 115: + case 77: + case 78: + case 85: + case 103: + case 117: + case 118: + case 106: + case 107: + case 108: + case 109: + case 124: + if (getDeclarationFlags() & flags) { + return parseDeclaration(); } - default: - if (ts.isModifier(token) || token === 52) { - var result = tryParse(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return result; - } - } - return parseExpressionOrLabeledStatement(); + break; } + return parseExpressionOrLabeledStatement(); } - function parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers() { - var start = scanner.getStartPos(); + function parseDeclaration() { + var fullStart = getNodePos(); var decorators = parseDecorators(); var modifiers = parseModifiers(); switch (token) { - case 70: - var nextTokenIsEnum = lookAhead(nextTokenIsEnumKeyword); - if (nextTokenIsEnum) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); - case 104: - if (!isLetDeclaration()) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); case 98: - return parseVariableStatement(start, decorators, modifiers); + case 104: + case 70: + return parseVariableStatement(fullStart, decorators, modifiers); case 83: - return parseFunctionDeclaration(start, decorators, modifiers); + return parseFunctionDeclaration(fullStart, decorators, modifiers); case 69: - return parseClassDeclaration(start, decorators, modifiers); + return parseClassDeclaration(fullStart, decorators, modifiers); + case 103: + return parseInterfaceDeclaration(fullStart, decorators, modifiers); + case 124: + return parseTypeAliasDeclaration(fullStart, decorators, modifiers); + case 77: + return parseEnumDeclaration(fullStart, decorators, modifiers); + case 117: + case 118: + return parseModuleDeclaration(fullStart, decorators, modifiers); + case 85: + return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); + case 78: + nextToken(); + return token === 73 || token === 53 ? + parseExportAssignment(fullStart, decorators, modifiers) : + parseExportDeclaration(fullStart, decorators, modifiers); + default: + if (decorators) { + var node = createMissingNode(219, true, ts.Diagnostics.Declaration_expected); + node.pos = fullStart; + node.decorators = decorators; + setModifiers(node, modifiers); + return finishNode(node); + } } - return undefined; } function parseFunctionBlockOrSemicolon(isGenerator, diagnosticMessage) { if (token !== 14 && canParseSemicolon()) { @@ -7607,7 +8011,9 @@ var ts; property.name = name; property.questionToken = questionToken; property.type = parseTypeAnnotation(); - property.initializer = allowInAnd(parseNonParameterInitializer); + property.initializer = modifiers && modifiers.flags & 128 + ? allowInAnd(parseNonParameterInitializer) + : doOutsideOfContext(4 | 2, parseNonParameterInitializer); parseSemicolon(); return finishNode(property); } @@ -7753,8 +8159,8 @@ var ts; return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers); } if (decorators) { - var name_3 = createMissingNode(65, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_3, undefined); + var name_5 = createMissingNode(65, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_5, undefined); } ts.Debug.fail("Should not have attempted to parse class member declaration."); } @@ -8061,125 +8467,6 @@ var ts; parseSemicolon(); return finishNode(node); } - function isLetDeclaration() { - return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); - } - function isDeclarationStart(followsModifier) { - switch (token) { - case 98: - case 70: - case 83: - return true; - case 104: - return isLetDeclaration(); - case 69: - case 103: - case 77: - case 124: - return lookAhead(nextTokenIsIdentifierOrKeyword); - case 85: - return lookAhead(nextTokenCanFollowImportKeyword); - case 117: - case 118: - return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral); - case 78: - return lookAhead(nextTokenCanFollowExportKeyword); - case 115: - case 108: - case 106: - case 107: - case 109: - return lookAhead(nextTokenIsDeclarationStart); - case 52: - return !followsModifier; - } - } - function isIdentifierOrKeyword() { - return token >= 65; - } - function nextTokenIsIdentifierOrKeyword() { - nextToken(); - return isIdentifierOrKeyword(); - } - function nextTokenIsIdentifierOrKeywordOrStringLiteral() { - nextToken(); - return isIdentifierOrKeyword() || token === 8; - } - function nextTokenCanFollowImportKeyword() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 || - token === 35 || token === 14; - } - function nextTokenCanFollowExportKeyword() { - nextToken(); - return token === 53 || token === 35 || - token === 14 || token === 73 || isDeclarationStart(true); - } - function nextTokenIsDeclarationStart() { - nextToken(); - return isDeclarationStart(true); - } - function nextTokenIsAsKeyword() { - return nextToken() === 111; - } - function parseDeclaration() { - var fullStart = getNodePos(); - var decorators = parseDecorators(); - var modifiers = parseModifiers(); - if (token === 78) { - nextToken(); - if (token === 73 || token === 53) { - return parseExportAssignment(fullStart, decorators, modifiers); - } - if (token === 35 || token === 14) { - return parseExportDeclaration(fullStart, decorators, modifiers); - } - } - switch (token) { - case 98: - case 104: - case 70: - return parseVariableStatement(fullStart, decorators, modifiers); - case 83: - return parseFunctionDeclaration(fullStart, decorators, modifiers); - case 69: - return parseClassDeclaration(fullStart, decorators, modifiers); - case 103: - return parseInterfaceDeclaration(fullStart, decorators, modifiers); - case 124: - return parseTypeAliasDeclaration(fullStart, decorators, modifiers); - case 77: - return parseEnumDeclaration(fullStart, decorators, modifiers); - case 117: - case 118: - return parseModuleDeclaration(fullStart, decorators, modifiers); - case 85: - return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); - default: - if (decorators) { - var node = createMissingNode(219, true, ts.Diagnostics.Declaration_expected); - node.pos = fullStart; - node.decorators = decorators; - setModifiers(node, modifiers); - return finishNode(node); - } - ts.Debug.fail("Mismatch between isDeclarationStart and parseDeclaration"); - } - } - function isSourceElement(inErrorRecovery) { - return isDeclarationStart() || isStartOfStatement(inErrorRecovery); - } - function parseSourceElement() { - return parseSourceElementOrModuleElement(); - } - function parseModuleElement() { - return parseSourceElementOrModuleElement(); - } - function parseSourceElementOrModuleElement() { - return isDeclarationStart() - ? parseDeclaration() - : parseStatement(); - } function processReferenceComments(sourceFile) { var triviaScanner = ts.createScanner(sourceFile.languageVersion, false, sourceText); var referencedFiles = []; @@ -8204,7 +8491,7 @@ var ts; referencedFiles.push(fileReference); } if (diagnosticMessage) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); } } else { @@ -8212,7 +8499,7 @@ var ts; var amdModuleNameMatchResult = amdModuleNameRegEx.exec(comment); if (amdModuleNameMatchResult) { if (amdModuleName) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); } amdModuleName = amdModuleNameMatchResult[2]; } @@ -8245,6 +8532,520 @@ var ts; : undefined; }); } + var JSDocParser; + (function (JSDocParser) { + function isJSDocType() { + switch (token) { + case 35: + case 50: + case 16: + case 18: + case 46: + case 14: + case 83: + case 21: + case 88: + case 93: + return true; + } + return isIdentifierOrKeyword(); + } + JSDocParser.isJSDocType = isJSDocType; + function parseJSDocTypeExpressionForTests(content, start, length) { + initializeState("file.js", content, 2, undefined); + var jsDocTypeExpression = parseJSDocTypeExpression(start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocTypeExpression ? { jsDocTypeExpression: jsDocTypeExpression, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; + function parseJSDocTypeExpression(start, length) { + scanner.setText(sourceText, start, length); + token = nextToken(); + var result = createNode(229); + parseExpected(14); + result.type = parseJSDocTopLevelType(); + parseExpected(15); + fixupParentReferences(result); + return finishNode(result); + } + JSDocParser.parseJSDocTypeExpression = parseJSDocTypeExpression; + function setError(message) { + parseErrorAtCurrentToken(message); + if (ts.throwOnJSDocErrors) { + throw new Error(message.key); + } + } + function parseJSDocTopLevelType() { + var type = parseJSDocType(); + if (token === 44) { + var unionType = createNode(233, type.pos); + unionType.types = parseJSDocTypeList(type); + type = finishNode(unionType); + } + if (token === 53) { + var optionalType = createNode(240, type.pos); + nextToken(); + optionalType.type = type; + type = finishNode(optionalType); + } + return type; + } + function parseJSDocType() { + var type = parseBasicTypeExpression(); + while (true) { + if (token === 18) { + var arrayType = createNode(232, type.pos); + arrayType.elementType = type; + nextToken(); + parseExpected(19); + type = finishNode(arrayType); + } + else if (token === 50) { + var nullableType = createNode(235, type.pos); + nullableType.type = type; + nextToken(); + type = finishNode(nullableType); + } + else if (token === 46) { + var nonNullableType = createNode(236, type.pos); + nonNullableType.type = type; + nextToken(); + type = finishNode(nonNullableType); + } + else { + break; + } + } + return type; + } + function parseBasicTypeExpression() { + switch (token) { + case 35: + return parseJSDocAllType(); + case 50: + return parseJSDocUnknownOrNullableType(); + case 16: + return parseJSDocUnionType(); + case 18: + return parseJSDocTupleType(); + case 46: + return parseJSDocNonNullableType(); + case 14: + return parseJSDocRecordType(); + case 83: + return parseJSDocFunctionType(); + case 21: + return parseJSDocVariadicType(); + case 88: + return parseJSDocConstructorType(); + case 93: + return parseJSDocThisType(); + case 112: + case 122: + case 120: + case 113: + case 123: + case 99: + return parseTokenNode(); + } + return parseJSDocTypeReference(); + } + function parseJSDocThisType() { + var result = createNode(244); + nextToken(); + parseExpected(51); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocConstructorType() { + var result = createNode(243); + nextToken(); + parseExpected(51); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocVariadicType() { + var result = createNode(242); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocFunctionType() { + var result = createNode(241); + nextToken(); + parseExpected(16); + result.parameters = parseDelimitedList(21, parseJSDocParameter); + checkForTrailingComma(result.parameters); + parseExpected(17); + if (token === 51) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocParameter() { + var parameter = createNode(130); + parameter.type = parseJSDocType(); + return finishNode(parameter); + } + function parseJSDocOptionalType(type) { + var result = createNode(240, type.pos); + nextToken(); + result.type = type; + return finishNode(result); + } + function parseJSDocTypeReference() { + var result = createNode(239); + result.name = parseSimplePropertyName(); + while (parseOptional(20)) { + if (token === 24) { + result.typeArguments = parseTypeArguments(); + break; + } + else { + result.name = parseQualifiedName(result.name); + } + } + return finishNode(result); + } + function parseTypeArguments() { + nextToken(); + var typeArguments = parseDelimitedList(22, parseJSDocType); + checkForTrailingComma(typeArguments); + checkForEmptyTypeArgumentList(typeArguments); + parseExpected(25); + return typeArguments; + } + function checkForEmptyTypeArgumentList(typeArguments) { + if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceText, typeArguments.end) + ">".length; + return parseErrorAtPosition(start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function parseQualifiedName(left) { + var result = createNode(127, left.pos); + result.left = left; + result.right = parseIdentifierName(); + return finishNode(result); + } + function parseJSDocRecordType() { + var result = createNode(237); + nextToken(); + result.members = parseDelimitedList(23, parseJSDocRecordMember); + checkForTrailingComma(result.members); + parseExpected(15); + return finishNode(result); + } + function parseJSDocRecordMember() { + var result = createNode(238); + result.name = parseSimplePropertyName(); + if (token === 51) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocNonNullableType() { + var result = createNode(236); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocTupleType() { + var result = createNode(234); + nextToken(); + result.types = parseDelimitedList(24, parseJSDocType); + checkForTrailingComma(result.types); + parseExpected(19); + return finishNode(result); + } + function checkForTrailingComma(list) { + if (parseDiagnostics.length === 0 && list.hasTrailingComma) { + var start = list.end - ",".length; + parseErrorAtPosition(start, ",".length, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function parseJSDocUnionType() { + var result = createNode(233); + nextToken(); + result.types = parseJSDocTypeList(parseJSDocType()); + parseExpected(17); + return finishNode(result); + } + function parseJSDocTypeList(firstType) { + ts.Debug.assert(!!firstType); + var types = []; + types.pos = firstType.pos; + types.push(firstType); + while (parseOptional(44)) { + types.push(parseJSDocType()); + } + types.end = scanner.getStartPos(); + return types; + } + function parseJSDocAllType() { + var result = createNode(230); + nextToken(); + return finishNode(result); + } + function parseJSDocUnknownOrNullableType() { + var pos = scanner.getStartPos(); + nextToken(); + if (token === 23 || + token === 15 || + token === 17 || + token === 25 || + token === 53 || + token === 44) { + var result = createNode(231, pos); + return finishNode(result); + } + else { + var result = createNode(235, pos); + result.type = parseJSDocType(); + return finishNode(result); + } + } + function parseIsolatedJSDocComment(content, start, length) { + initializeState("file.js", content, 2, undefined); + var jsDocComment = parseJSDocComment(undefined, start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocComment ? { jsDocComment: jsDocComment, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocComment(parent, start, length) { + var comment = parseJSDocCommentWorker(start, length); + if (comment) { + fixupParentReferences(comment); + comment.parent = parent; + } + return comment; + } + JSDocParser.parseJSDocComment = parseJSDocComment; + function parseJSDocCommentWorker(start, length) { + var content = sourceText; + start = start || 0; + var end = length === undefined ? content.length : start + length; + length = end - start; + ts.Debug.assert(start >= 0); + ts.Debug.assert(start <= end); + ts.Debug.assert(end <= content.length); + var tags; + var pos; + if (length >= "/** */".length) { + if (content.charCodeAt(start) === 47 && + content.charCodeAt(start + 1) === 42 && + content.charCodeAt(start + 2) === 42 && + content.charCodeAt(start + 3) !== 42) { + var canParseTag = true; + var seenAsterisk = true; + for (pos = start + "/**".length; pos < end;) { + var ch = content.charCodeAt(pos); + pos++; + if (ch === 64 && canParseTag) { + parseTag(); + canParseTag = false; + continue; + } + if (ts.isLineBreak(ch)) { + canParseTag = true; + seenAsterisk = false; + continue; + } + if (ts.isWhiteSpace(ch)) { + continue; + } + if (ch === 42) { + if (seenAsterisk) { + canParseTag = false; + } + seenAsterisk = true; + continue; + } + canParseTag = false; + } + } + } + return createJSDocComment(); + function createJSDocComment() { + if (!tags) { + return undefined; + } + var result = createNode(245, start); + result.tags = tags; + return finishNode(result, end); + } + function skipWhitespace() { + while (pos < end && ts.isWhiteSpace(content.charCodeAt(pos))) { + pos++; + } + } + function parseTag() { + ts.Debug.assert(content.charCodeAt(pos - 1) === 64); + var atToken = createNode(52, pos - 1); + atToken.end = pos; + var startPos = pos; + var tagName = scanIdentifier(); + if (!tagName) { + return; + } + var tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); + addTag(tag); + } + function handleTag(atToken, tagName) { + if (tagName) { + switch (tagName.text) { + case "param": + return handleParamTag(atToken, tagName); + case "return": + case "returns": + return handleReturnTag(atToken, tagName); + case "template": + return handleTemplateTag(atToken, tagName); + case "type": + return handleTypeTag(atToken, tagName); + } + } + return undefined; + } + function handleUnknownTag(atToken, tagName) { + var result = createNode(246, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + return finishNode(result, pos); + } + function addTag(tag) { + if (tag) { + if (!tags) { + tags = []; + tags.pos = tag.pos; + } + tags.push(tag); + tags.end = tag.end; + } + } + function tryParseTypeExpression() { + skipWhitespace(); + if (content.charCodeAt(pos) !== 123) { + return undefined; + } + var typeExpression = parseJSDocTypeExpression(pos, end - pos); + pos = typeExpression.end; + return typeExpression; + } + function handleParamTag(atToken, tagName) { + var typeExpression = tryParseTypeExpression(); + skipWhitespace(); + var name; + var isBracketed; + if (content.charCodeAt(pos) === 91) { + pos++; + skipWhitespace(); + name = scanIdentifier(); + isBracketed = true; + } + else { + name = scanIdentifier(); + } + if (!name) { + parseErrorAtPosition(pos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var preName, postName; + if (typeExpression) { + postName = name; + } + else { + preName = name; + } + if (!typeExpression) { + typeExpression = tryParseTypeExpression(); + } + var result = createNode(247, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.preParameterName = preName; + result.typeExpression = typeExpression; + result.postParameterName = postName; + result.isBracketed = isBracketed; + return finishNode(result, pos); + } + function handleReturnTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 248; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(248, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTypeTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 249; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(249, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTemplateTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 250; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var typeParameters = []; + typeParameters.pos = pos; + while (true) { + skipWhitespace(); + var startPos = pos; + var name_6 = scanIdentifier(); + if (!name_6) { + parseErrorAtPosition(startPos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var typeParameter = createNode(129, name_6.pos); + typeParameter.name = name_6; + finishNode(typeParameter, pos); + typeParameters.push(typeParameter); + skipWhitespace(); + if (content.charCodeAt(pos) !== 44) { + break; + } + pos++; + } + typeParameters.end = pos; + var result = createNode(250, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeParameters = typeParameters; + return finishNode(result, pos); + } + function scanIdentifier() { + var startPos = pos; + for (; pos < end; pos++) { + var ch = content.charCodeAt(pos); + if (pos === startPos && ts.isIdentifierStart(ch, 2)) { + continue; + } + else if (pos > startPos && ts.isIdentifierPart(ch, 2)) { + continue; + } + break; + } + if (startPos === pos) { + return undefined; + } + var result = createNode(65, startPos); + result.text = content.substring(startPos, pos); + return finishNode(result, pos); + } + } + JSDocParser.parseJSDocCommentWorker = parseJSDocCommentWorker; + })(JSDocParser = Parser.JSDocParser || (Parser.JSDocParser = {})); })(Parser || (Parser = {})); var IncrementalParser; (function (IncrementalParser) { @@ -8285,7 +9086,12 @@ var ts; if (aggressiveChecks && shouldCheckNode(node)) { var text = oldText.substring(node.pos, node.end); } - node._children = undefined; + if (node._children) { + node._children = undefined; + } + if (node.jsDocComment) { + node.jsDocComment = undefined; + } node.pos += delta; node.end += delta; if (aggressiveChecks && shouldCheckNode(node)) { @@ -8601,13 +9407,15 @@ var ts; var undefinedType = createIntrinsicType(32 | 262144, "undefined"); var nullType = createIntrinsicType(64 | 262144, "null"); var unknownType = createIntrinsicType(1, "unknown"); + var circularType = createIntrinsicType(1, "__circular__"); var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = {}; var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = 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 = {}; - var globalArraySymbol; var globalESSymbolConstructorSymbol; var globalObjectType; var globalFunctionType; @@ -8619,6 +9427,8 @@ var ts; var globalTemplateStringsArrayType; var globalESSymbolType; var globalIterableType; + var globalIteratorType; + var globalIterableIteratorType; var anyArrayType; var getGlobalClassDecoratorType; var getGlobalParameterDecoratorType; @@ -8835,7 +9645,13 @@ var ts; loop: while (location) { if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - break loop; + if (!(meaning & 793056) || + !(result.flags & (793056 & ~262144)) || + !ts.isFunctionLike(location) || + lastLocation === location.body) { + break loop; + } + result = undefined; } } switch (location.kind) { @@ -9067,15 +9883,15 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_4 = specifier.propertyName || specifier.name; - if (name_4.text) { - var symbolFromModule = getExportOfModule(targetSymbol, name_4.text); - var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_4.text); + var name_7 = specifier.propertyName || specifier.name; + if (name_7.text) { + var symbolFromModule = getExportOfModule(targetSymbol, name_7.text); + var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_7.text); var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_4, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_4)); + error(name_7, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_7)); } return symbol; } @@ -9723,17 +10539,46 @@ var ts; writeType(types[i], union ? 64 : 0); } } + function writeSymbolTypeReference(symbol, typeArguments, pos, end) { + if (!isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793056); + } + if (pos < end) { + writePunctuation(writer, 24); + writeType(typeArguments[pos++], 0); + while (pos < end) { + writePunctuation(writer, 23); + writeSpace(writer); + writeType(typeArguments[pos++], 0); + } + writePunctuation(writer, 25); + } + } function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments; if (type.target === globalArrayType && !(flags & 1)) { - writeType(type.typeArguments[0], 64); + writeType(typeArguments[0], 64); writePunctuation(writer, 18); writePunctuation(writer, 19); } else { - buildSymbolDisplay(type.target.symbol, writer, enclosingDeclaration, 793056); - writePunctuation(writer, 24); - writeTypeList(type.typeArguments, false); - writePunctuation(writer, 25); + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + var start = i; + var parent_3 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_3); + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_3, typeArguments, start, i); + writePunctuation(writer, 20); + } + } + } + writeSymbolTypeReference(type.symbol, typeArguments, i, typeArguments.length); } } function writeTupleType(type) { @@ -9912,7 +10757,7 @@ var ts; function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaraiton, flags) { var targetSymbol = getTargetSymbol(symbol); if (targetSymbol.flags & 32 || targetSymbol.flags & 64) { - buildDisplayForTypeParametersAndDelimiters(getTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); } } function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, typeStack) { @@ -10075,12 +10920,12 @@ var ts; case 201: case 205: case 209: - var parent_2 = getDeclarationContainer(node); + var parent_4 = getDeclarationContainer(node); if (!(ts.getCombinedNodeFlags(node) & 1) && - !(node.kind !== 209 && parent_2.kind !== 228 && ts.isInAmbientContext(parent_2))) { - return isGlobalSourceFile(parent_2); + !(node.kind !== 209 && parent_4.kind !== 228 && ts.isInAmbientContext(parent_4))) { + return isGlobalSourceFile(parent_4); } - return isDeclarationVisible(parent_2); + return isDeclarationVisible(parent_4); case 133: case 132: case 137: @@ -10201,12 +11046,12 @@ var ts; } var type; if (pattern.kind === 151) { - var name_5 = declaration.propertyName || declaration.name; - type = getTypeOfPropertyOfType(parentType, name_5.text) || - isNumericLiteralName(name_5.text) && getIndexTypeOfType(parentType, 1) || + var name_8 = declaration.propertyName || declaration.name; + type = getTypeOfPropertyOfType(parentType, name_8.text) || + isNumericLiteralName(name_8.text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { - error(name_5, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_5)); + error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_8)); return unknownType; } } @@ -10484,26 +11329,55 @@ var ts; return target === checkBase || ts.forEach(getBaseTypes(target), check); } } - function getTypeParametersOfClassOrInterface(symbol) { - var result; - ts.forEach(symbol.declarations, function (node) { - if (node.kind === 203 || node.kind === 202) { - var declaration = node; - if (declaration.typeParameters && declaration.typeParameters.length) { - ts.forEach(declaration.typeParameters, function (node) { - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node)); - if (!result) { - result = [tp]; - } - else if (!ts.contains(result, tp)) { - result.push(tp); - } - }); + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0; _i < declarations.length; _i++) { + var declaration = declarations[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); + } + } + return typeParameters; + } + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; + } + if (node.kind === 202 || node.kind === 201 || + node.kind === 163 || node.kind === 135 || + node.kind === 164) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - }); + } + } + function getOuterTypeParametersOfClassOrInterface(symbol) { + var kind = symbol.flags & 32 ? 202 : 203; + return appendOuterTypeParameters(undefined, ts.getDeclarationOfKind(symbol, kind)); + } + function getLocalTypeParametersOfClassOrInterface(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 203 || node.kind === 202) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } + } return result; } + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterface(symbol)); + } function getBaseTypes(type) { var typeWithBaseTypes = type; if (!typeWithBaseTypes.baseTypes) { @@ -10570,10 +11444,13 @@ var ts; if (!links.declaredType) { var kind = symbol.flags & 32 ? 1024 : 2048; var type = links.declaredType = createObjectType(kind, symbol); - var typeParameters = getTypeParametersOfClassOrInterface(symbol); - if (typeParameters) { + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterface(symbol); + if (outerTypeParameters || localTypeParameters) { type.flags |= 4096; - type.typeParameters = typeParameters; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; type.instantiations = {}; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; @@ -10749,12 +11626,12 @@ var ts; return ts.map(baseSignatures, function (baseSignature) { var signature = baseType.flags & 4096 ? getSignatureInstantiation(baseSignature, baseType.typeArguments) : cloneSignature(baseSignature); - signature.typeParameters = classType.typeParameters; + signature.typeParameters = classType.localTypeParameters; signature.resolvedReturnType = classType; return signature; }); } - return [createSignature(undefined, classType.typeParameters, emptyArray, classType, 0, false, false)]; + return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, 0, false, false)]; } function createTupleTypeMemberSymbols(memberTypes) { var members = {}; @@ -11060,7 +11937,7 @@ var ts; var links = getNodeLinks(declaration); if (!links.resolvedSignature) { var classType = declaration.kind === 136 ? getDeclaredTypeOfClassOrInterface(declaration.parent.symbol) : undefined; - var typeParameters = classType ? classType.typeParameters : + var typeParameters = classType ? classType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : undefined; var parameters = []; var hasStringLiterals = false; @@ -11238,6 +12115,9 @@ var ts; } return type.constraint === noConstraintType ? undefined : type.constraint; } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 129).parent); + } function getTypeListId(types) { switch (types.length) { case 1: @@ -11324,12 +12204,16 @@ var ts; else { type = getDeclaredTypeOfSymbol(symbol); if (type.flags & (1024 | 2048) && type.flags & 4096) { - var typeParameters = type.typeParameters; - if (node.typeArguments && node.typeArguments.length === typeParameters.length) { - type = createTypeReference(type, ts.map(node.typeArguments, getTypeFromTypeNode)); + var localTypeParameters = type.localTypeParameters; + var expectedTypeArgCount = localTypeParameters ? localTypeParameters.length : 0; + var typeArgCount = node.typeArguments ? node.typeArguments.length : 0; + if (typeArgCount === expectedTypeArgCount) { + if (typeArgCount) { + type = createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNode))); + } } else { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1), typeParameters.length); + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1), expectedTypeArgCount); type = undefined; } } @@ -11367,16 +12251,16 @@ var ts; } } if (!symbol) { - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 48128)) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } return type; } @@ -11396,12 +12280,17 @@ var ts; function getGlobalESSymbolConstructorSymbol() { return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol")); } + function createTypeFromGenericGlobalType(genericGlobalType, elementType) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, [elementType]) : emptyObjectType; + } function createIterableType(elementType) { - return globalIterableType !== emptyObjectType ? createTypeReference(globalIterableType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalIterableType, elementType); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(globalIterableIteratorType, elementType); } function createArrayType(elementType) { - var arrayType = globalArrayType || getDeclaredTypeOfSymbol(globalArraySymbol); - return arrayType !== emptyObjectType ? createTypeReference(arrayType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalArrayType, elementType); } function getTypeFromArrayTypeNode(node) { var links = getNodeLinks(node); @@ -11456,13 +12345,7 @@ var ts; } return false; } - var removeSubtypesStack = []; function removeSubtypes(types) { - var typeListId = getTypeListId(types); - if (removeSubtypesStack.lastIndexOf(typeListId) >= 0) { - return; - } - removeSubtypesStack.push(typeListId); var i = types.length; while (i > 0) { i--; @@ -11470,7 +12353,6 @@ var ts; types.splice(i, 1); } } - removeSubtypesStack.pop(); } function containsAnyType(types) { for (var _i = 0; _i < types.length; _i++) { @@ -11520,7 +12402,14 @@ var ts; } function getReducedTypeOfUnionType(type) { if (!type.reducedType) { - type.reducedType = getUnionType(type.types, false); + type.reducedType = circularType; + var reducedType = getUnionType(type.types, false); + if (type.reducedType === circularType) { + type.reducedType = reducedType; + } + } + else if (type.reducedType === circularType) { + type.reducedType = type; } return type.reducedType; } @@ -11704,6 +12593,15 @@ var ts; return result; } function instantiateAnonymousType(type, mapper) { + if (mapper.mappings) { + var cached = mapper.mappings[type.id]; + if (cached) { + return cached; + } + } + else { + mapper.mappings = {}; + } var result = createObjectType(32768, type.symbol); result.properties = instantiateList(getPropertiesOfObjectType(type), mapper, instantiateSymbol); result.members = createSymbolTable(result.properties); @@ -11715,6 +12613,7 @@ var ts; result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); + mapper.mappings[type.id] = result; return result; } function instantiateType(type, mapper) { @@ -11723,7 +12622,7 @@ var ts; return mapper(type); } if (type.flags & 32768) { - return type.symbol && type.symbol.flags & (16 | 8192 | 2048 | 4096) ? + return type.symbol && type.symbol.flags & (16 | 8192 | 32 | 2048 | 4096) ? instantiateAnonymousType(type, mapper) : type; } if (type.flags & 4096) { @@ -12900,10 +13799,10 @@ var ts; } function resolveLocation(node) { var containerNodes = []; - for (var parent_3 = node.parent; parent_3; parent_3 = parent_3.parent) { - if ((ts.isExpression(parent_3) || ts.isObjectLiteralMethod(node)) && - isContextSensitive(parent_3)) { - containerNodes.unshift(parent_3); + for (var parent_5 = node.parent; parent_5; parent_5 = parent_5.parent) { + if ((ts.isExpression(parent_5) || ts.isObjectLiteralMethod(node)) && + isContextSensitive(parent_5)) { + containerNodes.unshift(parent_5); } } ts.forEach(containerNodes, function (node) { getTypeOfNode(node); }); @@ -13308,18 +14207,36 @@ var ts; return undefined; } function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (func && !func.asteriskToken) { + return getContextualReturnType(func); + } + return undefined; + } + function getContextualTypeForYieldOperand(node) { var func = ts.getContainingFunction(node); if (func) { - if (func.type || func.kind === 136 || func.kind === 137 && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(func.symbol, 138))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - } - var signature = getContextualSignatureForFunctionLikeDeclaration(func); - if (signature) { - return getReturnTypeOfSignature(signature); + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } return undefined; } + function getContextualReturnType(functionDecl) { + if (functionDecl.type || + functionDecl.kind === 136 || + functionDecl.kind === 137 && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 138))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); + } + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); + } + return undefined; + } function getContextualTypeForArgument(callTarget, arg) { var args = getEffectiveCallArguments(callTarget); var argIndex = ts.indexOf(args, arg); @@ -13421,7 +14338,7 @@ var ts; var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) || getIndexTypeOfContextualType(type, 1) - || (languageVersion >= 2 ? checkIteratedType(type, undefined) : undefined); + || (languageVersion >= 2 ? getElementTypeOfIterable(type, undefined) : undefined); } return undefined; } @@ -13447,6 +14364,8 @@ var ts; case 164: case 192: return getContextualTypeForReturnExpression(node); + case 173: + return getContextualTypeForYieldOperand(parent); case 158: case 159: return getContextualTypeForArgument(parent, node); @@ -13481,7 +14400,9 @@ var ts; return node.kind === 163 || node.kind === 164; } function getContextualSignatureForFunctionLikeDeclaration(node) { - return isFunctionExpressionOrArrowFunction(node) ? getContextualSignature(node) : undefined; + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; } function getContextualSignature(node) { ts.Debug.assert(node.kind !== 135 || ts.isObjectLiteralMethod(node)); @@ -13556,7 +14477,7 @@ var ts; if (inDestructuringPattern && e.kind === 174) { var restArrayType = checkExpression(e.expression, contextualMapper); var restElementType = getIndexTypeOfType(restArrayType, 1) || - (languageVersion >= 2 ? checkIteratedType(restArrayType, undefined) : undefined); + (languageVersion >= 2 ? getElementTypeOfIterable(restArrayType, undefined) : undefined); if (restElementType) { elementTypes.push(restElementType); } @@ -13779,15 +14700,15 @@ var ts; return unknownType; } if (node.argumentExpression) { - var name_6 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_6 !== undefined) { - var prop = getPropertyOfType(objectType, name_6); + var name_9 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_9 !== undefined) { + var prop = getPropertyOfType(objectType, name_9); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_6, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_9, symbolToString(objectType.symbol)); return unknownType; } } @@ -13877,19 +14798,19 @@ var ts; for (var _i = 0; _i < signatures.length; _i++) { var signature = signatures[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_4 = signature.declaration && signature.declaration.parent; + var parent_6 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_4 === lastParent) { + if (lastParent && parent_6 === lastParent) { index++; } else { - lastParent = parent_4; + lastParent = parent_6; index = cutoffIndex; } } else { index = cutoffIndex = result.length; - lastParent = parent_4; + lastParent = parent_6; } lastSymbol = symbol; if (signature.hasStringLiterals) { @@ -14228,10 +15149,10 @@ var ts; return resolveCall(node, callSignatures, candidatesOutArray); } function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 2) { + if (node.arguments && languageVersion < 1) { var spreadIndex = getSpreadArgumentIndex(node.arguments); if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher); + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); } } var expressionType = checkExpression(node.expression); @@ -14357,14 +15278,37 @@ var ts; type = checkExpressionCached(func.body, contextualMapper); } else { - var types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); - if (types.length === 0) { - return voidType; + var types; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func.body, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; + } + } + else { + types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); + if (types.length === 0) { + return voidType; + } } type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); if (!type) { - error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); - return unknownType; + if (funcIsGenerator) { + error(func, ts.Diagnostics.No_best_common_type_exists_among_yield_expressions); + return createIterableIteratorType(unknownType); + } + else { + error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); + return unknownType; + } + } + if (funcIsGenerator) { + type = createIterableIteratorType(type); } } if (!contextualSignature) { @@ -14372,6 +15316,22 @@ var ts; } return getWidenedType(type); } + function checkAndAggregateYieldOperandTypes(body, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); + } + } + }); + return aggregatedTypes; + } function checkAndAggregateReturnExpressionTypes(body, contextualMapper) { var aggregatedTypes = []; ts.forEachReturnStatement(body, function (returnStatement) { @@ -14507,8 +15467,8 @@ var ts; var index = n.argumentExpression; var symbol = findSymbol(n.expression); if (symbol && index && index.kind === 8) { - var name_7 = index.text; - var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_7); + var name_10 = index.text; + var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_10); return prop && (prop.flags & 3) !== 0 && (getDeclarationFlagsFromSymbol(prop) & 8192) !== 0; } return false; @@ -14639,16 +15599,16 @@ var ts; for (var _i = 0; _i < properties.length; _i++) { var p = properties[_i]; if (p.kind === 225 || p.kind === 226) { - var name_8 = p.name; + var name_11 = p.name; var type = sourceType.flags & 1 ? sourceType : - getTypeOfPropertyOfType(sourceType, name_8.text) || - isNumericLiteralName(name_8.text) && getIndexTypeOfType(sourceType, 1) || + getTypeOfPropertyOfType(sourceType, name_11.text) || + isNumericLiteralName(name_11.text) && getIndexTypeOfType(sourceType, 1) || getIndexTypeOfType(sourceType, 0); if (type) { - checkDestructuringAssignment(p.initializer || name_8, type); + checkDestructuringAssignment(p.initializer || name_11, type); } else { - error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_8)); + error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_11)); } } else { @@ -14863,13 +15823,46 @@ var ts; error(node, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(node.operatorToken.kind), typeToString(leftType), typeToString(rightType)); } } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (current.kind === 202 || current.kind === 175) { + return true; + } + current = parent; + parent = parent.parent; + } + return false; + } function checkYieldExpression(node) { - if (!(node.parserContextFlags & 4)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration); + if (!(node.parserContextFlags & 4) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); } - else { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expressions_are_not_currently_supported); + if (node.expression) { + var func = ts.getContainingFunction(node); + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, undefined); + var expressionElementType; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, undefined); + } + } + } } + return anyType; } function checkConditionalExpression(node, contextualMapper) { checkExpression(node.condition); @@ -15016,8 +16009,7 @@ var ts; case 176: return undefinedType; case 173: - checkYieldExpression(node); - return unknownType; + return checkYieldExpression(node); } return unknownType; } @@ -15055,6 +16047,14 @@ var ts; error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); } } + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; + } + return node.kind === 135 || + node.kind === 201 || + node.kind === 163; + } function checkSignatureDeclaration(node) { if (node.kind === 141) { checkGrammarIndexSignature(node); @@ -15081,6 +16081,19 @@ var ts; break; } } + if (node.type) { + if (languageVersion >= 2 && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } + } + } } checkSpecializedSignatureDeclaration(node); } @@ -15624,7 +16637,6 @@ var ts; function checkFunctionDeclaration(node) { if (produceDiagnostics) { checkFunctionLikeDeclaration(node) || - checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarFunctionName(node.name) || checkGrammarForGenerator(node); checkCollisionWithCapturedSuperVariable(node, node.name); @@ -15656,8 +16668,13 @@ var ts; if (node.type && !isAccessor(node.kind) && !node.asteriskToken) { checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type)); } - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !node.type && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (produceDiagnostics && !node.type) { + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + } } } function checkBlock(node) { @@ -15778,8 +16795,8 @@ var ts; container.kind === 206 || container.kind === 228); if (!namesShareScope) { - var name_9 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_9, name_9); + var name_12 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_12, name_12); } } } @@ -15872,7 +16889,7 @@ var ts; return checkVariableLikeDeclaration(node); } function checkVariableStatement(node) { - checkGrammarDecorators(node) || checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); ts.forEach(node.declarationList.declarations, checkSourceElement); } function checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) { @@ -15995,7 +17012,7 @@ var ts; return inputType; } if (languageVersion >= 2) { - return checkIteratedType(inputType, errorNode) || anyType; + return checkElementTypeOfIterable(inputType, errorNode); } if (allowStringInput) { return checkElementTypeOfArrayOrString(inputType, errorNode); @@ -16009,84 +17026,85 @@ var ts; error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); return unknownType; } - function checkIteratedType(iterable, errorNode) { - ts.Debug.assert(languageVersion >= 2); - var iteratedType = getIteratedType(iterable, errorNode); - if (errorNode && iteratedType) { - checkTypeAssignableTo(iterable, createIterableType(iteratedType), errorNode); + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - return iteratedType; - function getIteratedType(iterable, errorNode) { - // We want to treat type as an iterable, and get the type it is an iterable of. The iterable - // must have the following structure (annotated with the names of the variables below): - // - // { // iterable - // [Symbol.iterator]: { // iteratorFunction - // (): { // iterator - // next: { // iteratorNextFunction - // (): { // iteratorNextResult - // value: T // iteratorNextValue - // } - // } - // } - // } - // } - // - // T is the type we are after. At every level that involves analyzing return types - // of signatures, we union the return types of all the signatures. - // - // Another thing to note is that at any step of this process, we could run into a dead end, - // meaning either the property is missing, or we run into the anyType. If either of these things - // happens, we return undefined to signal that we could not find the iterated type. If a property - // is missing, and the previous step did not result in 'any', then we also give an error if the - // caller requested it. Then the caller can decide what to do in the case where there is no iterated - // type. This is different from returning anyType, because that would signify that we have matched the - // whole pattern and that T (above) is 'any'. - if (allConstituentTypesHaveKind(iterable, 1)) { - return undefined; - } - if ((iterable.flags & 4096) && iterable.target === globalIterableType) { - return iterable.typeArguments[0]; - } - var iteratorFunction = getTypeOfPropertyOfType(iterable, ts.getPropertyNameForKnownSymbolName("iterator")); - if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, 1)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); - } - return undefined; - } - var iterator = getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iterator, 1)) { - return undefined; - } - var iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next"); - if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, 1)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); - } - return undefined; - } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iteratorNextResult, 1)) { - return undefined; - } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; - } - return iteratorNextValue; + return elementType || anyType; + } + function getElementTypeOfIterable(type, errorNode) { + if (type.flags & 1) { + return undefined; } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { + if ((type.flags & 4096) && type.target === globalIterableType) { + typeAsIterable.iterableElementType = type.typeArguments[0]; + } + else { + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (iteratorFunction && iteratorFunction.flags & 1) { + return undefined; + } + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; + } + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)), errorNode); + } + } + return typeAsIterable.iterableElementType; + } + function getElementTypeOfIterator(type, errorNode) { + if (type.flags & 1) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + if ((type.flags & 4096) && type.target === globalIteratorType) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; + } + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (iteratorNextFunction && iteratorNextFunction.flags & 1) { + return undefined; + } + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; + } + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); + if (iteratorNextResult.flags & 1) { + return undefined; + } + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; + } + } + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (type.flags & 1) { + return undefined; + } + if ((type.flags & 4096) && type.target === globalIterableIteratorType) { + return type.typeArguments[0]; + } + return getElementTypeOfIterable(type, undefined) || + getElementTypeOfIterator(type, undefined); } function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { ts.Debug.assert(languageVersion < 2); @@ -16138,19 +17156,20 @@ var ts; if (func) { var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var exprType = checkExpressionCached(node.expression); + if (func.asteriskToken) { + return; + } if (func.kind === 138) { error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); } - else { - if (func.kind === 136) { - if (!isTypeAssignableTo(exprType, returnType)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { - checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + else if (func.kind === 136) { + if (!isTypeAssignableTo(exprType, returnType)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } + else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { + checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + } } } } @@ -16344,9 +17363,6 @@ var ts; } function checkClassDeclaration(node) { checkGrammarDeclarationNameInStrictMode(node); - if (node.parent.kind !== 207 && node.parent.kind !== 228) { - grammarErrorOnNode(node, ts.Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration); - } if (!node.name && !(node.flags & 256)) { grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); } @@ -17386,74 +18402,6 @@ var ts; } return node.parent && node.parent.kind === 177; } - function isTypeNode(node) { - if (142 <= node.kind && node.kind <= 150) { - return true; - } - switch (node.kind) { - case 112: - case 120: - case 122: - case 113: - case 123: - return true; - case 99: - return node.parent.kind !== 167; - case 8: - return node.parent.kind === 130; - case 177: - return true; - case 65: - if (node.parent.kind === 127 && node.parent.right === node) { - node = node.parent; - } - else if (node.parent.kind === 156 && node.parent.name === node) { - node = node.parent; - } - case 127: - case 156: - ts.Debug.assert(node.kind === 65 || node.kind === 127 || node.kind === 156, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); - var parent_5 = node.parent; - if (parent_5.kind === 145) { - return false; - } - if (142 <= parent_5.kind && parent_5.kind <= 150) { - return true; - } - switch (parent_5.kind) { - case 177: - return true; - case 129: - return node === parent_5.constraint; - case 133: - case 132: - case 130: - case 199: - return node === parent_5.type; - case 201: - case 163: - case 164: - case 136: - case 135: - case 134: - case 137: - case 138: - return node === parent_5.type; - case 139: - case 140: - case 141: - return node === parent_5.type; - case 161: - return node === parent_5.type; - case 158: - case 159: - return parent_5.typeArguments && ts.indexOf(parent_5.typeArguments, node) >= 0; - case 160: - return false; - } - } - return false; - } function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { while (nodeOnRightSide.parent.kind === 127) { nodeOnRightSide = nodeOnRightSide.parent; @@ -17578,7 +18526,7 @@ var ts; if (isInsideWithStatementBody(node)) { return unknownType; } - if (isTypeNode(node)) { + if (ts.isTypeNode(node)) { return getTypeFromTypeNode(node); } if (ts.isExpression(node)) { @@ -17628,9 +18576,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456) { var symbols = []; - var name_10 = symbol.name; + var name_13 = symbol.name; ts.forEach(getSymbolLinks(symbol).unionType.types, function (t) { - symbols.push(getPropertyOfType(t, name_10)); + symbols.push(getPropertyOfType(t, name_13)); }); return symbols; } @@ -18008,8 +18956,7 @@ var ts; getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; globals[undefinedSymbol.name] = undefinedSymbol; - globalArraySymbol = getGlobalTypeSymbol("Array"); - globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1); + globalArrayType = getGlobalType("Array", 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); @@ -18025,11 +18972,16 @@ var ts; globalESSymbolType = getGlobalType("Symbol"); globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"); globalIterableType = getGlobalType("Iterable", 1); + globalIteratorType = getGlobalType("Iterator", 1); + globalIterableIteratorType = getGlobalType("IterableIterator", 1); } else { globalTemplateStringsArrayType = unknownType; globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); globalESSymbolConstructorSymbol = undefined; + globalIterableType = emptyGenericType; + globalIteratorType = emptyGenericType; + globalIterableIteratorType = emptyGenericType; } anyArrayType = createArrayType(anyType); } @@ -18049,20 +19001,20 @@ var ts; if (impotClause.namedBindings) { var nameBindings = impotClause.namedBindings; if (nameBindings.kind === 212) { - var name_11 = nameBindings.name; - if (isReservedWordInStrictMode(name_11)) { - var nameText = ts.declarationNameToString(name_11); - return grammarErrorOnNode(name_11, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_14 = nameBindings.name; + if (isReservedWordInStrictMode(name_14)) { + var nameText = ts.declarationNameToString(name_14); + return grammarErrorOnNode(name_14, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } else if (nameBindings.kind === 213) { var reportError = false; for (var _i = 0, _a = nameBindings.elements; _i < _a.length; _i++) { var element = _a[_i]; - var name_12 = element.name; - if (isReservedWordInStrictMode(name_12)) { - var nameText = ts.declarationNameToString(name_12); - reportError = reportError || grammarErrorOnNode(name_12, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_15 = element.name; + if (isReservedWordInStrictMode(name_15)) { + var nameText = ts.declarationNameToString(name_15); + reportError = reportError || grammarErrorOnNode(name_15, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } return reportError; @@ -18160,19 +19112,28 @@ var ts; case 135: case 134: case 141: - case 202: - case 203: case 206: - case 205: - case 181: - case 201: - case 204: case 210: case 209: case 216: case 215: case 130: break; + case 202: + case 203: + case 181: + case 201: + case 204: + if (node.modifiers && node.parent.kind !== 207 && node.parent.kind !== 228) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; + case 205: + if (node.modifiers && (node.modifiers.length > 1 || node.modifiers[0].kind !== 70) && + node.parent.kind !== 207 && node.parent.kind !== 228) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; default: return false; } @@ -18270,9 +19231,6 @@ var ts; else if ((node.kind === 210 || node.kind === 209) && flags & 2) { return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_import_declaration, "declare"); } - else if (node.kind === 203 && flags & 2) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare"); - } else if (node.kind === 130 && (flags & 112) && ts.isBindingPattern(node.name)) { return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_a_binding_pattern); } @@ -18484,7 +19442,18 @@ var ts; } function checkGrammarForGenerator(node) { if (node.asteriskToken) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_currently_supported); + ts.Debug.assert(node.kind === 201 || + node.kind === 163 || + node.kind === 135); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_6_or_higher); + } } } function checkGrammarFunctionName(name) { @@ -18504,17 +19473,17 @@ var ts; var inStrictMode = (node.parserContextFlags & 1) !== 0; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_13 = prop.name; + var name_16 = prop.name; if (prop.kind === 176 || - name_13.kind === 128) { - checkGrammarComputedPropertyName(name_13); + name_16.kind === 128) { + checkGrammarComputedPropertyName(name_16); continue; } var currentKind = void 0; if (prop.kind === 225 || prop.kind === 226) { checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_13.kind === 7) { - checkGrammarNumericLiteral(name_13); + if (name_16.kind === 7) { + checkGrammarNumericLiteral(name_16); } currentKind = Property; } @@ -18530,26 +19499,26 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - if (!ts.hasProperty(seen, name_13.text)) { - seen[name_13.text] = currentKind; + if (!ts.hasProperty(seen, name_16.text)) { + seen[name_16.text] = currentKind; } else { - var existingKind = seen[name_13.text]; + var existingKind = seen[name_16.text]; if (currentKind === Property && existingKind === Property) { if (inStrictMode) { - grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); + grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); } } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[name_13.text] = currentKind | existingKind; + seen[name_16.text] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -19328,9 +20297,9 @@ var ts; } var count = 0; while (true) { - var name_14 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_14)) { - return name_14; + var name_17 = baseName + "_" + (++count); + if (!ts.hasProperty(currentSourceFile.identifiers, name_17)) { + return name_17; } } } @@ -20414,9 +21383,9 @@ var ts; var count = tempFlags & 268435455; tempFlags++; if (count !== 8 && count !== 13) { - var name_15 = count < 26 ? "_" + String.fromCharCode(97 + count) : "_" + (count - 26); - if (isUniqueName(name_15)) { - return name_15; + var name_18 = count < 26 ? "_" + String.fromCharCode(97 + count) : "_" + (count - 26); + if (isUniqueName(name_18)) { + return name_18; } } } @@ -20444,8 +21413,8 @@ var ts; } function generateNameForModuleOrEnum(node) { if (node.name.kind === 65) { - var name_16 = node.name.text; - assignGeneratedName(node, isUniqueLocalName(name_16, node) ? name_16 : makeUniqueName(name_16)); + var name_19 = node.name.text; + assignGeneratedName(node, isUniqueLocalName(name_19, node) ? name_19 : makeUniqueName(name_19)); } } function generateNameForImportOrExportDeclaration(node) { @@ -20631,8 +21600,8 @@ var ts; if (scopeName) { var parentIndex = getSourceMapNameIndex(); if (parentIndex !== -1) { - var name_17 = node.name; - if (!name_17 || name_17.kind !== 128) { + var name_20 = node.name; + if (!name_20 || name_20.kind !== 128) { scopeName = "." + scopeName; } scopeName = sourceMapData.sourceMapNames[parentIndex] + scopeName; @@ -20659,9 +21628,9 @@ var ts; node.kind === 202 || node.kind === 205) { if (node.name) { - var name_18 = node.name; - scopeName = name_18.kind === 128 - ? ts.getTextOfNode(name_18) + var name_21 = node.name; + scopeName = name_21.kind === 128 + ? ts.getTextOfNode(name_21) : node.name.text; } recordScopeNameStart(scopeName); @@ -21282,15 +22251,15 @@ var ts; } return true; } - function emitListWithSpread(elements, alwaysCopy, multiLine, trailingComma) { + function emitListWithSpread(elements, needsUniqueCopy, multiLine, trailingComma, useConcat) { var pos = 0; var group = 0; var length = elements.length; while (pos < length) { - if (group === 1) { + if (group === 1 && useConcat) { write(".concat("); } - else if (group > 1) { + else if (group > 0) { write(", "); } var e = elements[pos]; @@ -21298,7 +22267,7 @@ var ts; e = e.expression; emitParenthesizedIf(e, group === 0 && needsParenthesisForPropertyAccessOrInvocation(e)); pos++; - if (pos === length && group === 0 && alwaysCopy && e.kind !== 154) { + if (pos === length && group === 0 && needsUniqueCopy && e.kind !== 154) { write(".slice()"); } } @@ -21321,7 +22290,9 @@ var ts; group++; } if (group > 1) { - write(")"); + if (useConcat) { + write(")"); + } } } function isSpreadElementExpression(node) { @@ -21338,7 +22309,7 @@ var ts; write("]"); } else { - emitListWithSpread(elements, true, (node.flags & 512) !== 0, elements.hasTrailingComma); + emitListWithSpread(elements, true, (node.flags & 512) !== 0, elements.hasTrailingComma, true); } } function emitObjectLiteralBody(node, numElements) { @@ -21664,7 +22635,7 @@ var ts; write("void 0"); } write(", "); - emitListWithSpread(node.arguments, false, false, false); + emitListWithSpread(node.arguments, false, false, false, true); write(")"); } function emitCallExpression(node) { @@ -21698,11 +22669,25 @@ var ts; } function emitNewExpression(node) { write("new "); - emit(node.expression); - if (node.arguments) { + if (languageVersion === 1 && + node.arguments && + hasSpreadElement(node.arguments)) { write("("); - emitCommaList(node.arguments); - write(")"); + var target = emitCallTarget(node.expression); + write(".bind.apply("); + emit(target); + write(", [void 0].concat("); + emitListWithSpread(node.arguments, false, false, false, false); + write(")))"); + write("()"); + } + else { + emit(node.expression); + if (node.arguments) { + write("("); + emitCommaList(node.arguments); + write(")"); + } } } function emitTaggedTemplateExpression(node) { @@ -22654,15 +23639,30 @@ var ts; ts.forEach(node.declarationList.declarations, emitExportVariableAssignments); } } + function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node) { + if (!(node.flags & 1)) { + return true; + } + if (isES6ExportedDeclaration(node)) { + return true; + } + for (var _a = 0, _b = node.declarationList.declarations; _a < _b.length; _a++) { + var declaration = _b[_a]; + if (declaration.initializer) { + return true; + } + } + return false; + } function emitParameter(node) { if (languageVersion < 2) { if (ts.isBindingPattern(node.name)) { - var name_19 = createTempVariable(0); + var name_22 = createTempVariable(0); if (!tempParameters) { tempParameters = []; } - tempParameters.push(name_19); - emit(name_19); + tempParameters.push(name_22); + emit(name_22); } else { emit(node.name); @@ -24122,8 +25122,8 @@ var ts; else { for (var _c = 0, _d = node.exportClause.elements; _c < _d.length; _c++) { var specifier = _d[_c]; - var name_20 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_20] || (exportSpecifiers[name_20] = [])).push(specifier); + var name_23 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_23] || (exportSpecifiers[name_23] = [])).push(specifier); } } break; @@ -24296,11 +25296,11 @@ var ts; var seen = {}; for (var i = 0; i < hoistedVars.length; ++i) { var local = hoistedVars[i]; - var name_21 = local.kind === 65 + var name_24 = local.kind === 65 ? local : local.name; - if (name_21) { - var text = ts.unescapeIdentifier(name_21.text); + if (name_24) { + var text = ts.unescapeIdentifier(name_24.text); if (ts.hasProperty(seen, text)) { continue; } @@ -24379,15 +25379,15 @@ var ts; } if (node.kind === 199 || node.kind === 153) { if (shouldHoistVariable(node, false)) { - var name_22 = node.name; - if (name_22.kind === 65) { + var name_25 = node.name; + if (name_25.kind === 65) { if (!hoistedVars) { hoistedVars = []; } - hoistedVars.push(name_22); + hoistedVars.push(name_25); } else { - ts.forEachChild(name_22, visit); + ts.forEachChild(name_25, visit); } } return; @@ -24754,6 +25754,8 @@ var ts; case 204: case 215: return false; + case 181: + return shouldEmitLeadingAndTrailingCommentsForVariableStatement(node); case 206: return shouldEmitModuleDeclaration(node); case 205: diff --git a/bin/tsserver.js b/bin/tsserver.js index 49c7b69b302..5795b1e7e36 100644 --- a/bin/tsserver.js +++ b/bin/tsserver.js @@ -145,6 +145,16 @@ var ts; } } ts.addRange = addRange; + function rangeEquals(array1, array2, pos, end) { + while (pos < end) { + if (array1[pos] !== array2[pos]) { + return false; + } + pos++; + } + return true; + } + ts.rangeEquals = rangeEquals; function lastOrUndefined(array) { if (array.length === 0) { return undefined; @@ -301,8 +311,10 @@ var ts; var end = start + length; Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); - Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); - Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + if (file) { + Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); + Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + } var text = getLocaleSpecificMessage(message.key); if (arguments.length > 4) { text = formatStringFromArgs(text, arguments, 4); @@ -837,7 +849,7 @@ var ts; for (var _i = 0; _i < files.length; _i++) { var current = files[_i]; var name = ts.combinePaths(path, current); - var stat = _fs.lstatSync(name); + var stat = _fs.statSync(name); if (stat.isFile()) { if (!extension || ts.fileExtensionIs(name, extension)) { result.push(name); @@ -1039,7 +1051,7 @@ var ts; Unterminated_template_literal: { code: 1160, category: ts.DiagnosticCategory.Error, key: "Unterminated template literal." }, Unterminated_regular_expression_literal: { code: 1161, category: ts.DiagnosticCategory.Error, key: "Unterminated regular expression literal." }, An_object_member_cannot_be_declared_optional: { code: 1162, category: ts.DiagnosticCategory.Error, key: "An object member cannot be declared optional." }, - yield_expression_must_be_contained_within_a_generator_declaration: { code: 1163, category: ts.DiagnosticCategory.Error, key: "'yield' expression must be contained_within a generator declaration." }, + A_yield_expression_is_only_allowed_in_a_generator_body: { code: 1163, category: ts.DiagnosticCategory.Error, key: "A 'yield' expression is only allowed in a generator body." }, Computed_property_names_are_not_allowed_in_enums: { code: 1164, category: ts.DiagnosticCategory.Error, key: "Computed property names are not allowed in enums." }, A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol: { code: 1165, category: ts.DiagnosticCategory.Error, key: "A computed property name in an ambient context must directly refer to a built-in symbol." }, A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol: { code: 1166, category: ts.DiagnosticCategory.Error, key: "A computed property name in a class property declaration must directly refer to a built-in symbol." }, @@ -1094,6 +1106,10 @@ var ts; Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: ts.DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." }, Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: ts.DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." }, Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Specify_experimentalDecorators_to_remove_this_warning: { code: 1219, category: ts.DiagnosticCategory.Error, key: "Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning." }, + Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1220, category: ts.DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." }, + Generators_are_not_allowed_in_an_ambient_context: { code: 1221, category: ts.DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." }, + An_overload_signature_cannot_be_declared_as_a_generator: { code: 1222, category: ts.DiagnosticCategory.Error, key: "An overload signature cannot be declared as a generator." }, + _0_tag_already_specified: { code: 1223, category: ts.DiagnosticCategory.Error, key: "'{0}' tag already specified." }, Duplicate_identifier_0: { code: 2300, category: ts.DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: ts.DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: ts.DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, @@ -1254,7 +1270,7 @@ var ts; The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: ts.DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." }, Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: ts.DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." }, A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: ts.DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." }, - Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." }, + Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher." }, Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: ts.DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." }, In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: ts.DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." }, const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: ts.DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, @@ -1285,6 +1301,8 @@ var ts; A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: ts.DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." }, _0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: ts.DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." }, Cannot_find_namespace_0: { code: 2503, category: ts.DiagnosticCategory.Error, key: "Cannot find namespace '{0}'." }, + No_best_common_type_exists_among_yield_expressions: { code: 2504, category: ts.DiagnosticCategory.Error, key: "No best common type exists among yield expressions." }, + A_generator_cannot_have_a_void_type_annotation: { code: 2505, category: ts.DiagnosticCategory.Error, key: "A generator cannot have a 'void' type annotation." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: ts.DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." }, @@ -1445,6 +1463,7 @@ var ts; _0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer: { code: 7022, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer." }, _0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7023, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7024, category: ts.DiagnosticCategory.Error, key: "Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, + Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type: { code: 7025, category: ts.DiagnosticCategory.Error, key: "Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "'import ... =' can only be used in a .ts file." }, @@ -1458,16 +1477,12 @@ var ts; types_can_only_be_used_in_a_ts_file: { code: 8010, category: ts.DiagnosticCategory.Error, key: "'types' can only be used in a .ts file." }, type_arguments_can_only_be_used_in_a_ts_file: { code: 8011, category: ts.DiagnosticCategory.Error, key: "'type arguments' can only be used in a .ts file." }, parameter_modifiers_can_only_be_used_in_a_ts_file: { code: 8012, category: ts.DiagnosticCategory.Error, key: "'parameter modifiers' can only be used in a .ts file." }, - can_only_be_used_in_a_ts_file: { code: 8013, category: ts.DiagnosticCategory.Error, key: "'?' can only be used in a .ts file." }, property_declarations_can_only_be_used_in_a_ts_file: { code: 8014, category: ts.DiagnosticCategory.Error, key: "'property declarations' can only be used in a .ts file." }, enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: ts.DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." }, type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: ts.DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." }, decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: ts.DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." }, - yield_expressions_are_not_currently_supported: { code: 9000, category: ts.DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." }, - Generators_are_not_currently_supported: { code: 9001, category: ts.DiagnosticCategory.Error, key: "Generators are not currently supported." }, Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: ts.DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." }, - class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." }, - class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: ts.DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." } + class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." } }; })(ts || (ts = {})); /// @@ -3155,17 +3170,17 @@ var ts; ts.getFullWidth = getFullWidth; function containsParseError(node) { aggregateChildData(node); - return (node.parserContextFlags & 64) !== 0; + return (node.parserContextFlags & 128) !== 0; } ts.containsParseError = containsParseError; function aggregateChildData(node) { - if (!(node.parserContextFlags & 128)) { + if (!(node.parserContextFlags & 256)) { var thisNodeOrAnySubNodesHasError = ((node.parserContextFlags & 32) !== 0) || ts.forEachChild(node, containsParseError); if (thisNodeOrAnySubNodesHasError) { - node.parserContextFlags |= 64; + node.parserContextFlags |= 128; } - node.parserContextFlags |= 128; + node.parserContextFlags |= 256; } } function getSourceFileOfNode(node) { @@ -3408,6 +3423,75 @@ var ts; } ts.getJsDocComments = getJsDocComments; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; + function isTypeNode(node) { + if (142 <= node.kind && node.kind <= 150) { + return true; + } + switch (node.kind) { + case 112: + case 120: + case 122: + case 113: + case 123: + return true; + case 99: + return node.parent.kind !== 167; + case 8: + return node.parent.kind === 130; + case 177: + return true; + case 65: + if (node.parent.kind === 127 && node.parent.right === node) { + node = node.parent; + } + else if (node.parent.kind === 156 && node.parent.name === node) { + node = node.parent; + } + case 127: + case 156: + ts.Debug.assert(node.kind === 65 || node.kind === 127 || node.kind === 156, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); + var parent_1 = node.parent; + if (parent_1.kind === 145) { + return false; + } + if (142 <= parent_1.kind && parent_1.kind <= 150) { + return true; + } + switch (parent_1.kind) { + case 177: + return true; + case 129: + return node === parent_1.constraint; + case 133: + case 132: + case 130: + case 199: + return node === parent_1.type; + case 201: + case 163: + case 164: + case 136: + case 135: + case 134: + case 137: + case 138: + return node === parent_1.type; + case 139: + case 140: + case 141: + return node === parent_1.type; + case 161: + return node === parent_1.type; + case 158: + case 159: + return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + case 160: + return false; + } + } + return false; + } + ts.isTypeNode = isTypeNode; function forEachReturnStatement(body, visitor) { return traverse(body); function traverse(node) { @@ -3434,6 +3518,37 @@ var ts; } } ts.forEachReturnStatement = forEachReturnStatement; + function forEachYieldExpression(body, visitor) { + return traverse(body); + function traverse(node) { + switch (node.kind) { + case 173: + visitor(node); + var operand = node.expression; + if (operand) { + traverse(operand); + } + case 205: + case 203: + case 206: + case 204: + case 202: + return; + default: + if (isFunctionLike(node)) { + var name_3 = node.name; + if (name_3 && name_3.kind === 128) { + traverse(name_3.expression); + return; + } + } + else if (!isTypeNode(node)) { + ts.forEachChild(node, traverse); + } + } + } + } + ts.forEachYieldExpression = forEachYieldExpression; function isVariableLike(node) { if (node) { switch (node.kind) { @@ -3462,6 +3577,12 @@ var ts; return false; } ts.isAccessor = isAccessor; + function isClassLike(node) { + if (node) { + return node.kind === 202 || node.kind === 175; + } + } + ts.isClassLike = isClassLike; function isFunctionLike(node) { if (node) { switch (node.kind) { @@ -3678,6 +3799,7 @@ var ts; case 172: case 10: case 176: + case 173: return true; case 127: while (node.parent.kind === 127) { @@ -3690,8 +3812,8 @@ var ts; } case 7: case 8: - var parent_1 = node.parent; - switch (parent_1.kind) { + var parent_2 = node.parent; + switch (parent_2.kind) { case 199: case 130: case 133: @@ -3699,7 +3821,7 @@ var ts; case 227: case 225: case 153: - return parent_1.initializer === node; + return parent_2.initializer === node; case 183: case 184: case 185: @@ -3710,27 +3832,27 @@ var ts; case 221: case 196: case 194: - return parent_1.expression === node; + return parent_2.expression === node; case 187: - var forStatement = parent_1; + var forStatement = parent_2; return (forStatement.initializer === node && forStatement.initializer.kind !== 200) || forStatement.condition === node || forStatement.incrementor === node; case 188: case 189: - var forInStatement = parent_1; + var forInStatement = parent_2; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 200) || forInStatement.expression === node; case 161: - return node === parent_1.expression; + return node === parent_2.expression; case 178: - return node === parent_1.expression; + return node === parent_2.expression; case 128: - return node === parent_1.expression; + return node === parent_2.expression; case 131: return true; default: - if (isExpression(parent_1)) { + if (isExpression(parent_2)) { return true; } } @@ -3798,6 +3920,72 @@ var ts; return s.parameters.length > 0 && ts.lastOrUndefined(s.parameters).dotDotDotToken !== undefined; } ts.hasRestParameters = hasRestParameters; + function isJSDocConstructSignature(node) { + return node.kind === 241 && + node.parameters.length > 0 && + node.parameters[0].type.kind === 243; + } + ts.isJSDocConstructSignature = isJSDocConstructSignature; + function getJSDocTag(node, kind) { + if (node && node.jsDocComment) { + for (var _i = 0, _a = node.jsDocComment.tags; _i < _a.length; _i++) { + var tag = _a[_i]; + if (tag.kind === kind) { + return tag; + } + } + } + } + function getJSDocTypeTag(node) { + return getJSDocTag(node, 249); + } + ts.getJSDocTypeTag = getJSDocTypeTag; + function getJSDocReturnTag(node) { + return getJSDocTag(node, 248); + } + ts.getJSDocReturnTag = getJSDocReturnTag; + function getJSDocTemplateTag(node) { + return getJSDocTag(node, 250); + } + ts.getJSDocTemplateTag = getJSDocTemplateTag; + function getCorrespondingJSDocParameterTag(parameter) { + if (parameter.name && parameter.name.kind === 65) { + var parameterName = parameter.name.text; + var docComment = parameter.parent.jsDocComment; + if (docComment) { + return ts.forEach(docComment.tags, function (t) { + if (t.kind === 247) { + var parameterTag = t; + var name_4 = parameterTag.preParameterName || parameterTag.postParameterName; + if (name_4.text === parameterName) { + return t; + } + } + }); + } + } + } + ts.getCorrespondingJSDocParameterTag = getCorrespondingJSDocParameterTag; + function hasRestParameter(s) { + return isRestParameter(ts.lastOrUndefined(s.parameters)); + } + ts.hasRestParameter = hasRestParameter; + function isRestParameter(node) { + if (node) { + if (node.parserContextFlags & 64) { + if (node.type && node.type.kind === 242) { + return true; + } + var paramTag = getCorrespondingJSDocParameterTag(node); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 242; + } + } + return node.dotDotDotToken !== undefined; + } + return false; + } + ts.isRestParameter = isRestParameter; function isLiteralKind(kind) { return 7 <= kind && kind <= 10; } @@ -4521,6 +4709,10 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 256) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; + function isJavaScript(fileName) { + return ts.fileExtensionIs(fileName, ".js"); + } + ts.isJavaScript = isJavaScript; function getExpandedCharCodes(input) { var output = []; var length = input.length; @@ -4691,12 +4883,23 @@ var ts; return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), newEndN - oldStartN); } ts.collapseTextChangeRangesAcrossMultipleVersions = collapseTextChangeRangesAcrossMultipleVersions; + function getTypeParameterOwner(d) { + if (d && d.kind === 129) { + for (var current = d; current; current = current.parent) { + if (ts.isFunctionLike(current) || ts.isClassLike(current) || current.kind === 203) { + return current; + } + } + } + } + ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); /// /// var ts; (function (ts) { - var nodeConstructors = new Array(230); + ts.throwOnJSDocErrors = false; + var nodeConstructors = new Array(252); ts.parseTime = 0; function getNodeConstructor(kind) { return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); @@ -4999,6 +5202,49 @@ var ts; return visitNode(cbNode, node.expression); case 219: return visitNodes(cbNodes, node.decorators); + case 229: + return visitNode(cbNode, node.type); + case 233: + return visitNodes(cbNodes, node.types); + case 234: + return visitNodes(cbNodes, node.types); + case 232: + return visitNode(cbNode, node.elementType); + case 236: + return visitNode(cbNode, node.type); + case 235: + return visitNode(cbNode, node.type); + case 237: + return visitNodes(cbNodes, node.members); + case 239: + return visitNode(cbNode, node.name) || + visitNodes(cbNodes, node.typeArguments); + case 240: + return visitNode(cbNode, node.type); + case 241: + return visitNodes(cbNodes, node.parameters) || + visitNode(cbNode, node.type); + case 242: + return visitNode(cbNode, node.type); + case 243: + return visitNode(cbNode, node.type); + case 244: + return visitNode(cbNode, node.type); + case 238: + return visitNode(cbNode, node.name) || + visitNode(cbNode, node.type); + case 245: + return visitNodes(cbNodes, node.tags); + case 247: + return visitNode(cbNode, node.preParameterName) || + visitNode(cbNode, node.typeExpression) || + visitNode(cbNode, node.postParameterName); + case 248: + return visitNode(cbNode, node.typeExpression); + case 249: + return visitNode(cbNode, node.typeExpression); + case 250: + return visitNodes(cbNodes, node.typeParameters); } } ts.forEachChild = forEachChild; @@ -5014,11 +5260,20 @@ var ts; return IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks); } ts.updateSourceFile = updateSourceFile; + function parseIsolatedJSDocComment(content, start, length) { + return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + } + ts.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocTypeExpressionForTests(content, start, length) { + return Parser.JSDocParser.parseJSDocTypeExpressionForTests(content, start, length); + } + ts.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; var Parser; (function (Parser) { var scanner = ts.createScanner(2, true); var disallowInAndDecoratorContext = 2 | 16; var sourceFile; + var parseDiagnostics; var syntaxCursor; var token; var sourceText; @@ -5026,21 +5281,40 @@ var ts; var identifiers; var identifierCount; var parsingContext; - var contextFlags = 0; + var contextFlags; var parseErrorBeforeNextFinishedNode = false; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { + initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); + clearState(); + return result; + } + Parser.parseSourceFile = parseSourceFile; + function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { sourceText = _sourceText; syntaxCursor = _syntaxCursor; + parseDiagnostics = []; parsingContext = 0; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = 0; + contextFlags = ts.isJavaScript(fileName) ? 64 : 0; parseErrorBeforeNextFinishedNode = false; - createSourceFile(fileName, languageVersion); scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); + } + function clearState() { + scanner.setText(""); + scanner.setOnError(undefined); + parseDiagnostics = undefined; + sourceFile = undefined; + identifiers = undefined; + syntaxCursor = undefined; + sourceText = undefined; + } + function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { + sourceFile = createSourceFile(fileName, languageVersion); token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0, true, parseSourceElement); @@ -5050,20 +5324,40 @@ var ts; sourceFile.nodeCount = nodeCount; sourceFile.identifierCount = identifierCount; sourceFile.identifiers = identifiers; + sourceFile.parseDiagnostics = parseDiagnostics; if (setParentNodes) { fixupParentReferences(sourceFile); } - syntaxCursor = undefined; - scanner.setText(""); - scanner.setOnError(undefined); - var result = sourceFile; - sourceFile = undefined; - identifiers = undefined; - syntaxCursor = undefined; - sourceText = undefined; - return result; + if (ts.isJavaScript(fileName)) { + addJSDocComments(); + } + return sourceFile; + } + function addJSDocComments() { + forEachChild(sourceFile, visit); + return; + function visit(node) { + switch (node.kind) { + case 181: + case 201: + case 130: + addJSDocComment(node); + } + forEachChild(node, visit); + } + } + function addJSDocComment(node) { + var comments = ts.getLeadingCommentRangesOfNode(node, sourceFile); + if (comments) { + for (var _i = 0; _i < comments.length; _i++) { + var comment = comments[_i]; + var jsDocComment = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); + if (jsDocComment) { + node.jsDocComment = jsDocComment; + } + } + } } - Parser.parseSourceFile = parseSourceFile; function fixupParentReferences(sourceFile) { // normally parent references are set during binding. However, for clients that only need // a syntax tree, and no semantic features, then the binding process is an unnecessary @@ -5082,16 +5376,17 @@ var ts; } } } + Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - sourceFile = createNode(228, 0); + var sourceFile = createNode(228, 0); sourceFile.pos = 0; sourceFile.end = sourceText.length; sourceFile.text = sourceText; - sourceFile.parseDiagnostics = []; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 2048 : 0; + return sourceFile; } function setContextFlag(val, flag) { if (val) { @@ -5192,9 +5487,9 @@ var ts; parseErrorAtPosition(start, length, message, arg0); } function parseErrorAtPosition(start, length, message, arg0) { - var lastError = ts.lastOrUndefined(sourceFile.parseDiagnostics); + var lastError = ts.lastOrUndefined(parseDiagnostics); if (!lastError || start !== lastError.start) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); } parseErrorBeforeNextFinishedNode = true; } @@ -5225,7 +5520,7 @@ var ts; } function speculationHelper(callback, isLookAhead) { var saveToken = token; - var saveParseDiagnosticsLength = sourceFile.parseDiagnostics.length; + var saveParseDiagnosticsLength = parseDiagnostics.length; var saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; var saveContextFlags = contextFlags; var result = isLookAhead @@ -5234,7 +5529,7 @@ var ts; ts.Debug.assert(saveContextFlags === contextFlags); if (!result || isLookAhead) { token = saveToken; - sourceFile.parseDiagnostics.length = saveParseDiagnosticsLength; + parseDiagnostics.length = saveParseDiagnosticsLength; parseErrorBeforeNextFinishedNode = saveParseErrorBeforeNextFinishedNode; } return result; @@ -5316,8 +5611,8 @@ var ts; node.end = pos; return node; } - function finishNode(node) { - node.end = scanner.getStartPos(); + function finishNode(node, end) { + node.end = end === undefined ? scanner.getStartPos() : end; if (contextFlags) { node.parserContextFlags = contextFlags; } @@ -5366,15 +5661,24 @@ var ts; token === 8 || token === 7; } - function parsePropertyName() { + function parsePropertyNameWorker(allowComputedPropertyNames) { if (token === 8 || token === 7) { return parseLiteralNode(true); } - if (token === 18) { + if (allowComputedPropertyNames && token === 18) { return parseComputedPropertyName(); } return parseIdentifierName(); } + function parsePropertyName() { + return parsePropertyNameWorker(true); + } + function parseSimplePropertyName() { + return parsePropertyNameWorker(false); + } + function isSimplePropertyName() { + return token === 8 || token === 7 || isIdentifierOrKeyword(); + } function parseComputedPropertyName() { var node = createNode(128); parseExpected(18); @@ -5430,10 +5734,10 @@ var ts; switch (parsingContext) { case 0: case 1: - return isSourceElement(inErrorRecovery); + return !(token === 22 && inErrorRecovery) && isStartOfModuleElement(); case 2: case 4: - return isStartOfStatement(inErrorRecovery); + return !(token === 22 && inErrorRecovery) && isStartOfStatement(); case 3: return token === 67 || token === 73; case 5: @@ -5474,6 +5778,12 @@ var ts; return isHeritageClause(); case 20: return isIdentifierOrKeyword(); + case 21: + case 22: + case 24: + return JSDocParser.isJSDocType(); + case 23: + return isSimplePropertyName(); } ts.Debug.fail("Non-exhaustive case in 'isListElement'."); } @@ -5535,6 +5845,14 @@ var ts; return token === 25 || token === 16; case 19: return token === 14 || token === 15; + case 21: + return token === 17 || token === 51 || token === 15; + case 22: + return token === 25 || token === 15; + case 24: + return token === 19 || token === 15; + case 23: + return token === 15; } } function isVariableDeclaratorListTerminator() { @@ -5550,7 +5868,7 @@ var ts; return false; } function isInSomeParsingContext() { - for (var kind = 0; kind < 21; kind++) { + for (var kind = 0; kind < 25; kind++) { if (parsingContext & (1 << kind)) { if (isListElement(kind, true) || isListTerminator(kind)) { return true; @@ -5571,7 +5889,7 @@ var ts; result.push(element); if (checkForStrictMode && !inStrictModeContext()) { if (ts.isPrologueDirective(element)) { - if (isUseStrictPrologueDirective(sourceFile, element)) { + if (isUseStrictPrologueDirective(element)) { setStrictModeContext(true); checkForStrictMode = false; } @@ -5591,9 +5909,9 @@ var ts; parsingContext = saveParsingContext; return result; } - function isUseStrictPrologueDirective(sourceFile, node) { + function isUseStrictPrologueDirective(node) { ts.Debug.assert(ts.isPrologueDirective(node)); - var nodeText = ts.getSourceTextOfNodeFromSourceFile(sourceFile, node.expression); + var nodeText = ts.getTextOfNodeFromSourceText(sourceText, node.expression); return nodeText === '"use strict"' || nodeText === "'use strict'"; } function parseListElement(parsingContext, parseElement) { @@ -5794,6 +6112,10 @@ var ts; case 18: return ts.Diagnostics.Type_expected; case 19: return ts.Diagnostics.Unexpected_token_expected; case 20: return ts.Diagnostics.Identifier_expected; + case 21: return ts.Diagnostics.Parameter_declaration_expected; + case 22: return ts.Diagnostics.Type_argument_expected; + case 24: return ts.Diagnostics.Type_expected; + case 23: return ts.Diagnostics.Property_assignment_expected; } } ; @@ -6442,11 +6764,6 @@ var ts; nextToken(); return !scanner.hasPrecedingLineBreak() && isIdentifier(); } - function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { - nextToken(); - return !scanner.hasPrecedingLineBreak() && - (isIdentifier() || token === 14 || token === 18); - } function parseYieldExpression() { var node = createNode(173); nextToken(); @@ -6555,10 +6872,11 @@ var ts; if (token === 14) { return parseFunctionBlock(false, false); } - if (isStartOfStatement(true) && - !isStartOfExpressionStatement() && + if (token !== 22 && token !== 83 && - token !== 69) { + token !== 69 && + isStartOfStatement() && + !isStartOfExpressionStatement()) { return parseFunctionBlock(false, true); } return parseAssignmentExpressionOrHigher(); @@ -7201,21 +7519,68 @@ var ts; return finishNode(expressionStatement); } } - function isStartOfStatement(inErrorRecovery) { - if (ts.isModifier(token)) { - var result = lookAhead(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return true; + function isIdentifierOrKeyword() { + return token >= 65; + } + function nextTokenIsIdentifierOrKeywordOnSameLine() { + nextToken(); + return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + } + function parseDeclarationFlags() { + while (true) { + switch (token) { + case 98: + case 104: + case 70: + case 83: + case 69: + case 77: + return 1; + case 103: + case 124: + nextToken(); + return isIdentifierOrKeyword() ? 1 : 0; + case 117: + case 118: + nextToken(); + return isIdentifierOrKeyword() || token === 8 ? 2 : 0; + case 85: + nextToken(); + return token === 8 || token === 35 || + token === 14 || isIdentifierOrKeyword() ? + 2 : 0; + case 78: + nextToken(); + if (token === 53 || token === 35 || + token === 14 || token === 73) { + return 2; + } + continue; + case 115: + case 108: + case 106: + case 107: + case 109: + nextToken(); + continue; + default: + return 0; } } + } + function getDeclarationFlags() { + return lookAhead(parseDeclarationFlags); + } + function getStatementFlags() { switch (token) { + case 52: case 22: - return !inErrorRecovery; case 14: case 98: case 104: case 83: case 69: + case 77: case 84: case 75: case 100: @@ -7230,50 +7595,67 @@ var ts; case 72: case 68: case 81: - return true; + return 1; case 70: - var isConstEnum = lookAhead(nextTokenIsEnumKeyword); - return !isConstEnum; + case 78: + case 85: + return getDeclarationFlags(); + case 115: case 103: case 117: case 118: - case 77: case 124: - if (isDeclarationStart()) { - return false; - } + return getDeclarationFlags() || 1; case 108: case 106: case 107: case 109: - if (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine)) { - return false; - } + return getDeclarationFlags() || + (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine) ? 0 : 1); default: - return isStartOfExpression(); + return isStartOfExpression() ? 1 : 0; } } - function nextTokenIsEnumKeyword() { - nextToken(); - return token === 77; + function isStartOfStatement() { + return (getStatementFlags() & 1) !== 0; } - function nextTokenIsIdentifierOrKeywordOnSameLine() { + function isStartOfModuleElement() { + return (getStatementFlags() & 3) !== 0; + } + function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { nextToken(); - return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + return !scanner.hasPrecedingLineBreak() && + (isIdentifier() || token === 14 || token === 18); + } + function isLetDeclaration() { + return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); } function parseStatement() { + return parseModuleElementOfKind(1); + } + function parseModuleElement() { + return parseModuleElementOfKind(3); + } + function parseSourceElement() { + return parseModuleElementOfKind(3); + } + function parseModuleElementOfKind(flags) { switch (token) { + case 22: + return parseEmptyStatement(); case 14: return parseBlock(false, false); case 98: - case 70: return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 104: + if (isLetDeclaration()) { + return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + } + break; case 83: return parseFunctionDeclaration(scanner.getStartPos(), undefined, undefined); case 69: return parseClassDeclaration(scanner.getStartPos(), undefined, undefined); - case 22: - return parseEmptyStatement(); case 84: return parseIfStatement(); case 75: @@ -7300,44 +7682,66 @@ var ts; return parseTryStatement(); case 72: return parseDebuggerStatement(); - case 104: - if (isLetDeclaration()) { - return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 52: + return parseDeclaration(); + case 70: + case 115: + case 77: + case 78: + case 85: + case 103: + case 117: + case 118: + case 106: + case 107: + case 108: + case 109: + case 124: + if (getDeclarationFlags() & flags) { + return parseDeclaration(); } - default: - if (ts.isModifier(token) || token === 52) { - var result = tryParse(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return result; - } - } - return parseExpressionOrLabeledStatement(); + break; } + return parseExpressionOrLabeledStatement(); } - function parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers() { - var start = scanner.getStartPos(); + function parseDeclaration() { + var fullStart = getNodePos(); var decorators = parseDecorators(); var modifiers = parseModifiers(); switch (token) { - case 70: - var nextTokenIsEnum = lookAhead(nextTokenIsEnumKeyword); - if (nextTokenIsEnum) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); - case 104: - if (!isLetDeclaration()) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); case 98: - return parseVariableStatement(start, decorators, modifiers); + case 104: + case 70: + return parseVariableStatement(fullStart, decorators, modifiers); case 83: - return parseFunctionDeclaration(start, decorators, modifiers); + return parseFunctionDeclaration(fullStart, decorators, modifiers); case 69: - return parseClassDeclaration(start, decorators, modifiers); + return parseClassDeclaration(fullStart, decorators, modifiers); + case 103: + return parseInterfaceDeclaration(fullStart, decorators, modifiers); + case 124: + return parseTypeAliasDeclaration(fullStart, decorators, modifiers); + case 77: + return parseEnumDeclaration(fullStart, decorators, modifiers); + case 117: + case 118: + return parseModuleDeclaration(fullStart, decorators, modifiers); + case 85: + return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); + case 78: + nextToken(); + return token === 73 || token === 53 ? + parseExportAssignment(fullStart, decorators, modifiers) : + parseExportDeclaration(fullStart, decorators, modifiers); + default: + if (decorators) { + var node = createMissingNode(219, true, ts.Diagnostics.Declaration_expected); + node.pos = fullStart; + node.decorators = decorators; + setModifiers(node, modifiers); + return finishNode(node); + } } - return undefined; } function parseFunctionBlockOrSemicolon(isGenerator, diagnosticMessage) { if (token !== 14 && canParseSemicolon()) { @@ -7481,7 +7885,9 @@ var ts; property.name = name; property.questionToken = questionToken; property.type = parseTypeAnnotation(); - property.initializer = allowInAnd(parseNonParameterInitializer); + property.initializer = modifiers && modifiers.flags & 128 + ? allowInAnd(parseNonParameterInitializer) + : doOutsideOfContext(4 | 2, parseNonParameterInitializer); parseSemicolon(); return finishNode(property); } @@ -7627,8 +8033,8 @@ var ts; return parsePropertyOrMethodDeclaration(fullStart, decorators, modifiers); } if (decorators) { - var name_3 = createMissingNode(65, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_3, undefined); + var name_5 = createMissingNode(65, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_5, undefined); } ts.Debug.fail("Should not have attempted to parse class member declaration."); } @@ -7935,125 +8341,6 @@ var ts; parseSemicolon(); return finishNode(node); } - function isLetDeclaration() { - return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); - } - function isDeclarationStart(followsModifier) { - switch (token) { - case 98: - case 70: - case 83: - return true; - case 104: - return isLetDeclaration(); - case 69: - case 103: - case 77: - case 124: - return lookAhead(nextTokenIsIdentifierOrKeyword); - case 85: - return lookAhead(nextTokenCanFollowImportKeyword); - case 117: - case 118: - return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral); - case 78: - return lookAhead(nextTokenCanFollowExportKeyword); - case 115: - case 108: - case 106: - case 107: - case 109: - return lookAhead(nextTokenIsDeclarationStart); - case 52: - return !followsModifier; - } - } - function isIdentifierOrKeyword() { - return token >= 65; - } - function nextTokenIsIdentifierOrKeyword() { - nextToken(); - return isIdentifierOrKeyword(); - } - function nextTokenIsIdentifierOrKeywordOrStringLiteral() { - nextToken(); - return isIdentifierOrKeyword() || token === 8; - } - function nextTokenCanFollowImportKeyword() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 || - token === 35 || token === 14; - } - function nextTokenCanFollowExportKeyword() { - nextToken(); - return token === 53 || token === 35 || - token === 14 || token === 73 || isDeclarationStart(true); - } - function nextTokenIsDeclarationStart() { - nextToken(); - return isDeclarationStart(true); - } - function nextTokenIsAsKeyword() { - return nextToken() === 111; - } - function parseDeclaration() { - var fullStart = getNodePos(); - var decorators = parseDecorators(); - var modifiers = parseModifiers(); - if (token === 78) { - nextToken(); - if (token === 73 || token === 53) { - return parseExportAssignment(fullStart, decorators, modifiers); - } - if (token === 35 || token === 14) { - return parseExportDeclaration(fullStart, decorators, modifiers); - } - } - switch (token) { - case 98: - case 104: - case 70: - return parseVariableStatement(fullStart, decorators, modifiers); - case 83: - return parseFunctionDeclaration(fullStart, decorators, modifiers); - case 69: - return parseClassDeclaration(fullStart, decorators, modifiers); - case 103: - return parseInterfaceDeclaration(fullStart, decorators, modifiers); - case 124: - return parseTypeAliasDeclaration(fullStart, decorators, modifiers); - case 77: - return parseEnumDeclaration(fullStart, decorators, modifiers); - case 117: - case 118: - return parseModuleDeclaration(fullStart, decorators, modifiers); - case 85: - return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); - default: - if (decorators) { - var node = createMissingNode(219, true, ts.Diagnostics.Declaration_expected); - node.pos = fullStart; - node.decorators = decorators; - setModifiers(node, modifiers); - return finishNode(node); - } - ts.Debug.fail("Mismatch between isDeclarationStart and parseDeclaration"); - } - } - function isSourceElement(inErrorRecovery) { - return isDeclarationStart() || isStartOfStatement(inErrorRecovery); - } - function parseSourceElement() { - return parseSourceElementOrModuleElement(); - } - function parseModuleElement() { - return parseSourceElementOrModuleElement(); - } - function parseSourceElementOrModuleElement() { - return isDeclarationStart() - ? parseDeclaration() - : parseStatement(); - } function processReferenceComments(sourceFile) { var triviaScanner = ts.createScanner(sourceFile.languageVersion, false, sourceText); var referencedFiles = []; @@ -8078,7 +8365,7 @@ var ts; referencedFiles.push(fileReference); } if (diagnosticMessage) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); } } else { @@ -8086,7 +8373,7 @@ var ts; var amdModuleNameMatchResult = amdModuleNameRegEx.exec(comment); if (amdModuleNameMatchResult) { if (amdModuleName) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); } amdModuleName = amdModuleNameMatchResult[2]; } @@ -8119,6 +8406,520 @@ var ts; : undefined; }); } + var JSDocParser; + (function (JSDocParser) { + function isJSDocType() { + switch (token) { + case 35: + case 50: + case 16: + case 18: + case 46: + case 14: + case 83: + case 21: + case 88: + case 93: + return true; + } + return isIdentifierOrKeyword(); + } + JSDocParser.isJSDocType = isJSDocType; + function parseJSDocTypeExpressionForTests(content, start, length) { + initializeState("file.js", content, 2, undefined); + var jsDocTypeExpression = parseJSDocTypeExpression(start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocTypeExpression ? { jsDocTypeExpression: jsDocTypeExpression, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; + function parseJSDocTypeExpression(start, length) { + scanner.setText(sourceText, start, length); + token = nextToken(); + var result = createNode(229); + parseExpected(14); + result.type = parseJSDocTopLevelType(); + parseExpected(15); + fixupParentReferences(result); + return finishNode(result); + } + JSDocParser.parseJSDocTypeExpression = parseJSDocTypeExpression; + function setError(message) { + parseErrorAtCurrentToken(message); + if (ts.throwOnJSDocErrors) { + throw new Error(message.key); + } + } + function parseJSDocTopLevelType() { + var type = parseJSDocType(); + if (token === 44) { + var unionType = createNode(233, type.pos); + unionType.types = parseJSDocTypeList(type); + type = finishNode(unionType); + } + if (token === 53) { + var optionalType = createNode(240, type.pos); + nextToken(); + optionalType.type = type; + type = finishNode(optionalType); + } + return type; + } + function parseJSDocType() { + var type = parseBasicTypeExpression(); + while (true) { + if (token === 18) { + var arrayType = createNode(232, type.pos); + arrayType.elementType = type; + nextToken(); + parseExpected(19); + type = finishNode(arrayType); + } + else if (token === 50) { + var nullableType = createNode(235, type.pos); + nullableType.type = type; + nextToken(); + type = finishNode(nullableType); + } + else if (token === 46) { + var nonNullableType = createNode(236, type.pos); + nonNullableType.type = type; + nextToken(); + type = finishNode(nonNullableType); + } + else { + break; + } + } + return type; + } + function parseBasicTypeExpression() { + switch (token) { + case 35: + return parseJSDocAllType(); + case 50: + return parseJSDocUnknownOrNullableType(); + case 16: + return parseJSDocUnionType(); + case 18: + return parseJSDocTupleType(); + case 46: + return parseJSDocNonNullableType(); + case 14: + return parseJSDocRecordType(); + case 83: + return parseJSDocFunctionType(); + case 21: + return parseJSDocVariadicType(); + case 88: + return parseJSDocConstructorType(); + case 93: + return parseJSDocThisType(); + case 112: + case 122: + case 120: + case 113: + case 123: + case 99: + return parseTokenNode(); + } + return parseJSDocTypeReference(); + } + function parseJSDocThisType() { + var result = createNode(244); + nextToken(); + parseExpected(51); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocConstructorType() { + var result = createNode(243); + nextToken(); + parseExpected(51); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocVariadicType() { + var result = createNode(242); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocFunctionType() { + var result = createNode(241); + nextToken(); + parseExpected(16); + result.parameters = parseDelimitedList(21, parseJSDocParameter); + checkForTrailingComma(result.parameters); + parseExpected(17); + if (token === 51) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocParameter() { + var parameter = createNode(130); + parameter.type = parseJSDocType(); + return finishNode(parameter); + } + function parseJSDocOptionalType(type) { + var result = createNode(240, type.pos); + nextToken(); + result.type = type; + return finishNode(result); + } + function parseJSDocTypeReference() { + var result = createNode(239); + result.name = parseSimplePropertyName(); + while (parseOptional(20)) { + if (token === 24) { + result.typeArguments = parseTypeArguments(); + break; + } + else { + result.name = parseQualifiedName(result.name); + } + } + return finishNode(result); + } + function parseTypeArguments() { + nextToken(); + var typeArguments = parseDelimitedList(22, parseJSDocType); + checkForTrailingComma(typeArguments); + checkForEmptyTypeArgumentList(typeArguments); + parseExpected(25); + return typeArguments; + } + function checkForEmptyTypeArgumentList(typeArguments) { + if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceText, typeArguments.end) + ">".length; + return parseErrorAtPosition(start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function parseQualifiedName(left) { + var result = createNode(127, left.pos); + result.left = left; + result.right = parseIdentifierName(); + return finishNode(result); + } + function parseJSDocRecordType() { + var result = createNode(237); + nextToken(); + result.members = parseDelimitedList(23, parseJSDocRecordMember); + checkForTrailingComma(result.members); + parseExpected(15); + return finishNode(result); + } + function parseJSDocRecordMember() { + var result = createNode(238); + result.name = parseSimplePropertyName(); + if (token === 51) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocNonNullableType() { + var result = createNode(236); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocTupleType() { + var result = createNode(234); + nextToken(); + result.types = parseDelimitedList(24, parseJSDocType); + checkForTrailingComma(result.types); + parseExpected(19); + return finishNode(result); + } + function checkForTrailingComma(list) { + if (parseDiagnostics.length === 0 && list.hasTrailingComma) { + var start = list.end - ",".length; + parseErrorAtPosition(start, ",".length, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function parseJSDocUnionType() { + var result = createNode(233); + nextToken(); + result.types = parseJSDocTypeList(parseJSDocType()); + parseExpected(17); + return finishNode(result); + } + function parseJSDocTypeList(firstType) { + ts.Debug.assert(!!firstType); + var types = []; + types.pos = firstType.pos; + types.push(firstType); + while (parseOptional(44)) { + types.push(parseJSDocType()); + } + types.end = scanner.getStartPos(); + return types; + } + function parseJSDocAllType() { + var result = createNode(230); + nextToken(); + return finishNode(result); + } + function parseJSDocUnknownOrNullableType() { + var pos = scanner.getStartPos(); + nextToken(); + if (token === 23 || + token === 15 || + token === 17 || + token === 25 || + token === 53 || + token === 44) { + var result = createNode(231, pos); + return finishNode(result); + } + else { + var result = createNode(235, pos); + result.type = parseJSDocType(); + return finishNode(result); + } + } + function parseIsolatedJSDocComment(content, start, length) { + initializeState("file.js", content, 2, undefined); + var jsDocComment = parseJSDocComment(undefined, start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocComment ? { jsDocComment: jsDocComment, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocComment(parent, start, length) { + var comment = parseJSDocCommentWorker(start, length); + if (comment) { + fixupParentReferences(comment); + comment.parent = parent; + } + return comment; + } + JSDocParser.parseJSDocComment = parseJSDocComment; + function parseJSDocCommentWorker(start, length) { + var content = sourceText; + start = start || 0; + var end = length === undefined ? content.length : start + length; + length = end - start; + ts.Debug.assert(start >= 0); + ts.Debug.assert(start <= end); + ts.Debug.assert(end <= content.length); + var tags; + var pos; + if (length >= "/** */".length) { + if (content.charCodeAt(start) === 47 && + content.charCodeAt(start + 1) === 42 && + content.charCodeAt(start + 2) === 42 && + content.charCodeAt(start + 3) !== 42) { + var canParseTag = true; + var seenAsterisk = true; + for (pos = start + "/**".length; pos < end;) { + var ch = content.charCodeAt(pos); + pos++; + if (ch === 64 && canParseTag) { + parseTag(); + canParseTag = false; + continue; + } + if (ts.isLineBreak(ch)) { + canParseTag = true; + seenAsterisk = false; + continue; + } + if (ts.isWhiteSpace(ch)) { + continue; + } + if (ch === 42) { + if (seenAsterisk) { + canParseTag = false; + } + seenAsterisk = true; + continue; + } + canParseTag = false; + } + } + } + return createJSDocComment(); + function createJSDocComment() { + if (!tags) { + return undefined; + } + var result = createNode(245, start); + result.tags = tags; + return finishNode(result, end); + } + function skipWhitespace() { + while (pos < end && ts.isWhiteSpace(content.charCodeAt(pos))) { + pos++; + } + } + function parseTag() { + ts.Debug.assert(content.charCodeAt(pos - 1) === 64); + var atToken = createNode(52, pos - 1); + atToken.end = pos; + var startPos = pos; + var tagName = scanIdentifier(); + if (!tagName) { + return; + } + var tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); + addTag(tag); + } + function handleTag(atToken, tagName) { + if (tagName) { + switch (tagName.text) { + case "param": + return handleParamTag(atToken, tagName); + case "return": + case "returns": + return handleReturnTag(atToken, tagName); + case "template": + return handleTemplateTag(atToken, tagName); + case "type": + return handleTypeTag(atToken, tagName); + } + } + return undefined; + } + function handleUnknownTag(atToken, tagName) { + var result = createNode(246, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + return finishNode(result, pos); + } + function addTag(tag) { + if (tag) { + if (!tags) { + tags = []; + tags.pos = tag.pos; + } + tags.push(tag); + tags.end = tag.end; + } + } + function tryParseTypeExpression() { + skipWhitespace(); + if (content.charCodeAt(pos) !== 123) { + return undefined; + } + var typeExpression = parseJSDocTypeExpression(pos, end - pos); + pos = typeExpression.end; + return typeExpression; + } + function handleParamTag(atToken, tagName) { + var typeExpression = tryParseTypeExpression(); + skipWhitespace(); + var name; + var isBracketed; + if (content.charCodeAt(pos) === 91) { + pos++; + skipWhitespace(); + name = scanIdentifier(); + isBracketed = true; + } + else { + name = scanIdentifier(); + } + if (!name) { + parseErrorAtPosition(pos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var preName, postName; + if (typeExpression) { + postName = name; + } + else { + preName = name; + } + if (!typeExpression) { + typeExpression = tryParseTypeExpression(); + } + var result = createNode(247, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.preParameterName = preName; + result.typeExpression = typeExpression; + result.postParameterName = postName; + result.isBracketed = isBracketed; + return finishNode(result, pos); + } + function handleReturnTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 248; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(248, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTypeTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 249; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(249, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTemplateTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 250; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var typeParameters = []; + typeParameters.pos = pos; + while (true) { + skipWhitespace(); + var startPos = pos; + var name_6 = scanIdentifier(); + if (!name_6) { + parseErrorAtPosition(startPos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var typeParameter = createNode(129, name_6.pos); + typeParameter.name = name_6; + finishNode(typeParameter, pos); + typeParameters.push(typeParameter); + skipWhitespace(); + if (content.charCodeAt(pos) !== 44) { + break; + } + pos++; + } + typeParameters.end = pos; + var result = createNode(250, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeParameters = typeParameters; + return finishNode(result, pos); + } + function scanIdentifier() { + var startPos = pos; + for (; pos < end; pos++) { + var ch = content.charCodeAt(pos); + if (pos === startPos && ts.isIdentifierStart(ch, 2)) { + continue; + } + else if (pos > startPos && ts.isIdentifierPart(ch, 2)) { + continue; + } + break; + } + if (startPos === pos) { + return undefined; + } + var result = createNode(65, startPos); + result.text = content.substring(startPos, pos); + return finishNode(result, pos); + } + } + JSDocParser.parseJSDocCommentWorker = parseJSDocCommentWorker; + })(JSDocParser = Parser.JSDocParser || (Parser.JSDocParser = {})); })(Parser || (Parser = {})); var IncrementalParser; (function (IncrementalParser) { @@ -8159,7 +8960,12 @@ var ts; if (aggressiveChecks && shouldCheckNode(node)) { var text = oldText.substring(node.pos, node.end); } - node._children = undefined; + if (node._children) { + node._children = undefined; + } + if (node.jsDocComment) { + node.jsDocComment = undefined; + } node.pos += delta; node.end += delta; if (aggressiveChecks && shouldCheckNode(node)) { @@ -8820,17 +9626,17 @@ var ts; bindBlockScopedDeclaration(node, 32, 899583); break; case 203: - bindDeclaration(node, 64, 792992, false); + bindBlockScopedDeclaration(node, 64, 792992); break; case 204: - bindDeclaration(node, 524288, 793056, false); + bindBlockScopedDeclaration(node, 524288, 793056); break; case 205: if (ts.isConst(node)) { - bindDeclaration(node, 128, 899967, false); + bindBlockScopedDeclaration(node, 128, 899967); } else { - bindDeclaration(node, 256, 899327, false); + bindBlockScopedDeclaration(node, 256, 899327); } break; case 206: @@ -8991,13 +9797,15 @@ var ts; var undefinedType = createIntrinsicType(32 | 262144, "undefined"); var nullType = createIntrinsicType(64 | 262144, "null"); var unknownType = createIntrinsicType(1, "unknown"); + var circularType = createIntrinsicType(1, "__circular__"); var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = {}; var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = 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 = {}; - var globalArraySymbol; var globalESSymbolConstructorSymbol; var globalObjectType; var globalFunctionType; @@ -9009,6 +9817,8 @@ var ts; var globalTemplateStringsArrayType; var globalESSymbolType; var globalIterableType; + var globalIteratorType; + var globalIterableIteratorType; var anyArrayType; var getGlobalClassDecoratorType; var getGlobalParameterDecoratorType; @@ -9225,7 +10035,13 @@ var ts; loop: while (location) { if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - break loop; + if (!(meaning & 793056) || + !(result.flags & (793056 & ~262144)) || + !ts.isFunctionLike(location) || + lastLocation === location.body) { + break loop; + } + result = undefined; } } switch (location.kind) { @@ -9457,15 +10273,15 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_4 = specifier.propertyName || specifier.name; - if (name_4.text) { - var symbolFromModule = getExportOfModule(targetSymbol, name_4.text); - var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_4.text); + var name_7 = specifier.propertyName || specifier.name; + if (name_7.text) { + var symbolFromModule = getExportOfModule(targetSymbol, name_7.text); + var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_7.text); var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_4, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_4)); + error(name_7, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_7)); } return symbol; } @@ -10113,17 +10929,46 @@ var ts; writeType(types[i], union ? 64 : 0); } } + function writeSymbolTypeReference(symbol, typeArguments, pos, end) { + if (!isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793056); + } + if (pos < end) { + writePunctuation(writer, 24); + writeType(typeArguments[pos++], 0); + while (pos < end) { + writePunctuation(writer, 23); + writeSpace(writer); + writeType(typeArguments[pos++], 0); + } + writePunctuation(writer, 25); + } + } function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments; if (type.target === globalArrayType && !(flags & 1)) { - writeType(type.typeArguments[0], 64); + writeType(typeArguments[0], 64); writePunctuation(writer, 18); writePunctuation(writer, 19); } else { - buildSymbolDisplay(type.target.symbol, writer, enclosingDeclaration, 793056); - writePunctuation(writer, 24); - writeTypeList(type.typeArguments, false); - writePunctuation(writer, 25); + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + var start = i; + var parent_3 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_3); + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_3, typeArguments, start, i); + writePunctuation(writer, 20); + } + } + } + writeSymbolTypeReference(type.symbol, typeArguments, i, typeArguments.length); } } function writeTupleType(type) { @@ -10302,7 +11147,7 @@ var ts; function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaraiton, flags) { var targetSymbol = getTargetSymbol(symbol); if (targetSymbol.flags & 32 || targetSymbol.flags & 64) { - buildDisplayForTypeParametersAndDelimiters(getTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); } } function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, typeStack) { @@ -10465,12 +11310,12 @@ var ts; case 201: case 205: case 209: - var parent_2 = getDeclarationContainer(node); + var parent_4 = getDeclarationContainer(node); if (!(ts.getCombinedNodeFlags(node) & 1) && - !(node.kind !== 209 && parent_2.kind !== 228 && ts.isInAmbientContext(parent_2))) { - return isGlobalSourceFile(parent_2); + !(node.kind !== 209 && parent_4.kind !== 228 && ts.isInAmbientContext(parent_4))) { + return isGlobalSourceFile(parent_4); } - return isDeclarationVisible(parent_2); + return isDeclarationVisible(parent_4); case 133: case 132: case 137: @@ -10591,12 +11436,12 @@ var ts; } var type; if (pattern.kind === 151) { - var name_5 = declaration.propertyName || declaration.name; - type = getTypeOfPropertyOfType(parentType, name_5.text) || - isNumericLiteralName(name_5.text) && getIndexTypeOfType(parentType, 1) || + var name_8 = declaration.propertyName || declaration.name; + type = getTypeOfPropertyOfType(parentType, name_8.text) || + isNumericLiteralName(name_8.text) && getIndexTypeOfType(parentType, 1) || getIndexTypeOfType(parentType, 0); if (!type) { - error(name_5, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_5)); + error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_8)); return unknownType; } } @@ -10874,26 +11719,55 @@ var ts; return target === checkBase || ts.forEach(getBaseTypes(target), check); } } - function getTypeParametersOfClassOrInterface(symbol) { - var result; - ts.forEach(symbol.declarations, function (node) { - if (node.kind === 203 || node.kind === 202) { - var declaration = node; - if (declaration.typeParameters && declaration.typeParameters.length) { - ts.forEach(declaration.typeParameters, function (node) { - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node)); - if (!result) { - result = [tp]; - } - else if (!ts.contains(result, tp)) { - result.push(tp); - } - }); + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0; _i < declarations.length; _i++) { + var declaration = declarations[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); + } + } + return typeParameters; + } + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; + } + if (node.kind === 202 || node.kind === 201 || + node.kind === 163 || node.kind === 135 || + node.kind === 164) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - }); + } + } + function getOuterTypeParametersOfClassOrInterface(symbol) { + var kind = symbol.flags & 32 ? 202 : 203; + return appendOuterTypeParameters(undefined, ts.getDeclarationOfKind(symbol, kind)); + } + function getLocalTypeParametersOfClassOrInterface(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 203 || node.kind === 202) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } + } return result; } + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterface(symbol)); + } function getBaseTypes(type) { var typeWithBaseTypes = type; if (!typeWithBaseTypes.baseTypes) { @@ -10960,10 +11834,13 @@ var ts; if (!links.declaredType) { var kind = symbol.flags & 32 ? 1024 : 2048; var type = links.declaredType = createObjectType(kind, symbol); - var typeParameters = getTypeParametersOfClassOrInterface(symbol); - if (typeParameters) { + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterface(symbol); + if (outerTypeParameters || localTypeParameters) { type.flags |= 4096; - type.typeParameters = typeParameters; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; type.instantiations = {}; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; @@ -11139,12 +12016,12 @@ var ts; return ts.map(baseSignatures, function (baseSignature) { var signature = baseType.flags & 4096 ? getSignatureInstantiation(baseSignature, baseType.typeArguments) : cloneSignature(baseSignature); - signature.typeParameters = classType.typeParameters; + signature.typeParameters = classType.localTypeParameters; signature.resolvedReturnType = classType; return signature; }); } - return [createSignature(undefined, classType.typeParameters, emptyArray, classType, 0, false, false)]; + return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, 0, false, false)]; } function createTupleTypeMemberSymbols(memberTypes) { var members = {}; @@ -11450,7 +12327,7 @@ var ts; var links = getNodeLinks(declaration); if (!links.resolvedSignature) { var classType = declaration.kind === 136 ? getDeclaredTypeOfClassOrInterface(declaration.parent.symbol) : undefined; - var typeParameters = classType ? classType.typeParameters : + var typeParameters = classType ? classType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : undefined; var parameters = []; var hasStringLiterals = false; @@ -11628,6 +12505,9 @@ var ts; } return type.constraint === noConstraintType ? undefined : type.constraint; } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 129).parent); + } function getTypeListId(types) { switch (types.length) { case 1: @@ -11714,12 +12594,16 @@ var ts; else { type = getDeclaredTypeOfSymbol(symbol); if (type.flags & (1024 | 2048) && type.flags & 4096) { - var typeParameters = type.typeParameters; - if (node.typeArguments && node.typeArguments.length === typeParameters.length) { - type = createTypeReference(type, ts.map(node.typeArguments, getTypeFromTypeNode)); + var localTypeParameters = type.localTypeParameters; + var expectedTypeArgCount = localTypeParameters ? localTypeParameters.length : 0; + var typeArgCount = node.typeArguments ? node.typeArguments.length : 0; + if (typeArgCount === expectedTypeArgCount) { + if (typeArgCount) { + type = createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNode))); + } } else { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1), typeParameters.length); + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1), expectedTypeArgCount); type = undefined; } } @@ -11757,16 +12641,16 @@ var ts; } } if (!symbol) { - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 48128)) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } return type; } @@ -11786,12 +12670,17 @@ var ts; function getGlobalESSymbolConstructorSymbol() { return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol")); } + function createTypeFromGenericGlobalType(genericGlobalType, elementType) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, [elementType]) : emptyObjectType; + } function createIterableType(elementType) { - return globalIterableType !== emptyObjectType ? createTypeReference(globalIterableType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalIterableType, elementType); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(globalIterableIteratorType, elementType); } function createArrayType(elementType) { - var arrayType = globalArrayType || getDeclaredTypeOfSymbol(globalArraySymbol); - return arrayType !== emptyObjectType ? createTypeReference(arrayType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalArrayType, elementType); } function getTypeFromArrayTypeNode(node) { var links = getNodeLinks(node); @@ -11846,13 +12735,7 @@ var ts; } return false; } - var removeSubtypesStack = []; function removeSubtypes(types) { - var typeListId = getTypeListId(types); - if (removeSubtypesStack.lastIndexOf(typeListId) >= 0) { - return; - } - removeSubtypesStack.push(typeListId); var i = types.length; while (i > 0) { i--; @@ -11860,7 +12743,6 @@ var ts; types.splice(i, 1); } } - removeSubtypesStack.pop(); } function containsAnyType(types) { for (var _i = 0; _i < types.length; _i++) { @@ -11910,7 +12792,14 @@ var ts; } function getReducedTypeOfUnionType(type) { if (!type.reducedType) { - type.reducedType = getUnionType(type.types, false); + type.reducedType = circularType; + var reducedType = getUnionType(type.types, false); + if (type.reducedType === circularType) { + type.reducedType = reducedType; + } + } + else if (type.reducedType === circularType) { + type.reducedType = type; } return type.reducedType; } @@ -12094,6 +12983,15 @@ var ts; return result; } function instantiateAnonymousType(type, mapper) { + if (mapper.mappings) { + var cached = mapper.mappings[type.id]; + if (cached) { + return cached; + } + } + else { + mapper.mappings = {}; + } var result = createObjectType(32768, type.symbol); result.properties = instantiateList(getPropertiesOfObjectType(type), mapper, instantiateSymbol); result.members = createSymbolTable(result.properties); @@ -12105,6 +13003,7 @@ var ts; result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); + mapper.mappings[type.id] = result; return result; } function instantiateType(type, mapper) { @@ -12113,7 +13012,7 @@ var ts; return mapper(type); } if (type.flags & 32768) { - return type.symbol && type.symbol.flags & (16 | 8192 | 2048 | 4096) ? + return type.symbol && type.symbol.flags & (16 | 8192 | 32 | 2048 | 4096) ? instantiateAnonymousType(type, mapper) : type; } if (type.flags & 4096) { @@ -13290,10 +14189,10 @@ var ts; } function resolveLocation(node) { var containerNodes = []; - for (var parent_3 = node.parent; parent_3; parent_3 = parent_3.parent) { - if ((ts.isExpression(parent_3) || ts.isObjectLiteralMethod(node)) && - isContextSensitive(parent_3)) { - containerNodes.unshift(parent_3); + for (var parent_5 = node.parent; parent_5; parent_5 = parent_5.parent) { + if ((ts.isExpression(parent_5) || ts.isObjectLiteralMethod(node)) && + isContextSensitive(parent_5)) { + containerNodes.unshift(parent_5); } } ts.forEach(containerNodes, function (node) { getTypeOfNode(node); }); @@ -13698,18 +14597,36 @@ var ts; return undefined; } function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (func && !func.asteriskToken) { + return getContextualReturnType(func); + } + return undefined; + } + function getContextualTypeForYieldOperand(node) { var func = ts.getContainingFunction(node); if (func) { - if (func.type || func.kind === 136 || func.kind === 137 && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(func.symbol, 138))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - } - var signature = getContextualSignatureForFunctionLikeDeclaration(func); - if (signature) { - return getReturnTypeOfSignature(signature); + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } return undefined; } + function getContextualReturnType(functionDecl) { + if (functionDecl.type || + functionDecl.kind === 136 || + functionDecl.kind === 137 && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 138))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); + } + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); + } + return undefined; + } function getContextualTypeForArgument(callTarget, arg) { var args = getEffectiveCallArguments(callTarget); var argIndex = ts.indexOf(args, arg); @@ -13811,7 +14728,7 @@ var ts; var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) || getIndexTypeOfContextualType(type, 1) - || (languageVersion >= 2 ? checkIteratedType(type, undefined) : undefined); + || (languageVersion >= 2 ? getElementTypeOfIterable(type, undefined) : undefined); } return undefined; } @@ -13837,6 +14754,8 @@ var ts; case 164: case 192: return getContextualTypeForReturnExpression(node); + case 173: + return getContextualTypeForYieldOperand(parent); case 158: case 159: return getContextualTypeForArgument(parent, node); @@ -13871,7 +14790,9 @@ var ts; return node.kind === 163 || node.kind === 164; } function getContextualSignatureForFunctionLikeDeclaration(node) { - return isFunctionExpressionOrArrowFunction(node) ? getContextualSignature(node) : undefined; + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; } function getContextualSignature(node) { ts.Debug.assert(node.kind !== 135 || ts.isObjectLiteralMethod(node)); @@ -13946,7 +14867,7 @@ var ts; if (inDestructuringPattern && e.kind === 174) { var restArrayType = checkExpression(e.expression, contextualMapper); var restElementType = getIndexTypeOfType(restArrayType, 1) || - (languageVersion >= 2 ? checkIteratedType(restArrayType, undefined) : undefined); + (languageVersion >= 2 ? getElementTypeOfIterable(restArrayType, undefined) : undefined); if (restElementType) { elementTypes.push(restElementType); } @@ -14169,15 +15090,15 @@ var ts; return unknownType; } if (node.argumentExpression) { - var name_6 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_6 !== undefined) { - var prop = getPropertyOfType(objectType, name_6); + var name_9 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_9 !== undefined) { + var prop = getPropertyOfType(objectType, name_9); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_6, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_9, symbolToString(objectType.symbol)); return unknownType; } } @@ -14267,19 +15188,19 @@ var ts; for (var _i = 0; _i < signatures.length; _i++) { var signature = signatures[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_4 = signature.declaration && signature.declaration.parent; + var parent_6 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_4 === lastParent) { + if (lastParent && parent_6 === lastParent) { index++; } else { - lastParent = parent_4; + lastParent = parent_6; index = cutoffIndex; } } else { index = cutoffIndex = result.length; - lastParent = parent_4; + lastParent = parent_6; } lastSymbol = symbol; if (signature.hasStringLiterals) { @@ -14618,10 +15539,10 @@ var ts; return resolveCall(node, callSignatures, candidatesOutArray); } function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 2) { + if (node.arguments && languageVersion < 1) { var spreadIndex = getSpreadArgumentIndex(node.arguments); if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher); + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); } } var expressionType = checkExpression(node.expression); @@ -14747,14 +15668,37 @@ var ts; type = checkExpressionCached(func.body, contextualMapper); } else { - var types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); - if (types.length === 0) { - return voidType; + var types; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func.body, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; + } + } + else { + types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); + if (types.length === 0) { + return voidType; + } } type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); if (!type) { - error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); - return unknownType; + if (funcIsGenerator) { + error(func, ts.Diagnostics.No_best_common_type_exists_among_yield_expressions); + return createIterableIteratorType(unknownType); + } + else { + error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); + return unknownType; + } + } + if (funcIsGenerator) { + type = createIterableIteratorType(type); } } if (!contextualSignature) { @@ -14762,6 +15706,22 @@ var ts; } return getWidenedType(type); } + function checkAndAggregateYieldOperandTypes(body, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); + } + } + }); + return aggregatedTypes; + } function checkAndAggregateReturnExpressionTypes(body, contextualMapper) { var aggregatedTypes = []; ts.forEachReturnStatement(body, function (returnStatement) { @@ -14897,8 +15857,8 @@ var ts; var index = n.argumentExpression; var symbol = findSymbol(n.expression); if (symbol && index && index.kind === 8) { - var name_7 = index.text; - var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_7); + var name_10 = index.text; + var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_10); return prop && (prop.flags & 3) !== 0 && (getDeclarationFlagsFromSymbol(prop) & 8192) !== 0; } return false; @@ -15029,16 +15989,16 @@ var ts; for (var _i = 0; _i < properties.length; _i++) { var p = properties[_i]; if (p.kind === 225 || p.kind === 226) { - var name_8 = p.name; + var name_11 = p.name; var type = sourceType.flags & 1 ? sourceType : - getTypeOfPropertyOfType(sourceType, name_8.text) || - isNumericLiteralName(name_8.text) && getIndexTypeOfType(sourceType, 1) || + getTypeOfPropertyOfType(sourceType, name_11.text) || + isNumericLiteralName(name_11.text) && getIndexTypeOfType(sourceType, 1) || getIndexTypeOfType(sourceType, 0); if (type) { - checkDestructuringAssignment(p.initializer || name_8, type); + checkDestructuringAssignment(p.initializer || name_11, type); } else { - error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_8)); + error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_11)); } } else { @@ -15253,13 +16213,46 @@ var ts; error(node, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(node.operatorToken.kind), typeToString(leftType), typeToString(rightType)); } } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (current.kind === 202 || current.kind === 175) { + return true; + } + current = parent; + parent = parent.parent; + } + return false; + } function checkYieldExpression(node) { - if (!(node.parserContextFlags & 4)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration); + if (!(node.parserContextFlags & 4) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); } - else { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expressions_are_not_currently_supported); + if (node.expression) { + var func = ts.getContainingFunction(node); + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, undefined); + var expressionElementType; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, undefined); + } + } + } } + return anyType; } function checkConditionalExpression(node, contextualMapper) { checkExpression(node.condition); @@ -15406,8 +16399,7 @@ var ts; case 176: return undefinedType; case 173: - checkYieldExpression(node); - return unknownType; + return checkYieldExpression(node); } return unknownType; } @@ -15445,6 +16437,14 @@ var ts; error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); } } + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; + } + return node.kind === 135 || + node.kind === 201 || + node.kind === 163; + } function checkSignatureDeclaration(node) { if (node.kind === 141) { checkGrammarIndexSignature(node); @@ -15471,6 +16471,19 @@ var ts; break; } } + if (node.type) { + if (languageVersion >= 2 && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } + } + } } checkSpecializedSignatureDeclaration(node); } @@ -16014,7 +17027,6 @@ var ts; function checkFunctionDeclaration(node) { if (produceDiagnostics) { checkFunctionLikeDeclaration(node) || - checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarFunctionName(node.name) || checkGrammarForGenerator(node); checkCollisionWithCapturedSuperVariable(node, node.name); @@ -16046,8 +17058,13 @@ var ts; if (node.type && !isAccessor(node.kind) && !node.asteriskToken) { checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type)); } - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !node.type && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (produceDiagnostics && !node.type) { + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + } } } function checkBlock(node) { @@ -16168,8 +17185,8 @@ var ts; container.kind === 206 || container.kind === 228); if (!namesShareScope) { - var name_9 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_9, name_9); + var name_12 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_12, name_12); } } } @@ -16262,7 +17279,7 @@ var ts; return checkVariableLikeDeclaration(node); } function checkVariableStatement(node) { - checkGrammarDecorators(node) || checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); ts.forEach(node.declarationList.declarations, checkSourceElement); } function checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) { @@ -16385,7 +17402,7 @@ var ts; return inputType; } if (languageVersion >= 2) { - return checkIteratedType(inputType, errorNode) || anyType; + return checkElementTypeOfIterable(inputType, errorNode); } if (allowStringInput) { return checkElementTypeOfArrayOrString(inputType, errorNode); @@ -16399,84 +17416,85 @@ var ts; error(errorNode, ts.Diagnostics.Type_0_is_not_an_array_type, typeToString(inputType)); return unknownType; } - function checkIteratedType(iterable, errorNode) { - ts.Debug.assert(languageVersion >= 2); - var iteratedType = getIteratedType(iterable, errorNode); - if (errorNode && iteratedType) { - checkTypeAssignableTo(iterable, createIterableType(iteratedType), errorNode); + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - return iteratedType; - function getIteratedType(iterable, errorNode) { - // We want to treat type as an iterable, and get the type it is an iterable of. The iterable - // must have the following structure (annotated with the names of the variables below): - // - // { // iterable - // [Symbol.iterator]: { // iteratorFunction - // (): { // iterator - // next: { // iteratorNextFunction - // (): { // iteratorNextResult - // value: T // iteratorNextValue - // } - // } - // } - // } - // } - // - // T is the type we are after. At every level that involves analyzing return types - // of signatures, we union the return types of all the signatures. - // - // Another thing to note is that at any step of this process, we could run into a dead end, - // meaning either the property is missing, or we run into the anyType. If either of these things - // happens, we return undefined to signal that we could not find the iterated type. If a property - // is missing, and the previous step did not result in 'any', then we also give an error if the - // caller requested it. Then the caller can decide what to do in the case where there is no iterated - // type. This is different from returning anyType, because that would signify that we have matched the - // whole pattern and that T (above) is 'any'. - if (allConstituentTypesHaveKind(iterable, 1)) { - return undefined; - } - if ((iterable.flags & 4096) && iterable.target === globalIterableType) { - return iterable.typeArguments[0]; - } - var iteratorFunction = getTypeOfPropertyOfType(iterable, ts.getPropertyNameForKnownSymbolName("iterator")); - if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, 1)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); - } - return undefined; - } - var iterator = getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iterator, 1)) { - return undefined; - } - var iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next"); - if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, 1)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); - } - return undefined; - } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iteratorNextResult, 1)) { - return undefined; - } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; - } - return iteratorNextValue; + return elementType || anyType; + } + function getElementTypeOfIterable(type, errorNode) { + if (type.flags & 1) { + return undefined; } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { + if ((type.flags & 4096) && type.target === globalIterableType) { + typeAsIterable.iterableElementType = type.typeArguments[0]; + } + else { + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (iteratorFunction && iteratorFunction.flags & 1) { + return undefined; + } + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; + } + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)), errorNode); + } + } + return typeAsIterable.iterableElementType; + } + function getElementTypeOfIterator(type, errorNode) { + if (type.flags & 1) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + if ((type.flags & 4096) && type.target === globalIteratorType) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; + } + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (iteratorNextFunction && iteratorNextFunction.flags & 1) { + return undefined; + } + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; + } + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); + if (iteratorNextResult.flags & 1) { + return undefined; + } + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; + } + } + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (type.flags & 1) { + return undefined; + } + if ((type.flags & 4096) && type.target === globalIterableIteratorType) { + return type.typeArguments[0]; + } + return getElementTypeOfIterable(type, undefined) || + getElementTypeOfIterator(type, undefined); } function checkElementTypeOfArrayOrString(arrayOrStringType, errorNode) { ts.Debug.assert(languageVersion < 2); @@ -16528,19 +17546,20 @@ var ts; if (func) { var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var exprType = checkExpressionCached(node.expression); + if (func.asteriskToken) { + return; + } if (func.kind === 138) { error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); } - else { - if (func.kind === 136) { - if (!isTypeAssignableTo(exprType, returnType)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { - checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + else if (func.kind === 136) { + if (!isTypeAssignableTo(exprType, returnType)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } + else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { + checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + } } } } @@ -16734,9 +17753,6 @@ var ts; } function checkClassDeclaration(node) { checkGrammarDeclarationNameInStrictMode(node); - if (node.parent.kind !== 207 && node.parent.kind !== 228) { - grammarErrorOnNode(node, ts.Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration); - } if (!node.name && !(node.flags & 256)) { grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); } @@ -17776,74 +18792,6 @@ var ts; } return node.parent && node.parent.kind === 177; } - function isTypeNode(node) { - if (142 <= node.kind && node.kind <= 150) { - return true; - } - switch (node.kind) { - case 112: - case 120: - case 122: - case 113: - case 123: - return true; - case 99: - return node.parent.kind !== 167; - case 8: - return node.parent.kind === 130; - case 177: - return true; - case 65: - if (node.parent.kind === 127 && node.parent.right === node) { - node = node.parent; - } - else if (node.parent.kind === 156 && node.parent.name === node) { - node = node.parent; - } - case 127: - case 156: - ts.Debug.assert(node.kind === 65 || node.kind === 127 || node.kind === 156, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); - var parent_5 = node.parent; - if (parent_5.kind === 145) { - return false; - } - if (142 <= parent_5.kind && parent_5.kind <= 150) { - return true; - } - switch (parent_5.kind) { - case 177: - return true; - case 129: - return node === parent_5.constraint; - case 133: - case 132: - case 130: - case 199: - return node === parent_5.type; - case 201: - case 163: - case 164: - case 136: - case 135: - case 134: - case 137: - case 138: - return node === parent_5.type; - case 139: - case 140: - case 141: - return node === parent_5.type; - case 161: - return node === parent_5.type; - case 158: - case 159: - return parent_5.typeArguments && ts.indexOf(parent_5.typeArguments, node) >= 0; - case 160: - return false; - } - } - return false; - } function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { while (nodeOnRightSide.parent.kind === 127) { nodeOnRightSide = nodeOnRightSide.parent; @@ -17968,7 +18916,7 @@ var ts; if (isInsideWithStatementBody(node)) { return unknownType; } - if (isTypeNode(node)) { + if (ts.isTypeNode(node)) { return getTypeFromTypeNode(node); } if (ts.isExpression(node)) { @@ -18018,9 +18966,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456) { var symbols = []; - var name_10 = symbol.name; + var name_13 = symbol.name; ts.forEach(getSymbolLinks(symbol).unionType.types, function (t) { - symbols.push(getPropertyOfType(t, name_10)); + symbols.push(getPropertyOfType(t, name_13)); }); return symbols; } @@ -18398,8 +19346,7 @@ var ts; getSymbolLinks(argumentsSymbol).type = getGlobalType("IArguments"); getSymbolLinks(unknownSymbol).type = unknownType; globals[undefinedSymbol.name] = undefinedSymbol; - globalArraySymbol = getGlobalTypeSymbol("Array"); - globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1); + globalArrayType = getGlobalType("Array", 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); @@ -18415,11 +19362,16 @@ var ts; globalESSymbolType = getGlobalType("Symbol"); globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"); globalIterableType = getGlobalType("Iterable", 1); + globalIteratorType = getGlobalType("Iterator", 1); + globalIterableIteratorType = getGlobalType("IterableIterator", 1); } else { globalTemplateStringsArrayType = unknownType; globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); globalESSymbolConstructorSymbol = undefined; + globalIterableType = emptyGenericType; + globalIteratorType = emptyGenericType; + globalIterableIteratorType = emptyGenericType; } anyArrayType = createArrayType(anyType); } @@ -18439,20 +19391,20 @@ var ts; if (impotClause.namedBindings) { var nameBindings = impotClause.namedBindings; if (nameBindings.kind === 212) { - var name_11 = nameBindings.name; - if (isReservedWordInStrictMode(name_11)) { - var nameText = ts.declarationNameToString(name_11); - return grammarErrorOnNode(name_11, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_14 = nameBindings.name; + if (isReservedWordInStrictMode(name_14)) { + var nameText = ts.declarationNameToString(name_14); + return grammarErrorOnNode(name_14, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } else if (nameBindings.kind === 213) { var reportError = false; for (var _i = 0, _a = nameBindings.elements; _i < _a.length; _i++) { var element = _a[_i]; - var name_12 = element.name; - if (isReservedWordInStrictMode(name_12)) { - var nameText = ts.declarationNameToString(name_12); - reportError = reportError || grammarErrorOnNode(name_12, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_15 = element.name; + if (isReservedWordInStrictMode(name_15)) { + var nameText = ts.declarationNameToString(name_15); + reportError = reportError || grammarErrorOnNode(name_15, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } return reportError; @@ -18550,19 +19502,28 @@ var ts; case 135: case 134: case 141: - case 202: - case 203: case 206: - case 205: - case 181: - case 201: - case 204: case 210: case 209: case 216: case 215: case 130: break; + case 202: + case 203: + case 181: + case 201: + case 204: + if (node.modifiers && node.parent.kind !== 207 && node.parent.kind !== 228) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; + case 205: + if (node.modifiers && (node.modifiers.length > 1 || node.modifiers[0].kind !== 70) && + node.parent.kind !== 207 && node.parent.kind !== 228) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; default: return false; } @@ -18660,9 +19621,6 @@ var ts; else if ((node.kind === 210 || node.kind === 209) && flags & 2) { return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_import_declaration, "declare"); } - else if (node.kind === 203 && flags & 2) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare"); - } else if (node.kind === 130 && (flags & 112) && ts.isBindingPattern(node.name)) { return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_a_binding_pattern); } @@ -18874,7 +19832,18 @@ var ts; } function checkGrammarForGenerator(node) { if (node.asteriskToken) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_currently_supported); + ts.Debug.assert(node.kind === 201 || + node.kind === 163 || + node.kind === 135); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_6_or_higher); + } } } function checkGrammarFunctionName(name) { @@ -18894,17 +19863,17 @@ var ts; var inStrictMode = (node.parserContextFlags & 1) !== 0; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_13 = prop.name; + var name_16 = prop.name; if (prop.kind === 176 || - name_13.kind === 128) { - checkGrammarComputedPropertyName(name_13); + name_16.kind === 128) { + checkGrammarComputedPropertyName(name_16); continue; } var currentKind = void 0; if (prop.kind === 225 || prop.kind === 226) { checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_13.kind === 7) { - checkGrammarNumericLiteral(name_13); + if (name_16.kind === 7) { + checkGrammarNumericLiteral(name_16); } currentKind = Property; } @@ -18920,26 +19889,26 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - if (!ts.hasProperty(seen, name_13.text)) { - seen[name_13.text] = currentKind; + if (!ts.hasProperty(seen, name_16.text)) { + seen[name_16.text] = currentKind; } else { - var existingKind = seen[name_13.text]; + var existingKind = seen[name_16.text]; if (currentKind === Property && existingKind === Property) { if (inStrictMode) { - grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); + grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); } } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[name_13.text] = currentKind | existingKind; + seen[name_16.text] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -19718,9 +20687,9 @@ var ts; } var count = 0; while (true) { - var name_14 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_14)) { - return name_14; + var name_17 = baseName + "_" + (++count); + if (!ts.hasProperty(currentSourceFile.identifiers, name_17)) { + return name_17; } } } @@ -20804,9 +21773,9 @@ var ts; var count = tempFlags & 268435455; tempFlags++; if (count !== 8 && count !== 13) { - var name_15 = count < 26 ? "_" + String.fromCharCode(97 + count) : "_" + (count - 26); - if (isUniqueName(name_15)) { - return name_15; + var name_18 = count < 26 ? "_" + String.fromCharCode(97 + count) : "_" + (count - 26); + if (isUniqueName(name_18)) { + return name_18; } } } @@ -20834,8 +21803,8 @@ var ts; } function generateNameForModuleOrEnum(node) { if (node.name.kind === 65) { - var name_16 = node.name.text; - assignGeneratedName(node, isUniqueLocalName(name_16, node) ? name_16 : makeUniqueName(name_16)); + var name_19 = node.name.text; + assignGeneratedName(node, isUniqueLocalName(name_19, node) ? name_19 : makeUniqueName(name_19)); } } function generateNameForImportOrExportDeclaration(node) { @@ -21021,8 +21990,8 @@ var ts; if (scopeName) { var parentIndex = getSourceMapNameIndex(); if (parentIndex !== -1) { - var name_17 = node.name; - if (!name_17 || name_17.kind !== 128) { + var name_20 = node.name; + if (!name_20 || name_20.kind !== 128) { scopeName = "." + scopeName; } scopeName = sourceMapData.sourceMapNames[parentIndex] + scopeName; @@ -21049,9 +22018,9 @@ var ts; node.kind === 202 || node.kind === 205) { if (node.name) { - var name_18 = node.name; - scopeName = name_18.kind === 128 - ? ts.getTextOfNode(name_18) + var name_21 = node.name; + scopeName = name_21.kind === 128 + ? ts.getTextOfNode(name_21) : node.name.text; } recordScopeNameStart(scopeName); @@ -21672,15 +22641,15 @@ var ts; } return true; } - function emitListWithSpread(elements, alwaysCopy, multiLine, trailingComma) { + function emitListWithSpread(elements, needsUniqueCopy, multiLine, trailingComma, useConcat) { var pos = 0; var group = 0; var length = elements.length; while (pos < length) { - if (group === 1) { + if (group === 1 && useConcat) { write(".concat("); } - else if (group > 1) { + else if (group > 0) { write(", "); } var e = elements[pos]; @@ -21688,7 +22657,7 @@ var ts; e = e.expression; emitParenthesizedIf(e, group === 0 && needsParenthesisForPropertyAccessOrInvocation(e)); pos++; - if (pos === length && group === 0 && alwaysCopy && e.kind !== 154) { + if (pos === length && group === 0 && needsUniqueCopy && e.kind !== 154) { write(".slice()"); } } @@ -21711,7 +22680,9 @@ var ts; group++; } if (group > 1) { - write(")"); + if (useConcat) { + write(")"); + } } } function isSpreadElementExpression(node) { @@ -21728,7 +22699,7 @@ var ts; write("]"); } else { - emitListWithSpread(elements, true, (node.flags & 512) !== 0, elements.hasTrailingComma); + emitListWithSpread(elements, true, (node.flags & 512) !== 0, elements.hasTrailingComma, true); } } function emitObjectLiteralBody(node, numElements) { @@ -22054,7 +23025,7 @@ var ts; write("void 0"); } write(", "); - emitListWithSpread(node.arguments, false, false, false); + emitListWithSpread(node.arguments, false, false, false, true); write(")"); } function emitCallExpression(node) { @@ -22088,11 +23059,25 @@ var ts; } function emitNewExpression(node) { write("new "); - emit(node.expression); - if (node.arguments) { + if (languageVersion === 1 && + node.arguments && + hasSpreadElement(node.arguments)) { write("("); - emitCommaList(node.arguments); - write(")"); + var target = emitCallTarget(node.expression); + write(".bind.apply("); + emit(target); + write(", [void 0].concat("); + emitListWithSpread(node.arguments, false, false, false, false); + write(")))"); + write("()"); + } + else { + emit(node.expression); + if (node.arguments) { + write("("); + emitCommaList(node.arguments); + write(")"); + } } } function emitTaggedTemplateExpression(node) { @@ -23044,15 +24029,30 @@ var ts; ts.forEach(node.declarationList.declarations, emitExportVariableAssignments); } } + function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node) { + if (!(node.flags & 1)) { + return true; + } + if (isES6ExportedDeclaration(node)) { + return true; + } + for (var _a = 0, _b = node.declarationList.declarations; _a < _b.length; _a++) { + var declaration = _b[_a]; + if (declaration.initializer) { + return true; + } + } + return false; + } function emitParameter(node) { if (languageVersion < 2) { if (ts.isBindingPattern(node.name)) { - var name_19 = createTempVariable(0); + var name_22 = createTempVariable(0); if (!tempParameters) { tempParameters = []; } - tempParameters.push(name_19); - emit(name_19); + tempParameters.push(name_22); + emit(name_22); } else { emit(node.name); @@ -24512,8 +25512,8 @@ var ts; else { for (var _c = 0, _d = node.exportClause.elements; _c < _d.length; _c++) { var specifier = _d[_c]; - var name_20 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_20] || (exportSpecifiers[name_20] = [])).push(specifier); + var name_23 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_23] || (exportSpecifiers[name_23] = [])).push(specifier); } } break; @@ -24686,11 +25686,11 @@ var ts; var seen = {}; for (var i = 0; i < hoistedVars.length; ++i) { var local = hoistedVars[i]; - var name_21 = local.kind === 65 + var name_24 = local.kind === 65 ? local : local.name; - if (name_21) { - var text = ts.unescapeIdentifier(name_21.text); + if (name_24) { + var text = ts.unescapeIdentifier(name_24.text); if (ts.hasProperty(seen, text)) { continue; } @@ -24769,15 +25769,15 @@ var ts; } if (node.kind === 199 || node.kind === 153) { if (shouldHoistVariable(node, false)) { - var name_22 = node.name; - if (name_22.kind === 65) { + var name_25 = node.name; + if (name_25.kind === 65) { if (!hoistedVars) { hoistedVars = []; } - hoistedVars.push(name_22); + hoistedVars.push(name_25); } else { - ts.forEachChild(name_22, visit); + ts.forEachChild(name_25, visit); } } return; @@ -25144,6 +26144,8 @@ var ts; case 204: case 215: return false; + case 181: + return shouldEmitLeadingAndTrailingCommentsForVariableStatement(node); case 206: return shouldEmitModuleDeclaration(node); case 205: @@ -26403,24 +27405,24 @@ var ts; switch (n.kind) { case 180: if (!ts.isFunctionBlock(n)) { - var parent_6 = n.parent; + var parent_7 = n.parent; var openBrace = ts.findChildOfKind(n, 14, sourceFile); var closeBrace = ts.findChildOfKind(n, 15, sourceFile); - if (parent_6.kind === 185 || - parent_6.kind === 188 || - parent_6.kind === 189 || - parent_6.kind === 187 || - parent_6.kind === 184 || - parent_6.kind === 186 || - parent_6.kind === 193 || - parent_6.kind === 224) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + if (parent_7.kind === 185 || + parent_7.kind === 188 || + parent_7.kind === 189 || + parent_7.kind === 187 || + parent_7.kind === 184 || + parent_7.kind === 186 || + parent_7.kind === 193 || + parent_7.kind === 224) { + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } - if (parent_6.kind === 197) { - var tryStatement = parent_6; + if (parent_7.kind === 197) { + var tryStatement = parent_7; if (tryStatement.tryBlock === n) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } else if (tryStatement.finallyBlock === n) { @@ -26482,10 +27484,10 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_23 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_23); + for (var name_26 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_26); if (declarations) { - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_23); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_26); if (!matches) { continue; } @@ -26496,14 +27498,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_23); + matches = patternMatcher.getMatches(containers, name_26); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_23, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_26, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -26833,9 +27835,9 @@ var ts; case 199: case 153: var variableDeclarationNode; - var name_24; + var name_27; if (node.kind === 153) { - name_24 = node.name; + name_27 = node.name; variableDeclarationNode = node; while (variableDeclarationNode && variableDeclarationNode.kind !== 199) { variableDeclarationNode = variableDeclarationNode.parent; @@ -26845,16 +27847,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_24 = node.name; + name_27 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.variableElement); } case 136: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -27943,7 +28945,7 @@ var ts; ts.findChildOfKind = findChildOfKind; function findContainingList(node) { var syntaxList = ts.forEach(node.parent.getChildren(), function (c) { - if (c.kind === 229 && c.pos <= node.pos && c.end >= node.end) { + if (c.kind === 251 && c.pos <= node.pos && c.end >= node.end) { return c; } }); @@ -28313,10 +29315,6 @@ var ts; }); } ts.signatureToDisplayParts = signatureToDisplayParts; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); - } - ts.isJavaScript = isJavaScript; })(ts || (ts = {})); /// /// @@ -28720,7 +29718,7 @@ var ts; this.NoSpaceBeforeOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 18), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8)); this.NoSpaceAfterOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create3(18, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8)); this.NoSpaceBeforeCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 19), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8)); - this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8)); + this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), 8)); this.FunctionOpenBraceLeftTokenRange = formatting.Shared.TokenRange.AnyIncludingMultilineComments; this.SpaceBeforeOpenBraceInFunction = new formatting.Rule(formatting.RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, 14), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), 2), 1); this.TypeScriptOpenBraceLeftTokenRange = formatting.Shared.TokenRange.FromTokens([65, 3]); @@ -28773,6 +29771,10 @@ var ts; this.SpaceBeforeAt = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 52), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 2)); this.NoSpaceAfterAt = new formatting.Rule(formatting.RuleDescriptor.create3(52, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8)); this.SpaceAfterDecorator = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.Any, formatting.Shared.TokenRange.FromTokens([65, 78, 73, 69, 109, 108, 106, 107, 116, 121, 18, 35])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsEndOfDecoratorContextOnSameLine), 2)); + this.NoSpaceBetweenFunctionKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(83, 35), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 8)); + this.SpaceAfterStarInGeneratorDeclaration = new formatting.Rule(formatting.RuleDescriptor.create3(35, formatting.Shared.TokenRange.FromTokens([65, 16])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 2)); + this.NoSpaceBetweenYieldKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(110, 35), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 8)); + this.SpaceBetweenYieldOrYieldStarAndOperand = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.FromTokens([110, 35]), formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 2)); this.HighPriorityCommonRules = [ this.IgnoreBeforeComment, this.IgnoreAfterLineComment, @@ -28789,7 +29791,9 @@ var ts; this.NoSpaceAfterCloseBrace, this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext, this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets, + this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGeneratorDeclaration, this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, + this.NoSpaceBetweenYieldKeywordAndStar, this.SpaceBetweenYieldOrYieldStarAndOperand, this.NoSpaceBetweenReturnAndSemicolon, this.SpaceAfterCertainKeywords, this.SpaceAfterLetConstInVariableDeclaration, @@ -28846,9 +29850,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_25 in o) { - if (o[name_25] === rule) { - return name_25; + for (var name_28 in o) { + if (o[name_28] === rule) { + return name_28; } } throw new Error("Unknown rule"); @@ -28949,6 +29953,9 @@ var ts; } return false; }; + Rules.IsFunctionDeclarationOrFunctionExpressionContext = function (context) { + return context.contextNode.kind === 201 || context.contextNode.kind === 163; + }; Rules.IsTypeScriptDeclWithBlockContext = function (context) { return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode); }; @@ -29011,6 +30018,9 @@ var ts; Rules.IsSameLineTokenContext = function (context) { return context.TokensAreOnSameLine(); }; + Rules.IsNotBeforeBlockInFunctionDeclarationContext = function (context) { + return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context); + }; Rules.IsEndOfDecoratorContextOnSameLine = function (context) { return context.TokensAreOnSameLine() && context.contextNode.decorators && @@ -29065,6 +30075,9 @@ var ts; Rules.IsVoidOpContext = function (context) { return context.currentTokenSpan.kind === 99 && context.currentTokenParent.kind === 167; }; + Rules.IsYieldOrYieldStarWithOperand = function (context) { + return context.contextNode.kind === 173 && context.contextNode.expression !== undefined; + }; return Rules; })(); formatting.Rules = Rules; @@ -30564,7 +31577,7 @@ var ts; return pos; }; NodeObject.prototype.createSyntaxList = function (nodes) { - var list = createNode(229, nodes.pos, nodes.end, 1024, this); + var list = createNode(251, nodes.pos, nodes.end, 1024, this); list._children = []; var pos = nodes.pos; for (var _i = 0; _i < nodes.length; _i++) { @@ -31244,8 +32257,8 @@ var ts; if (declaration.kind !== 199 && declaration.kind !== 201) { return false; } - for (var parent_7 = declaration.parent; !ts.isFunctionBlock(parent_7); parent_7 = parent_7.parent) { - if (parent_7.kind === 228 || parent_7.kind === 207) { + for (var parent_8 = declaration.parent; !ts.isFunctionBlock(parent_8); parent_8 = parent_8.parent) { + if (parent_8.kind === 228 || parent_8.kind === 207) { return false; } } @@ -32064,7 +33077,7 @@ var ts; return true; } if (parameter.questionToken) { - diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics.can_only_be_used_in_a_ts_file)); + diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics._0_can_only_be_used_in_a_ts_file, '?')); return true; } if (parameter.type) { @@ -32381,12 +33394,12 @@ var ts; function getContainingObjectLiteralApplicableForCompletion(previousToken) { // The locations in an object literal expression that are applicable for completion are property name definition locations. if (previousToken) { - var parent_8 = previousToken.parent; + var parent_9 = previousToken.parent; switch (previousToken.kind) { case 14: case 23: - if (parent_8 && parent_8.kind === 155) { - return parent_8; + if (parent_9 && parent_9.kind === 155) { + return parent_9; } break; } @@ -32564,10 +33577,10 @@ var ts; for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { var sourceFile = _a[_i]; var nameTable = getNameTable(sourceFile); - for (var name_26 in nameTable) { - if (!allNames[name_26]) { - allNames[name_26] = name_26; - var displayName = getCompletionEntryDisplayName(name_26, target, true); + for (var name_29 in nameTable) { + if (!allNames[name_29]) { + allNames[name_29] = name_29; + var displayName = getCompletionEntryDisplayName(name_29, target, true); if (displayName) { var entry = { name: displayName, @@ -33403,17 +34416,17 @@ var ts; function getThrowStatementOwner(throwStatement) { var child = throwStatement; while (child.parent) { - var parent_9 = child.parent; - if (ts.isFunctionBlock(parent_9) || parent_9.kind === 228) { - return parent_9; + var parent_10 = child.parent; + if (ts.isFunctionBlock(parent_10) || parent_10.kind === 228) { + return parent_10; } - if (parent_9.kind === 197) { - var tryStatement = parent_9; + if (parent_10.kind === 197) { + var tryStatement = parent_10; if (tryStatement.tryBlock === child && tryStatement.catchClause) { return child; } } - child = parent_9; + child = parent_10; } return undefined; } @@ -34246,17 +35259,17 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_27 = node.text; + var name_30 = node.text; if (contextualType) { if (contextualType.flags & 16384) { - var unionProperty = contextualType.getProperty(name_27); + var unionProperty = contextualType.getProperty(name_30); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_27); + var symbol = t.getProperty(name_30); if (symbol) { result_4.push(symbol); } @@ -34265,7 +35278,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_27); + var symbol_1 = contextualType.getProperty(name_30); if (symbol_1) { return [symbol_1]; } @@ -35076,7 +36089,7 @@ var ts; var lastEnd = 0; for (var i = 0, n = dense.length; i < n; i += 3) { var start = dense[i]; - var length_1 = dense[i + 1]; + var length_2 = dense[i + 1]; var type = dense[i + 2]; if (lastEnd >= 0) { var whitespaceLength_1 = start - lastEnd; @@ -35084,8 +36097,8 @@ var ts; entries.push({ length: whitespaceLength_1, classification: TokenClass.Whitespace }); } } - entries.push({ length: length_1, classification: convertClassification(type) }); - lastEnd = start + length_1; + entries.push({ length: length_2, classification: convertClassification(type) }); + lastEnd = start + length_2; } var whitespaceLength = text.length - lastEnd; if (whitespaceLength > 0) { diff --git a/bin/typescript.d.ts b/bin/typescript.d.ts index 50f5c1ade22..ede16aa7ed2 100644 --- a/bin/typescript.d.ts +++ b/bin/typescript.d.ts @@ -251,8 +251,30 @@ declare module "typescript" { ShorthandPropertyAssignment = 226, EnumMember = 227, SourceFile = 228, - SyntaxList = 229, - Count = 230, + JSDocTypeExpression = 229, + JSDocAllType = 230, + JSDocUnknownType = 231, + JSDocArrayType = 232, + JSDocUnionType = 233, + JSDocTupleType = 234, + JSDocNullableType = 235, + JSDocNonNullableType = 236, + JSDocRecordType = 237, + JSDocRecordMember = 238, + JSDocTypeReference = 239, + JSDocOptionalType = 240, + JSDocFunctionType = 241, + JSDocVariadicType = 242, + JSDocConstructorType = 243, + JSDocThisType = 244, + JSDocComment = 245, + JSDocTag = 246, + JSDocParameterTag = 247, + JSDocReturnTag = 248, + JSDocTypeTag = 249, + JSDocTemplateTag = 250, + SyntaxList = 251, + Count = 252, FirstAssignment = 53, LastAssignment = 64, FirstReservedWord = 66, @@ -495,7 +517,7 @@ declare module "typescript" { } interface YieldExpression extends Expression { asteriskToken?: Node; - expression: Expression; + expression?: Expression; } interface BinaryExpression extends Expression { left: Expression; @@ -666,7 +688,7 @@ declare module "typescript" { interface ClassElement extends Declaration { _classElementBrand: any; } - interface InterfaceDeclaration extends Declaration, ModuleElement { + interface InterfaceDeclaration extends Declaration, Statement { name: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; @@ -676,7 +698,7 @@ declare module "typescript" { token: SyntaxKind; types?: NodeArray; } - interface TypeAliasDeclaration extends Declaration, ModuleElement { + interface TypeAliasDeclaration extends Declaration, Statement { name: Identifier; type: TypeNode; } @@ -684,7 +706,7 @@ declare module "typescript" { name: DeclarationName; initializer?: Expression; } - interface EnumDeclaration extends Declaration, ModuleElement { + interface EnumDeclaration extends Declaration, Statement { name: Identifier; members: NodeArray; } @@ -739,6 +761,82 @@ declare module "typescript" { hasTrailingNewLine?: boolean; kind: SyntaxKind; } + interface JSDocTypeExpression extends Node { + type: JSDocType; + } + interface JSDocType extends TypeNode { + _jsDocTypeBrand: any; + } + interface JSDocAllType extends JSDocType { + _JSDocAllTypeBrand: any; + } + interface JSDocUnknownType extends JSDocType { + _JSDocUnknownTypeBrand: any; + } + interface JSDocArrayType extends JSDocType { + elementType: JSDocType; + } + interface JSDocUnionType extends JSDocType { + types: NodeArray; + } + interface JSDocTupleType extends JSDocType { + types: NodeArray; + } + interface JSDocNonNullableType extends JSDocType { + type: JSDocType; + } + interface JSDocNullableType extends JSDocType { + type: JSDocType; + } + interface JSDocRecordType extends JSDocType, TypeLiteralNode { + members: NodeArray; + } + interface JSDocTypeReference extends JSDocType { + name: EntityName; + typeArguments: NodeArray; + } + interface JSDocOptionalType extends JSDocType { + type: JSDocType; + } + interface JSDocFunctionType extends JSDocType, SignatureDeclaration { + parameters: NodeArray; + type: JSDocType; + } + interface JSDocVariadicType extends JSDocType { + type: JSDocType; + } + interface JSDocConstructorType extends JSDocType { + type: JSDocType; + } + interface JSDocThisType extends JSDocType { + type: JSDocType; + } + interface JSDocRecordMember extends PropertyDeclaration { + name: Identifier | LiteralExpression; + type?: JSDocType; + } + interface JSDocComment extends Node { + tags: NodeArray; + } + interface JSDocTag extends Node { + atToken: Node; + tagName: Identifier; + } + interface JSDocTemplateTag extends JSDocTag { + typeParameters: NodeArray; + } + interface JSDocReturnTag extends JSDocTag { + typeExpression: JSDocTypeExpression; + } + interface JSDocTypeTag extends JSDocTag { + typeExpression: JSDocTypeExpression; + } + interface JSDocParameterTag extends JSDocTag { + preParameterName?: Identifier; + typeExpression?: JSDocTypeExpression; + postParameterName?: Identifier; + isBracketed: boolean; + } interface SourceFile extends Declaration { statements: NodeArray; endOfFileToken: Node; @@ -1011,6 +1109,8 @@ declare module "typescript" { } interface InterfaceType extends ObjectType { typeParameters: TypeParameter[]; + outerTypeParameters: TypeParameter[]; + localTypeParameters: TypeParameter[]; } interface InterfaceTypeWithBaseTypes extends InterfaceType { baseTypes: ObjectType[]; @@ -1227,8 +1327,6 @@ declare module "typescript" { function getTrailingCommentRanges(text: string, pos: number): CommentRange[]; function isIdentifierStart(ch: number, languageVersion: ScriptTarget): boolean; function isIdentifierPart(ch: number, languageVersion: ScriptTarget): boolean; - /** Creates a scanner over a (possibly unspecified) range of a piece of text. */ - function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, start?: number, length?: number): Scanner; } declare module "typescript" { function getDefaultLibFileName(options: CompilerOptions): string; @@ -1257,8 +1355,10 @@ declare module "typescript" { * Vn. */ function collapseTextChangeRangesAcrossMultipleVersions(changes: TextChangeRange[]): TextChangeRange; + function getTypeParameterOwner(d: Declaration): Declaration; } declare module "typescript" { + var throwOnJSDocErrors: boolean; function getNodeConstructor(kind: SyntaxKind): new () => Node; function createNode(kind: SyntaxKind): Node; function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T; diff --git a/bin/typescript.js b/bin/typescript.js index f91570ad95f..1b5c7949463 100644 --- a/bin/typescript.js +++ b/bin/typescript.js @@ -270,10 +270,35 @@ var ts; SyntaxKind[SyntaxKind["EnumMember"] = 227] = "EnumMember"; // Top-level nodes SyntaxKind[SyntaxKind["SourceFile"] = 228] = "SourceFile"; + // JSDoc nodes. + SyntaxKind[SyntaxKind["JSDocTypeExpression"] = 229] = "JSDocTypeExpression"; + // The * type. + SyntaxKind[SyntaxKind["JSDocAllType"] = 230] = "JSDocAllType"; + // The ? type. + SyntaxKind[SyntaxKind["JSDocUnknownType"] = 231] = "JSDocUnknownType"; + SyntaxKind[SyntaxKind["JSDocArrayType"] = 232] = "JSDocArrayType"; + SyntaxKind[SyntaxKind["JSDocUnionType"] = 233] = "JSDocUnionType"; + SyntaxKind[SyntaxKind["JSDocTupleType"] = 234] = "JSDocTupleType"; + SyntaxKind[SyntaxKind["JSDocNullableType"] = 235] = "JSDocNullableType"; + SyntaxKind[SyntaxKind["JSDocNonNullableType"] = 236] = "JSDocNonNullableType"; + SyntaxKind[SyntaxKind["JSDocRecordType"] = 237] = "JSDocRecordType"; + SyntaxKind[SyntaxKind["JSDocRecordMember"] = 238] = "JSDocRecordMember"; + SyntaxKind[SyntaxKind["JSDocTypeReference"] = 239] = "JSDocTypeReference"; + SyntaxKind[SyntaxKind["JSDocOptionalType"] = 240] = "JSDocOptionalType"; + SyntaxKind[SyntaxKind["JSDocFunctionType"] = 241] = "JSDocFunctionType"; + SyntaxKind[SyntaxKind["JSDocVariadicType"] = 242] = "JSDocVariadicType"; + SyntaxKind[SyntaxKind["JSDocConstructorType"] = 243] = "JSDocConstructorType"; + SyntaxKind[SyntaxKind["JSDocThisType"] = 244] = "JSDocThisType"; + SyntaxKind[SyntaxKind["JSDocComment"] = 245] = "JSDocComment"; + SyntaxKind[SyntaxKind["JSDocTag"] = 246] = "JSDocTag"; + SyntaxKind[SyntaxKind["JSDocParameterTag"] = 247] = "JSDocParameterTag"; + SyntaxKind[SyntaxKind["JSDocReturnTag"] = 248] = "JSDocReturnTag"; + SyntaxKind[SyntaxKind["JSDocTypeTag"] = 249] = "JSDocTypeTag"; + SyntaxKind[SyntaxKind["JSDocTemplateTag"] = 250] = "JSDocTemplateTag"; // Synthesized list - SyntaxKind[SyntaxKind["SyntaxList"] = 229] = "SyntaxList"; + SyntaxKind[SyntaxKind["SyntaxList"] = 251] = "SyntaxList"; // Enum value count - SyntaxKind[SyntaxKind["Count"] = 230] = "Count"; + SyntaxKind[SyntaxKind["Count"] = 252] = "Count"; // Markers SyntaxKind[SyntaxKind["FirstAssignment"] = 53] = "FirstAssignment"; SyntaxKind[SyntaxKind["LastAssignment"] = 64] = "LastAssignment"; @@ -323,6 +348,7 @@ var ts; var NodeFlags = ts.NodeFlags; /* @internal */ (function (ParserContextFlags) { + ParserContextFlags[ParserContextFlags["None"] = 0] = "None"; // Set if this node was parsed in strict mode. Used for grammar error checks, as well as // checking if the node can be reused in incremental settings. ParserContextFlags[ParserContextFlags["StrictMode"] = 1] = "StrictMode"; @@ -338,14 +364,17 @@ var ts; // the parser only sets this directly on the node it creates right after encountering the // error. ParserContextFlags[ParserContextFlags["ThisNodeHasError"] = 32] = "ThisNodeHasError"; + // This node was parsed in a JavaScript file and can be processed differently. For example + // its type can be specified usign a JSDoc comment. + ParserContextFlags[ParserContextFlags["JavaScriptFile"] = 64] = "JavaScriptFile"; // Context flags set directly by the parser. ParserContextFlags[ParserContextFlags["ParserGeneratedFlags"] = 63] = "ParserGeneratedFlags"; // Context flags computed by aggregating child flags upwards. // Used during incremental parsing to determine if this node or any of its children had an // error. Computed only once and then cached. - ParserContextFlags[ParserContextFlags["ThisNodeOrAnySubNodesHasError"] = 64] = "ThisNodeOrAnySubNodesHasError"; + ParserContextFlags[ParserContextFlags["ThisNodeOrAnySubNodesHasError"] = 128] = "ThisNodeOrAnySubNodesHasError"; // Used to know if we've computed data from children and cached it in this node. - ParserContextFlags[ParserContextFlags["HasAggregatedChildData"] = 128] = "HasAggregatedChildData"; + ParserContextFlags[ParserContextFlags["HasAggregatedChildData"] = 256] = "HasAggregatedChildData"; })(ts.ParserContextFlags || (ts.ParserContextFlags = {})); var ParserContextFlags = ts.ParserContextFlags; /* @internal */ @@ -826,6 +855,16 @@ var ts; } } ts.addRange = addRange; + function rangeEquals(array1, array2, pos, end) { + while (pos < end) { + if (array1[pos] !== array2[pos]) { + return false; + } + pos++; + } + return true; + } + ts.rangeEquals = rangeEquals; /** * Returns the last element of an array if non-empty, undefined otherwise. */ @@ -995,8 +1034,10 @@ var ts; var end = start + length; Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); - Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); - Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + if (file) { + Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); + Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + } var text = getLocaleSpecificMessage(message.key); if (arguments.length > 4) { text = formatStringFromArgs(text, arguments, 4); @@ -1584,7 +1625,7 @@ var ts; for (var _i = 0; _i < files.length; _i++) { var current = files[_i]; var name = ts.combinePaths(path, current); - var stat = _fs.lstatSync(name); + var stat = _fs.statSync(name); if (stat.isFile()) { if (!extension || ts.fileExtensionIs(name, extension)) { result.push(name); @@ -1790,7 +1831,7 @@ var ts; Unterminated_template_literal: { code: 1160, category: ts.DiagnosticCategory.Error, key: "Unterminated template literal." }, Unterminated_regular_expression_literal: { code: 1161, category: ts.DiagnosticCategory.Error, key: "Unterminated regular expression literal." }, An_object_member_cannot_be_declared_optional: { code: 1162, category: ts.DiagnosticCategory.Error, key: "An object member cannot be declared optional." }, - yield_expression_must_be_contained_within_a_generator_declaration: { code: 1163, category: ts.DiagnosticCategory.Error, key: "'yield' expression must be contained_within a generator declaration." }, + A_yield_expression_is_only_allowed_in_a_generator_body: { code: 1163, category: ts.DiagnosticCategory.Error, key: "A 'yield' expression is only allowed in a generator body." }, Computed_property_names_are_not_allowed_in_enums: { code: 1164, category: ts.DiagnosticCategory.Error, key: "Computed property names are not allowed in enums." }, A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol: { code: 1165, category: ts.DiagnosticCategory.Error, key: "A computed property name in an ambient context must directly refer to a built-in symbol." }, A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol: { code: 1166, category: ts.DiagnosticCategory.Error, key: "A computed property name in a class property declaration must directly refer to a built-in symbol." }, @@ -1845,6 +1886,10 @@ var ts; Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: ts.DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." }, Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: ts.DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." }, Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Specify_experimentalDecorators_to_remove_this_warning: { code: 1219, category: ts.DiagnosticCategory.Error, key: "Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning." }, + Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1220, category: ts.DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." }, + Generators_are_not_allowed_in_an_ambient_context: { code: 1221, category: ts.DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." }, + An_overload_signature_cannot_be_declared_as_a_generator: { code: 1222, category: ts.DiagnosticCategory.Error, key: "An overload signature cannot be declared as a generator." }, + _0_tag_already_specified: { code: 1223, category: ts.DiagnosticCategory.Error, key: "'{0}' tag already specified." }, Duplicate_identifier_0: { code: 2300, category: ts.DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: ts.DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: ts.DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, @@ -2005,7 +2050,7 @@ var ts; The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: ts.DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." }, Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: ts.DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." }, A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: ts.DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." }, - Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." }, + Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher." }, Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: ts.DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." }, In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: ts.DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." }, const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: ts.DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, @@ -2036,6 +2081,8 @@ var ts; A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: ts.DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." }, _0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: ts.DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." }, Cannot_find_namespace_0: { code: 2503, category: ts.DiagnosticCategory.Error, key: "Cannot find namespace '{0}'." }, + No_best_common_type_exists_among_yield_expressions: { code: 2504, category: ts.DiagnosticCategory.Error, key: "No best common type exists among yield expressions." }, + A_generator_cannot_have_a_void_type_annotation: { code: 2505, category: ts.DiagnosticCategory.Error, key: "A generator cannot have a 'void' type annotation." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: ts.DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." }, @@ -2196,6 +2243,7 @@ var ts; _0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer: { code: 7022, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer." }, _0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7023, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7024, category: ts.DiagnosticCategory.Error, key: "Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, + Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type: { code: 7025, category: ts.DiagnosticCategory.Error, key: "Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "'import ... =' can only be used in a .ts file." }, @@ -2209,16 +2257,12 @@ var ts; types_can_only_be_used_in_a_ts_file: { code: 8010, category: ts.DiagnosticCategory.Error, key: "'types' can only be used in a .ts file." }, type_arguments_can_only_be_used_in_a_ts_file: { code: 8011, category: ts.DiagnosticCategory.Error, key: "'type arguments' can only be used in a .ts file." }, parameter_modifiers_can_only_be_used_in_a_ts_file: { code: 8012, category: ts.DiagnosticCategory.Error, key: "'parameter modifiers' can only be used in a .ts file." }, - can_only_be_used_in_a_ts_file: { code: 8013, category: ts.DiagnosticCategory.Error, key: "'?' can only be used in a .ts file." }, property_declarations_can_only_be_used_in_a_ts_file: { code: 8014, category: ts.DiagnosticCategory.Error, key: "'property declarations' can only be used in a .ts file." }, enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: ts.DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." }, type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: ts.DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." }, decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: ts.DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." }, - yield_expressions_are_not_currently_supported: { code: 9000, category: ts.DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." }, - Generators_are_not_currently_supported: { code: 9001, category: ts.DiagnosticCategory.Error, key: "Generators are not currently supported." }, Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: ts.DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." }, - class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." }, - class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: ts.DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." } + class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." } }; })(ts || (ts = {})); /// @@ -2754,12 +2798,17 @@ var ts; ch > 127 /* maxAsciiCharacter */ && isUnicodeIdentifierPart(ch, languageVersion); } ts.isIdentifierPart = isIdentifierPart; - /** Creates a scanner over a (possibly unspecified) range of a piece of text. */ + /* @internal */ + // Creates a scanner over a (possibly unspecified) range of a piece of text. function createScanner(languageVersion, skipTrivia, text, onError, start, length) { - var pos; // Current position (end position of text of current token) - var end; // end of text - var startPos; // Start position of whitespace before current token - var tokenPos; // Start position of text of current token + // Current position (end position of text of current token) + var pos; + // end of text + var end; + // Start position of whitespace before current token + var startPos; + // Start position of text of current token + var tokenPos; var token; var tokenValue; var precedingLineBreak; @@ -4071,17 +4120,17 @@ var ts; bindBlockScopedDeclaration(node, 32 /* Class */, 899583 /* ClassExcludes */); break; case 203 /* InterfaceDeclaration */: - bindDeclaration(node, 64 /* Interface */, 792992 /* InterfaceExcludes */, false); + bindBlockScopedDeclaration(node, 64 /* Interface */, 792992 /* InterfaceExcludes */); break; case 204 /* TypeAliasDeclaration */: - bindDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */, false); + bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); break; case 205 /* EnumDeclaration */: if (ts.isConst(node)) { - bindDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */, false); + bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */); } else { - bindDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */, false); + bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); } break; case 206 /* ModuleDeclaration */: @@ -4230,11 +4279,11 @@ var ts; // Returns true if this node contains a parse error anywhere underneath it. function containsParseError(node) { aggregateChildData(node); - return (node.parserContextFlags & 64 /* ThisNodeOrAnySubNodesHasError */) !== 0; + return (node.parserContextFlags & 128 /* ThisNodeOrAnySubNodesHasError */) !== 0; } ts.containsParseError = containsParseError; function aggregateChildData(node) { - if (!(node.parserContextFlags & 128 /* HasAggregatedChildData */)) { + if (!(node.parserContextFlags & 256 /* HasAggregatedChildData */)) { // A node is considered to contain a parse error if: // a) the parser explicitly marked that it had an error // b) any of it's children reported that it had an error. @@ -4242,12 +4291,12 @@ var ts; ts.forEachChild(node, containsParseError); // If so, mark ourselves accordingly. if (thisNodeOrAnySubNodesHasError) { - node.parserContextFlags |= 64 /* ThisNodeOrAnySubNodesHasError */; + node.parserContextFlags |= 128 /* ThisNodeOrAnySubNodesHasError */; } // Also mark that we've propogated the child information to this node. This way we can // always consult the bit directly on this node without needing to check its children // again. - node.parserContextFlags |= 128 /* HasAggregatedChildData */; + node.parserContextFlags |= 256 /* HasAggregatedChildData */; } } function getSourceFileOfNode(node) { @@ -4534,6 +4583,88 @@ var ts; } ts.getJsDocComments = getJsDocComments; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; + function isTypeNode(node) { + if (142 /* FirstTypeNode */ <= node.kind && node.kind <= 150 /* LastTypeNode */) { + return true; + } + switch (node.kind) { + case 112 /* AnyKeyword */: + case 120 /* NumberKeyword */: + case 122 /* StringKeyword */: + case 113 /* BooleanKeyword */: + case 123 /* SymbolKeyword */: + return true; + case 99 /* VoidKeyword */: + return node.parent.kind !== 167 /* VoidExpression */; + case 8 /* StringLiteral */: + // Specialized signatures can have string literals as their parameters' type names + return node.parent.kind === 130 /* Parameter */; + case 177 /* ExpressionWithTypeArguments */: + return true; + // Identifiers and qualified names may be type nodes, depending on their context. Climb + // above them to find the lowest container + case 65 /* Identifier */: + // If the identifier is the RHS of a qualified name, then it's a type iff its parent is. + if (node.parent.kind === 127 /* QualifiedName */ && node.parent.right === node) { + node = node.parent; + } + else if (node.parent.kind === 156 /* PropertyAccessExpression */ && node.parent.name === node) { + node = node.parent; + } + // fall through + case 127 /* QualifiedName */: + case 156 /* PropertyAccessExpression */: + // At this point, node is either a qualified name or an identifier + ts.Debug.assert(node.kind === 65 /* Identifier */ || node.kind === 127 /* QualifiedName */ || node.kind === 156 /* PropertyAccessExpression */, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); + var parent_1 = node.parent; + if (parent_1.kind === 145 /* TypeQuery */) { + return false; + } + // Do not recursively call isTypeNode on the parent. In the example: + // + // let a: A.B.C; + // + // Calling isTypeNode would consider the qualified name A.B a type node. Only C or + // A.B.C is a type node. + if (142 /* FirstTypeNode */ <= parent_1.kind && parent_1.kind <= 150 /* LastTypeNode */) { + return true; + } + switch (parent_1.kind) { + case 177 /* ExpressionWithTypeArguments */: + return true; + case 129 /* TypeParameter */: + return node === parent_1.constraint; + case 133 /* PropertyDeclaration */: + case 132 /* PropertySignature */: + case 130 /* Parameter */: + case 199 /* VariableDeclaration */: + return node === parent_1.type; + case 201 /* FunctionDeclaration */: + case 163 /* FunctionExpression */: + case 164 /* ArrowFunction */: + case 136 /* Constructor */: + case 135 /* MethodDeclaration */: + case 134 /* MethodSignature */: + case 137 /* GetAccessor */: + case 138 /* SetAccessor */: + return node === parent_1.type; + case 139 /* CallSignature */: + case 140 /* ConstructSignature */: + case 141 /* IndexSignature */: + return node === parent_1.type; + case 161 /* TypeAssertionExpression */: + return node === parent_1.type; + case 158 /* CallExpression */: + case 159 /* NewExpression */: + return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + case 160 /* TaggedTemplateExpression */: + // TODO (drosen): TaggedTemplateExpressions may eventually support type arguments. + return false; + } + } + return false; + } + ts.isTypeNode = isTypeNode; // Warning: This has the same semantics as the forEach family of functions, // in that traversal terminates in the event that 'visitor' supplies a truthy value. function forEachReturnStatement(body, visitor) { @@ -4562,6 +4693,44 @@ var ts; } } ts.forEachReturnStatement = forEachReturnStatement; + function forEachYieldExpression(body, visitor) { + return traverse(body); + function traverse(node) { + switch (node.kind) { + case 173 /* YieldExpression */: + visitor(node); + var operand = node.expression; + if (operand) { + traverse(operand); + } + case 205 /* EnumDeclaration */: + case 203 /* InterfaceDeclaration */: + case 206 /* ModuleDeclaration */: + case 204 /* TypeAliasDeclaration */: + case 202 /* ClassDeclaration */: + // These are not allowed inside a generator now, but eventually they may be allowed + // as local types. Regardless, any yield statements contained within them should be + // skipped in this traversal. + return; + default: + if (isFunctionLike(node)) { + var name_3 = node.name; + if (name_3 && name_3.kind === 128 /* ComputedPropertyName */) { + // Note that we will not include methods/accessors of a class because they would require + // first descending into the class. This is by design. + traverse(name_3.expression); + return; + } + } + else if (!isTypeNode(node)) { + // This is the general case, which should include mostly expressions and statements. + // Also includes NodeArrays. + ts.forEachChild(node, traverse); + } + } + } + } + ts.forEachYieldExpression = forEachYieldExpression; function isVariableLike(node) { if (node) { switch (node.kind) { @@ -4590,6 +4759,12 @@ var ts; return false; } ts.isAccessor = isAccessor; + function isClassLike(node) { + if (node) { + return node.kind === 202 /* ClassDeclaration */ || node.kind === 175 /* ClassExpression */; + } + } + ts.isClassLike = isClassLike; function isFunctionLike(node) { if (node) { switch (node.kind) { @@ -4840,6 +5015,7 @@ var ts; case 172 /* TemplateExpression */: case 10 /* NoSubstitutionTemplateLiteral */: case 176 /* OmittedExpression */: + case 173 /* YieldExpression */: return true; case 127 /* QualifiedName */: while (node.parent.kind === 127 /* QualifiedName */) { @@ -4853,8 +5029,8 @@ var ts; // fall through case 7 /* NumericLiteral */: case 8 /* StringLiteral */: - var parent_1 = node.parent; - switch (parent_1.kind) { + var parent_2 = node.parent; + switch (parent_2.kind) { case 199 /* VariableDeclaration */: case 130 /* Parameter */: case 133 /* PropertyDeclaration */: @@ -4862,7 +5038,7 @@ var ts; case 227 /* EnumMember */: case 225 /* PropertyAssignment */: case 153 /* BindingElement */: - return parent_1.initializer === node; + return parent_2.initializer === node; case 183 /* ExpressionStatement */: case 184 /* IfStatement */: case 185 /* DoStatement */: @@ -4873,27 +5049,27 @@ var ts; case 221 /* CaseClause */: case 196 /* ThrowStatement */: case 194 /* SwitchStatement */: - return parent_1.expression === node; + return parent_2.expression === node; case 187 /* ForStatement */: - var forStatement = parent_1; + var forStatement = parent_2; return (forStatement.initializer === node && forStatement.initializer.kind !== 200 /* VariableDeclarationList */) || forStatement.condition === node || forStatement.incrementor === node; case 188 /* ForInStatement */: case 189 /* ForOfStatement */: - var forInStatement = parent_1; + var forInStatement = parent_2; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 200 /* VariableDeclarationList */) || forInStatement.expression === node; case 161 /* TypeAssertionExpression */: - return node === parent_1.expression; + return node === parent_2.expression; case 178 /* TemplateSpan */: - return node === parent_1.expression; + return node === parent_2.expression; case 128 /* ComputedPropertyName */: - return node === parent_1.expression; + return node === parent_2.expression; case 131 /* Decorator */: return true; default: - if (isExpression(parent_1)) { + if (isExpression(parent_2)) { return true; } } @@ -4961,6 +5137,74 @@ var ts; return s.parameters.length > 0 && ts.lastOrUndefined(s.parameters).dotDotDotToken !== undefined; } ts.hasRestParameters = hasRestParameters; + function isJSDocConstructSignature(node) { + return node.kind === 241 /* JSDocFunctionType */ && + node.parameters.length > 0 && + node.parameters[0].type.kind === 243 /* JSDocConstructorType */; + } + ts.isJSDocConstructSignature = isJSDocConstructSignature; + function getJSDocTag(node, kind) { + if (node && node.jsDocComment) { + for (var _i = 0, _a = node.jsDocComment.tags; _i < _a.length; _i++) { + var tag = _a[_i]; + if (tag.kind === kind) { + return tag; + } + } + } + } + function getJSDocTypeTag(node) { + return getJSDocTag(node, 249 /* JSDocTypeTag */); + } + ts.getJSDocTypeTag = getJSDocTypeTag; + function getJSDocReturnTag(node) { + return getJSDocTag(node, 248 /* JSDocReturnTag */); + } + ts.getJSDocReturnTag = getJSDocReturnTag; + function getJSDocTemplateTag(node) { + return getJSDocTag(node, 250 /* JSDocTemplateTag */); + } + ts.getJSDocTemplateTag = getJSDocTemplateTag; + function getCorrespondingJSDocParameterTag(parameter) { + if (parameter.name && parameter.name.kind === 65 /* Identifier */) { + // If it's a parameter, see if the parent has a jsdoc comment with an @param + // annotation. + var parameterName = parameter.name.text; + var docComment = parameter.parent.jsDocComment; + if (docComment) { + return ts.forEach(docComment.tags, function (t) { + if (t.kind === 247 /* JSDocParameterTag */) { + var parameterTag = t; + var name_4 = parameterTag.preParameterName || parameterTag.postParameterName; + if (name_4.text === parameterName) { + return t; + } + } + }); + } + } + } + ts.getCorrespondingJSDocParameterTag = getCorrespondingJSDocParameterTag; + function hasRestParameter(s) { + return isRestParameter(ts.lastOrUndefined(s.parameters)); + } + ts.hasRestParameter = hasRestParameter; + function isRestParameter(node) { + if (node) { + if (node.parserContextFlags & 64 /* JavaScriptFile */) { + if (node.type && node.type.kind === 242 /* JSDocVariadicType */) { + return true; + } + var paramTag = getCorrespondingJSDocParameterTag(node); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 242 /* JSDocVariadicType */; + } + } + return node.dotDotDotToken !== undefined; + } + return false; + } + ts.isRestParameter = isRestParameter; function isLiteralKind(kind) { return 7 /* FirstLiteralToken */ <= kind && kind <= 10 /* LastLiteralToken */; } @@ -5751,6 +5995,10 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 256 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; + function isJavaScript(fileName) { + return ts.fileExtensionIs(fileName, ".js"); + } + ts.isJavaScript = isJavaScript; /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. @@ -6024,12 +6272,23 @@ var ts; return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), newEndN - oldStartN); } ts.collapseTextChangeRangesAcrossMultipleVersions = collapseTextChangeRangesAcrossMultipleVersions; + function getTypeParameterOwner(d) { + if (d && d.kind === 129 /* TypeParameter */) { + for (var current = d; current; current = current.parent) { + if (ts.isFunctionLike(current) || ts.isClassLike(current) || current.kind === 203 /* InterfaceDeclaration */) { + return current; + } + } + } + } + ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); /// /// var ts; (function (ts) { - var nodeConstructors = new Array(230 /* Count */); + ts.throwOnJSDocErrors = false; + var nodeConstructors = new Array(252 /* Count */); /* @internal */ ts.parseTime = 0; function getNodeConstructor(kind) { return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); @@ -6339,6 +6598,49 @@ var ts; return visitNode(cbNode, node.expression); case 219 /* MissingDeclaration */: return visitNodes(cbNodes, node.decorators); + case 229 /* JSDocTypeExpression */: + return visitNode(cbNode, node.type); + case 233 /* JSDocUnionType */: + return visitNodes(cbNodes, node.types); + case 234 /* JSDocTupleType */: + return visitNodes(cbNodes, node.types); + case 232 /* JSDocArrayType */: + return visitNode(cbNode, node.elementType); + case 236 /* JSDocNonNullableType */: + return visitNode(cbNode, node.type); + case 235 /* JSDocNullableType */: + return visitNode(cbNode, node.type); + case 237 /* JSDocRecordType */: + return visitNodes(cbNodes, node.members); + case 239 /* JSDocTypeReference */: + return visitNode(cbNode, node.name) || + visitNodes(cbNodes, node.typeArguments); + case 240 /* JSDocOptionalType */: + return visitNode(cbNode, node.type); + case 241 /* JSDocFunctionType */: + return visitNodes(cbNodes, node.parameters) || + visitNode(cbNode, node.type); + case 242 /* JSDocVariadicType */: + return visitNode(cbNode, node.type); + case 243 /* JSDocConstructorType */: + return visitNode(cbNode, node.type); + case 244 /* JSDocThisType */: + return visitNode(cbNode, node.type); + case 238 /* JSDocRecordMember */: + return visitNode(cbNode, node.name) || + visitNode(cbNode, node.type); + case 245 /* JSDocComment */: + return visitNodes(cbNodes, node.tags); + case 247 /* JSDocParameterTag */: + return visitNode(cbNode, node.preParameterName) || + visitNode(cbNode, node.typeExpression) || + visitNode(cbNode, node.postParameterName); + case 248 /* JSDocReturnTag */: + return visitNode(cbNode, node.typeExpression); + case 249 /* JSDocTypeTag */: + return visitNode(cbNode, node.typeExpression); + case 250 /* JSDocTemplateTag */: + return visitNodes(cbNodes, node.typeParameters); } } ts.forEachChild = forEachChild; @@ -6363,6 +6665,17 @@ var ts; return IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks); } ts.updateSourceFile = updateSourceFile; + /* @internal */ + function parseIsolatedJSDocComment(content, start, length) { + return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + } + ts.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + /* @internal */ + // Exposed only for testing. + function parseJSDocTypeExpressionForTests(content, start, length) { + return Parser.JSDocParser.parseJSDocTypeExpressionForTests(content, start, length); + } + ts.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; // Implement the parser as a singleton module. We do this for perf reasons because creating // parser instances can actually be expensive enough to impact us on projects with many source // files. @@ -6373,6 +6686,7 @@ var ts; var scanner = ts.createScanner(2 /* Latest */, true); var disallowInAndDecoratorContext = 2 /* DisallowIn */ | 16 /* Decorator */; var sourceFile; + var parseDiagnostics; var syntaxCursor; var token; var sourceText; @@ -6426,7 +6740,7 @@ var ts; // Note: it should not be necessary to save/restore these flags during speculative/lookahead // parsing. These context flags are naturally stored and restored through normal recursive // descent parsing and unwinding. - var contextFlags = 0; + var contextFlags; // Whether or not we've had a parse error since creating the last AST node. If we have // encountered an error, it will be stored on the next AST node we create. Parse errors // can be broken down into three categories: @@ -6455,20 +6769,49 @@ var ts; // Note: any errors at the end of the file that do not precede a regular node, should get // attached to the EOF token. var parseErrorBeforeNextFinishedNode = false; + (function (StatementFlags) { + StatementFlags[StatementFlags["None"] = 0] = "None"; + StatementFlags[StatementFlags["Statement"] = 1] = "Statement"; + StatementFlags[StatementFlags["ModuleElement"] = 2] = "ModuleElement"; + StatementFlags[StatementFlags["StatementOrModuleElement"] = 3] = "StatementOrModuleElement"; + })(Parser.StatementFlags || (Parser.StatementFlags = {})); + var StatementFlags = Parser.StatementFlags; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { + initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); + clearState(); + return result; + } + Parser.parseSourceFile = parseSourceFile; + function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { sourceText = _sourceText; syntaxCursor = _syntaxCursor; + parseDiagnostics = []; parsingContext = 0; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = 0; + contextFlags = ts.isJavaScript(fileName) ? 64 /* JavaScriptFile */ : 0 /* None */; parseErrorBeforeNextFinishedNode = false; - createSourceFile(fileName, languageVersion); // Initialize and prime the scanner before parsing the source elements. scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); + } + function clearState() { + // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. + scanner.setText(""); + scanner.setOnError(undefined); + // Clear any data. We don't want to accidently hold onto it for too long. + parseDiagnostics = undefined; + sourceFile = undefined; + identifiers = undefined; + syntaxCursor = undefined; + sourceText = undefined; + } + function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { + sourceFile = createSourceFile(fileName, languageVersion); + // Prime the scanner. token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0 /* SourceElements */, true, parseSourceElement); @@ -6478,22 +6821,45 @@ var ts; sourceFile.nodeCount = nodeCount; sourceFile.identifierCount = identifierCount; sourceFile.identifiers = identifiers; + sourceFile.parseDiagnostics = parseDiagnostics; if (setParentNodes) { fixupParentReferences(sourceFile); } - syntaxCursor = undefined; - // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. - scanner.setText(""); - scanner.setOnError(undefined); - var result = sourceFile; - // Clear any data. We don't want to accidently hold onto it for too long. - sourceFile = undefined; - identifiers = undefined; - syntaxCursor = undefined; - sourceText = undefined; - return result; + // If this is a javascript file, proactively see if we can get JSDoc comments for + // relevant nodes in the file. We'll use these to provide typing informaion if they're + // available. + if (ts.isJavaScript(fileName)) { + addJSDocComments(); + } + return sourceFile; + } + function addJSDocComments() { + forEachChild(sourceFile, visit); + return; + function visit(node) { + // Add additional cases as necessary depending on how we see JSDoc comments used + // in the wild. + switch (node.kind) { + case 181 /* VariableStatement */: + case 201 /* FunctionDeclaration */: + case 130 /* Parameter */: + addJSDocComment(node); + } + forEachChild(node, visit); + } + } + function addJSDocComment(node) { + var comments = ts.getLeadingCommentRangesOfNode(node, sourceFile); + if (comments) { + for (var _i = 0; _i < comments.length; _i++) { + var comment = comments[_i]; + var jsDocComment = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); + if (jsDocComment) { + node.jsDocComment = jsDocComment; + } + } + } } - Parser.parseSourceFile = parseSourceFile; function fixupParentReferences(sourceFile) { // normally parent references are set during binding. However, for clients that only need // a syntax tree, and no semantic features, then the binding process is an unnecessary @@ -6515,16 +6881,17 @@ var ts; } } } + Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - sourceFile = createNode(228 /* SourceFile */, 0); + var sourceFile = createNode(228 /* SourceFile */, 0); sourceFile.pos = 0; sourceFile.end = sourceText.length; sourceFile.text = sourceText; - sourceFile.parseDiagnostics = []; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 2048 /* DeclarationFile */ : 0; + return sourceFile; } function setContextFlag(val, flag) { if (val) { @@ -6632,9 +6999,9 @@ var ts; } function parseErrorAtPosition(start, length, message, arg0) { // Don't report another error if it would just be at the same position as the last error. - var lastError = ts.lastOrUndefined(sourceFile.parseDiagnostics); + var lastError = ts.lastOrUndefined(parseDiagnostics); if (!lastError || start !== lastError.start) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); } // Mark that we've encountered an error. We'll set an appropriate bit on the next // node we finish so that it can't be reused incrementally. @@ -6669,7 +7036,7 @@ var ts; // Keep track of the state we'll need to rollback to if lookahead fails (or if the // caller asked us to always reset our state). var saveToken = token; - var saveParseDiagnosticsLength = sourceFile.parseDiagnostics.length; + var saveParseDiagnosticsLength = parseDiagnostics.length; var saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; // Note: it is not actually necessary to save/restore the context flags here. That's // because the saving/restorating of these flags happens naturally through the recursive @@ -6687,7 +7054,7 @@ var ts; // then unconditionally restore us to where we were. if (!result || isLookAhead) { token = saveToken; - sourceFile.parseDiagnostics.length = saveParseDiagnosticsLength; + parseDiagnostics.length = saveParseDiagnosticsLength; parseErrorBeforeNextFinishedNode = saveParseErrorBeforeNextFinishedNode; } return result; @@ -6783,8 +7150,8 @@ var ts; node.end = pos; return node; } - function finishNode(node) { - node.end = scanner.getStartPos(); + function finishNode(node, end) { + node.end = end === undefined ? scanner.getStartPos() : end; if (contextFlags) { node.parserContextFlags = contextFlags; } @@ -6840,15 +7207,24 @@ var ts; token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */; } - function parsePropertyName() { + function parsePropertyNameWorker(allowComputedPropertyNames) { if (token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */) { return parseLiteralNode(true); } - if (token === 18 /* OpenBracketToken */) { + if (allowComputedPropertyNames && token === 18 /* OpenBracketToken */) { return parseComputedPropertyName(); } return parseIdentifierName(); } + function parsePropertyName() { + return parsePropertyNameWorker(true); + } + function parseSimplePropertyName() { + return parsePropertyNameWorker(false); + } + function isSimplePropertyName() { + return token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */ || isIdentifierOrKeyword(); + } function parseComputedPropertyName() { // PropertyName[Yield,GeneratorParameter] : // LiteralPropertyName @@ -6917,10 +7293,17 @@ var ts; switch (parsingContext) { case 0 /* SourceElements */: case 1 /* ModuleElements */: - return isSourceElement(inErrorRecovery); + // If we're in error recovery, then we don't want to treat ';' as an empty statement. + // The problem is that ';' can show up in far too many contexts, and if we see one + // and assume it's a statement, then we may bail out inappropriately from whatever + // we're parsing. For example, if we have a semicolon in the middle of a class, then + // we really don't want to assume the class is over and we're on a statement in the + // outer module. We just want to consume and move on. + return !(token === 22 /* SemicolonToken */ && inErrorRecovery) && isStartOfModuleElement(); case 2 /* BlockStatements */: case 4 /* SwitchClauseStatements */: - return isStartOfStatement(inErrorRecovery); + // During error recovery we don't treat empty statements as statements + return !(token === 22 /* SemicolonToken */ && inErrorRecovery) && isStartOfStatement(); case 3 /* SwitchClauses */: return token === 67 /* CaseKeyword */ || token === 73 /* DefaultKeyword */; case 5 /* TypeMembers */: @@ -6972,6 +7355,12 @@ var ts; return isHeritageClause(); case 20 /* ImportOrExportSpecifiers */: return isIdentifierOrKeyword(); + case 21 /* JSDocFunctionParameters */: + case 22 /* JSDocTypeArguments */: + case 24 /* JSDocTupleTypes */: + return JSDocParser.isJSDocType(); + case 23 /* JSDocRecordMembers */: + return isSimplePropertyName(); } ts.Debug.fail("Non-exhaustive case in 'isListElement'."); } @@ -7046,6 +7435,14 @@ var ts; return token === 25 /* GreaterThanToken */ || token === 16 /* OpenParenToken */; case 19 /* HeritageClauses */: return token === 14 /* OpenBraceToken */ || token === 15 /* CloseBraceToken */; + case 21 /* JSDocFunctionParameters */: + return token === 17 /* CloseParenToken */ || token === 51 /* ColonToken */ || token === 15 /* CloseBraceToken */; + case 22 /* JSDocTypeArguments */: + return token === 25 /* GreaterThanToken */ || token === 15 /* CloseBraceToken */; + case 24 /* JSDocTupleTypes */: + return token === 19 /* CloseBracketToken */ || token === 15 /* CloseBraceToken */; + case 23 /* JSDocRecordMembers */: + return token === 15 /* CloseBraceToken */; } } function isVariableDeclaratorListTerminator() { @@ -7071,7 +7468,7 @@ var ts; } // True if positioned at element or terminator of the current list or any enclosing list function isInSomeParsingContext() { - for (var kind = 0; kind < 21 /* Count */; kind++) { + for (var kind = 0; kind < 25 /* Count */; kind++) { if (parsingContext & (1 << kind)) { if (isListElement(kind, true) || isListTerminator(kind)) { return true; @@ -7094,7 +7491,7 @@ var ts; // test elements only if we are not already in strict mode if (checkForStrictMode && !inStrictModeContext()) { if (ts.isPrologueDirective(element)) { - if (isUseStrictPrologueDirective(sourceFile, element)) { + if (isUseStrictPrologueDirective(element)) { setStrictModeContext(true); checkForStrictMode = false; } @@ -7115,9 +7512,9 @@ var ts; return result; } /// Should be called only on prologue directives (isPrologueDirective(node) should be true) - function isUseStrictPrologueDirective(sourceFile, node) { + function isUseStrictPrologueDirective(node) { ts.Debug.assert(ts.isPrologueDirective(node)); - var nodeText = ts.getSourceTextOfNodeFromSourceFile(sourceFile, node.expression); + var nodeText = ts.getTextOfNodeFromSourceText(sourceText, node.expression); // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the // string to contain unicode escapes (as per ES5). return nodeText === '"use strict"' || nodeText === "'use strict'"; @@ -7390,6 +7787,10 @@ var ts; case 18 /* TupleElementTypes */: return ts.Diagnostics.Type_expected; case 19 /* HeritageClauses */: return ts.Diagnostics.Unexpected_token_expected; case 20 /* ImportOrExportSpecifiers */: return ts.Diagnostics.Identifier_expected; + case 21 /* JSDocFunctionParameters */: return ts.Diagnostics.Parameter_declaration_expected; + case 22 /* JSDocTypeArguments */: return ts.Diagnostics.Type_argument_expected; + case 24 /* JSDocTupleTypes */: return ts.Diagnostics.Type_expected; + case 23 /* JSDocRecordMembers */: return ts.Diagnostics.Property_assignment_expected; } } ; @@ -8249,11 +8650,6 @@ var ts; nextToken(); return !scanner.hasPrecedingLineBreak() && isIdentifier(); } - function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { - nextToken(); - return !scanner.hasPrecedingLineBreak() && - (isIdentifier() || token === 14 /* OpenBraceToken */ || token === 18 /* OpenBracketToken */); - } function parseYieldExpression() { var node = createNode(173 /* YieldExpression */); // YieldExpression[In] : @@ -8425,10 +8821,11 @@ var ts; if (token === 14 /* OpenBraceToken */) { return parseFunctionBlock(false, false); } - if (isStartOfStatement(true) && - !isStartOfExpressionStatement() && + if (token !== 22 /* SemicolonToken */ && token !== 83 /* FunctionKeyword */ && - token !== 69 /* ClassKeyword */) { + token !== 69 /* ClassKeyword */ && + isStartOfStatement() && + !isStartOfExpressionStatement()) { // Check if we got a plain statement (i.e. no expression-statements, no function/class expressions/declarations) // // Here we try to recover from a potential error situation in the case where the @@ -9217,33 +9614,68 @@ var ts; return finishNode(expressionStatement); } } - function isStartOfStatement(inErrorRecovery) { - // Functions, variable statements and classes are allowed as a statement. But as per - // the grammar, they also allow modifiers. So we have to check for those statements - // that might be following modifiers.This ensures that things work properly when - // incrementally parsing as the parser will produce the same FunctionDeclaraiton, - // VariableStatement or ClassDeclaration, if it has the same text regardless of whether - // it is inside a block or not. - if (ts.isModifier(token)) { - var result = lookAhead(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return true; + function isIdentifierOrKeyword() { + return token >= 65 /* Identifier */; + } + function nextTokenIsIdentifierOrKeywordOnSameLine() { + nextToken(); + return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + } + function parseDeclarationFlags() { + while (true) { + switch (token) { + case 98 /* VarKeyword */: + case 104 /* LetKeyword */: + case 70 /* ConstKeyword */: + case 83 /* FunctionKeyword */: + case 69 /* ClassKeyword */: + case 77 /* EnumKeyword */: + return 1 /* Statement */; + case 103 /* InterfaceKeyword */: + case 124 /* TypeKeyword */: + nextToken(); + return isIdentifierOrKeyword() ? 1 /* Statement */ : 0 /* None */; + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + nextToken(); + return isIdentifierOrKeyword() || token === 8 /* StringLiteral */ ? 2 /* ModuleElement */ : 0 /* None */; + case 85 /* ImportKeyword */: + nextToken(); + return token === 8 /* StringLiteral */ || token === 35 /* AsteriskToken */ || + token === 14 /* OpenBraceToken */ || isIdentifierOrKeyword() ? + 2 /* ModuleElement */ : 0 /* None */; + case 78 /* ExportKeyword */: + nextToken(); + if (token === 53 /* EqualsToken */ || token === 35 /* AsteriskToken */ || + token === 14 /* OpenBraceToken */ || token === 73 /* DefaultKeyword */) { + return 2 /* ModuleElement */; + } + continue; + case 115 /* DeclareKeyword */: + case 108 /* PublicKeyword */: + case 106 /* PrivateKeyword */: + case 107 /* ProtectedKeyword */: + case 109 /* StaticKeyword */: + nextToken(); + continue; + default: + return 0 /* None */; } } + } + function getDeclarationFlags() { + return lookAhead(parseDeclarationFlags); + } + function getStatementFlags() { switch (token) { + case 52 /* AtToken */: case 22 /* SemicolonToken */: - // If we're in error recovery, then we don't want to treat ';' as an empty statement. - // The problem is that ';' can show up in far too many contexts, and if we see one - // and assume it's a statement, then we may bail out inappropriately from whatever - // we're parsing. For example, if we have a semicolon in the middle of a class, then - // we really don't want to assume the class is over and we're on a statement in the - // outer module. We just want to consume and move on. - return !inErrorRecovery; case 14 /* OpenBraceToken */: case 98 /* VarKeyword */: case 104 /* LetKeyword */: case 83 /* FunctionKeyword */: case 69 /* ClassKeyword */: + case 77 /* EnumKeyword */: case 84 /* IfKeyword */: case 75 /* DoKeyword */: case 100 /* WhileKeyword */: @@ -9260,58 +9692,72 @@ var ts; // however, we say they are here so that we may gracefully parse them and error later. case 68 /* CatchKeyword */: case 81 /* FinallyKeyword */: - return true; + return 1 /* Statement */; case 70 /* ConstKeyword */: - // const keyword can precede enum keyword when defining constant enums - // 'const enum' do not start statement. - // In ES 6 'enum' is a future reserved keyword, so it should not be used as identifier - var isConstEnum = lookAhead(nextTokenIsEnumKeyword); - return !isConstEnum; + case 78 /* ExportKeyword */: + case 85 /* ImportKeyword */: + return getDeclarationFlags(); + case 115 /* DeclareKeyword */: case 103 /* InterfaceKeyword */: case 117 /* ModuleKeyword */: case 118 /* NamespaceKeyword */: - case 77 /* EnumKeyword */: case 124 /* TypeKeyword */: - // When followed by an identifier, these do not start a statement but might - // instead be following declarations - if (isDeclarationStart()) { - return false; - } + // When these don't start a declaration, they're an identifier in an expression statement + return getDeclarationFlags() || 1 /* Statement */; case 108 /* PublicKeyword */: case 106 /* PrivateKeyword */: case 107 /* ProtectedKeyword */: case 109 /* StaticKeyword */: - // When followed by an identifier or keyword, these do not start a statement but - // might instead be following type members - if (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine)) { - return false; - } + // When these don't start a declaration, they may be the start of a class member if an identifier + // immediately follows. Otherwise they're an identifier in an expression statement. + return getDeclarationFlags() || + (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine) ? 0 /* None */ : 1 /* Statement */); default: - return isStartOfExpression(); + return isStartOfExpression() ? 1 /* Statement */ : 0 /* None */; } } - function nextTokenIsEnumKeyword() { - nextToken(); - return token === 77 /* EnumKeyword */; + function isStartOfStatement() { + return (getStatementFlags() & 1 /* Statement */) !== 0; } - function nextTokenIsIdentifierOrKeywordOnSameLine() { + function isStartOfModuleElement() { + return (getStatementFlags() & 3 /* StatementOrModuleElement */) !== 0; + } + function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { nextToken(); - return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + return !scanner.hasPrecedingLineBreak() && + (isIdentifier() || token === 14 /* OpenBraceToken */ || token === 18 /* OpenBracketToken */); + } + function isLetDeclaration() { + // It is let declaration if in strict mode or next token is identifier\open bracket\open curly on same line. + // otherwise it needs to be treated like identifier + return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); } function parseStatement() { + return parseModuleElementOfKind(1 /* Statement */); + } + function parseModuleElement() { + return parseModuleElementOfKind(3 /* StatementOrModuleElement */); + } + function parseSourceElement() { + return parseModuleElementOfKind(3 /* StatementOrModuleElement */); + } + function parseModuleElementOfKind(flags) { switch (token) { + case 22 /* SemicolonToken */: + return parseEmptyStatement(); case 14 /* OpenBraceToken */: return parseBlock(false, false); case 98 /* VarKeyword */: - case 70 /* ConstKeyword */: - // const here should always be parsed as const declaration because of check in 'isStatement' return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 104 /* LetKeyword */: + if (isLetDeclaration()) { + return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + } + break; case 83 /* FunctionKeyword */: return parseFunctionDeclaration(scanner.getStartPos(), undefined, undefined); case 69 /* ClassKeyword */: return parseClassDeclaration(scanner.getStartPos(), undefined, undefined); - case 22 /* SemicolonToken */: - return parseEmptyStatement(); case 84 /* IfKeyword */: return parseIfStatement(); case 75 /* DoKeyword */: @@ -9339,54 +9785,68 @@ var ts; return parseTryStatement(); case 72 /* DebuggerKeyword */: return parseDebuggerStatement(); - case 104 /* LetKeyword */: - // If let follows identifier on the same line, it is declaration parse it as variable statement - if (isLetDeclaration()) { - return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 52 /* AtToken */: + return parseDeclaration(); + case 70 /* ConstKeyword */: + case 115 /* DeclareKeyword */: + case 77 /* EnumKeyword */: + case 78 /* ExportKeyword */: + case 85 /* ImportKeyword */: + case 103 /* InterfaceKeyword */: + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + case 106 /* PrivateKeyword */: + case 107 /* ProtectedKeyword */: + case 108 /* PublicKeyword */: + case 109 /* StaticKeyword */: + case 124 /* TypeKeyword */: + if (getDeclarationFlags() & flags) { + return parseDeclaration(); } - // Else parse it like identifier - fall through - default: - // Functions and variable statements are allowed as a statement. But as per - // the grammar, they also allow modifiers. So we have to check for those - // statements that might be following modifiers. This ensures that things - // work properly when incrementally parsing as the parser will produce the - // same FunctionDeclaraiton or VariableStatement if it has the same text - // regardless of whether it is inside a block or not. - // Even though variable statements and function declarations cannot have decorators, - // we parse them here to provide better error recovery. - if (ts.isModifier(token) || token === 52 /* AtToken */) { - var result = tryParse(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return result; - } - } - return parseExpressionOrLabeledStatement(); + break; } + return parseExpressionOrLabeledStatement(); } - function parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers() { - var start = scanner.getStartPos(); + function parseDeclaration() { + var fullStart = getNodePos(); var decorators = parseDecorators(); var modifiers = parseModifiers(); switch (token) { - case 70 /* ConstKeyword */: - var nextTokenIsEnum = lookAhead(nextTokenIsEnumKeyword); - if (nextTokenIsEnum) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); - case 104 /* LetKeyword */: - if (!isLetDeclaration()) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); case 98 /* VarKeyword */: - return parseVariableStatement(start, decorators, modifiers); + case 104 /* LetKeyword */: + case 70 /* ConstKeyword */: + return parseVariableStatement(fullStart, decorators, modifiers); case 83 /* FunctionKeyword */: - return parseFunctionDeclaration(start, decorators, modifiers); + return parseFunctionDeclaration(fullStart, decorators, modifiers); case 69 /* ClassKeyword */: - return parseClassDeclaration(start, decorators, modifiers); + return parseClassDeclaration(fullStart, decorators, modifiers); + case 103 /* InterfaceKeyword */: + return parseInterfaceDeclaration(fullStart, decorators, modifiers); + case 124 /* TypeKeyword */: + return parseTypeAliasDeclaration(fullStart, decorators, modifiers); + case 77 /* EnumKeyword */: + return parseEnumDeclaration(fullStart, decorators, modifiers); + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + return parseModuleDeclaration(fullStart, decorators, modifiers); + case 85 /* ImportKeyword */: + return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); + case 78 /* ExportKeyword */: + nextToken(); + return token === 73 /* DefaultKeyword */ || token === 53 /* EqualsToken */ ? + parseExportAssignment(fullStart, decorators, modifiers) : + parseExportDeclaration(fullStart, decorators, modifiers); + default: + if (decorators) { + // We reached this point because we encountered decorators and/or modifiers and assumed a declaration + // would follow. For recovery and error reporting purposes, return an incomplete declaration. + var node = createMissingNode(219 /* MissingDeclaration */, true, ts.Diagnostics.Declaration_expected); + node.pos = fullStart; + node.decorators = decorators; + setModifiers(node, modifiers); + return finishNode(node); + } } - return undefined; } function parseFunctionBlockOrSemicolon(isGenerator, diagnosticMessage) { if (token !== 14 /* OpenBraceToken */ && canParseSemicolon()) { @@ -9541,7 +10001,18 @@ var ts; property.name = name; property.questionToken = questionToken; property.type = parseTypeAnnotation(); - property.initializer = allowInAnd(parseNonParameterInitializer); + // For instance properties specifically, since they are evaluated inside the constructor, + // we do *not * want to parse yield expressions, so we specifically turn the yield context + // off. The grammar would look something like this: + // + // MemberVariableDeclaration[Yield]: + // AccessibilityModifier_opt PropertyName TypeAnnotation_opt Initialiser_opt[In]; + // AccessibilityModifier_opt static_opt PropertyName TypeAnnotation_opt Initialiser_opt[In, ?Yield]; + // + // The checker may still error in the static case to explicitly disallow the yield expression. + property.initializer = modifiers && modifiers.flags & 128 /* Static */ + ? allowInAnd(parseNonParameterInitializer) + : doOutsideOfContext(4 /* Yield */ | 2 /* DisallowIn */, parseNonParameterInitializer); parseSemicolon(); return finishNode(property); } @@ -9711,17 +10182,17 @@ var ts; } if (decorators) { // treat this as a property declaration with a missing name. - var name_3 = createMissingNode(65 /* Identifier */, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_3, undefined); + var name_5 = createMissingNode(65 /* Identifier */, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_5, undefined); } // 'isClassMemberStart' should have hinted not to attempt parsing. ts.Debug.fail("Should not have attempted to parse class member declaration."); } function parseClassExpression() { return parseClassDeclarationOrExpression( - /*fullStart:*/ scanner.getStartPos(), - /*decorators:*/ undefined, - /*modifiers:*/ undefined, 175 /* ClassExpression */); + /*fullStart*/ scanner.getStartPos(), + /*decorators*/ undefined, + /*modifiers*/ undefined, 175 /* ClassExpression */); } function parseClassDeclaration(fullStart, decorators, modifiers) { return parseClassDeclarationOrExpression(fullStart, decorators, modifiers, 202 /* ClassDeclaration */); @@ -10063,136 +10534,6 @@ var ts; parseSemicolon(); return finishNode(node); } - function isLetDeclaration() { - // It is let declaration if in strict mode or next token is identifier\open bracket\open curly on same line. - // otherwise it needs to be treated like identifier - return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); - } - function isDeclarationStart(followsModifier) { - switch (token) { - case 98 /* VarKeyword */: - case 70 /* ConstKeyword */: - case 83 /* FunctionKeyword */: - return true; - case 104 /* LetKeyword */: - return isLetDeclaration(); - case 69 /* ClassKeyword */: - case 103 /* InterfaceKeyword */: - case 77 /* EnumKeyword */: - case 124 /* TypeKeyword */: - // Not true keywords so ensure an identifier follows - return lookAhead(nextTokenIsIdentifierOrKeyword); - case 85 /* ImportKeyword */: - // Not true keywords so ensure an identifier follows or is string literal or asterisk or open brace - return lookAhead(nextTokenCanFollowImportKeyword); - case 117 /* ModuleKeyword */: - case 118 /* NamespaceKeyword */: - // Not a true keyword so ensure an identifier or string literal follows - return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral); - case 78 /* ExportKeyword */: - // Check for export assignment or modifier on source element - return lookAhead(nextTokenCanFollowExportKeyword); - case 115 /* DeclareKeyword */: - case 108 /* PublicKeyword */: - case 106 /* PrivateKeyword */: - case 107 /* ProtectedKeyword */: - case 109 /* StaticKeyword */: - // Check for modifier on source element - return lookAhead(nextTokenIsDeclarationStart); - case 52 /* AtToken */: - // a lookahead here is too costly, and decorators are only valid on a declaration. - // We will assume we are parsing a declaration here and report an error later - return !followsModifier; - } - } - function isIdentifierOrKeyword() { - return token >= 65 /* Identifier */; - } - function nextTokenIsIdentifierOrKeyword() { - nextToken(); - return isIdentifierOrKeyword(); - } - function nextTokenIsIdentifierOrKeywordOrStringLiteral() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 /* StringLiteral */; - } - function nextTokenCanFollowImportKeyword() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 /* StringLiteral */ || - token === 35 /* AsteriskToken */ || token === 14 /* OpenBraceToken */; - } - function nextTokenCanFollowExportKeyword() { - nextToken(); - return token === 53 /* EqualsToken */ || token === 35 /* AsteriskToken */ || - token === 14 /* OpenBraceToken */ || token === 73 /* DefaultKeyword */ || isDeclarationStart(true); - } - function nextTokenIsDeclarationStart() { - nextToken(); - return isDeclarationStart(true); - } - function nextTokenIsAsKeyword() { - return nextToken() === 111 /* AsKeyword */; - } - function parseDeclaration() { - var fullStart = getNodePos(); - var decorators = parseDecorators(); - var modifiers = parseModifiers(); - if (token === 78 /* ExportKeyword */) { - nextToken(); - if (token === 73 /* DefaultKeyword */ || token === 53 /* EqualsToken */) { - return parseExportAssignment(fullStart, decorators, modifiers); - } - if (token === 35 /* AsteriskToken */ || token === 14 /* OpenBraceToken */) { - return parseExportDeclaration(fullStart, decorators, modifiers); - } - } - switch (token) { - case 98 /* VarKeyword */: - case 104 /* LetKeyword */: - case 70 /* ConstKeyword */: - return parseVariableStatement(fullStart, decorators, modifiers); - case 83 /* FunctionKeyword */: - return parseFunctionDeclaration(fullStart, decorators, modifiers); - case 69 /* ClassKeyword */: - return parseClassDeclaration(fullStart, decorators, modifiers); - case 103 /* InterfaceKeyword */: - return parseInterfaceDeclaration(fullStart, decorators, modifiers); - case 124 /* TypeKeyword */: - return parseTypeAliasDeclaration(fullStart, decorators, modifiers); - case 77 /* EnumKeyword */: - return parseEnumDeclaration(fullStart, decorators, modifiers); - case 117 /* ModuleKeyword */: - case 118 /* NamespaceKeyword */: - return parseModuleDeclaration(fullStart, decorators, modifiers); - case 85 /* ImportKeyword */: - return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); - default: - if (decorators) { - // We reached this point because we encountered an AtToken and assumed a declaration would - // follow. For recovery and error reporting purposes, return an incomplete declaration. - var node = createMissingNode(219 /* MissingDeclaration */, true, ts.Diagnostics.Declaration_expected); - node.pos = fullStart; - node.decorators = decorators; - setModifiers(node, modifiers); - return finishNode(node); - } - ts.Debug.fail("Mismatch between isDeclarationStart and parseDeclaration"); - } - } - function isSourceElement(inErrorRecovery) { - return isDeclarationStart() || isStartOfStatement(inErrorRecovery); - } - function parseSourceElement() { - return parseSourceElementOrModuleElement(); - } - function parseModuleElement() { - return parseSourceElementOrModuleElement(); - } - function parseSourceElementOrModuleElement() { - return isDeclarationStart() - ? parseDeclaration() - : parseStatement(); - } function processReferenceComments(sourceFile) { var triviaScanner = ts.createScanner(sourceFile.languageVersion, false, sourceText); var referencedFiles = []; @@ -10220,7 +10561,7 @@ var ts; referencedFiles.push(fileReference); } if (diagnosticMessage) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); } } else { @@ -10228,7 +10569,7 @@ var ts; var amdModuleNameMatchResult = amdModuleNameRegEx.exec(comment); if (amdModuleNameMatchResult) { if (amdModuleName) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); } amdModuleName = amdModuleNameMatchResult[2]; } @@ -10284,7 +10625,11 @@ var ts; ParsingContext[ParsingContext["TupleElementTypes"] = 18] = "TupleElementTypes"; ParsingContext[ParsingContext["HeritageClauses"] = 19] = "HeritageClauses"; ParsingContext[ParsingContext["ImportOrExportSpecifiers"] = 20] = "ImportOrExportSpecifiers"; - ParsingContext[ParsingContext["Count"] = 21] = "Count"; // Number of parsing contexts + ParsingContext[ParsingContext["JSDocFunctionParameters"] = 21] = "JSDocFunctionParameters"; + ParsingContext[ParsingContext["JSDocTypeArguments"] = 22] = "JSDocTypeArguments"; + ParsingContext[ParsingContext["JSDocRecordMembers"] = 23] = "JSDocRecordMembers"; + ParsingContext[ParsingContext["JSDocTupleTypes"] = 24] = "JSDocTupleTypes"; + ParsingContext[ParsingContext["Count"] = 25] = "Count"; // Number of parsing contexts })(ParsingContext || (ParsingContext = {})); var Tristate; (function (Tristate) { @@ -10292,6 +10637,552 @@ var ts; Tristate[Tristate["True"] = 1] = "True"; Tristate[Tristate["Unknown"] = 2] = "Unknown"; })(Tristate || (Tristate = {})); + var JSDocParser; + (function (JSDocParser) { + function isJSDocType() { + switch (token) { + case 35 /* AsteriskToken */: + case 50 /* QuestionToken */: + case 16 /* OpenParenToken */: + case 18 /* OpenBracketToken */: + case 46 /* ExclamationToken */: + case 14 /* OpenBraceToken */: + case 83 /* FunctionKeyword */: + case 21 /* DotDotDotToken */: + case 88 /* NewKeyword */: + case 93 /* ThisKeyword */: + return true; + } + return isIdentifierOrKeyword(); + } + JSDocParser.isJSDocType = isJSDocType; + function parseJSDocTypeExpressionForTests(content, start, length) { + initializeState("file.js", content, 2 /* Latest */, undefined); + var jsDocTypeExpression = parseJSDocTypeExpression(start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocTypeExpression ? { jsDocTypeExpression: jsDocTypeExpression, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; + // Parses out a JSDoc type expression. The starting position should be right at the open + // curly in the type expression. Returns 'undefined' if it encounters any errors while parsing. + /* @internal */ + function parseJSDocTypeExpression(start, length) { + scanner.setText(sourceText, start, length); + // Prime the first token for us to start processing. + token = nextToken(); + var result = createNode(229 /* JSDocTypeExpression */); + parseExpected(14 /* OpenBraceToken */); + result.type = parseJSDocTopLevelType(); + parseExpected(15 /* CloseBraceToken */); + fixupParentReferences(result); + return finishNode(result); + } + JSDocParser.parseJSDocTypeExpression = parseJSDocTypeExpression; + function setError(message) { + parseErrorAtCurrentToken(message); + if (ts.throwOnJSDocErrors) { + throw new Error(message.key); + } + } + function parseJSDocTopLevelType() { + var type = parseJSDocType(); + if (token === 44 /* BarToken */) { + var unionType = createNode(233 /* JSDocUnionType */, type.pos); + unionType.types = parseJSDocTypeList(type); + type = finishNode(unionType); + } + if (token === 53 /* EqualsToken */) { + var optionalType = createNode(240 /* JSDocOptionalType */, type.pos); + nextToken(); + optionalType.type = type; + type = finishNode(optionalType); + } + return type; + } + function parseJSDocType() { + var type = parseBasicTypeExpression(); + while (true) { + if (token === 18 /* OpenBracketToken */) { + var arrayType = createNode(232 /* JSDocArrayType */, type.pos); + arrayType.elementType = type; + nextToken(); + parseExpected(19 /* CloseBracketToken */); + type = finishNode(arrayType); + } + else if (token === 50 /* QuestionToken */) { + var nullableType = createNode(235 /* JSDocNullableType */, type.pos); + nullableType.type = type; + nextToken(); + type = finishNode(nullableType); + } + else if (token === 46 /* ExclamationToken */) { + var nonNullableType = createNode(236 /* JSDocNonNullableType */, type.pos); + nonNullableType.type = type; + nextToken(); + type = finishNode(nonNullableType); + } + else { + break; + } + } + return type; + } + function parseBasicTypeExpression() { + switch (token) { + case 35 /* AsteriskToken */: + return parseJSDocAllType(); + case 50 /* QuestionToken */: + return parseJSDocUnknownOrNullableType(); + case 16 /* OpenParenToken */: + return parseJSDocUnionType(); + case 18 /* OpenBracketToken */: + return parseJSDocTupleType(); + case 46 /* ExclamationToken */: + return parseJSDocNonNullableType(); + case 14 /* OpenBraceToken */: + return parseJSDocRecordType(); + case 83 /* FunctionKeyword */: + return parseJSDocFunctionType(); + case 21 /* DotDotDotToken */: + return parseJSDocVariadicType(); + case 88 /* NewKeyword */: + return parseJSDocConstructorType(); + case 93 /* ThisKeyword */: + return parseJSDocThisType(); + case 112 /* AnyKeyword */: + case 122 /* StringKeyword */: + case 120 /* NumberKeyword */: + case 113 /* BooleanKeyword */: + case 123 /* SymbolKeyword */: + case 99 /* VoidKeyword */: + return parseTokenNode(); + } + return parseJSDocTypeReference(); + } + function parseJSDocThisType() { + var result = createNode(244 /* JSDocThisType */); + nextToken(); + parseExpected(51 /* ColonToken */); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocConstructorType() { + var result = createNode(243 /* JSDocConstructorType */); + nextToken(); + parseExpected(51 /* ColonToken */); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocVariadicType() { + var result = createNode(242 /* JSDocVariadicType */); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocFunctionType() { + var result = createNode(241 /* JSDocFunctionType */); + nextToken(); + parseExpected(16 /* OpenParenToken */); + result.parameters = parseDelimitedList(21 /* JSDocFunctionParameters */, parseJSDocParameter); + checkForTrailingComma(result.parameters); + parseExpected(17 /* CloseParenToken */); + if (token === 51 /* ColonToken */) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocParameter() { + var parameter = createNode(130 /* Parameter */); + parameter.type = parseJSDocType(); + return finishNode(parameter); + } + function parseJSDocOptionalType(type) { + var result = createNode(240 /* JSDocOptionalType */, type.pos); + nextToken(); + result.type = type; + return finishNode(result); + } + function parseJSDocTypeReference() { + var result = createNode(239 /* JSDocTypeReference */); + result.name = parseSimplePropertyName(); + while (parseOptional(20 /* DotToken */)) { + if (token === 24 /* LessThanToken */) { + result.typeArguments = parseTypeArguments(); + break; + } + else { + result.name = parseQualifiedName(result.name); + } + } + return finishNode(result); + } + function parseTypeArguments() { + // Move past the < + nextToken(); + var typeArguments = parseDelimitedList(22 /* JSDocTypeArguments */, parseJSDocType); + checkForTrailingComma(typeArguments); + checkForEmptyTypeArgumentList(typeArguments); + parseExpected(25 /* GreaterThanToken */); + return typeArguments; + } + function checkForEmptyTypeArgumentList(typeArguments) { + if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceText, typeArguments.end) + ">".length; + return parseErrorAtPosition(start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function parseQualifiedName(left) { + var result = createNode(127 /* QualifiedName */, left.pos); + result.left = left; + result.right = parseIdentifierName(); + return finishNode(result); + } + function parseJSDocRecordType() { + var result = createNode(237 /* JSDocRecordType */); + nextToken(); + result.members = parseDelimitedList(23 /* JSDocRecordMembers */, parseJSDocRecordMember); + checkForTrailingComma(result.members); + parseExpected(15 /* CloseBraceToken */); + return finishNode(result); + } + function parseJSDocRecordMember() { + var result = createNode(238 /* JSDocRecordMember */); + result.name = parseSimplePropertyName(); + if (token === 51 /* ColonToken */) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocNonNullableType() { + var result = createNode(236 /* JSDocNonNullableType */); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocTupleType() { + var result = createNode(234 /* JSDocTupleType */); + nextToken(); + result.types = parseDelimitedList(24 /* JSDocTupleTypes */, parseJSDocType); + checkForTrailingComma(result.types); + parseExpected(19 /* CloseBracketToken */); + return finishNode(result); + } + function checkForTrailingComma(list) { + if (parseDiagnostics.length === 0 && list.hasTrailingComma) { + var start = list.end - ",".length; + parseErrorAtPosition(start, ",".length, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function parseJSDocUnionType() { + var result = createNode(233 /* JSDocUnionType */); + nextToken(); + result.types = parseJSDocTypeList(parseJSDocType()); + parseExpected(17 /* CloseParenToken */); + return finishNode(result); + } + function parseJSDocTypeList(firstType) { + ts.Debug.assert(!!firstType); + var types = []; + types.pos = firstType.pos; + types.push(firstType); + while (parseOptional(44 /* BarToken */)) { + types.push(parseJSDocType()); + } + types.end = scanner.getStartPos(); + return types; + } + function parseJSDocAllType() { + var result = createNode(230 /* JSDocAllType */); + nextToken(); + return finishNode(result); + } + function parseJSDocUnknownOrNullableType() { + var pos = scanner.getStartPos(); + // skip the ? + nextToken(); + // Need to lookahead to decide if this is a nullable or unknown type. + // Here are cases where we'll pick the unknown type: + // + // Foo(?, + // { a: ? } + // Foo(?) + // Foo + // Foo(?= + // (?| + if (token === 23 /* CommaToken */ || + token === 15 /* CloseBraceToken */ || + token === 17 /* CloseParenToken */ || + token === 25 /* GreaterThanToken */ || + token === 53 /* EqualsToken */ || + token === 44 /* BarToken */) { + var result = createNode(231 /* JSDocUnknownType */, pos); + return finishNode(result); + } + else { + var result = createNode(235 /* JSDocNullableType */, pos); + result.type = parseJSDocType(); + return finishNode(result); + } + } + function parseIsolatedJSDocComment(content, start, length) { + initializeState("file.js", content, 2 /* Latest */, undefined); + var jsDocComment = parseJSDocComment(undefined, start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocComment ? { jsDocComment: jsDocComment, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocComment(parent, start, length) { + var comment = parseJSDocCommentWorker(start, length); + if (comment) { + fixupParentReferences(comment); + comment.parent = parent; + } + return comment; + } + JSDocParser.parseJSDocComment = parseJSDocComment; + function parseJSDocCommentWorker(start, length) { + var content = sourceText; + start = start || 0; + var end = length === undefined ? content.length : start + length; + length = end - start; + ts.Debug.assert(start >= 0); + ts.Debug.assert(start <= end); + ts.Debug.assert(end <= content.length); + var tags; + var pos; + // NOTE(cyrusn): This is essentially a handwritten scanner for JSDocComments. I + // considered using an actual Scanner, but this would complicate things. The + // scanner would need to know it was in a Doc Comment. Otherwise, it would then + // produce comments *inside* the doc comment. In the end it was just easier to + // write a simple scanner rather than go that route. + if (length >= "/** */".length) { + if (content.charCodeAt(start) === 47 /* slash */ && + content.charCodeAt(start + 1) === 42 /* asterisk */ && + content.charCodeAt(start + 2) === 42 /* asterisk */ && + content.charCodeAt(start + 3) !== 42 /* asterisk */) { + // Initially we can parse out a tag. We also have seen a starting asterisk. + // This is so that /** * @type */ doesn't parse. + var canParseTag = true; + var seenAsterisk = true; + for (pos = start + "/**".length; pos < end;) { + var ch = content.charCodeAt(pos); + pos++; + if (ch === 64 /* at */ && canParseTag) { + parseTag(); + // Once we parse out a tag, we cannot keep parsing out tags on this line. + canParseTag = false; + continue; + } + if (ts.isLineBreak(ch)) { + // After a line break, we can parse a tag, and we haven't seen as asterisk + // on the next line yet. + canParseTag = true; + seenAsterisk = false; + continue; + } + if (ts.isWhiteSpace(ch)) { + // Whitespace doesn't affect any of our parsing. + continue; + } + // Ignore the first asterisk on a line. + if (ch === 42 /* asterisk */) { + if (seenAsterisk) { + // If we've already seen an asterisk, then we can no longer parse a tag + // on this line. + canParseTag = false; + } + seenAsterisk = true; + continue; + } + // Anything else is doc comment text. We can't do anything with it. Because it + // wasn't a tag, we can no longer parse a tag on this line until we hit the next + // line break. + canParseTag = false; + } + } + } + return createJSDocComment(); + function createJSDocComment() { + if (!tags) { + return undefined; + } + var result = createNode(245 /* JSDocComment */, start); + result.tags = tags; + return finishNode(result, end); + } + function skipWhitespace() { + while (pos < end && ts.isWhiteSpace(content.charCodeAt(pos))) { + pos++; + } + } + function parseTag() { + ts.Debug.assert(content.charCodeAt(pos - 1) === 64 /* at */); + var atToken = createNode(52 /* AtToken */, pos - 1); + atToken.end = pos; + var startPos = pos; + var tagName = scanIdentifier(); + if (!tagName) { + return; + } + var tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); + addTag(tag); + } + function handleTag(atToken, tagName) { + if (tagName) { + switch (tagName.text) { + case "param": + return handleParamTag(atToken, tagName); + case "return": + case "returns": + return handleReturnTag(atToken, tagName); + case "template": + return handleTemplateTag(atToken, tagName); + case "type": + return handleTypeTag(atToken, tagName); + } + } + return undefined; + } + function handleUnknownTag(atToken, tagName) { + var result = createNode(246 /* JSDocTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + return finishNode(result, pos); + } + function addTag(tag) { + if (tag) { + if (!tags) { + tags = []; + tags.pos = tag.pos; + } + tags.push(tag); + tags.end = tag.end; + } + } + function tryParseTypeExpression() { + skipWhitespace(); + if (content.charCodeAt(pos) !== 123 /* openBrace */) { + return undefined; + } + var typeExpression = parseJSDocTypeExpression(pos, end - pos); + pos = typeExpression.end; + return typeExpression; + } + function handleParamTag(atToken, tagName) { + var typeExpression = tryParseTypeExpression(); + skipWhitespace(); + var name; + var isBracketed; + if (content.charCodeAt(pos) === 91 /* openBracket */) { + pos++; + skipWhitespace(); + name = scanIdentifier(); + isBracketed = true; + } + else { + name = scanIdentifier(); + } + if (!name) { + parseErrorAtPosition(pos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var preName, postName; + if (typeExpression) { + postName = name; + } + else { + preName = name; + } + if (!typeExpression) { + typeExpression = tryParseTypeExpression(); + } + var result = createNode(247 /* JSDocParameterTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.preParameterName = preName; + result.typeExpression = typeExpression; + result.postParameterName = postName; + result.isBracketed = isBracketed; + return finishNode(result, pos); + } + function handleReturnTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 248 /* JSDocReturnTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(248 /* JSDocReturnTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTypeTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 249 /* JSDocTypeTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(249 /* JSDocTypeTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTemplateTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 250 /* JSDocTemplateTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var typeParameters = []; + typeParameters.pos = pos; + while (true) { + skipWhitespace(); + var startPos = pos; + var name_6 = scanIdentifier(); + if (!name_6) { + parseErrorAtPosition(startPos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var typeParameter = createNode(129 /* TypeParameter */, name_6.pos); + typeParameter.name = name_6; + finishNode(typeParameter, pos); + typeParameters.push(typeParameter); + skipWhitespace(); + if (content.charCodeAt(pos) !== 44 /* comma */) { + break; + } + pos++; + } + typeParameters.end = pos; + var result = createNode(250 /* JSDocTemplateTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeParameters = typeParameters; + return finishNode(result, pos); + } + function scanIdentifier() { + var startPos = pos; + for (; pos < end; pos++) { + var ch = content.charCodeAt(pos); + if (pos === startPos && ts.isIdentifierStart(ch, 2 /* Latest */)) { + continue; + } + else if (pos > startPos && ts.isIdentifierPart(ch, 2 /* Latest */)) { + continue; + } + break; + } + if (startPos === pos) { + return undefined; + } + var result = createNode(65 /* Identifier */, startPos); + result.text = content.substring(startPos, pos); + return finishNode(result, pos); + } + } + JSDocParser.parseJSDocCommentWorker = parseJSDocCommentWorker; + })(JSDocParser = Parser.JSDocParser || (Parser.JSDocParser = {})); })(Parser || (Parser = {})); var IncrementalParser; (function (IncrementalParser) { @@ -10379,7 +11270,12 @@ var ts; } // Ditch any existing LS children we may have created. This way we can avoid // moving them forward. - node._children = undefined; + if (node._children) { + node._children = undefined; + } + if (node.jsDocComment) { + node.jsDocComment = undefined; + } node.pos += delta; node.end += delta; if (aggressiveChecks && shouldCheckNode(node)) { @@ -10839,13 +11735,15 @@ var ts; var undefinedType = createIntrinsicType(32 /* Undefined */ | 262144 /* ContainsUndefinedOrNull */, "undefined"); var nullType = createIntrinsicType(64 /* Null */ | 262144 /* ContainsUndefinedOrNull */, "null"); var unknownType = createIntrinsicType(1 /* Any */, "unknown"); + var circularType = createIntrinsicType(1 /* Any */, "__circular__"); var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = {}; var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = 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 = {}; - var globalArraySymbol; var globalESSymbolConstructorSymbol; var globalObjectType; var globalFunctionType; @@ -10857,6 +11755,8 @@ var ts; var globalTemplateStringsArrayType; var globalESSymbolType; var globalIterableType; + var globalIteratorType; + var globalIterableIteratorType; var anyArrayType; var getGlobalClassDecoratorType; var getGlobalParameterDecoratorType; @@ -11083,7 +11983,15 @@ var ts; // Locals of a source file are not in scope (because they get merged into the global symbol table) if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - break loop; + // Type parameters of a function are in scope in the entire function declaration, including the parameter + // list and return type. However, local types are only in scope in the function body. + if (!(meaning & 793056 /* Type */) || + !(result.flags & (793056 /* Type */ & ~262144 /* TypeParameter */)) || + !ts.isFunctionLike(location) || + lastLocation === location.body) { + break loop; + } + result = undefined; } } switch (location.kind) { @@ -11385,15 +12293,15 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_4 = specifier.propertyName || specifier.name; - if (name_4.text) { - var symbolFromModule = getExportOfModule(targetSymbol, name_4.text); - var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_4.text); + var name_7 = specifier.propertyName || specifier.name; + if (name_7.text) { + var symbolFromModule = getExportOfModule(targetSymbol, name_7.text); + var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_7.text); var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_4, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_4)); + error(name_7, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_7)); } return symbol; } @@ -12150,17 +13058,54 @@ var ts; writeType(types[i], union ? 64 /* InElementType */ : 0 /* None */); } } + function writeSymbolTypeReference(symbol, typeArguments, pos, end) { + // Unnamed function expressions, arrow functions, and unnamed class expressions have reserved names that + // we don't want to display + if (!isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793056 /* Type */); + } + if (pos < end) { + writePunctuation(writer, 24 /* LessThanToken */); + writeType(typeArguments[pos++], 0 /* None */); + while (pos < end) { + writePunctuation(writer, 23 /* CommaToken */); + writeSpace(writer); + writeType(typeArguments[pos++], 0 /* None */); + } + writePunctuation(writer, 25 /* GreaterThanToken */); + } + } function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments; if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { - writeType(type.typeArguments[0], 64 /* InElementType */); + writeType(typeArguments[0], 64 /* InElementType */); writePunctuation(writer, 18 /* OpenBracketToken */); writePunctuation(writer, 19 /* CloseBracketToken */); } else { - buildSymbolDisplay(type.target.symbol, writer, enclosingDeclaration, 793056 /* Type */); - writePunctuation(writer, 24 /* LessThanToken */); - writeTypeList(type.typeArguments, false); - writePunctuation(writer, 25 /* GreaterThanToken */); + // Write the type reference in the format f.g.C where A and B are type arguments + // for outer type parameters, and f and g are the respective declaring containers of those + // type parameters. + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + // Find group of type arguments for type parameters with the same declaring container. + var start = i; + var parent_3 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_3); + // When type parameters are their own type arguments for the whole group (i.e. we have + // the default outer type arguments), we don't show the group. + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_3, typeArguments, start, i); + writePunctuation(writer, 20 /* DotToken */); + } + } + } + writeSymbolTypeReference(type.symbol, typeArguments, i, typeArguments.length); } } function writeTupleType(type) { @@ -12348,7 +13293,7 @@ var ts; function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaraiton, flags) { var targetSymbol = getTargetSymbol(symbol); if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */) { - buildDisplayForTypeParametersAndDelimiters(getTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); } } function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, typeStack) { @@ -12521,14 +13466,14 @@ var ts; case 201 /* FunctionDeclaration */: case 205 /* EnumDeclaration */: case 209 /* ImportEqualsDeclaration */: - var parent_2 = getDeclarationContainer(node); + var parent_4 = getDeclarationContainer(node); // If the node is not exported or it is not ambient module element (except import declaration) if (!(ts.getCombinedNodeFlags(node) & 1 /* Export */) && - !(node.kind !== 209 /* ImportEqualsDeclaration */ && parent_2.kind !== 228 /* SourceFile */ && ts.isInAmbientContext(parent_2))) { - return isGlobalSourceFile(parent_2); + !(node.kind !== 209 /* ImportEqualsDeclaration */ && parent_4.kind !== 228 /* SourceFile */ && ts.isInAmbientContext(parent_4))) { + return isGlobalSourceFile(parent_4); } // Exported members/ambient module elements (exception import declaration) are visible if parent is visible - return isDeclarationVisible(parent_2); + return isDeclarationVisible(parent_4); case 133 /* PropertyDeclaration */: case 132 /* PropertySignature */: case 137 /* GetAccessor */: @@ -12679,14 +13624,14 @@ var ts; var type; if (pattern.kind === 151 /* ObjectBindingPattern */) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) - var name_5 = declaration.propertyName || declaration.name; + var name_8 = declaration.propertyName || declaration.name; // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, // or otherwise the type of the string index signature. - type = getTypeOfPropertyOfType(parentType, name_5.text) || - isNumericLiteralName(name_5.text) && getIndexTypeOfType(parentType, 1 /* Number */) || + type = getTypeOfPropertyOfType(parentType, name_8.text) || + isNumericLiteralName(name_8.text) && getIndexTypeOfType(parentType, 1 /* Number */) || getIndexTypeOfType(parentType, 0 /* String */); if (!type) { - error(name_5, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_5)); + error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_8)); return unknownType; } } @@ -12731,7 +13676,7 @@ var ts; // checkRightHandSideOfForOf will return undefined if the for-of expression type was // missing properties/signatures required to get its iteratedType (like // [Symbol.iterator] or next). This may be because we accessed properties from anyType, - // or it may have led to an error inside getIteratedType. + // or it may have led to an error inside getElementTypeOfIterable. return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; } if (ts.isBindingPattern(declaration.parent)) { @@ -13024,29 +13969,65 @@ var ts; return target === checkBase || ts.forEach(getBaseTypes(target), check); } } - // Return combined list of type parameters from all declarations of a class or interface. Elsewhere we check they're all - // the same, but even if they're not we still need the complete list to ensure instantiations supply type arguments - // for all type parameters. - function getTypeParametersOfClassOrInterface(symbol) { - var result; - ts.forEach(symbol.declarations, function (node) { - if (node.kind === 203 /* InterfaceDeclaration */ || node.kind === 202 /* ClassDeclaration */) { - var declaration = node; - if (declaration.typeParameters && declaration.typeParameters.length) { - ts.forEach(declaration.typeParameters, function (node) { - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node)); - if (!result) { - result = [tp]; - } - else if (!ts.contains(result, tp)) { - result.push(tp); - } - }); + // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. + // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set + // in-place and returns the same array. + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0; _i < declarations.length; _i++) { + var declaration = declarations[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); + } + } + return typeParameters; + } + // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function + // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and + // returns the same array. + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; + } + if (node.kind === 202 /* ClassDeclaration */ || node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */ || node.kind === 135 /* MethodDeclaration */ || + node.kind === 164 /* ArrowFunction */) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - }); + } + } + // The outer type parameters are those defined by enclosing generic classes, methods, or functions. + function getOuterTypeParametersOfClassOrInterface(symbol) { + var kind = symbol.flags & 32 /* Class */ ? 202 /* ClassDeclaration */ : 203 /* InterfaceDeclaration */; + return appendOuterTypeParameters(undefined, ts.getDeclarationOfKind(symbol, kind)); + } + // The local type parameters are the combined set of type parameters from all declarations of the class or interface. + function getLocalTypeParametersOfClassOrInterface(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 203 /* InterfaceDeclaration */ || node.kind === 202 /* ClassDeclaration */) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } + } return result; } + // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus + // its locally declared type parameters. + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterface(symbol)); + } function getBaseTypes(type) { var typeWithBaseTypes = type; if (!typeWithBaseTypes.baseTypes) { @@ -13113,10 +14094,13 @@ var ts; if (!links.declaredType) { var kind = symbol.flags & 32 /* Class */ ? 1024 /* Class */ : 2048 /* Interface */; var type = links.declaredType = createObjectType(kind, symbol); - var typeParameters = getTypeParametersOfClassOrInterface(symbol); - if (typeParameters) { + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterface(symbol); + if (outerTypeParameters || localTypeParameters) { type.flags |= 4096 /* Reference */; - type.typeParameters = typeParameters; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; type.instantiations = {}; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; @@ -13294,12 +14278,12 @@ var ts; return ts.map(baseSignatures, function (baseSignature) { var signature = baseType.flags & 4096 /* Reference */ ? getSignatureInstantiation(baseSignature, baseType.typeArguments) : cloneSignature(baseSignature); - signature.typeParameters = classType.typeParameters; + signature.typeParameters = classType.localTypeParameters; signature.resolvedReturnType = classType; return signature; }); } - return [createSignature(undefined, classType.typeParameters, emptyArray, classType, 0, false, false)]; + return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, 0, false, false)]; } function createTupleTypeMemberSymbols(memberTypes) { var members = {}; @@ -13627,7 +14611,7 @@ var ts; var links = getNodeLinks(declaration); if (!links.resolvedSignature) { var classType = declaration.kind === 136 /* Constructor */ ? getDeclaredTypeOfClassOrInterface(declaration.parent.symbol) : undefined; - var typeParameters = classType ? classType.typeParameters : + var typeParameters = classType ? classType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : undefined; var parameters = []; var hasStringLiterals = false; @@ -13814,6 +14798,9 @@ var ts; } return type.constraint === noConstraintType ? undefined : type.constraint; } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 129 /* TypeParameter */).parent); + } function getTypeListId(types) { switch (types.length) { case 1: @@ -13919,12 +14906,21 @@ var ts; else { type = getDeclaredTypeOfSymbol(symbol); if (type.flags & (1024 /* Class */ | 2048 /* Interface */) && type.flags & 4096 /* Reference */) { - var typeParameters = type.typeParameters; - if (node.typeArguments && node.typeArguments.length === typeParameters.length) { - type = createTypeReference(type, ts.map(node.typeArguments, getTypeFromTypeNode)); + // In a type reference, the outer type parameters of the referenced class or interface are automatically + // supplied as type arguments and the type reference only specifies arguments for the local type parameters + // of the class or interface. + var localTypeParameters = type.localTypeParameters; + var expectedTypeArgCount = localTypeParameters ? localTypeParameters.length : 0; + var typeArgCount = node.typeArguments ? node.typeArguments.length : 0; + if (typeArgCount === expectedTypeArgCount) { + // When no type arguments are expected we already have the right type because all outer type parameters + // have themselves as default type arguments. + if (typeArgCount) { + type = createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNode))); + } } else { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1 /* WriteArrayAsGenericType */), expectedTypeArgCount); type = undefined; } } @@ -13966,16 +14962,16 @@ var ts; } } if (!symbol) { - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 48128 /* ObjectType */)) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } return type; } @@ -13995,15 +14991,20 @@ var ts; function getGlobalESSymbolConstructorSymbol() { return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol")); } + /** + * Instantiates a global type that is generic with some element type, and returns that instantiation. + */ + function createTypeFromGenericGlobalType(genericGlobalType, elementType) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, [elementType]) : emptyObjectType; + } function createIterableType(elementType) { - return globalIterableType !== emptyObjectType ? createTypeReference(globalIterableType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalIterableType, elementType); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(globalIterableIteratorType, elementType); } function createArrayType(elementType) { - // globalArrayType will be undefined if we get here during creation of the Array type. This for example happens if - // user code augments the Array type with call or construct signatures that have an array type as the return type. - // We instead use globalArraySymbol to obtain the (not yet fully constructed) Array type. - var arrayType = globalArrayType || getDeclaredTypeOfSymbol(globalArraySymbol); - return arrayType !== emptyObjectType ? createTypeReference(arrayType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalArrayType, elementType); } function getTypeFromArrayTypeNode(node) { var links = getNodeLinks(node); @@ -14058,17 +15059,7 @@ var ts; } return false; } - // Since removeSubtypes checks the subtype relation, and the subtype relation on a union - // may attempt to reduce a union, it is possible that removeSubtypes could be called - // recursively on the same set of types. The removeSubtypesStack is used to track which - // sets of types are currently undergoing subtype reduction. - var removeSubtypesStack = []; function removeSubtypes(types) { - var typeListId = getTypeListId(types); - if (removeSubtypesStack.lastIndexOf(typeListId) >= 0) { - return; - } - removeSubtypesStack.push(typeListId); var i = types.length; while (i > 0) { i--; @@ -14076,7 +15067,6 @@ var ts; types.splice(i, 1); } } - removeSubtypesStack.pop(); } function containsAnyType(types) { for (var _i = 0; _i < types.length; _i++) { @@ -14128,10 +15118,20 @@ var ts; } return type; } + // Subtype reduction is basically an optimization we do to avoid excessively large union types, which take longer + // to process and look strange in quick info and error messages. Semantically there is no difference between the + // reduced type and the type itself. So, when we detect a circularity we simply say that the reduced type is the + // type itself. function getReducedTypeOfUnionType(type) { - // If union type was created without subtype reduction, perform the deferred reduction now if (!type.reducedType) { - type.reducedType = getUnionType(type.types, false); + type.reducedType = circularType; + var reducedType = getUnionType(type.types, false); + if (type.reducedType === circularType) { + type.reducedType = reducedType; + } + } + else if (type.reducedType === circularType) { + type.reducedType = type; } return type.reducedType; } @@ -14323,6 +15323,18 @@ var ts; return result; } function instantiateAnonymousType(type, mapper) { + // If this type has already been instantiated using this mapper, returned the cached result. This guards against + // infinite instantiations of cyclic types, e.g. "var x: { a: T, b: typeof x };" + if (mapper.mappings) { + var cached = mapper.mappings[type.id]; + if (cached) { + return cached; + } + } + else { + mapper.mappings = {}; + } + // Instantiate the given type using the given mapper and cache the result var result = createObjectType(32768 /* Anonymous */, type.symbol); result.properties = instantiateList(getPropertiesOfObjectType(type), mapper, instantiateSymbol); result.members = createSymbolTable(result.properties); @@ -14334,6 +15346,7 @@ var ts; result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); + mapper.mappings[type.id] = result; return result; } function instantiateType(type, mapper) { @@ -14342,7 +15355,7 @@ var ts; return mapper(type); } if (type.flags & 32768 /* Anonymous */) { - return type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) ? + return type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) ? instantiateAnonymousType(type, mapper) : type; } if (type.flags & 4096 /* Reference */) { @@ -15617,10 +16630,10 @@ var ts; // Resolve location from top down towards node if it is a context sensitive expression // That helps in making sure not assigning types as any when resolved out of order var containerNodes = []; - for (var parent_3 = node.parent; parent_3; parent_3 = parent_3.parent) { - if ((ts.isExpression(parent_3) || ts.isObjectLiteralMethod(node)) && - isContextSensitive(parent_3)) { - containerNodes.unshift(parent_3); + for (var parent_5 = node.parent; parent_5; parent_5 = parent_5.parent) { + if ((ts.isExpression(parent_5) || ts.isObjectLiteralMethod(node)) && + isContextSensitive(parent_5)) { + containerNodes.unshift(parent_5); } } ts.forEach(containerNodes, function (node) { getTypeOfNode(node); }); @@ -16105,22 +17118,40 @@ var ts; return undefined; } function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (func && !func.asteriskToken) { + return getContextualReturnType(func); + } + return undefined; + } + function getContextualTypeForYieldOperand(node) { var func = ts.getContainingFunction(node); if (func) { - // If the containing function has a return type annotation, is a constructor, or is a get accessor whose - // corresponding set accessor has a type annotation, return statements in the function are contextually typed - if (func.type || func.kind === 136 /* Constructor */ || func.kind === 137 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(func.symbol, 138 /* SetAccessor */))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - } - // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature - // and that call signature is non-generic, return statements are contextually typed by the return type of the signature - var signature = getContextualSignatureForFunctionLikeDeclaration(func); - if (signature) { - return getReturnTypeOfSignature(signature); + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } return undefined; } + function getContextualReturnType(functionDecl) { + // If the containing function has a return type annotation, is a constructor, or is a get accessor whose + // corresponding set accessor has a type annotation, return statements in the function are contextually typed + if (functionDecl.type || + functionDecl.kind === 136 /* Constructor */ || + functionDecl.kind === 137 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 138 /* SetAccessor */))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); + } + // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature + // and that call signature is non-generic, return statements are contextually typed by the return type of the signature + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); + } + return undefined; + } // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. function getContextualTypeForArgument(callTarget, arg) { var args = getEffectiveCallArguments(callTarget); @@ -16242,7 +17273,7 @@ var ts; var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) || getIndexTypeOfContextualType(type, 1 /* Number */) - || (languageVersion >= 2 /* ES6 */ ? checkIteratedType(type, undefined) : undefined); + || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, undefined) : undefined); } return undefined; } @@ -16272,6 +17303,8 @@ var ts; case 164 /* ArrowFunction */: case 192 /* ReturnStatement */: return getContextualTypeForReturnExpression(node); + case 173 /* YieldExpression */: + return getContextualTypeForYieldOperand(parent); case 158 /* CallExpression */: case 159 /* NewExpression */: return getContextualTypeForArgument(parent, node); @@ -16308,8 +17341,10 @@ var ts; return node.kind === 163 /* FunctionExpression */ || node.kind === 164 /* ArrowFunction */; } function getContextualSignatureForFunctionLikeDeclaration(node) { - // Only function expressions and arrow functions are contextually typed. - return isFunctionExpressionOrArrowFunction(node) ? getContextualSignature(node) : undefined; + // Only function expressions, arrow functions, and object literal methods are contextually typed. + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; } // Return the contextual signature for a given expression node. A contextual type provides a // contextual signature if it has a single call signature and if that call signature is non-generic. @@ -16419,7 +17454,7 @@ var ts; // if there is no index type / iterated type. var restArrayType = checkExpression(e.expression, contextualMapper); var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || - (languageVersion >= 2 /* ES6 */ ? checkIteratedType(restArrayType, undefined) : undefined); + (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, undefined) : undefined); if (restElementType) { elementTypes.push(restElementType); } @@ -16707,15 +17742,15 @@ var ts; // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. // See if we can index as a property. if (node.argumentExpression) { - var name_6 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_6 !== undefined) { - var prop = getPropertyOfType(objectType, name_6); + var name_9 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_9 !== undefined) { + var prop = getPropertyOfType(objectType, name_9); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_6, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_9, symbolToString(objectType.symbol)); return unknownType; } } @@ -16836,13 +17871,13 @@ var ts; for (var _i = 0; _i < signatures.length; _i++) { var signature = signatures[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_4 = signature.declaration && signature.declaration.parent; + var parent_6 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_4 === lastParent) { + if (lastParent && parent_6 === lastParent) { index++; } else { - lastParent = parent_4; + lastParent = parent_6; index = cutoffIndex; } } @@ -16850,7 +17885,7 @@ var ts; // current declaration belongs to a different symbol // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex index = cutoffIndex = result.length; - lastParent = parent_4; + lastParent = parent_6; } lastSymbol = symbol; // specialized signatures always need to be placed before non-specialized signatures regardless @@ -17335,10 +18370,10 @@ var ts; return resolveCall(node, callSignatures, candidatesOutArray); } function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 2 /* ES6 */) { + if (node.arguments && languageVersion < 1 /* ES5 */) { var spreadIndex = getSpreadArgumentIndex(node.arguments); if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher); + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); } } var expressionType = checkExpression(node.expression); @@ -17354,7 +18389,7 @@ var ts; // If ConstructExpr's apparent type(section 3.8.1) is an object type with one or // more construct signatures, the expression is processed in the same manner as a // function call, but using the construct signatures as the initial set of candidate - // signatures for overload resolution.The result type of the function call becomes + // signatures for overload resolution. The result type of the function call becomes // the result type of the operation. expressionType = getApparentType(expressionType); if (expressionType === unknownType) { @@ -17490,17 +18525,39 @@ var ts; type = checkExpressionCached(func.body, contextualMapper); } else { - // Aggregate the types of expressions within all the return statements. - var types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); - if (types.length === 0) { - return voidType; + var types; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func.body, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; + } } - // When return statements are contextually typed we allow the return type to be a union type. Otherwise we require the - // return expressions to have a best common supertype. + else { + types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); + if (types.length === 0) { + return voidType; + } + } + // When yield/return statements are contextually typed we allow the return type to be a union type. + // Otherwise we require the yield/return expressions to have a best common supertype. type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); if (!type) { - error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); - return unknownType; + if (funcIsGenerator) { + error(func, ts.Diagnostics.No_best_common_type_exists_among_yield_expressions); + return createIterableIteratorType(unknownType); + } + else { + error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); + return unknownType; + } + } + if (funcIsGenerator) { + type = createIterableIteratorType(type); } } if (!contextualSignature) { @@ -17508,7 +18565,23 @@ var ts; } return getWidenedType(type); } - /// Returns a set of types relating to every return expression relating to a function block. + function checkAndAggregateYieldOperandTypes(body, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + // A yield* expression effectively yields everything that its operand yields + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); + } + } + }); + return aggregatedTypes; + } function checkAndAggregateReturnExpressionTypes(body, contextualMapper) { var aggregatedTypes = []; ts.forEachReturnStatement(body, function (returnStatement) { @@ -17677,8 +18750,8 @@ var ts; var index = n.argumentExpression; var symbol = findSymbol(n.expression); if (symbol && index && index.kind === 8 /* StringLiteral */) { - var name_7 = index.text; - var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_7); + var name_10 = index.text; + var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_10); return prop && (prop.flags & 3 /* Variable */) !== 0 && (getDeclarationFlagsFromSymbol(prop) & 8192 /* Const */) !== 0; } return false; @@ -17836,16 +18909,16 @@ var ts; var p = properties[_i]; if (p.kind === 225 /* PropertyAssignment */ || p.kind === 226 /* ShorthandPropertyAssignment */) { // TODO(andersh): Computed property support - var name_8 = p.name; + var name_11 = p.name; var type = sourceType.flags & 1 /* Any */ ? sourceType : - getTypeOfPropertyOfType(sourceType, name_8.text) || - isNumericLiteralName(name_8.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || + getTypeOfPropertyOfType(sourceType, name_11.text) || + isNumericLiteralName(name_11.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || getIndexTypeOfType(sourceType, 0 /* String */); if (type) { - checkDestructuringAssignment(p.initializer || name_8, type); + checkDestructuringAssignment(p.initializer || name_11, type); } else { - error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_8)); + error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_11)); } } else { @@ -18095,14 +19168,53 @@ var ts; error(node, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(node.operatorToken.kind), typeToString(leftType), typeToString(rightType)); } } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (current.kind === 202 /* ClassDeclaration */ || current.kind === 175 /* ClassExpression */) { + return true; + } + current = parent; + parent = parent.parent; + } + return false; + } function checkYieldExpression(node) { // Grammar checking - if (!(node.parserContextFlags & 4 /* Yield */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration); + if (!(node.parserContextFlags & 4 /* Yield */) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); } - else { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expressions_are_not_currently_supported); + if (node.expression) { + var func = ts.getContainingFunction(node); + // If the user's code is syntactically correct, the func should always have a star. After all, + // we are in a yield context. + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, undefined); + var expressionElementType; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + // There is no point in doing an assignability check if the function + // has no explicit return type because the return type is directly computed + // from the yield expressions. + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, undefined); + } + } + } } + // Both yield and yield* expressions have type 'any' + return anyType; } function checkConditionalExpression(node, contextualMapper) { checkExpression(node.condition); @@ -18273,8 +19385,7 @@ var ts; case 176 /* OmittedExpression */: return undefinedType; case 173 /* YieldExpression */: - checkYieldExpression(node); - return unknownType; + return checkYieldExpression(node); } return unknownType; } @@ -18318,6 +19429,14 @@ var ts; error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); } } + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; + } + return node.kind === 135 /* MethodDeclaration */ || + node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */; + } function checkSignatureDeclaration(node) { // Grammar checking if (node.kind === 141 /* IndexSignature */) { @@ -18345,6 +19464,25 @@ var ts; break; } } + if (node.type) { + if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + // Naively, one could check that IterableIterator is assignable to the return type annotation. + // However, that would not catch the error in the following case. + // + // interface BadGenerator extends Iterable, Iterator { } + // function* g(): BadGenerator { } // Iterable and Iterator have different types! + // + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } + } + } } checkSpecializedSignatureDeclaration(node); } @@ -18984,7 +20122,6 @@ var ts; function checkFunctionDeclaration(node) { if (produceDiagnostics) { checkFunctionLikeDeclaration(node) || - checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarFunctionName(node.name) || checkGrammarForGenerator(node); checkCollisionWithCapturedSuperVariable(node, node.name); @@ -19027,10 +20164,18 @@ var ts; if (node.type && !isAccessor(node.kind) && !node.asteriskToken) { checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type)); } - // Report an implicit any error if there is no body, no explicit return type, and node is not a private method - // in an ambient context - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !node.type && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (produceDiagnostics && !node.type) { + // Report an implicit any error if there is no body, no explicit return type, and node is not a private method + // in an ambient context + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + // A generator with a body and no type annotation can still cause errors. It can error if the + // yielded values have no common supertype, or it can give an implicit any error if it has no + // yielded values. The only way to trigger these errors is to try checking its return type. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + } } } function checkBlock(node) { @@ -19191,8 +20336,8 @@ var ts; // otherwise if variable has an initializer - show error that initialization will fail // since LHS will be block scoped name instead of function scoped if (!namesShareScope) { - var name_9 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_9, name_9); + var name_12 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_12, name_12); } } } @@ -19302,7 +20447,7 @@ var ts; } function checkVariableStatement(node) { // Grammar checking - checkGrammarDecorators(node) || checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); ts.forEach(node.declarationList.declarations, checkSourceElement); } function checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) { @@ -19392,7 +20537,7 @@ var ts; // iteratedType will be undefined if the rightType was missing properties/signatures // required to get its iteratedType (like [Symbol.iterator] or next). This may be // because we accessed properties from anyType, or it may have led to an error inside - // getIteratedType. + // getElementTypeOfIterable. if (iteratedType) { checkTypeAssignableTo(iteratedType, leftType, varExpr, undefined); } @@ -19458,7 +20603,7 @@ var ts; return inputType; } if (languageVersion >= 2 /* ES6 */) { - return checkIteratedType(inputType, errorNode) || anyType; + return checkElementTypeOfIterable(inputType, errorNode); } if (allowStringInput) { return checkElementTypeOfArrayOrString(inputType, errorNode); @@ -19475,88 +20620,127 @@ var ts; /** * When errorNode is undefined, it means we should not report any errors. */ - function checkIteratedType(iterable, errorNode) { - ts.Debug.assert(languageVersion >= 2 /* ES6 */); - var iteratedType = getIteratedType(iterable, errorNode); + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); // Now even though we have extracted the iteratedType, we will have to validate that the type // passed in is actually an Iterable. - if (errorNode && iteratedType) { - checkTypeAssignableTo(iterable, createIterableType(iteratedType), errorNode); + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - return iteratedType; - function getIteratedType(iterable, errorNode) { - // We want to treat type as an iterable, and get the type it is an iterable of. The iterable - // must have the following structure (annotated with the names of the variables below): - // - // { // iterable - // [Symbol.iterator]: { // iteratorFunction - // (): { // iterator - // next: { // iteratorNextFunction - // (): { // iteratorNextResult - // value: T // iteratorNextValue - // } - // } - // } - // } - // } - // - // T is the type we are after. At every level that involves analyzing return types - // of signatures, we union the return types of all the signatures. - // - // Another thing to note is that at any step of this process, we could run into a dead end, - // meaning either the property is missing, or we run into the anyType. If either of these things - // happens, we return undefined to signal that we could not find the iterated type. If a property - // is missing, and the previous step did not result in 'any', then we also give an error if the - // caller requested it. Then the caller can decide what to do in the case where there is no iterated - // type. This is different from returning anyType, because that would signify that we have matched the - // whole pattern and that T (above) is 'any'. - if (allConstituentTypesHaveKind(iterable, 1 /* Any */)) { - return undefined; - } + return elementType || anyType; + } + /** + * We want to treat type as an iterable, and get the type it is an iterable of. The iterable + * must have the following structure (annotated with the names of the variables below): + * + * { // iterable + * [Symbol.iterator]: { // iteratorFunction + * (): Iterator + * } + * } + * + * T is the type we are after. At every level that involves analyzing return types + * of signatures, we union the return types of all the signatures. + * + * Another thing to note is that at any step of this process, we could run into a dead end, + * meaning either the property is missing, or we run into the anyType. If either of these things + * happens, we return undefined to signal that we could not find the iterated type. If a property + * is missing, and the previous step did not result in 'any', then we also give an error if the + * caller requested it. Then the caller can decide what to do in the case where there is no iterated + * type. This is different from returning anyType, because that would signify that we have matched the + * whole pattern and that T (above) is 'any'. + */ + function getElementTypeOfIterable(type, errorNode) { + if (type.flags & 1 /* Any */) { + return undefined; + } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), // then just grab its type argument. - if ((iterable.flags & 4096 /* Reference */) && iterable.target === globalIterableType) { - return iterable.typeArguments[0]; + if ((type.flags & 4096 /* Reference */) && type.target === globalIterableType) { + typeAsIterable.iterableElementType = type.typeArguments[0]; } - var iteratorFunction = getTypeOfPropertyOfType(iterable, ts.getPropertyNameForKnownSymbolName("iterator")); - if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, 1 /* Any */)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + else { + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (iteratorFunction && iteratorFunction.flags & 1 /* Any */) { + return undefined; } - return undefined; - } - var iterator = getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iterator, 1 /* Any */)) { - return undefined; - } - var iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next"); - if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, 1 /* Any */)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; } - return undefined; + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)), errorNode); } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iteratorNextResult, 1 /* Any */)) { - return undefined; - } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; - } - return iteratorNextValue; } + return typeAsIterable.iterableElementType; + } + /** + * This function has very similar logic as getElementTypeOfIterable, except that it operates on + * Iterators instead of Iterables. Here is the structure: + * + * { // iterator + * next: { // iteratorNextFunction + * (): { // iteratorNextResult + * value: T // iteratorNextValue + * } + * } + * } + * + */ + function getElementTypeOfIterator(type, errorNode) { + if (type.flags & 1 /* Any */) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), + // then just grab its type argument. + if ((type.flags & 4096 /* Reference */) && type.target === globalIteratorType) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; + } + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (iteratorNextFunction && iteratorNextFunction.flags & 1 /* Any */) { + return undefined; + } + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; + } + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); + if (iteratorNextResult.flags & 1 /* Any */) { + return undefined; + } + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; + } + } + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (type.flags & 1 /* Any */) { + return undefined; + } + // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), + // then just grab its type argument. + if ((type.flags & 4096 /* Reference */) && type.target === globalIterableIteratorType) { + return type.typeArguments[0]; + } + return getElementTypeOfIterable(type, undefined) || + getElementTypeOfIterator(type, undefined); } /** * This function does the following steps: @@ -19637,19 +20821,24 @@ var ts; if (func) { var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var exprType = checkExpressionCached(node.expression); + if (func.asteriskToken) { + // A generator does not need its return expressions checked against its return type. + // Instead, the yield expressions are checked against the element type. + // TODO: Check return expressions of generators when return type tracking is added + // for generators. + return; + } if (func.kind === 138 /* SetAccessor */) { error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); } - else { - if (func.kind === 136 /* Constructor */) { - if (!isTypeAssignableTo(exprType, returnType)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { - checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + else if (func.kind === 136 /* Constructor */) { + if (!isTypeAssignableTo(exprType, returnType)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } + else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { + checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + } } } } @@ -19870,9 +21059,6 @@ var ts; function checkClassDeclaration(node) { checkGrammarDeclarationNameInStrictMode(node); // Grammar checking - if (node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { - grammarErrorOnNode(node, ts.Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration); - } if (!node.name && !(node.flags & 256 /* Default */)) { grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); } @@ -20978,87 +22164,6 @@ var ts; } return node.parent && node.parent.kind === 177 /* ExpressionWithTypeArguments */; } - function isTypeNode(node) { - if (142 /* FirstTypeNode */ <= node.kind && node.kind <= 150 /* LastTypeNode */) { - return true; - } - switch (node.kind) { - case 112 /* AnyKeyword */: - case 120 /* NumberKeyword */: - case 122 /* StringKeyword */: - case 113 /* BooleanKeyword */: - case 123 /* SymbolKeyword */: - return true; - case 99 /* VoidKeyword */: - return node.parent.kind !== 167 /* VoidExpression */; - case 8 /* StringLiteral */: - // Specialized signatures can have string literals as their parameters' type names - return node.parent.kind === 130 /* Parameter */; - case 177 /* ExpressionWithTypeArguments */: - return true; - // Identifiers and qualified names may be type nodes, depending on their context. Climb - // above them to find the lowest container - case 65 /* Identifier */: - // If the identifier is the RHS of a qualified name, then it's a type iff its parent is. - if (node.parent.kind === 127 /* QualifiedName */ && node.parent.right === node) { - node = node.parent; - } - else if (node.parent.kind === 156 /* PropertyAccessExpression */ && node.parent.name === node) { - node = node.parent; - } - // fall through - case 127 /* QualifiedName */: - case 156 /* PropertyAccessExpression */: - // At this point, node is either a qualified name or an identifier - ts.Debug.assert(node.kind === 65 /* Identifier */ || node.kind === 127 /* QualifiedName */ || node.kind === 156 /* PropertyAccessExpression */, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); - var parent_5 = node.parent; - if (parent_5.kind === 145 /* TypeQuery */) { - return false; - } - // Do not recursively call isTypeNode on the parent. In the example: - // - // let a: A.B.C; - // - // Calling isTypeNode would consider the qualified name A.B a type node. Only C or - // A.B.C is a type node. - if (142 /* FirstTypeNode */ <= parent_5.kind && parent_5.kind <= 150 /* LastTypeNode */) { - return true; - } - switch (parent_5.kind) { - case 177 /* ExpressionWithTypeArguments */: - return true; - case 129 /* TypeParameter */: - return node === parent_5.constraint; - case 133 /* PropertyDeclaration */: - case 132 /* PropertySignature */: - case 130 /* Parameter */: - case 199 /* VariableDeclaration */: - return node === parent_5.type; - case 201 /* FunctionDeclaration */: - case 163 /* FunctionExpression */: - case 164 /* ArrowFunction */: - case 136 /* Constructor */: - case 135 /* MethodDeclaration */: - case 134 /* MethodSignature */: - case 137 /* GetAccessor */: - case 138 /* SetAccessor */: - return node === parent_5.type; - case 139 /* CallSignature */: - case 140 /* ConstructSignature */: - case 141 /* IndexSignature */: - return node === parent_5.type; - case 161 /* TypeAssertionExpression */: - return node === parent_5.type; - case 158 /* CallExpression */: - case 159 /* NewExpression */: - return parent_5.typeArguments && ts.indexOf(parent_5.typeArguments, node) >= 0; - case 160 /* TaggedTemplateExpression */: - // TODO (drosen): TaggedTemplateExpressions may eventually support type arguments. - return false; - } - } - return false; - } function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { while (nodeOnRightSide.parent.kind === 127 /* QualifiedName */) { nodeOnRightSide = nodeOnRightSide.parent; @@ -21201,7 +22306,7 @@ var ts; // We cannot answer semantic questions within a with block, do not proceed any further return unknownType; } - if (isTypeNode(node)) { + if (ts.isTypeNode(node)) { return getTypeFromTypeNode(node); } if (ts.isExpression(node)) { @@ -21255,9 +22360,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456 /* UnionProperty */) { var symbols = []; - var name_10 = symbol.name; + var name_13 = symbol.name; ts.forEach(getSymbolLinks(symbol).unionType.types, function (t) { - symbols.push(getPropertyOfType(t, name_10)); + symbols.push(getPropertyOfType(t, name_13)); }); return symbols; } @@ -21712,8 +22817,7 @@ var ts; getSymbolLinks(unknownSymbol).type = unknownType; globals[undefinedSymbol.name] = undefinedSymbol; // Initialize special types - globalArraySymbol = getGlobalTypeSymbol("Array"); - globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1); + globalArrayType = getGlobalType("Array", 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); @@ -21731,6 +22835,8 @@ var ts; globalESSymbolType = getGlobalType("Symbol"); globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"); globalIterableType = getGlobalType("Iterable", 1); + globalIteratorType = getGlobalType("Iterator", 1); + globalIterableIteratorType = getGlobalType("IterableIterator", 1); } else { globalTemplateStringsArrayType = unknownType; @@ -21739,6 +22845,9 @@ var ts; // a global Symbol already, particularly if it is a class. globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); globalESSymbolConstructorSymbol = undefined; + globalIterableType = emptyGenericType; + globalIteratorType = emptyGenericType; + globalIterableIteratorType = emptyGenericType; } anyArrayType = createArrayType(anyType); } @@ -21763,20 +22872,20 @@ var ts; if (impotClause.namedBindings) { var nameBindings = impotClause.namedBindings; if (nameBindings.kind === 212 /* NamespaceImport */) { - var name_11 = nameBindings.name; - if (isReservedWordInStrictMode(name_11)) { - var nameText = ts.declarationNameToString(name_11); - return grammarErrorOnNode(name_11, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_14 = nameBindings.name; + if (isReservedWordInStrictMode(name_14)) { + var nameText = ts.declarationNameToString(name_14); + return grammarErrorOnNode(name_14, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } else if (nameBindings.kind === 213 /* NamedImports */) { var reportError = false; for (var _i = 0, _a = nameBindings.elements; _i < _a.length; _i++) { var element = _a[_i]; - var name_12 = element.name; - if (isReservedWordInStrictMode(name_12)) { - var nameText = ts.declarationNameToString(name_12); - reportError = reportError || grammarErrorOnNode(name_12, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_15 = element.name; + if (isReservedWordInStrictMode(name_15)) { + var nameText = ts.declarationNameToString(name_15); + reportError = reportError || grammarErrorOnNode(name_15, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } return reportError; @@ -21902,19 +23011,28 @@ var ts; case 135 /* MethodDeclaration */: case 134 /* MethodSignature */: case 141 /* IndexSignature */: - case 202 /* ClassDeclaration */: - case 203 /* InterfaceDeclaration */: case 206 /* ModuleDeclaration */: - case 205 /* EnumDeclaration */: - case 181 /* VariableStatement */: - case 201 /* FunctionDeclaration */: - case 204 /* TypeAliasDeclaration */: case 210 /* ImportDeclaration */: case 209 /* ImportEqualsDeclaration */: case 216 /* ExportDeclaration */: case 215 /* ExportAssignment */: case 130 /* Parameter */: break; + case 202 /* ClassDeclaration */: + case 203 /* InterfaceDeclaration */: + case 181 /* VariableStatement */: + case 201 /* FunctionDeclaration */: + case 204 /* TypeAliasDeclaration */: + if (node.modifiers && node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; + case 205 /* EnumDeclaration */: + if (node.modifiers && (node.modifiers.length > 1 || node.modifiers[0].kind !== 70 /* ConstKeyword */) && + node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; default: return false; } @@ -22012,9 +23130,6 @@ var ts; else if ((node.kind === 210 /* ImportDeclaration */ || node.kind === 209 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_import_declaration, "declare"); } - else if (node.kind === 203 /* InterfaceDeclaration */ && flags & 2 /* Ambient */) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare"); - } else if (node.kind === 130 /* Parameter */ && (flags & 112 /* AccessibilityModifier */) && ts.isBindingPattern(node.name)) { return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_a_binding_pattern); } @@ -22231,7 +23346,18 @@ var ts; } function checkGrammarForGenerator(node) { if (node.asteriskToken) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_currently_supported); + ts.Debug.assert(node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */ || + node.kind === 135 /* MethodDeclaration */); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2 /* ES6 */) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_6_or_higher); + } } } function checkGrammarFunctionName(name) { @@ -22252,11 +23378,11 @@ var ts; var inStrictMode = (node.parserContextFlags & 1 /* StrictMode */) !== 0; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_13 = prop.name; + var name_16 = prop.name; if (prop.kind === 176 /* OmittedExpression */ || - name_13.kind === 128 /* ComputedPropertyName */) { + name_16.kind === 128 /* ComputedPropertyName */) { // If the name is not a ComputedPropertyName, the grammar checking will skip it - checkGrammarComputedPropertyName(name_13); + checkGrammarComputedPropertyName(name_16); continue; } // ECMA-262 11.1.5 Object Initialiser @@ -22271,8 +23397,8 @@ var ts; if (prop.kind === 225 /* PropertyAssignment */ || prop.kind === 226 /* ShorthandPropertyAssignment */) { // Grammar checking for computedPropertName and shorthandPropertyAssignment checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_13.kind === 7 /* NumericLiteral */) { - checkGrammarNumericLiteral(name_13); + if (name_16.kind === 7 /* NumericLiteral */) { + checkGrammarNumericLiteral(name_16); } currentKind = Property; } @@ -22288,26 +23414,26 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - if (!ts.hasProperty(seen, name_13.text)) { - seen[name_13.text] = currentKind; + if (!ts.hasProperty(seen, name_16.text)) { + seen[name_16.text] = currentKind; } else { - var existingKind = seen[name_13.text]; + var existingKind = seen[name_16.text]; if (currentKind === Property && existingKind === Property) { if (inStrictMode) { - grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); + grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); } } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[name_13.text] = currentKind | existingKind; + seen[name_16.text] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -23168,9 +24294,9 @@ var ts; } var count = 0; while (true) { - var name_14 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_14)) { - return name_14; + var name_17 = baseName + "_" + (++count); + if (!ts.hasProperty(currentSourceFile.identifiers, name_17)) { + return name_17; } } } @@ -24379,9 +25505,9 @@ var ts; tempFlags++; // Skip over 'i' and 'n' if (count !== 8 && count !== 13) { - var name_15 = count < 26 ? "_" + String.fromCharCode(97 /* a */ + count) : "_" + (count - 26); - if (isUniqueName(name_15)) { - return name_15; + var name_18 = count < 26 ? "_" + String.fromCharCode(97 /* a */ + count) : "_" + (count - 26); + if (isUniqueName(name_18)) { + return name_18; } } } @@ -24414,9 +25540,9 @@ var ts; } function generateNameForModuleOrEnum(node) { if (node.name.kind === 65 /* Identifier */) { - var name_16 = node.name.text; + var name_19 = node.name.text; // Use module/enum name itself if it is unique, otherwise make a unique variation - assignGeneratedName(node, isUniqueLocalName(name_16, node) ? name_16 : makeUniqueName(name_16)); + assignGeneratedName(node, isUniqueLocalName(name_19, node) ? name_19 : makeUniqueName(name_19)); } } function generateNameForImportOrExportDeclaration(node) { @@ -24635,8 +25761,8 @@ var ts; // Child scopes are always shown with a dot (even if they have no name), // unless it is a computed property. Then it is shown with brackets, // but the brackets are included in the name. - var name_17 = node.name; - if (!name_17 || name_17.kind !== 128 /* ComputedPropertyName */) { + var name_20 = node.name; + if (!name_20 || name_20.kind !== 128 /* ComputedPropertyName */) { scopeName = "." + scopeName; } scopeName = sourceMapData.sourceMapNames[parentIndex] + scopeName; @@ -24665,10 +25791,10 @@ var ts; node.kind === 205 /* EnumDeclaration */) { // Declaration and has associated name use it if (node.name) { - var name_18 = node.name; + var name_21 = node.name; // For computed property names, the text will include the brackets - scopeName = name_18.kind === 128 /* ComputedPropertyName */ - ? ts.getTextOfNode(name_18) + scopeName = name_21.kind === 128 /* ComputedPropertyName */ + ? ts.getTextOfNode(name_21) : node.name.text; } recordScopeNameStart(scopeName); @@ -25369,16 +26495,16 @@ var ts; } return true; } - function emitListWithSpread(elements, alwaysCopy, multiLine, trailingComma) { + function emitListWithSpread(elements, needsUniqueCopy, multiLine, trailingComma, useConcat) { var pos = 0; var group = 0; var length = elements.length; while (pos < length) { // Emit using the pattern .concat(, , ...) - if (group === 1) { + if (group === 1 && useConcat) { write(".concat("); } - else if (group > 1) { + else if (group > 0) { write(", "); } var e = elements[pos]; @@ -25386,7 +26512,7 @@ var ts; e = e.expression; emitParenthesizedIf(e, group === 0 && needsParenthesisForPropertyAccessOrInvocation(e)); pos++; - if (pos === length && group === 0 && alwaysCopy && e.kind !== 154 /* ArrayLiteralExpression */) { + if (pos === length && group === 0 && needsUniqueCopy && e.kind !== 154 /* ArrayLiteralExpression */) { write(".slice()"); } } @@ -25409,7 +26535,9 @@ var ts; group++; } if (group > 1) { - write(")"); + if (useConcat) { + write(")"); + } } } function isSpreadElementExpression(node) { @@ -25427,7 +26555,7 @@ var ts; } else { emitListWithSpread(elements, true, (node.flags & 512 /* MultiLine */) !== 0, - /*trailingComma*/ elements.hasTrailingComma); + /*trailingComma*/ elements.hasTrailingComma, true); } } function emitObjectLiteralBody(node, numElements) { @@ -25798,7 +26926,7 @@ var ts; write("void 0"); } write(", "); - emitListWithSpread(node.arguments, false, false, false); + emitListWithSpread(node.arguments, false, false, false, true); write(")"); } function emitCallExpression(node) { @@ -25832,11 +26960,42 @@ var ts; } function emitNewExpression(node) { write("new "); - emit(node.expression); - if (node.arguments) { + // Spread operator logic can be supported in new expressions in ES5 using a combination + // of Function.prototype.bind() and Function.prototype.apply(). + // + // Example: + // + // var arguments = [1, 2, 3, 4, 5]; + // new Array(...arguments); + // + // Could be transpiled into ES5: + // + // var arguments = [1, 2, 3, 4, 5]; + // new (Array.bind.apply(Array, [void 0].concat(arguments))); + // + // `[void 0]` is the first argument which represents `thisArg` to the bind method above. + // And `thisArg` will be set to the return value of the constructor when instantiated + // with the new operator — regardless of any value we set `thisArg` to. Thus, we set it + // to an undefined, `void 0`. + if (languageVersion === 1 /* ES5 */ && + node.arguments && + hasSpreadElement(node.arguments)) { write("("); - emitCommaList(node.arguments); - write(")"); + var target = emitCallTarget(node.expression); + write(".bind.apply("); + emit(target); + write(", [void 0].concat("); + emitListWithSpread(node.arguments, false, false, false, false); + write(")))"); + write("()"); + } + else { + emit(node.expression); + if (node.arguments) { + write("("); + emitCommaList(node.arguments); + write(")"); + } } } function emitTaggedTemplateExpression(node) { @@ -26136,9 +27295,10 @@ var ts; write(")"); emitEmbeddedStatement(node.statement); } - /* Returns true if start of variable declaration list was emitted. - * Return false if nothing was written - this can happen for source file level variable declarations - * in system modules - such variable declarations are hoisted. + /** + * Returns true if start of variable declaration list was emitted. + * Returns false if nothing was written - this can happen for source file level variable declarations + * in system modules where such variable declarations are hoisted. */ function tryEmitStartOfVariableDeclarationList(decl, startPos) { if (shouldHoistVariable(decl, true)) { @@ -26901,15 +28061,35 @@ var ts; ts.forEach(node.declarationList.declarations, emitExportVariableAssignments); } } + function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node) { + // If we're not exporting the variables, there's nothing special here. + // Always emit comments for these nodes. + if (!(node.flags & 1 /* Export */)) { + return true; + } + // If we are exporting, but it's a top-level ES6 module exports, + // we'll emit the declaration list verbatim, so emit comments too. + if (isES6ExportedDeclaration(node)) { + return true; + } + // Otherwise, only emit if we have at least one initializer present. + for (var _a = 0, _b = node.declarationList.declarations; _a < _b.length; _a++) { + var declaration = _b[_a]; + if (declaration.initializer) { + return true; + } + } + return false; + } function emitParameter(node) { if (languageVersion < 2 /* ES6 */) { if (ts.isBindingPattern(node.name)) { - var name_19 = createTempVariable(0 /* Auto */); + var name_22 = createTempVariable(0 /* Auto */); if (!tempParameters) { tempParameters = []; } - tempParameters.push(name_19); - emit(name_19); + tempParameters.push(name_22); + emit(name_22); } else { emit(node.name); @@ -28577,8 +29757,8 @@ var ts; // export { x, y } for (var _c = 0, _d = node.exportClause.elements; _c < _d.length; _c++) { var specifier = _d[_c]; - var name_20 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_20] || (exportSpecifiers[name_20] = [])).push(specifier); + var name_23 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_23] || (exportSpecifiers[name_23] = [])).push(specifier); } } break; @@ -28778,12 +29958,12 @@ var ts; var seen = {}; for (var i = 0; i < hoistedVars.length; ++i) { var local = hoistedVars[i]; - var name_21 = local.kind === 65 /* Identifier */ + var name_24 = local.kind === 65 /* Identifier */ ? local : local.name; - if (name_21) { + if (name_24) { // do not emit duplicate entries (in case of declaration merging) in the list of hoisted variables - var text = ts.unescapeIdentifier(name_21.text); + var text = ts.unescapeIdentifier(name_24.text); if (ts.hasProperty(seen, text)) { continue; } @@ -28862,15 +30042,15 @@ var ts; } if (node.kind === 199 /* VariableDeclaration */ || node.kind === 153 /* BindingElement */) { if (shouldHoistVariable(node, false)) { - var name_22 = node.name; - if (name_22.kind === 65 /* Identifier */) { + var name_25 = node.name; + if (name_25.kind === 65 /* Identifier */) { if (!hoistedVars) { hoistedVars = []; } - hoistedVars.push(name_22); + hoistedVars.push(name_25); } else { - ts.forEachChild(name_22, visit); + ts.forEachChild(name_25, visit); } } return; @@ -29329,6 +30509,8 @@ var ts; case 204 /* TypeAliasDeclaration */: case 215 /* ExportAssignment */: return false; + case 181 /* VariableStatement */: + return shouldEmitLeadingAndTrailingCommentsForVariableStatement(node); case 206 /* ModuleDeclaration */: // Only emit the leading/trailing comments for a module if we're actually // emitting the module as well. @@ -30710,28 +31892,28 @@ var ts; switch (n.kind) { case 180 /* Block */: if (!ts.isFunctionBlock(n)) { - var parent_6 = n.parent; + var parent_7 = n.parent; var openBrace = ts.findChildOfKind(n, 14 /* OpenBraceToken */, sourceFile); var closeBrace = ts.findChildOfKind(n, 15 /* CloseBraceToken */, sourceFile); // Check if the block is standalone, or 'attached' to some parent statement. // If the latter, we want to collaps the block, but consider its hint span // to be the entire span of the parent. - if (parent_6.kind === 185 /* DoStatement */ || - parent_6.kind === 188 /* ForInStatement */ || - parent_6.kind === 189 /* ForOfStatement */ || - parent_6.kind === 187 /* ForStatement */ || - parent_6.kind === 184 /* IfStatement */ || - parent_6.kind === 186 /* WhileStatement */ || - parent_6.kind === 193 /* WithStatement */ || - parent_6.kind === 224 /* CatchClause */) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + if (parent_7.kind === 185 /* DoStatement */ || + parent_7.kind === 188 /* ForInStatement */ || + parent_7.kind === 189 /* ForOfStatement */ || + parent_7.kind === 187 /* ForStatement */ || + parent_7.kind === 184 /* IfStatement */ || + parent_7.kind === 186 /* WhileStatement */ || + parent_7.kind === 193 /* WithStatement */ || + parent_7.kind === 224 /* CatchClause */) { + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } - if (parent_6.kind === 197 /* TryStatement */) { + if (parent_7.kind === 197 /* TryStatement */) { // Could be the try-block, or the finally-block. - var tryStatement = parent_6; + var tryStatement = parent_7; if (tryStatement.tryBlock === n) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } else if (tryStatement.finallyBlock === n) { @@ -30798,12 +31980,12 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_23 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_23); + for (var name_26 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_26); if (declarations) { // First do a quick check to see if the name of the declaration matches the // last portion of the (possibly) dotted name they're searching for. - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_23); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_26); if (!matches) { continue; } @@ -30816,14 +31998,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_23); + matches = patternMatcher.getMatches(containers, name_26); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_23, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_26, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -31206,9 +32388,9 @@ var ts; case 199 /* VariableDeclaration */: case 153 /* BindingElement */: var variableDeclarationNode; - var name_24; + var name_27; if (node.kind === 153 /* BindingElement */) { - name_24 = node.name; + name_27 = node.name; variableDeclarationNode = node; // binding elements are added only for variable declarations // bubble up to the containing variable declaration @@ -31220,16 +32402,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_24 = node.name; + name_27 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.variableElement); } case 136 /* Constructor */: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -32751,7 +33933,7 @@ var ts; // for the position of the relevant node (or comma). var syntaxList = ts.forEach(node.parent.getChildren(), function (c) { // find syntax list that covers the span of the node - if (c.kind === 229 /* SyntaxList */ && c.pos <= node.pos && c.end >= node.end) { + if (c.kind === 251 /* SyntaxList */ && c.pos <= node.pos && c.end >= node.end) { return c; } }); @@ -33158,10 +34340,6 @@ var ts; }); } ts.signatureToDisplayParts = signatureToDisplayParts; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); - } - ts.isJavaScript = isJavaScript; })(ts || (ts = {})); /// /// @@ -33644,7 +34822,7 @@ var ts; this.NoSpaceBeforeOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 18 /* OpenBracketToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.NoSpaceAfterOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create3(18 /* OpenBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.NoSpaceBeforeCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 19 /* CloseBracketToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); - this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19 /* CloseBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); + this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19 /* CloseBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), 8 /* Delete */)); // Place a space before open brace in a function declaration this.FunctionOpenBraceLeftTokenRange = formatting.Shared.TokenRange.AnyIncludingMultilineComments; this.SpaceBeforeOpenBraceInFunction = new formatting.Rule(formatting.RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, 14 /* OpenBraceToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), 2 /* Space */), 1 /* CanDeleteNewLines */); @@ -33727,6 +34905,10 @@ var ts; this.SpaceBeforeAt = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 52 /* AtToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 2 /* Space */)); this.NoSpaceAfterAt = new formatting.Rule(formatting.RuleDescriptor.create3(52 /* AtToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.SpaceAfterDecorator = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.Any, formatting.Shared.TokenRange.FromTokens([65 /* Identifier */, 78 /* ExportKeyword */, 73 /* DefaultKeyword */, 69 /* ClassKeyword */, 109 /* StaticKeyword */, 108 /* PublicKeyword */, 106 /* PrivateKeyword */, 107 /* ProtectedKeyword */, 116 /* GetKeyword */, 121 /* SetKeyword */, 18 /* OpenBracketToken */, 35 /* AsteriskToken */])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsEndOfDecoratorContextOnSameLine), 2 /* Space */)); + this.NoSpaceBetweenFunctionKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(83 /* FunctionKeyword */, 35 /* AsteriskToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 8 /* Delete */)); + this.SpaceAfterStarInGeneratorDeclaration = new formatting.Rule(formatting.RuleDescriptor.create3(35 /* AsteriskToken */, formatting.Shared.TokenRange.FromTokens([65 /* Identifier */, 16 /* OpenParenToken */])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 2 /* Space */)); + this.NoSpaceBetweenYieldKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(110 /* YieldKeyword */, 35 /* AsteriskToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 8 /* Delete */)); + this.SpaceBetweenYieldOrYieldStarAndOperand = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.FromTokens([110 /* YieldKeyword */, 35 /* AsteriskToken */]), formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 2 /* Space */)); // These rules are higher in priority than user-configurable rules. this.HighPriorityCommonRules = [ @@ -33744,7 +34926,9 @@ var ts; this.NoSpaceAfterCloseBrace, this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext, this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets, + this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGeneratorDeclaration, this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, + this.NoSpaceBetweenYieldKeywordAndStar, this.SpaceBetweenYieldOrYieldStarAndOperand, this.NoSpaceBetweenReturnAndSemicolon, this.SpaceAfterCertainKeywords, this.SpaceAfterLetConstInVariableDeclaration, @@ -33816,9 +35000,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_25 in o) { - if (o[name_25] === rule) { - return name_25; + for (var name_28 in o) { + if (o[name_28] === rule) { + return name_28; } } throw new Error("Unknown rule"); @@ -33937,6 +35121,9 @@ var ts; } return false; }; + Rules.IsFunctionDeclarationOrFunctionExpressionContext = function (context) { + return context.contextNode.kind === 201 /* FunctionDeclaration */ || context.contextNode.kind === 163 /* FunctionExpression */; + }; Rules.IsTypeScriptDeclWithBlockContext = function (context) { return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode); }; @@ -34001,6 +35188,9 @@ var ts; Rules.IsSameLineTokenContext = function (context) { return context.TokensAreOnSameLine(); }; + Rules.IsNotBeforeBlockInFunctionDeclarationContext = function (context) { + return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context); + }; Rules.IsEndOfDecoratorContextOnSameLine = function (context) { return context.TokensAreOnSameLine() && context.contextNode.decorators && @@ -34055,6 +35245,9 @@ var ts; Rules.IsVoidOpContext = function (context) { return context.currentTokenSpan.kind === 99 /* VoidKeyword */ && context.currentTokenParent.kind === 167 /* VoidExpression */; }; + Rules.IsYieldOrYieldStarWithOperand = function (context) { + return context.contextNode.kind === 173 /* YieldExpression */ && context.contextNode.expression !== undefined; + }; return Rules; })(); formatting.Rules = Rules; @@ -35759,7 +36952,7 @@ var ts; return pos; }; NodeObject.prototype.createSyntaxList = function (nodes) { - var list = createNode(229 /* SyntaxList */, nodes.pos, nodes.end, 1024 /* Synthetic */, this); + var list = createNode(251 /* SyntaxList */, nodes.pos, nodes.end, 1024 /* Synthetic */, this); list._children = []; var pos = nodes.pos; for (var _i = 0; _i < nodes.length; _i++) { @@ -36557,9 +37750,9 @@ var ts; return false; } // If the parent is not sourceFile or module block it is local variable - for (var parent_7 = declaration.parent; !ts.isFunctionBlock(parent_7); parent_7 = parent_7.parent) { + for (var parent_8 = declaration.parent; !ts.isFunctionBlock(parent_8); parent_8 = parent_8.parent) { // Reached source file or module block - if (parent_7.kind === 228 /* SourceFile */ || parent_7.kind === 207 /* ModuleBlock */) { + if (parent_8.kind === 228 /* SourceFile */ || parent_8.kind === 207 /* ModuleBlock */) { return false; } } @@ -37537,7 +38730,7 @@ var ts; return true; } if (parameter.questionToken) { - diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics.can_only_be_used_in_a_ts_file)); + diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics._0_can_only_be_used_in_a_ts_file, '?')); return true; } if (parameter.type) { @@ -37919,12 +39112,12 @@ var ts; function getContainingObjectLiteralApplicableForCompletion(previousToken) { // The locations in an object literal expression that are applicable for completion are property name definition locations. if (previousToken) { - var parent_8 = previousToken.parent; + var parent_9 = previousToken.parent; switch (previousToken.kind) { case 14 /* OpenBraceToken */: // let x = { | case 23 /* CommaToken */: - if (parent_8 && parent_8.kind === 155 /* ObjectLiteralExpression */) { - return parent_8; + if (parent_9 && parent_9.kind === 155 /* ObjectLiteralExpression */) { + return parent_9; } break; } @@ -38107,10 +39300,10 @@ var ts; for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { var sourceFile = _a[_i]; var nameTable = getNameTable(sourceFile); - for (var name_26 in nameTable) { - if (!allNames[name_26]) { - allNames[name_26] = name_26; - var displayName = getCompletionEntryDisplayName(name_26, target, true); + for (var name_29 in nameTable) { + if (!allNames[name_29]) { + allNames[name_29] = name_29; + var displayName = getCompletionEntryDisplayName(name_29, target, true); if (displayName) { var entry = { name: displayName, @@ -39018,19 +40211,19 @@ var ts; function getThrowStatementOwner(throwStatement) { var child = throwStatement; while (child.parent) { - var parent_9 = child.parent; - if (ts.isFunctionBlock(parent_9) || parent_9.kind === 228 /* SourceFile */) { - return parent_9; + var parent_10 = child.parent; + if (ts.isFunctionBlock(parent_10) || parent_10.kind === 228 /* SourceFile */) { + return parent_10; } // A throw-statement is only owned by a try-statement if the try-statement has // a catch clause, and if the throw-statement occurs within the try block. - if (parent_9.kind === 197 /* TryStatement */) { - var tryStatement = parent_9; + if (parent_10.kind === 197 /* TryStatement */) { + var tryStatement = parent_10; if (tryStatement.tryBlock === child && tryStatement.catchClause) { return child; } } - child = parent_9; + child = parent_10; } return undefined; } @@ -39992,19 +41185,19 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_27 = node.text; + var name_30 = node.text; if (contextualType) { if (contextualType.flags & 16384 /* Union */) { // This is a union type, first see if the property we are looking for is a union property (i.e. exists in all types) // if not, search the constituent types for the property - var unionProperty = contextualType.getProperty(name_27); + var unionProperty = contextualType.getProperty(name_30); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_27); + var symbol = t.getProperty(name_30); if (symbol) { result_4.push(symbol); } @@ -40013,7 +41206,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_27); + var symbol_1 = contextualType.getProperty(name_30); if (symbol_1) { return [symbol_1]; } @@ -40994,7 +42187,7 @@ var ts; var lastEnd = 0; for (var i = 0, n = dense.length; i < n; i += 3) { var start = dense[i]; - var length_1 = dense[i + 1]; + var length_2 = dense[i + 1]; var type = dense[i + 2]; // Make a whitespace entry between the last item and this one. if (lastEnd >= 0) { @@ -41003,8 +42196,8 @@ var ts; entries.push({ length: whitespaceLength_1, classification: TokenClass.Whitespace }); } } - entries.push({ length: length_1, classification: convertClassification(type) }); - lastEnd = start + length_1; + entries.push({ length: length_2, classification: convertClassification(type) }); + lastEnd = start + length_2; } var whitespaceLength = text.length - lastEnd; if (whitespaceLength > 0) { diff --git a/bin/typescriptServices.d.ts b/bin/typescriptServices.d.ts index 558504fcede..0d54e446d8f 100644 --- a/bin/typescriptServices.d.ts +++ b/bin/typescriptServices.d.ts @@ -251,8 +251,30 @@ declare module ts { ShorthandPropertyAssignment = 226, EnumMember = 227, SourceFile = 228, - SyntaxList = 229, - Count = 230, + JSDocTypeExpression = 229, + JSDocAllType = 230, + JSDocUnknownType = 231, + JSDocArrayType = 232, + JSDocUnionType = 233, + JSDocTupleType = 234, + JSDocNullableType = 235, + JSDocNonNullableType = 236, + JSDocRecordType = 237, + JSDocRecordMember = 238, + JSDocTypeReference = 239, + JSDocOptionalType = 240, + JSDocFunctionType = 241, + JSDocVariadicType = 242, + JSDocConstructorType = 243, + JSDocThisType = 244, + JSDocComment = 245, + JSDocTag = 246, + JSDocParameterTag = 247, + JSDocReturnTag = 248, + JSDocTypeTag = 249, + JSDocTemplateTag = 250, + SyntaxList = 251, + Count = 252, FirstAssignment = 53, LastAssignment = 64, FirstReservedWord = 66, @@ -495,7 +517,7 @@ declare module ts { } interface YieldExpression extends Expression { asteriskToken?: Node; - expression: Expression; + expression?: Expression; } interface BinaryExpression extends Expression { left: Expression; @@ -666,7 +688,7 @@ declare module ts { interface ClassElement extends Declaration { _classElementBrand: any; } - interface InterfaceDeclaration extends Declaration, ModuleElement { + interface InterfaceDeclaration extends Declaration, Statement { name: Identifier; typeParameters?: NodeArray; heritageClauses?: NodeArray; @@ -676,7 +698,7 @@ declare module ts { token: SyntaxKind; types?: NodeArray; } - interface TypeAliasDeclaration extends Declaration, ModuleElement { + interface TypeAliasDeclaration extends Declaration, Statement { name: Identifier; type: TypeNode; } @@ -684,7 +706,7 @@ declare module ts { name: DeclarationName; initializer?: Expression; } - interface EnumDeclaration extends Declaration, ModuleElement { + interface EnumDeclaration extends Declaration, Statement { name: Identifier; members: NodeArray; } @@ -739,6 +761,82 @@ declare module ts { hasTrailingNewLine?: boolean; kind: SyntaxKind; } + interface JSDocTypeExpression extends Node { + type: JSDocType; + } + interface JSDocType extends TypeNode { + _jsDocTypeBrand: any; + } + interface JSDocAllType extends JSDocType { + _JSDocAllTypeBrand: any; + } + interface JSDocUnknownType extends JSDocType { + _JSDocUnknownTypeBrand: any; + } + interface JSDocArrayType extends JSDocType { + elementType: JSDocType; + } + interface JSDocUnionType extends JSDocType { + types: NodeArray; + } + interface JSDocTupleType extends JSDocType { + types: NodeArray; + } + interface JSDocNonNullableType extends JSDocType { + type: JSDocType; + } + interface JSDocNullableType extends JSDocType { + type: JSDocType; + } + interface JSDocRecordType extends JSDocType, TypeLiteralNode { + members: NodeArray; + } + interface JSDocTypeReference extends JSDocType { + name: EntityName; + typeArguments: NodeArray; + } + interface JSDocOptionalType extends JSDocType { + type: JSDocType; + } + interface JSDocFunctionType extends JSDocType, SignatureDeclaration { + parameters: NodeArray; + type: JSDocType; + } + interface JSDocVariadicType extends JSDocType { + type: JSDocType; + } + interface JSDocConstructorType extends JSDocType { + type: JSDocType; + } + interface JSDocThisType extends JSDocType { + type: JSDocType; + } + interface JSDocRecordMember extends PropertyDeclaration { + name: Identifier | LiteralExpression; + type?: JSDocType; + } + interface JSDocComment extends Node { + tags: NodeArray; + } + interface JSDocTag extends Node { + atToken: Node; + tagName: Identifier; + } + interface JSDocTemplateTag extends JSDocTag { + typeParameters: NodeArray; + } + interface JSDocReturnTag extends JSDocTag { + typeExpression: JSDocTypeExpression; + } + interface JSDocTypeTag extends JSDocTag { + typeExpression: JSDocTypeExpression; + } + interface JSDocParameterTag extends JSDocTag { + preParameterName?: Identifier; + typeExpression?: JSDocTypeExpression; + postParameterName?: Identifier; + isBracketed: boolean; + } interface SourceFile extends Declaration { statements: NodeArray; endOfFileToken: Node; @@ -1011,6 +1109,8 @@ declare module ts { } interface InterfaceType extends ObjectType { typeParameters: TypeParameter[]; + outerTypeParameters: TypeParameter[]; + localTypeParameters: TypeParameter[]; } interface InterfaceTypeWithBaseTypes extends InterfaceType { baseTypes: ObjectType[]; @@ -1227,8 +1327,6 @@ declare module ts { function getTrailingCommentRanges(text: string, pos: number): CommentRange[]; function isIdentifierStart(ch: number, languageVersion: ScriptTarget): boolean; function isIdentifierPart(ch: number, languageVersion: ScriptTarget): boolean; - /** Creates a scanner over a (possibly unspecified) range of a piece of text. */ - function createScanner(languageVersion: ScriptTarget, skipTrivia: boolean, text?: string, onError?: ErrorCallback, start?: number, length?: number): Scanner; } declare module ts { function getDefaultLibFileName(options: CompilerOptions): string; @@ -1257,8 +1355,10 @@ declare module ts { * Vn. */ function collapseTextChangeRangesAcrossMultipleVersions(changes: TextChangeRange[]): TextChangeRange; + function getTypeParameterOwner(d: Declaration): Declaration; } declare module ts { + var throwOnJSDocErrors: boolean; function getNodeConstructor(kind: SyntaxKind): new () => Node; function createNode(kind: SyntaxKind): Node; function forEachChild(node: Node, cbNode: (node: Node) => T, cbNodeArray?: (nodes: Node[]) => T): T; diff --git a/bin/typescriptServices.js b/bin/typescriptServices.js index f91570ad95f..1b5c7949463 100644 --- a/bin/typescriptServices.js +++ b/bin/typescriptServices.js @@ -270,10 +270,35 @@ var ts; SyntaxKind[SyntaxKind["EnumMember"] = 227] = "EnumMember"; // Top-level nodes SyntaxKind[SyntaxKind["SourceFile"] = 228] = "SourceFile"; + // JSDoc nodes. + SyntaxKind[SyntaxKind["JSDocTypeExpression"] = 229] = "JSDocTypeExpression"; + // The * type. + SyntaxKind[SyntaxKind["JSDocAllType"] = 230] = "JSDocAllType"; + // The ? type. + SyntaxKind[SyntaxKind["JSDocUnknownType"] = 231] = "JSDocUnknownType"; + SyntaxKind[SyntaxKind["JSDocArrayType"] = 232] = "JSDocArrayType"; + SyntaxKind[SyntaxKind["JSDocUnionType"] = 233] = "JSDocUnionType"; + SyntaxKind[SyntaxKind["JSDocTupleType"] = 234] = "JSDocTupleType"; + SyntaxKind[SyntaxKind["JSDocNullableType"] = 235] = "JSDocNullableType"; + SyntaxKind[SyntaxKind["JSDocNonNullableType"] = 236] = "JSDocNonNullableType"; + SyntaxKind[SyntaxKind["JSDocRecordType"] = 237] = "JSDocRecordType"; + SyntaxKind[SyntaxKind["JSDocRecordMember"] = 238] = "JSDocRecordMember"; + SyntaxKind[SyntaxKind["JSDocTypeReference"] = 239] = "JSDocTypeReference"; + SyntaxKind[SyntaxKind["JSDocOptionalType"] = 240] = "JSDocOptionalType"; + SyntaxKind[SyntaxKind["JSDocFunctionType"] = 241] = "JSDocFunctionType"; + SyntaxKind[SyntaxKind["JSDocVariadicType"] = 242] = "JSDocVariadicType"; + SyntaxKind[SyntaxKind["JSDocConstructorType"] = 243] = "JSDocConstructorType"; + SyntaxKind[SyntaxKind["JSDocThisType"] = 244] = "JSDocThisType"; + SyntaxKind[SyntaxKind["JSDocComment"] = 245] = "JSDocComment"; + SyntaxKind[SyntaxKind["JSDocTag"] = 246] = "JSDocTag"; + SyntaxKind[SyntaxKind["JSDocParameterTag"] = 247] = "JSDocParameterTag"; + SyntaxKind[SyntaxKind["JSDocReturnTag"] = 248] = "JSDocReturnTag"; + SyntaxKind[SyntaxKind["JSDocTypeTag"] = 249] = "JSDocTypeTag"; + SyntaxKind[SyntaxKind["JSDocTemplateTag"] = 250] = "JSDocTemplateTag"; // Synthesized list - SyntaxKind[SyntaxKind["SyntaxList"] = 229] = "SyntaxList"; + SyntaxKind[SyntaxKind["SyntaxList"] = 251] = "SyntaxList"; // Enum value count - SyntaxKind[SyntaxKind["Count"] = 230] = "Count"; + SyntaxKind[SyntaxKind["Count"] = 252] = "Count"; // Markers SyntaxKind[SyntaxKind["FirstAssignment"] = 53] = "FirstAssignment"; SyntaxKind[SyntaxKind["LastAssignment"] = 64] = "LastAssignment"; @@ -323,6 +348,7 @@ var ts; var NodeFlags = ts.NodeFlags; /* @internal */ (function (ParserContextFlags) { + ParserContextFlags[ParserContextFlags["None"] = 0] = "None"; // Set if this node was parsed in strict mode. Used for grammar error checks, as well as // checking if the node can be reused in incremental settings. ParserContextFlags[ParserContextFlags["StrictMode"] = 1] = "StrictMode"; @@ -338,14 +364,17 @@ var ts; // the parser only sets this directly on the node it creates right after encountering the // error. ParserContextFlags[ParserContextFlags["ThisNodeHasError"] = 32] = "ThisNodeHasError"; + // This node was parsed in a JavaScript file and can be processed differently. For example + // its type can be specified usign a JSDoc comment. + ParserContextFlags[ParserContextFlags["JavaScriptFile"] = 64] = "JavaScriptFile"; // Context flags set directly by the parser. ParserContextFlags[ParserContextFlags["ParserGeneratedFlags"] = 63] = "ParserGeneratedFlags"; // Context flags computed by aggregating child flags upwards. // Used during incremental parsing to determine if this node or any of its children had an // error. Computed only once and then cached. - ParserContextFlags[ParserContextFlags["ThisNodeOrAnySubNodesHasError"] = 64] = "ThisNodeOrAnySubNodesHasError"; + ParserContextFlags[ParserContextFlags["ThisNodeOrAnySubNodesHasError"] = 128] = "ThisNodeOrAnySubNodesHasError"; // Used to know if we've computed data from children and cached it in this node. - ParserContextFlags[ParserContextFlags["HasAggregatedChildData"] = 128] = "HasAggregatedChildData"; + ParserContextFlags[ParserContextFlags["HasAggregatedChildData"] = 256] = "HasAggregatedChildData"; })(ts.ParserContextFlags || (ts.ParserContextFlags = {})); var ParserContextFlags = ts.ParserContextFlags; /* @internal */ @@ -826,6 +855,16 @@ var ts; } } ts.addRange = addRange; + function rangeEquals(array1, array2, pos, end) { + while (pos < end) { + if (array1[pos] !== array2[pos]) { + return false; + } + pos++; + } + return true; + } + ts.rangeEquals = rangeEquals; /** * Returns the last element of an array if non-empty, undefined otherwise. */ @@ -995,8 +1034,10 @@ var ts; var end = start + length; Debug.assert(start >= 0, "start must be non-negative, is " + start); Debug.assert(length >= 0, "length must be non-negative, is " + length); - Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); - Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + if (file) { + Debug.assert(start <= file.text.length, "start must be within the bounds of the file. " + start + " > " + file.text.length); + Debug.assert(end <= file.text.length, "end must be the bounds of the file. " + end + " > " + file.text.length); + } var text = getLocaleSpecificMessage(message.key); if (arguments.length > 4) { text = formatStringFromArgs(text, arguments, 4); @@ -1584,7 +1625,7 @@ var ts; for (var _i = 0; _i < files.length; _i++) { var current = files[_i]; var name = ts.combinePaths(path, current); - var stat = _fs.lstatSync(name); + var stat = _fs.statSync(name); if (stat.isFile()) { if (!extension || ts.fileExtensionIs(name, extension)) { result.push(name); @@ -1790,7 +1831,7 @@ var ts; Unterminated_template_literal: { code: 1160, category: ts.DiagnosticCategory.Error, key: "Unterminated template literal." }, Unterminated_regular_expression_literal: { code: 1161, category: ts.DiagnosticCategory.Error, key: "Unterminated regular expression literal." }, An_object_member_cannot_be_declared_optional: { code: 1162, category: ts.DiagnosticCategory.Error, key: "An object member cannot be declared optional." }, - yield_expression_must_be_contained_within_a_generator_declaration: { code: 1163, category: ts.DiagnosticCategory.Error, key: "'yield' expression must be contained_within a generator declaration." }, + A_yield_expression_is_only_allowed_in_a_generator_body: { code: 1163, category: ts.DiagnosticCategory.Error, key: "A 'yield' expression is only allowed in a generator body." }, Computed_property_names_are_not_allowed_in_enums: { code: 1164, category: ts.DiagnosticCategory.Error, key: "Computed property names are not allowed in enums." }, A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol: { code: 1165, category: ts.DiagnosticCategory.Error, key: "A computed property name in an ambient context must directly refer to a built-in symbol." }, A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol: { code: 1166, category: ts.DiagnosticCategory.Error, key: "A computed property name in a class property declaration must directly refer to a built-in symbol." }, @@ -1845,6 +1886,10 @@ var ts; Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: ts.DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." }, Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: ts.DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." }, Experimental_support_for_decorators_is_a_feature_that_is_subject_to_change_in_a_future_release_Specify_experimentalDecorators_to_remove_this_warning: { code: 1219, category: ts.DiagnosticCategory.Error, key: "Experimental support for decorators is a feature that is subject to change in a future release. Specify '--experimentalDecorators' to remove this warning." }, + Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1220, category: ts.DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." }, + Generators_are_not_allowed_in_an_ambient_context: { code: 1221, category: ts.DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." }, + An_overload_signature_cannot_be_declared_as_a_generator: { code: 1222, category: ts.DiagnosticCategory.Error, key: "An overload signature cannot be declared as a generator." }, + _0_tag_already_specified: { code: 1223, category: ts.DiagnosticCategory.Error, key: "'{0}' tag already specified." }, Duplicate_identifier_0: { code: 2300, category: ts.DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." }, Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: ts.DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." }, Static_members_cannot_reference_class_type_parameters: { code: 2302, category: ts.DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." }, @@ -2005,7 +2050,7 @@ var ts; The_0_operator_cannot_be_applied_to_type_symbol: { code: 2469, category: ts.DiagnosticCategory.Error, key: "The '{0}' operator cannot be applied to type 'symbol'." }, Symbol_reference_does_not_refer_to_the_global_Symbol_constructor_object: { code: 2470, category: ts.DiagnosticCategory.Error, key: "'Symbol' reference does not refer to the global Symbol constructor object." }, A_computed_property_name_of_the_form_0_must_be_of_type_symbol: { code: 2471, category: ts.DiagnosticCategory.Error, key: "A computed property name of the form '{0}' must be of type 'symbol'." }, - Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 6 and higher." }, + Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher: { code: 2472, category: ts.DiagnosticCategory.Error, key: "Spread operator in 'new' expressions is only available when targeting ECMAScript 5 and higher." }, Enum_declarations_must_all_be_const_or_non_const: { code: 2473, category: ts.DiagnosticCategory.Error, key: "Enum declarations must all be const or non-const." }, In_const_enum_declarations_member_initializer_must_be_constant_expression: { code: 2474, category: ts.DiagnosticCategory.Error, key: "In 'const' enum declarations member initializer must be constant expression." }, const_enums_can_only_be_used_in_property_or_index_access_expressions_or_the_right_hand_side_of_an_import_declaration_or_export_assignment: { code: 2475, category: ts.DiagnosticCategory.Error, key: "'const' enums can only be used in property or index access expressions or the right hand side of an import declaration or export assignment." }, @@ -2036,6 +2081,8 @@ var ts; A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: ts.DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." }, _0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: ts.DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." }, Cannot_find_namespace_0: { code: 2503, category: ts.DiagnosticCategory.Error, key: "Cannot find namespace '{0}'." }, + No_best_common_type_exists_among_yield_expressions: { code: 2504, category: ts.DiagnosticCategory.Error, key: "No best common type exists among yield expressions." }, + A_generator_cannot_have_a_void_type_annotation: { code: 2505, category: ts.DiagnosticCategory.Error, key: "A generator cannot have a 'void' type annotation." }, Import_declaration_0_is_using_private_name_1: { code: 4000, category: ts.DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." }, Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." }, Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: ts.DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." }, @@ -2196,6 +2243,7 @@ var ts; _0_implicitly_has_type_any_because_it_is_does_not_have_a_type_annotation_and_is_referenced_directly_or_indirectly_in_its_own_initializer: { code: 7022, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has type 'any' because it is does not have a type annotation and is referenced directly or indirectly in its own initializer." }, _0_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7023, category: ts.DiagnosticCategory.Error, key: "'{0}' implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, Function_implicitly_has_return_type_any_because_it_does_not_have_a_return_type_annotation_and_is_referenced_directly_or_indirectly_in_one_of_its_return_expressions: { code: 7024, category: ts.DiagnosticCategory.Error, key: "Function implicitly has return type 'any' because it does not have a return type annotation and is referenced directly or indirectly in one of its return expressions." }, + Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type: { code: 7025, category: ts.DiagnosticCategory.Error, key: "Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type." }, You_cannot_rename_this_element: { code: 8000, category: ts.DiagnosticCategory.Error, key: "You cannot rename this element." }, You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: ts.DiagnosticCategory.Error, key: "You cannot rename elements that are defined in the standard TypeScript library." }, import_can_only_be_used_in_a_ts_file: { code: 8002, category: ts.DiagnosticCategory.Error, key: "'import ... =' can only be used in a .ts file." }, @@ -2209,16 +2257,12 @@ var ts; types_can_only_be_used_in_a_ts_file: { code: 8010, category: ts.DiagnosticCategory.Error, key: "'types' can only be used in a .ts file." }, type_arguments_can_only_be_used_in_a_ts_file: { code: 8011, category: ts.DiagnosticCategory.Error, key: "'type arguments' can only be used in a .ts file." }, parameter_modifiers_can_only_be_used_in_a_ts_file: { code: 8012, category: ts.DiagnosticCategory.Error, key: "'parameter modifiers' can only be used in a .ts file." }, - can_only_be_used_in_a_ts_file: { code: 8013, category: ts.DiagnosticCategory.Error, key: "'?' can only be used in a .ts file." }, property_declarations_can_only_be_used_in_a_ts_file: { code: 8014, category: ts.DiagnosticCategory.Error, key: "'property declarations' can only be used in a .ts file." }, enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: ts.DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." }, type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: ts.DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." }, decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: ts.DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." }, - yield_expressions_are_not_currently_supported: { code: 9000, category: ts.DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." }, - Generators_are_not_currently_supported: { code: 9001, category: ts.DiagnosticCategory.Error, key: "Generators are not currently supported." }, Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: ts.DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." }, - class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." }, - class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: ts.DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." } + class_expressions_are_not_currently_supported: { code: 9003, category: ts.DiagnosticCategory.Error, key: "'class' expressions are not currently supported." } }; })(ts || (ts = {})); /// @@ -2754,12 +2798,17 @@ var ts; ch > 127 /* maxAsciiCharacter */ && isUnicodeIdentifierPart(ch, languageVersion); } ts.isIdentifierPart = isIdentifierPart; - /** Creates a scanner over a (possibly unspecified) range of a piece of text. */ + /* @internal */ + // Creates a scanner over a (possibly unspecified) range of a piece of text. function createScanner(languageVersion, skipTrivia, text, onError, start, length) { - var pos; // Current position (end position of text of current token) - var end; // end of text - var startPos; // Start position of whitespace before current token - var tokenPos; // Start position of text of current token + // Current position (end position of text of current token) + var pos; + // end of text + var end; + // Start position of whitespace before current token + var startPos; + // Start position of text of current token + var tokenPos; var token; var tokenValue; var precedingLineBreak; @@ -4071,17 +4120,17 @@ var ts; bindBlockScopedDeclaration(node, 32 /* Class */, 899583 /* ClassExcludes */); break; case 203 /* InterfaceDeclaration */: - bindDeclaration(node, 64 /* Interface */, 792992 /* InterfaceExcludes */, false); + bindBlockScopedDeclaration(node, 64 /* Interface */, 792992 /* InterfaceExcludes */); break; case 204 /* TypeAliasDeclaration */: - bindDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */, false); + bindBlockScopedDeclaration(node, 524288 /* TypeAlias */, 793056 /* TypeAliasExcludes */); break; case 205 /* EnumDeclaration */: if (ts.isConst(node)) { - bindDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */, false); + bindBlockScopedDeclaration(node, 128 /* ConstEnum */, 899967 /* ConstEnumExcludes */); } else { - bindDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */, false); + bindBlockScopedDeclaration(node, 256 /* RegularEnum */, 899327 /* RegularEnumExcludes */); } break; case 206 /* ModuleDeclaration */: @@ -4230,11 +4279,11 @@ var ts; // Returns true if this node contains a parse error anywhere underneath it. function containsParseError(node) { aggregateChildData(node); - return (node.parserContextFlags & 64 /* ThisNodeOrAnySubNodesHasError */) !== 0; + return (node.parserContextFlags & 128 /* ThisNodeOrAnySubNodesHasError */) !== 0; } ts.containsParseError = containsParseError; function aggregateChildData(node) { - if (!(node.parserContextFlags & 128 /* HasAggregatedChildData */)) { + if (!(node.parserContextFlags & 256 /* HasAggregatedChildData */)) { // A node is considered to contain a parse error if: // a) the parser explicitly marked that it had an error // b) any of it's children reported that it had an error. @@ -4242,12 +4291,12 @@ var ts; ts.forEachChild(node, containsParseError); // If so, mark ourselves accordingly. if (thisNodeOrAnySubNodesHasError) { - node.parserContextFlags |= 64 /* ThisNodeOrAnySubNodesHasError */; + node.parserContextFlags |= 128 /* ThisNodeOrAnySubNodesHasError */; } // Also mark that we've propogated the child information to this node. This way we can // always consult the bit directly on this node without needing to check its children // again. - node.parserContextFlags |= 128 /* HasAggregatedChildData */; + node.parserContextFlags |= 256 /* HasAggregatedChildData */; } } function getSourceFileOfNode(node) { @@ -4534,6 +4583,88 @@ var ts; } ts.getJsDocComments = getJsDocComments; ts.fullTripleSlashReferencePathRegEx = /^(\/\/\/\s*/; + function isTypeNode(node) { + if (142 /* FirstTypeNode */ <= node.kind && node.kind <= 150 /* LastTypeNode */) { + return true; + } + switch (node.kind) { + case 112 /* AnyKeyword */: + case 120 /* NumberKeyword */: + case 122 /* StringKeyword */: + case 113 /* BooleanKeyword */: + case 123 /* SymbolKeyword */: + return true; + case 99 /* VoidKeyword */: + return node.parent.kind !== 167 /* VoidExpression */; + case 8 /* StringLiteral */: + // Specialized signatures can have string literals as their parameters' type names + return node.parent.kind === 130 /* Parameter */; + case 177 /* ExpressionWithTypeArguments */: + return true; + // Identifiers and qualified names may be type nodes, depending on their context. Climb + // above them to find the lowest container + case 65 /* Identifier */: + // If the identifier is the RHS of a qualified name, then it's a type iff its parent is. + if (node.parent.kind === 127 /* QualifiedName */ && node.parent.right === node) { + node = node.parent; + } + else if (node.parent.kind === 156 /* PropertyAccessExpression */ && node.parent.name === node) { + node = node.parent; + } + // fall through + case 127 /* QualifiedName */: + case 156 /* PropertyAccessExpression */: + // At this point, node is either a qualified name or an identifier + ts.Debug.assert(node.kind === 65 /* Identifier */ || node.kind === 127 /* QualifiedName */ || node.kind === 156 /* PropertyAccessExpression */, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); + var parent_1 = node.parent; + if (parent_1.kind === 145 /* TypeQuery */) { + return false; + } + // Do not recursively call isTypeNode on the parent. In the example: + // + // let a: A.B.C; + // + // Calling isTypeNode would consider the qualified name A.B a type node. Only C or + // A.B.C is a type node. + if (142 /* FirstTypeNode */ <= parent_1.kind && parent_1.kind <= 150 /* LastTypeNode */) { + return true; + } + switch (parent_1.kind) { + case 177 /* ExpressionWithTypeArguments */: + return true; + case 129 /* TypeParameter */: + return node === parent_1.constraint; + case 133 /* PropertyDeclaration */: + case 132 /* PropertySignature */: + case 130 /* Parameter */: + case 199 /* VariableDeclaration */: + return node === parent_1.type; + case 201 /* FunctionDeclaration */: + case 163 /* FunctionExpression */: + case 164 /* ArrowFunction */: + case 136 /* Constructor */: + case 135 /* MethodDeclaration */: + case 134 /* MethodSignature */: + case 137 /* GetAccessor */: + case 138 /* SetAccessor */: + return node === parent_1.type; + case 139 /* CallSignature */: + case 140 /* ConstructSignature */: + case 141 /* IndexSignature */: + return node === parent_1.type; + case 161 /* TypeAssertionExpression */: + return node === parent_1.type; + case 158 /* CallExpression */: + case 159 /* NewExpression */: + return parent_1.typeArguments && ts.indexOf(parent_1.typeArguments, node) >= 0; + case 160 /* TaggedTemplateExpression */: + // TODO (drosen): TaggedTemplateExpressions may eventually support type arguments. + return false; + } + } + return false; + } + ts.isTypeNode = isTypeNode; // Warning: This has the same semantics as the forEach family of functions, // in that traversal terminates in the event that 'visitor' supplies a truthy value. function forEachReturnStatement(body, visitor) { @@ -4562,6 +4693,44 @@ var ts; } } ts.forEachReturnStatement = forEachReturnStatement; + function forEachYieldExpression(body, visitor) { + return traverse(body); + function traverse(node) { + switch (node.kind) { + case 173 /* YieldExpression */: + visitor(node); + var operand = node.expression; + if (operand) { + traverse(operand); + } + case 205 /* EnumDeclaration */: + case 203 /* InterfaceDeclaration */: + case 206 /* ModuleDeclaration */: + case 204 /* TypeAliasDeclaration */: + case 202 /* ClassDeclaration */: + // These are not allowed inside a generator now, but eventually they may be allowed + // as local types. Regardless, any yield statements contained within them should be + // skipped in this traversal. + return; + default: + if (isFunctionLike(node)) { + var name_3 = node.name; + if (name_3 && name_3.kind === 128 /* ComputedPropertyName */) { + // Note that we will not include methods/accessors of a class because they would require + // first descending into the class. This is by design. + traverse(name_3.expression); + return; + } + } + else if (!isTypeNode(node)) { + // This is the general case, which should include mostly expressions and statements. + // Also includes NodeArrays. + ts.forEachChild(node, traverse); + } + } + } + } + ts.forEachYieldExpression = forEachYieldExpression; function isVariableLike(node) { if (node) { switch (node.kind) { @@ -4590,6 +4759,12 @@ var ts; return false; } ts.isAccessor = isAccessor; + function isClassLike(node) { + if (node) { + return node.kind === 202 /* ClassDeclaration */ || node.kind === 175 /* ClassExpression */; + } + } + ts.isClassLike = isClassLike; function isFunctionLike(node) { if (node) { switch (node.kind) { @@ -4840,6 +5015,7 @@ var ts; case 172 /* TemplateExpression */: case 10 /* NoSubstitutionTemplateLiteral */: case 176 /* OmittedExpression */: + case 173 /* YieldExpression */: return true; case 127 /* QualifiedName */: while (node.parent.kind === 127 /* QualifiedName */) { @@ -4853,8 +5029,8 @@ var ts; // fall through case 7 /* NumericLiteral */: case 8 /* StringLiteral */: - var parent_1 = node.parent; - switch (parent_1.kind) { + var parent_2 = node.parent; + switch (parent_2.kind) { case 199 /* VariableDeclaration */: case 130 /* Parameter */: case 133 /* PropertyDeclaration */: @@ -4862,7 +5038,7 @@ var ts; case 227 /* EnumMember */: case 225 /* PropertyAssignment */: case 153 /* BindingElement */: - return parent_1.initializer === node; + return parent_2.initializer === node; case 183 /* ExpressionStatement */: case 184 /* IfStatement */: case 185 /* DoStatement */: @@ -4873,27 +5049,27 @@ var ts; case 221 /* CaseClause */: case 196 /* ThrowStatement */: case 194 /* SwitchStatement */: - return parent_1.expression === node; + return parent_2.expression === node; case 187 /* ForStatement */: - var forStatement = parent_1; + var forStatement = parent_2; return (forStatement.initializer === node && forStatement.initializer.kind !== 200 /* VariableDeclarationList */) || forStatement.condition === node || forStatement.incrementor === node; case 188 /* ForInStatement */: case 189 /* ForOfStatement */: - var forInStatement = parent_1; + var forInStatement = parent_2; return (forInStatement.initializer === node && forInStatement.initializer.kind !== 200 /* VariableDeclarationList */) || forInStatement.expression === node; case 161 /* TypeAssertionExpression */: - return node === parent_1.expression; + return node === parent_2.expression; case 178 /* TemplateSpan */: - return node === parent_1.expression; + return node === parent_2.expression; case 128 /* ComputedPropertyName */: - return node === parent_1.expression; + return node === parent_2.expression; case 131 /* Decorator */: return true; default: - if (isExpression(parent_1)) { + if (isExpression(parent_2)) { return true; } } @@ -4961,6 +5137,74 @@ var ts; return s.parameters.length > 0 && ts.lastOrUndefined(s.parameters).dotDotDotToken !== undefined; } ts.hasRestParameters = hasRestParameters; + function isJSDocConstructSignature(node) { + return node.kind === 241 /* JSDocFunctionType */ && + node.parameters.length > 0 && + node.parameters[0].type.kind === 243 /* JSDocConstructorType */; + } + ts.isJSDocConstructSignature = isJSDocConstructSignature; + function getJSDocTag(node, kind) { + if (node && node.jsDocComment) { + for (var _i = 0, _a = node.jsDocComment.tags; _i < _a.length; _i++) { + var tag = _a[_i]; + if (tag.kind === kind) { + return tag; + } + } + } + } + function getJSDocTypeTag(node) { + return getJSDocTag(node, 249 /* JSDocTypeTag */); + } + ts.getJSDocTypeTag = getJSDocTypeTag; + function getJSDocReturnTag(node) { + return getJSDocTag(node, 248 /* JSDocReturnTag */); + } + ts.getJSDocReturnTag = getJSDocReturnTag; + function getJSDocTemplateTag(node) { + return getJSDocTag(node, 250 /* JSDocTemplateTag */); + } + ts.getJSDocTemplateTag = getJSDocTemplateTag; + function getCorrespondingJSDocParameterTag(parameter) { + if (parameter.name && parameter.name.kind === 65 /* Identifier */) { + // If it's a parameter, see if the parent has a jsdoc comment with an @param + // annotation. + var parameterName = parameter.name.text; + var docComment = parameter.parent.jsDocComment; + if (docComment) { + return ts.forEach(docComment.tags, function (t) { + if (t.kind === 247 /* JSDocParameterTag */) { + var parameterTag = t; + var name_4 = parameterTag.preParameterName || parameterTag.postParameterName; + if (name_4.text === parameterName) { + return t; + } + } + }); + } + } + } + ts.getCorrespondingJSDocParameterTag = getCorrespondingJSDocParameterTag; + function hasRestParameter(s) { + return isRestParameter(ts.lastOrUndefined(s.parameters)); + } + ts.hasRestParameter = hasRestParameter; + function isRestParameter(node) { + if (node) { + if (node.parserContextFlags & 64 /* JavaScriptFile */) { + if (node.type && node.type.kind === 242 /* JSDocVariadicType */) { + return true; + } + var paramTag = getCorrespondingJSDocParameterTag(node); + if (paramTag && paramTag.typeExpression) { + return paramTag.typeExpression.type.kind === 242 /* JSDocVariadicType */; + } + } + return node.dotDotDotToken !== undefined; + } + return false; + } + ts.isRestParameter = isRestParameter; function isLiteralKind(kind) { return 7 /* FirstLiteralToken */ <= kind && kind <= 10 /* LastLiteralToken */; } @@ -5751,6 +5995,10 @@ var ts; return symbol && symbol.valueDeclaration && (symbol.valueDeclaration.flags & 256 /* Default */) ? symbol.valueDeclaration.localSymbol : undefined; } ts.getLocalSymbolForExportDefault = getLocalSymbolForExportDefault; + function isJavaScript(fileName) { + return ts.fileExtensionIs(fileName, ".js"); + } + ts.isJavaScript = isJavaScript; /** * Replace each instance of non-ascii characters by one, two, three, or four escape sequences * representing the UTF-8 encoding of the character, and return the expanded char code list. @@ -6024,12 +6272,23 @@ var ts; return createTextChangeRange(createTextSpanFromBounds(oldStartN, oldEndN), newEndN - oldStartN); } ts.collapseTextChangeRangesAcrossMultipleVersions = collapseTextChangeRangesAcrossMultipleVersions; + function getTypeParameterOwner(d) { + if (d && d.kind === 129 /* TypeParameter */) { + for (var current = d; current; current = current.parent) { + if (ts.isFunctionLike(current) || ts.isClassLike(current) || current.kind === 203 /* InterfaceDeclaration */) { + return current; + } + } + } + } + ts.getTypeParameterOwner = getTypeParameterOwner; })(ts || (ts = {})); /// /// var ts; (function (ts) { - var nodeConstructors = new Array(230 /* Count */); + ts.throwOnJSDocErrors = false; + var nodeConstructors = new Array(252 /* Count */); /* @internal */ ts.parseTime = 0; function getNodeConstructor(kind) { return nodeConstructors[kind] || (nodeConstructors[kind] = ts.objectAllocator.getNodeConstructor(kind)); @@ -6339,6 +6598,49 @@ var ts; return visitNode(cbNode, node.expression); case 219 /* MissingDeclaration */: return visitNodes(cbNodes, node.decorators); + case 229 /* JSDocTypeExpression */: + return visitNode(cbNode, node.type); + case 233 /* JSDocUnionType */: + return visitNodes(cbNodes, node.types); + case 234 /* JSDocTupleType */: + return visitNodes(cbNodes, node.types); + case 232 /* JSDocArrayType */: + return visitNode(cbNode, node.elementType); + case 236 /* JSDocNonNullableType */: + return visitNode(cbNode, node.type); + case 235 /* JSDocNullableType */: + return visitNode(cbNode, node.type); + case 237 /* JSDocRecordType */: + return visitNodes(cbNodes, node.members); + case 239 /* JSDocTypeReference */: + return visitNode(cbNode, node.name) || + visitNodes(cbNodes, node.typeArguments); + case 240 /* JSDocOptionalType */: + return visitNode(cbNode, node.type); + case 241 /* JSDocFunctionType */: + return visitNodes(cbNodes, node.parameters) || + visitNode(cbNode, node.type); + case 242 /* JSDocVariadicType */: + return visitNode(cbNode, node.type); + case 243 /* JSDocConstructorType */: + return visitNode(cbNode, node.type); + case 244 /* JSDocThisType */: + return visitNode(cbNode, node.type); + case 238 /* JSDocRecordMember */: + return visitNode(cbNode, node.name) || + visitNode(cbNode, node.type); + case 245 /* JSDocComment */: + return visitNodes(cbNodes, node.tags); + case 247 /* JSDocParameterTag */: + return visitNode(cbNode, node.preParameterName) || + visitNode(cbNode, node.typeExpression) || + visitNode(cbNode, node.postParameterName); + case 248 /* JSDocReturnTag */: + return visitNode(cbNode, node.typeExpression); + case 249 /* JSDocTypeTag */: + return visitNode(cbNode, node.typeExpression); + case 250 /* JSDocTemplateTag */: + return visitNodes(cbNodes, node.typeParameters); } } ts.forEachChild = forEachChild; @@ -6363,6 +6665,17 @@ var ts; return IncrementalParser.updateSourceFile(sourceFile, newText, textChangeRange, aggressiveChecks); } ts.updateSourceFile = updateSourceFile; + /* @internal */ + function parseIsolatedJSDocComment(content, start, length) { + return Parser.JSDocParser.parseIsolatedJSDocComment(content, start, length); + } + ts.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + /* @internal */ + // Exposed only for testing. + function parseJSDocTypeExpressionForTests(content, start, length) { + return Parser.JSDocParser.parseJSDocTypeExpressionForTests(content, start, length); + } + ts.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; // Implement the parser as a singleton module. We do this for perf reasons because creating // parser instances can actually be expensive enough to impact us on projects with many source // files. @@ -6373,6 +6686,7 @@ var ts; var scanner = ts.createScanner(2 /* Latest */, true); var disallowInAndDecoratorContext = 2 /* DisallowIn */ | 16 /* Decorator */; var sourceFile; + var parseDiagnostics; var syntaxCursor; var token; var sourceText; @@ -6426,7 +6740,7 @@ var ts; // Note: it should not be necessary to save/restore these flags during speculative/lookahead // parsing. These context flags are naturally stored and restored through normal recursive // descent parsing and unwinding. - var contextFlags = 0; + var contextFlags; // Whether or not we've had a parse error since creating the last AST node. If we have // encountered an error, it will be stored on the next AST node we create. Parse errors // can be broken down into three categories: @@ -6455,20 +6769,49 @@ var ts; // Note: any errors at the end of the file that do not precede a regular node, should get // attached to the EOF token. var parseErrorBeforeNextFinishedNode = false; + (function (StatementFlags) { + StatementFlags[StatementFlags["None"] = 0] = "None"; + StatementFlags[StatementFlags["Statement"] = 1] = "Statement"; + StatementFlags[StatementFlags["ModuleElement"] = 2] = "ModuleElement"; + StatementFlags[StatementFlags["StatementOrModuleElement"] = 3] = "StatementOrModuleElement"; + })(Parser.StatementFlags || (Parser.StatementFlags = {})); + var StatementFlags = Parser.StatementFlags; function parseSourceFile(fileName, _sourceText, languageVersion, _syntaxCursor, setParentNodes) { + initializeState(fileName, _sourceText, languageVersion, _syntaxCursor); + var result = parseSourceFileWorker(fileName, languageVersion, setParentNodes); + clearState(); + return result; + } + Parser.parseSourceFile = parseSourceFile; + function initializeState(fileName, _sourceText, languageVersion, _syntaxCursor) { sourceText = _sourceText; syntaxCursor = _syntaxCursor; + parseDiagnostics = []; parsingContext = 0; identifiers = {}; identifierCount = 0; nodeCount = 0; - contextFlags = 0; + contextFlags = ts.isJavaScript(fileName) ? 64 /* JavaScriptFile */ : 0 /* None */; parseErrorBeforeNextFinishedNode = false; - createSourceFile(fileName, languageVersion); // Initialize and prime the scanner before parsing the source elements. scanner.setText(sourceText); scanner.setOnError(scanError); scanner.setScriptTarget(languageVersion); + } + function clearState() { + // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. + scanner.setText(""); + scanner.setOnError(undefined); + // Clear any data. We don't want to accidently hold onto it for too long. + parseDiagnostics = undefined; + sourceFile = undefined; + identifiers = undefined; + syntaxCursor = undefined; + sourceText = undefined; + } + function parseSourceFileWorker(fileName, languageVersion, setParentNodes) { + sourceFile = createSourceFile(fileName, languageVersion); + // Prime the scanner. token = nextToken(); processReferenceComments(sourceFile); sourceFile.statements = parseList(0 /* SourceElements */, true, parseSourceElement); @@ -6478,22 +6821,45 @@ var ts; sourceFile.nodeCount = nodeCount; sourceFile.identifierCount = identifierCount; sourceFile.identifiers = identifiers; + sourceFile.parseDiagnostics = parseDiagnostics; if (setParentNodes) { fixupParentReferences(sourceFile); } - syntaxCursor = undefined; - // Clear out the text the scanner is pointing at, so it doesn't keep anything alive unnecessarily. - scanner.setText(""); - scanner.setOnError(undefined); - var result = sourceFile; - // Clear any data. We don't want to accidently hold onto it for too long. - sourceFile = undefined; - identifiers = undefined; - syntaxCursor = undefined; - sourceText = undefined; - return result; + // If this is a javascript file, proactively see if we can get JSDoc comments for + // relevant nodes in the file. We'll use these to provide typing informaion if they're + // available. + if (ts.isJavaScript(fileName)) { + addJSDocComments(); + } + return sourceFile; + } + function addJSDocComments() { + forEachChild(sourceFile, visit); + return; + function visit(node) { + // Add additional cases as necessary depending on how we see JSDoc comments used + // in the wild. + switch (node.kind) { + case 181 /* VariableStatement */: + case 201 /* FunctionDeclaration */: + case 130 /* Parameter */: + addJSDocComment(node); + } + forEachChild(node, visit); + } + } + function addJSDocComment(node) { + var comments = ts.getLeadingCommentRangesOfNode(node, sourceFile); + if (comments) { + for (var _i = 0; _i < comments.length; _i++) { + var comment = comments[_i]; + var jsDocComment = JSDocParser.parseJSDocComment(node, comment.pos, comment.end - comment.pos); + if (jsDocComment) { + node.jsDocComment = jsDocComment; + } + } + } } - Parser.parseSourceFile = parseSourceFile; function fixupParentReferences(sourceFile) { // normally parent references are set during binding. However, for clients that only need // a syntax tree, and no semantic features, then the binding process is an unnecessary @@ -6515,16 +6881,17 @@ var ts; } } } + Parser.fixupParentReferences = fixupParentReferences; function createSourceFile(fileName, languageVersion) { - sourceFile = createNode(228 /* SourceFile */, 0); + var sourceFile = createNode(228 /* SourceFile */, 0); sourceFile.pos = 0; sourceFile.end = sourceText.length; sourceFile.text = sourceText; - sourceFile.parseDiagnostics = []; sourceFile.bindDiagnostics = []; sourceFile.languageVersion = languageVersion; sourceFile.fileName = ts.normalizePath(fileName); sourceFile.flags = ts.fileExtensionIs(sourceFile.fileName, ".d.ts") ? 2048 /* DeclarationFile */ : 0; + return sourceFile; } function setContextFlag(val, flag) { if (val) { @@ -6632,9 +6999,9 @@ var ts; } function parseErrorAtPosition(start, length, message, arg0) { // Don't report another error if it would just be at the same position as the last error. - var lastError = ts.lastOrUndefined(sourceFile.parseDiagnostics); + var lastError = ts.lastOrUndefined(parseDiagnostics); if (!lastError || start !== lastError.start) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, start, length, message, arg0)); } // Mark that we've encountered an error. We'll set an appropriate bit on the next // node we finish so that it can't be reused incrementally. @@ -6669,7 +7036,7 @@ var ts; // Keep track of the state we'll need to rollback to if lookahead fails (or if the // caller asked us to always reset our state). var saveToken = token; - var saveParseDiagnosticsLength = sourceFile.parseDiagnostics.length; + var saveParseDiagnosticsLength = parseDiagnostics.length; var saveParseErrorBeforeNextFinishedNode = parseErrorBeforeNextFinishedNode; // Note: it is not actually necessary to save/restore the context flags here. That's // because the saving/restorating of these flags happens naturally through the recursive @@ -6687,7 +7054,7 @@ var ts; // then unconditionally restore us to where we were. if (!result || isLookAhead) { token = saveToken; - sourceFile.parseDiagnostics.length = saveParseDiagnosticsLength; + parseDiagnostics.length = saveParseDiagnosticsLength; parseErrorBeforeNextFinishedNode = saveParseErrorBeforeNextFinishedNode; } return result; @@ -6783,8 +7150,8 @@ var ts; node.end = pos; return node; } - function finishNode(node) { - node.end = scanner.getStartPos(); + function finishNode(node, end) { + node.end = end === undefined ? scanner.getStartPos() : end; if (contextFlags) { node.parserContextFlags = contextFlags; } @@ -6840,15 +7207,24 @@ var ts; token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */; } - function parsePropertyName() { + function parsePropertyNameWorker(allowComputedPropertyNames) { if (token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */) { return parseLiteralNode(true); } - if (token === 18 /* OpenBracketToken */) { + if (allowComputedPropertyNames && token === 18 /* OpenBracketToken */) { return parseComputedPropertyName(); } return parseIdentifierName(); } + function parsePropertyName() { + return parsePropertyNameWorker(true); + } + function parseSimplePropertyName() { + return parsePropertyNameWorker(false); + } + function isSimplePropertyName() { + return token === 8 /* StringLiteral */ || token === 7 /* NumericLiteral */ || isIdentifierOrKeyword(); + } function parseComputedPropertyName() { // PropertyName[Yield,GeneratorParameter] : // LiteralPropertyName @@ -6917,10 +7293,17 @@ var ts; switch (parsingContext) { case 0 /* SourceElements */: case 1 /* ModuleElements */: - return isSourceElement(inErrorRecovery); + // If we're in error recovery, then we don't want to treat ';' as an empty statement. + // The problem is that ';' can show up in far too many contexts, and if we see one + // and assume it's a statement, then we may bail out inappropriately from whatever + // we're parsing. For example, if we have a semicolon in the middle of a class, then + // we really don't want to assume the class is over and we're on a statement in the + // outer module. We just want to consume and move on. + return !(token === 22 /* SemicolonToken */ && inErrorRecovery) && isStartOfModuleElement(); case 2 /* BlockStatements */: case 4 /* SwitchClauseStatements */: - return isStartOfStatement(inErrorRecovery); + // During error recovery we don't treat empty statements as statements + return !(token === 22 /* SemicolonToken */ && inErrorRecovery) && isStartOfStatement(); case 3 /* SwitchClauses */: return token === 67 /* CaseKeyword */ || token === 73 /* DefaultKeyword */; case 5 /* TypeMembers */: @@ -6972,6 +7355,12 @@ var ts; return isHeritageClause(); case 20 /* ImportOrExportSpecifiers */: return isIdentifierOrKeyword(); + case 21 /* JSDocFunctionParameters */: + case 22 /* JSDocTypeArguments */: + case 24 /* JSDocTupleTypes */: + return JSDocParser.isJSDocType(); + case 23 /* JSDocRecordMembers */: + return isSimplePropertyName(); } ts.Debug.fail("Non-exhaustive case in 'isListElement'."); } @@ -7046,6 +7435,14 @@ var ts; return token === 25 /* GreaterThanToken */ || token === 16 /* OpenParenToken */; case 19 /* HeritageClauses */: return token === 14 /* OpenBraceToken */ || token === 15 /* CloseBraceToken */; + case 21 /* JSDocFunctionParameters */: + return token === 17 /* CloseParenToken */ || token === 51 /* ColonToken */ || token === 15 /* CloseBraceToken */; + case 22 /* JSDocTypeArguments */: + return token === 25 /* GreaterThanToken */ || token === 15 /* CloseBraceToken */; + case 24 /* JSDocTupleTypes */: + return token === 19 /* CloseBracketToken */ || token === 15 /* CloseBraceToken */; + case 23 /* JSDocRecordMembers */: + return token === 15 /* CloseBraceToken */; } } function isVariableDeclaratorListTerminator() { @@ -7071,7 +7468,7 @@ var ts; } // True if positioned at element or terminator of the current list or any enclosing list function isInSomeParsingContext() { - for (var kind = 0; kind < 21 /* Count */; kind++) { + for (var kind = 0; kind < 25 /* Count */; kind++) { if (parsingContext & (1 << kind)) { if (isListElement(kind, true) || isListTerminator(kind)) { return true; @@ -7094,7 +7491,7 @@ var ts; // test elements only if we are not already in strict mode if (checkForStrictMode && !inStrictModeContext()) { if (ts.isPrologueDirective(element)) { - if (isUseStrictPrologueDirective(sourceFile, element)) { + if (isUseStrictPrologueDirective(element)) { setStrictModeContext(true); checkForStrictMode = false; } @@ -7115,9 +7512,9 @@ var ts; return result; } /// Should be called only on prologue directives (isPrologueDirective(node) should be true) - function isUseStrictPrologueDirective(sourceFile, node) { + function isUseStrictPrologueDirective(node) { ts.Debug.assert(ts.isPrologueDirective(node)); - var nodeText = ts.getSourceTextOfNodeFromSourceFile(sourceFile, node.expression); + var nodeText = ts.getTextOfNodeFromSourceText(sourceText, node.expression); // Note: the node text must be exactly "use strict" or 'use strict'. It is not ok for the // string to contain unicode escapes (as per ES5). return nodeText === '"use strict"' || nodeText === "'use strict'"; @@ -7390,6 +7787,10 @@ var ts; case 18 /* TupleElementTypes */: return ts.Diagnostics.Type_expected; case 19 /* HeritageClauses */: return ts.Diagnostics.Unexpected_token_expected; case 20 /* ImportOrExportSpecifiers */: return ts.Diagnostics.Identifier_expected; + case 21 /* JSDocFunctionParameters */: return ts.Diagnostics.Parameter_declaration_expected; + case 22 /* JSDocTypeArguments */: return ts.Diagnostics.Type_argument_expected; + case 24 /* JSDocTupleTypes */: return ts.Diagnostics.Type_expected; + case 23 /* JSDocRecordMembers */: return ts.Diagnostics.Property_assignment_expected; } } ; @@ -8249,11 +8650,6 @@ var ts; nextToken(); return !scanner.hasPrecedingLineBreak() && isIdentifier(); } - function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { - nextToken(); - return !scanner.hasPrecedingLineBreak() && - (isIdentifier() || token === 14 /* OpenBraceToken */ || token === 18 /* OpenBracketToken */); - } function parseYieldExpression() { var node = createNode(173 /* YieldExpression */); // YieldExpression[In] : @@ -8425,10 +8821,11 @@ var ts; if (token === 14 /* OpenBraceToken */) { return parseFunctionBlock(false, false); } - if (isStartOfStatement(true) && - !isStartOfExpressionStatement() && + if (token !== 22 /* SemicolonToken */ && token !== 83 /* FunctionKeyword */ && - token !== 69 /* ClassKeyword */) { + token !== 69 /* ClassKeyword */ && + isStartOfStatement() && + !isStartOfExpressionStatement()) { // Check if we got a plain statement (i.e. no expression-statements, no function/class expressions/declarations) // // Here we try to recover from a potential error situation in the case where the @@ -9217,33 +9614,68 @@ var ts; return finishNode(expressionStatement); } } - function isStartOfStatement(inErrorRecovery) { - // Functions, variable statements and classes are allowed as a statement. But as per - // the grammar, they also allow modifiers. So we have to check for those statements - // that might be following modifiers.This ensures that things work properly when - // incrementally parsing as the parser will produce the same FunctionDeclaraiton, - // VariableStatement or ClassDeclaration, if it has the same text regardless of whether - // it is inside a block or not. - if (ts.isModifier(token)) { - var result = lookAhead(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return true; + function isIdentifierOrKeyword() { + return token >= 65 /* Identifier */; + } + function nextTokenIsIdentifierOrKeywordOnSameLine() { + nextToken(); + return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + } + function parseDeclarationFlags() { + while (true) { + switch (token) { + case 98 /* VarKeyword */: + case 104 /* LetKeyword */: + case 70 /* ConstKeyword */: + case 83 /* FunctionKeyword */: + case 69 /* ClassKeyword */: + case 77 /* EnumKeyword */: + return 1 /* Statement */; + case 103 /* InterfaceKeyword */: + case 124 /* TypeKeyword */: + nextToken(); + return isIdentifierOrKeyword() ? 1 /* Statement */ : 0 /* None */; + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + nextToken(); + return isIdentifierOrKeyword() || token === 8 /* StringLiteral */ ? 2 /* ModuleElement */ : 0 /* None */; + case 85 /* ImportKeyword */: + nextToken(); + return token === 8 /* StringLiteral */ || token === 35 /* AsteriskToken */ || + token === 14 /* OpenBraceToken */ || isIdentifierOrKeyword() ? + 2 /* ModuleElement */ : 0 /* None */; + case 78 /* ExportKeyword */: + nextToken(); + if (token === 53 /* EqualsToken */ || token === 35 /* AsteriskToken */ || + token === 14 /* OpenBraceToken */ || token === 73 /* DefaultKeyword */) { + return 2 /* ModuleElement */; + } + continue; + case 115 /* DeclareKeyword */: + case 108 /* PublicKeyword */: + case 106 /* PrivateKeyword */: + case 107 /* ProtectedKeyword */: + case 109 /* StaticKeyword */: + nextToken(); + continue; + default: + return 0 /* None */; } } + } + function getDeclarationFlags() { + return lookAhead(parseDeclarationFlags); + } + function getStatementFlags() { switch (token) { + case 52 /* AtToken */: case 22 /* SemicolonToken */: - // If we're in error recovery, then we don't want to treat ';' as an empty statement. - // The problem is that ';' can show up in far too many contexts, and if we see one - // and assume it's a statement, then we may bail out inappropriately from whatever - // we're parsing. For example, if we have a semicolon in the middle of a class, then - // we really don't want to assume the class is over and we're on a statement in the - // outer module. We just want to consume and move on. - return !inErrorRecovery; case 14 /* OpenBraceToken */: case 98 /* VarKeyword */: case 104 /* LetKeyword */: case 83 /* FunctionKeyword */: case 69 /* ClassKeyword */: + case 77 /* EnumKeyword */: case 84 /* IfKeyword */: case 75 /* DoKeyword */: case 100 /* WhileKeyword */: @@ -9260,58 +9692,72 @@ var ts; // however, we say they are here so that we may gracefully parse them and error later. case 68 /* CatchKeyword */: case 81 /* FinallyKeyword */: - return true; + return 1 /* Statement */; case 70 /* ConstKeyword */: - // const keyword can precede enum keyword when defining constant enums - // 'const enum' do not start statement. - // In ES 6 'enum' is a future reserved keyword, so it should not be used as identifier - var isConstEnum = lookAhead(nextTokenIsEnumKeyword); - return !isConstEnum; + case 78 /* ExportKeyword */: + case 85 /* ImportKeyword */: + return getDeclarationFlags(); + case 115 /* DeclareKeyword */: case 103 /* InterfaceKeyword */: case 117 /* ModuleKeyword */: case 118 /* NamespaceKeyword */: - case 77 /* EnumKeyword */: case 124 /* TypeKeyword */: - // When followed by an identifier, these do not start a statement but might - // instead be following declarations - if (isDeclarationStart()) { - return false; - } + // When these don't start a declaration, they're an identifier in an expression statement + return getDeclarationFlags() || 1 /* Statement */; case 108 /* PublicKeyword */: case 106 /* PrivateKeyword */: case 107 /* ProtectedKeyword */: case 109 /* StaticKeyword */: - // When followed by an identifier or keyword, these do not start a statement but - // might instead be following type members - if (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine)) { - return false; - } + // When these don't start a declaration, they may be the start of a class member if an identifier + // immediately follows. Otherwise they're an identifier in an expression statement. + return getDeclarationFlags() || + (lookAhead(nextTokenIsIdentifierOrKeywordOnSameLine) ? 0 /* None */ : 1 /* Statement */); default: - return isStartOfExpression(); + return isStartOfExpression() ? 1 /* Statement */ : 0 /* None */; } } - function nextTokenIsEnumKeyword() { - nextToken(); - return token === 77 /* EnumKeyword */; + function isStartOfStatement() { + return (getStatementFlags() & 1 /* Statement */) !== 0; } - function nextTokenIsIdentifierOrKeywordOnSameLine() { + function isStartOfModuleElement() { + return (getStatementFlags() & 3 /* StatementOrModuleElement */) !== 0; + } + function nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine() { nextToken(); - return isIdentifierOrKeyword() && !scanner.hasPrecedingLineBreak(); + return !scanner.hasPrecedingLineBreak() && + (isIdentifier() || token === 14 /* OpenBraceToken */ || token === 18 /* OpenBracketToken */); + } + function isLetDeclaration() { + // It is let declaration if in strict mode or next token is identifier\open bracket\open curly on same line. + // otherwise it needs to be treated like identifier + return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); } function parseStatement() { + return parseModuleElementOfKind(1 /* Statement */); + } + function parseModuleElement() { + return parseModuleElementOfKind(3 /* StatementOrModuleElement */); + } + function parseSourceElement() { + return parseModuleElementOfKind(3 /* StatementOrModuleElement */); + } + function parseModuleElementOfKind(flags) { switch (token) { + case 22 /* SemicolonToken */: + return parseEmptyStatement(); case 14 /* OpenBraceToken */: return parseBlock(false, false); case 98 /* VarKeyword */: - case 70 /* ConstKeyword */: - // const here should always be parsed as const declaration because of check in 'isStatement' return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 104 /* LetKeyword */: + if (isLetDeclaration()) { + return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + } + break; case 83 /* FunctionKeyword */: return parseFunctionDeclaration(scanner.getStartPos(), undefined, undefined); case 69 /* ClassKeyword */: return parseClassDeclaration(scanner.getStartPos(), undefined, undefined); - case 22 /* SemicolonToken */: - return parseEmptyStatement(); case 84 /* IfKeyword */: return parseIfStatement(); case 75 /* DoKeyword */: @@ -9339,54 +9785,68 @@ var ts; return parseTryStatement(); case 72 /* DebuggerKeyword */: return parseDebuggerStatement(); - case 104 /* LetKeyword */: - // If let follows identifier on the same line, it is declaration parse it as variable statement - if (isLetDeclaration()) { - return parseVariableStatement(scanner.getStartPos(), undefined, undefined); + case 52 /* AtToken */: + return parseDeclaration(); + case 70 /* ConstKeyword */: + case 115 /* DeclareKeyword */: + case 77 /* EnumKeyword */: + case 78 /* ExportKeyword */: + case 85 /* ImportKeyword */: + case 103 /* InterfaceKeyword */: + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + case 106 /* PrivateKeyword */: + case 107 /* ProtectedKeyword */: + case 108 /* PublicKeyword */: + case 109 /* StaticKeyword */: + case 124 /* TypeKeyword */: + if (getDeclarationFlags() & flags) { + return parseDeclaration(); } - // Else parse it like identifier - fall through - default: - // Functions and variable statements are allowed as a statement. But as per - // the grammar, they also allow modifiers. So we have to check for those - // statements that might be following modifiers. This ensures that things - // work properly when incrementally parsing as the parser will produce the - // same FunctionDeclaraiton or VariableStatement if it has the same text - // regardless of whether it is inside a block or not. - // Even though variable statements and function declarations cannot have decorators, - // we parse them here to provide better error recovery. - if (ts.isModifier(token) || token === 52 /* AtToken */) { - var result = tryParse(parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers); - if (result) { - return result; - } - } - return parseExpressionOrLabeledStatement(); + break; } + return parseExpressionOrLabeledStatement(); } - function parseVariableStatementOrFunctionDeclarationOrClassDeclarationWithDecoratorsOrModifiers() { - var start = scanner.getStartPos(); + function parseDeclaration() { + var fullStart = getNodePos(); var decorators = parseDecorators(); var modifiers = parseModifiers(); switch (token) { - case 70 /* ConstKeyword */: - var nextTokenIsEnum = lookAhead(nextTokenIsEnumKeyword); - if (nextTokenIsEnum) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); - case 104 /* LetKeyword */: - if (!isLetDeclaration()) { - return undefined; - } - return parseVariableStatement(start, decorators, modifiers); case 98 /* VarKeyword */: - return parseVariableStatement(start, decorators, modifiers); + case 104 /* LetKeyword */: + case 70 /* ConstKeyword */: + return parseVariableStatement(fullStart, decorators, modifiers); case 83 /* FunctionKeyword */: - return parseFunctionDeclaration(start, decorators, modifiers); + return parseFunctionDeclaration(fullStart, decorators, modifiers); case 69 /* ClassKeyword */: - return parseClassDeclaration(start, decorators, modifiers); + return parseClassDeclaration(fullStart, decorators, modifiers); + case 103 /* InterfaceKeyword */: + return parseInterfaceDeclaration(fullStart, decorators, modifiers); + case 124 /* TypeKeyword */: + return parseTypeAliasDeclaration(fullStart, decorators, modifiers); + case 77 /* EnumKeyword */: + return parseEnumDeclaration(fullStart, decorators, modifiers); + case 117 /* ModuleKeyword */: + case 118 /* NamespaceKeyword */: + return parseModuleDeclaration(fullStart, decorators, modifiers); + case 85 /* ImportKeyword */: + return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); + case 78 /* ExportKeyword */: + nextToken(); + return token === 73 /* DefaultKeyword */ || token === 53 /* EqualsToken */ ? + parseExportAssignment(fullStart, decorators, modifiers) : + parseExportDeclaration(fullStart, decorators, modifiers); + default: + if (decorators) { + // We reached this point because we encountered decorators and/or modifiers and assumed a declaration + // would follow. For recovery and error reporting purposes, return an incomplete declaration. + var node = createMissingNode(219 /* MissingDeclaration */, true, ts.Diagnostics.Declaration_expected); + node.pos = fullStart; + node.decorators = decorators; + setModifiers(node, modifiers); + return finishNode(node); + } } - return undefined; } function parseFunctionBlockOrSemicolon(isGenerator, diagnosticMessage) { if (token !== 14 /* OpenBraceToken */ && canParseSemicolon()) { @@ -9541,7 +10001,18 @@ var ts; property.name = name; property.questionToken = questionToken; property.type = parseTypeAnnotation(); - property.initializer = allowInAnd(parseNonParameterInitializer); + // For instance properties specifically, since they are evaluated inside the constructor, + // we do *not * want to parse yield expressions, so we specifically turn the yield context + // off. The grammar would look something like this: + // + // MemberVariableDeclaration[Yield]: + // AccessibilityModifier_opt PropertyName TypeAnnotation_opt Initialiser_opt[In]; + // AccessibilityModifier_opt static_opt PropertyName TypeAnnotation_opt Initialiser_opt[In, ?Yield]; + // + // The checker may still error in the static case to explicitly disallow the yield expression. + property.initializer = modifiers && modifiers.flags & 128 /* Static */ + ? allowInAnd(parseNonParameterInitializer) + : doOutsideOfContext(4 /* Yield */ | 2 /* DisallowIn */, parseNonParameterInitializer); parseSemicolon(); return finishNode(property); } @@ -9711,17 +10182,17 @@ var ts; } if (decorators) { // treat this as a property declaration with a missing name. - var name_3 = createMissingNode(65 /* Identifier */, true, ts.Diagnostics.Declaration_expected); - return parsePropertyDeclaration(fullStart, decorators, modifiers, name_3, undefined); + var name_5 = createMissingNode(65 /* Identifier */, true, ts.Diagnostics.Declaration_expected); + return parsePropertyDeclaration(fullStart, decorators, modifiers, name_5, undefined); } // 'isClassMemberStart' should have hinted not to attempt parsing. ts.Debug.fail("Should not have attempted to parse class member declaration."); } function parseClassExpression() { return parseClassDeclarationOrExpression( - /*fullStart:*/ scanner.getStartPos(), - /*decorators:*/ undefined, - /*modifiers:*/ undefined, 175 /* ClassExpression */); + /*fullStart*/ scanner.getStartPos(), + /*decorators*/ undefined, + /*modifiers*/ undefined, 175 /* ClassExpression */); } function parseClassDeclaration(fullStart, decorators, modifiers) { return parseClassDeclarationOrExpression(fullStart, decorators, modifiers, 202 /* ClassDeclaration */); @@ -10063,136 +10534,6 @@ var ts; parseSemicolon(); return finishNode(node); } - function isLetDeclaration() { - // It is let declaration if in strict mode or next token is identifier\open bracket\open curly on same line. - // otherwise it needs to be treated like identifier - return inStrictModeContext() || lookAhead(nextTokenIsIdentifierOrStartOfDestructuringOnTheSameLine); - } - function isDeclarationStart(followsModifier) { - switch (token) { - case 98 /* VarKeyword */: - case 70 /* ConstKeyword */: - case 83 /* FunctionKeyword */: - return true; - case 104 /* LetKeyword */: - return isLetDeclaration(); - case 69 /* ClassKeyword */: - case 103 /* InterfaceKeyword */: - case 77 /* EnumKeyword */: - case 124 /* TypeKeyword */: - // Not true keywords so ensure an identifier follows - return lookAhead(nextTokenIsIdentifierOrKeyword); - case 85 /* ImportKeyword */: - // Not true keywords so ensure an identifier follows or is string literal or asterisk or open brace - return lookAhead(nextTokenCanFollowImportKeyword); - case 117 /* ModuleKeyword */: - case 118 /* NamespaceKeyword */: - // Not a true keyword so ensure an identifier or string literal follows - return lookAhead(nextTokenIsIdentifierOrKeywordOrStringLiteral); - case 78 /* ExportKeyword */: - // Check for export assignment or modifier on source element - return lookAhead(nextTokenCanFollowExportKeyword); - case 115 /* DeclareKeyword */: - case 108 /* PublicKeyword */: - case 106 /* PrivateKeyword */: - case 107 /* ProtectedKeyword */: - case 109 /* StaticKeyword */: - // Check for modifier on source element - return lookAhead(nextTokenIsDeclarationStart); - case 52 /* AtToken */: - // a lookahead here is too costly, and decorators are only valid on a declaration. - // We will assume we are parsing a declaration here and report an error later - return !followsModifier; - } - } - function isIdentifierOrKeyword() { - return token >= 65 /* Identifier */; - } - function nextTokenIsIdentifierOrKeyword() { - nextToken(); - return isIdentifierOrKeyword(); - } - function nextTokenIsIdentifierOrKeywordOrStringLiteral() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 /* StringLiteral */; - } - function nextTokenCanFollowImportKeyword() { - nextToken(); - return isIdentifierOrKeyword() || token === 8 /* StringLiteral */ || - token === 35 /* AsteriskToken */ || token === 14 /* OpenBraceToken */; - } - function nextTokenCanFollowExportKeyword() { - nextToken(); - return token === 53 /* EqualsToken */ || token === 35 /* AsteriskToken */ || - token === 14 /* OpenBraceToken */ || token === 73 /* DefaultKeyword */ || isDeclarationStart(true); - } - function nextTokenIsDeclarationStart() { - nextToken(); - return isDeclarationStart(true); - } - function nextTokenIsAsKeyword() { - return nextToken() === 111 /* AsKeyword */; - } - function parseDeclaration() { - var fullStart = getNodePos(); - var decorators = parseDecorators(); - var modifiers = parseModifiers(); - if (token === 78 /* ExportKeyword */) { - nextToken(); - if (token === 73 /* DefaultKeyword */ || token === 53 /* EqualsToken */) { - return parseExportAssignment(fullStart, decorators, modifiers); - } - if (token === 35 /* AsteriskToken */ || token === 14 /* OpenBraceToken */) { - return parseExportDeclaration(fullStart, decorators, modifiers); - } - } - switch (token) { - case 98 /* VarKeyword */: - case 104 /* LetKeyword */: - case 70 /* ConstKeyword */: - return parseVariableStatement(fullStart, decorators, modifiers); - case 83 /* FunctionKeyword */: - return parseFunctionDeclaration(fullStart, decorators, modifiers); - case 69 /* ClassKeyword */: - return parseClassDeclaration(fullStart, decorators, modifiers); - case 103 /* InterfaceKeyword */: - return parseInterfaceDeclaration(fullStart, decorators, modifiers); - case 124 /* TypeKeyword */: - return parseTypeAliasDeclaration(fullStart, decorators, modifiers); - case 77 /* EnumKeyword */: - return parseEnumDeclaration(fullStart, decorators, modifiers); - case 117 /* ModuleKeyword */: - case 118 /* NamespaceKeyword */: - return parseModuleDeclaration(fullStart, decorators, modifiers); - case 85 /* ImportKeyword */: - return parseImportDeclarationOrImportEqualsDeclaration(fullStart, decorators, modifiers); - default: - if (decorators) { - // We reached this point because we encountered an AtToken and assumed a declaration would - // follow. For recovery and error reporting purposes, return an incomplete declaration. - var node = createMissingNode(219 /* MissingDeclaration */, true, ts.Diagnostics.Declaration_expected); - node.pos = fullStart; - node.decorators = decorators; - setModifiers(node, modifiers); - return finishNode(node); - } - ts.Debug.fail("Mismatch between isDeclarationStart and parseDeclaration"); - } - } - function isSourceElement(inErrorRecovery) { - return isDeclarationStart() || isStartOfStatement(inErrorRecovery); - } - function parseSourceElement() { - return parseSourceElementOrModuleElement(); - } - function parseModuleElement() { - return parseSourceElementOrModuleElement(); - } - function parseSourceElementOrModuleElement() { - return isDeclarationStart() - ? parseDeclaration() - : parseStatement(); - } function processReferenceComments(sourceFile) { var triviaScanner = ts.createScanner(sourceFile.languageVersion, false, sourceText); var referencedFiles = []; @@ -10220,7 +10561,7 @@ var ts; referencedFiles.push(fileReference); } if (diagnosticMessage) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, diagnosticMessage)); } } else { @@ -10228,7 +10569,7 @@ var ts; var amdModuleNameMatchResult = amdModuleNameRegEx.exec(comment); if (amdModuleNameMatchResult) { if (amdModuleName) { - sourceFile.parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); + parseDiagnostics.push(ts.createFileDiagnostic(sourceFile, range.pos, range.end - range.pos, ts.Diagnostics.An_AMD_module_cannot_have_multiple_name_assignments)); } amdModuleName = amdModuleNameMatchResult[2]; } @@ -10284,7 +10625,11 @@ var ts; ParsingContext[ParsingContext["TupleElementTypes"] = 18] = "TupleElementTypes"; ParsingContext[ParsingContext["HeritageClauses"] = 19] = "HeritageClauses"; ParsingContext[ParsingContext["ImportOrExportSpecifiers"] = 20] = "ImportOrExportSpecifiers"; - ParsingContext[ParsingContext["Count"] = 21] = "Count"; // Number of parsing contexts + ParsingContext[ParsingContext["JSDocFunctionParameters"] = 21] = "JSDocFunctionParameters"; + ParsingContext[ParsingContext["JSDocTypeArguments"] = 22] = "JSDocTypeArguments"; + ParsingContext[ParsingContext["JSDocRecordMembers"] = 23] = "JSDocRecordMembers"; + ParsingContext[ParsingContext["JSDocTupleTypes"] = 24] = "JSDocTupleTypes"; + ParsingContext[ParsingContext["Count"] = 25] = "Count"; // Number of parsing contexts })(ParsingContext || (ParsingContext = {})); var Tristate; (function (Tristate) { @@ -10292,6 +10637,552 @@ var ts; Tristate[Tristate["True"] = 1] = "True"; Tristate[Tristate["Unknown"] = 2] = "Unknown"; })(Tristate || (Tristate = {})); + var JSDocParser; + (function (JSDocParser) { + function isJSDocType() { + switch (token) { + case 35 /* AsteriskToken */: + case 50 /* QuestionToken */: + case 16 /* OpenParenToken */: + case 18 /* OpenBracketToken */: + case 46 /* ExclamationToken */: + case 14 /* OpenBraceToken */: + case 83 /* FunctionKeyword */: + case 21 /* DotDotDotToken */: + case 88 /* NewKeyword */: + case 93 /* ThisKeyword */: + return true; + } + return isIdentifierOrKeyword(); + } + JSDocParser.isJSDocType = isJSDocType; + function parseJSDocTypeExpressionForTests(content, start, length) { + initializeState("file.js", content, 2 /* Latest */, undefined); + var jsDocTypeExpression = parseJSDocTypeExpression(start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocTypeExpression ? { jsDocTypeExpression: jsDocTypeExpression, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseJSDocTypeExpressionForTests = parseJSDocTypeExpressionForTests; + // Parses out a JSDoc type expression. The starting position should be right at the open + // curly in the type expression. Returns 'undefined' if it encounters any errors while parsing. + /* @internal */ + function parseJSDocTypeExpression(start, length) { + scanner.setText(sourceText, start, length); + // Prime the first token for us to start processing. + token = nextToken(); + var result = createNode(229 /* JSDocTypeExpression */); + parseExpected(14 /* OpenBraceToken */); + result.type = parseJSDocTopLevelType(); + parseExpected(15 /* CloseBraceToken */); + fixupParentReferences(result); + return finishNode(result); + } + JSDocParser.parseJSDocTypeExpression = parseJSDocTypeExpression; + function setError(message) { + parseErrorAtCurrentToken(message); + if (ts.throwOnJSDocErrors) { + throw new Error(message.key); + } + } + function parseJSDocTopLevelType() { + var type = parseJSDocType(); + if (token === 44 /* BarToken */) { + var unionType = createNode(233 /* JSDocUnionType */, type.pos); + unionType.types = parseJSDocTypeList(type); + type = finishNode(unionType); + } + if (token === 53 /* EqualsToken */) { + var optionalType = createNode(240 /* JSDocOptionalType */, type.pos); + nextToken(); + optionalType.type = type; + type = finishNode(optionalType); + } + return type; + } + function parseJSDocType() { + var type = parseBasicTypeExpression(); + while (true) { + if (token === 18 /* OpenBracketToken */) { + var arrayType = createNode(232 /* JSDocArrayType */, type.pos); + arrayType.elementType = type; + nextToken(); + parseExpected(19 /* CloseBracketToken */); + type = finishNode(arrayType); + } + else if (token === 50 /* QuestionToken */) { + var nullableType = createNode(235 /* JSDocNullableType */, type.pos); + nullableType.type = type; + nextToken(); + type = finishNode(nullableType); + } + else if (token === 46 /* ExclamationToken */) { + var nonNullableType = createNode(236 /* JSDocNonNullableType */, type.pos); + nonNullableType.type = type; + nextToken(); + type = finishNode(nonNullableType); + } + else { + break; + } + } + return type; + } + function parseBasicTypeExpression() { + switch (token) { + case 35 /* AsteriskToken */: + return parseJSDocAllType(); + case 50 /* QuestionToken */: + return parseJSDocUnknownOrNullableType(); + case 16 /* OpenParenToken */: + return parseJSDocUnionType(); + case 18 /* OpenBracketToken */: + return parseJSDocTupleType(); + case 46 /* ExclamationToken */: + return parseJSDocNonNullableType(); + case 14 /* OpenBraceToken */: + return parseJSDocRecordType(); + case 83 /* FunctionKeyword */: + return parseJSDocFunctionType(); + case 21 /* DotDotDotToken */: + return parseJSDocVariadicType(); + case 88 /* NewKeyword */: + return parseJSDocConstructorType(); + case 93 /* ThisKeyword */: + return parseJSDocThisType(); + case 112 /* AnyKeyword */: + case 122 /* StringKeyword */: + case 120 /* NumberKeyword */: + case 113 /* BooleanKeyword */: + case 123 /* SymbolKeyword */: + case 99 /* VoidKeyword */: + return parseTokenNode(); + } + return parseJSDocTypeReference(); + } + function parseJSDocThisType() { + var result = createNode(244 /* JSDocThisType */); + nextToken(); + parseExpected(51 /* ColonToken */); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocConstructorType() { + var result = createNode(243 /* JSDocConstructorType */); + nextToken(); + parseExpected(51 /* ColonToken */); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocVariadicType() { + var result = createNode(242 /* JSDocVariadicType */); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocFunctionType() { + var result = createNode(241 /* JSDocFunctionType */); + nextToken(); + parseExpected(16 /* OpenParenToken */); + result.parameters = parseDelimitedList(21 /* JSDocFunctionParameters */, parseJSDocParameter); + checkForTrailingComma(result.parameters); + parseExpected(17 /* CloseParenToken */); + if (token === 51 /* ColonToken */) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocParameter() { + var parameter = createNode(130 /* Parameter */); + parameter.type = parseJSDocType(); + return finishNode(parameter); + } + function parseJSDocOptionalType(type) { + var result = createNode(240 /* JSDocOptionalType */, type.pos); + nextToken(); + result.type = type; + return finishNode(result); + } + function parseJSDocTypeReference() { + var result = createNode(239 /* JSDocTypeReference */); + result.name = parseSimplePropertyName(); + while (parseOptional(20 /* DotToken */)) { + if (token === 24 /* LessThanToken */) { + result.typeArguments = parseTypeArguments(); + break; + } + else { + result.name = parseQualifiedName(result.name); + } + } + return finishNode(result); + } + function parseTypeArguments() { + // Move past the < + nextToken(); + var typeArguments = parseDelimitedList(22 /* JSDocTypeArguments */, parseJSDocType); + checkForTrailingComma(typeArguments); + checkForEmptyTypeArgumentList(typeArguments); + parseExpected(25 /* GreaterThanToken */); + return typeArguments; + } + function checkForEmptyTypeArgumentList(typeArguments) { + if (parseDiagnostics.length === 0 && typeArguments && typeArguments.length === 0) { + var start = typeArguments.pos - "<".length; + var end = ts.skipTrivia(sourceText, typeArguments.end) + ">".length; + return parseErrorAtPosition(start, end - start, ts.Diagnostics.Type_argument_list_cannot_be_empty); + } + } + function parseQualifiedName(left) { + var result = createNode(127 /* QualifiedName */, left.pos); + result.left = left; + result.right = parseIdentifierName(); + return finishNode(result); + } + function parseJSDocRecordType() { + var result = createNode(237 /* JSDocRecordType */); + nextToken(); + result.members = parseDelimitedList(23 /* JSDocRecordMembers */, parseJSDocRecordMember); + checkForTrailingComma(result.members); + parseExpected(15 /* CloseBraceToken */); + return finishNode(result); + } + function parseJSDocRecordMember() { + var result = createNode(238 /* JSDocRecordMember */); + result.name = parseSimplePropertyName(); + if (token === 51 /* ColonToken */) { + nextToken(); + result.type = parseJSDocType(); + } + return finishNode(result); + } + function parseJSDocNonNullableType() { + var result = createNode(236 /* JSDocNonNullableType */); + nextToken(); + result.type = parseJSDocType(); + return finishNode(result); + } + function parseJSDocTupleType() { + var result = createNode(234 /* JSDocTupleType */); + nextToken(); + result.types = parseDelimitedList(24 /* JSDocTupleTypes */, parseJSDocType); + checkForTrailingComma(result.types); + parseExpected(19 /* CloseBracketToken */); + return finishNode(result); + } + function checkForTrailingComma(list) { + if (parseDiagnostics.length === 0 && list.hasTrailingComma) { + var start = list.end - ",".length; + parseErrorAtPosition(start, ",".length, ts.Diagnostics.Trailing_comma_not_allowed); + } + } + function parseJSDocUnionType() { + var result = createNode(233 /* JSDocUnionType */); + nextToken(); + result.types = parseJSDocTypeList(parseJSDocType()); + parseExpected(17 /* CloseParenToken */); + return finishNode(result); + } + function parseJSDocTypeList(firstType) { + ts.Debug.assert(!!firstType); + var types = []; + types.pos = firstType.pos; + types.push(firstType); + while (parseOptional(44 /* BarToken */)) { + types.push(parseJSDocType()); + } + types.end = scanner.getStartPos(); + return types; + } + function parseJSDocAllType() { + var result = createNode(230 /* JSDocAllType */); + nextToken(); + return finishNode(result); + } + function parseJSDocUnknownOrNullableType() { + var pos = scanner.getStartPos(); + // skip the ? + nextToken(); + // Need to lookahead to decide if this is a nullable or unknown type. + // Here are cases where we'll pick the unknown type: + // + // Foo(?, + // { a: ? } + // Foo(?) + // Foo + // Foo(?= + // (?| + if (token === 23 /* CommaToken */ || + token === 15 /* CloseBraceToken */ || + token === 17 /* CloseParenToken */ || + token === 25 /* GreaterThanToken */ || + token === 53 /* EqualsToken */ || + token === 44 /* BarToken */) { + var result = createNode(231 /* JSDocUnknownType */, pos); + return finishNode(result); + } + else { + var result = createNode(235 /* JSDocNullableType */, pos); + result.type = parseJSDocType(); + return finishNode(result); + } + } + function parseIsolatedJSDocComment(content, start, length) { + initializeState("file.js", content, 2 /* Latest */, undefined); + var jsDocComment = parseJSDocComment(undefined, start, length); + var diagnostics = parseDiagnostics; + clearState(); + return jsDocComment ? { jsDocComment: jsDocComment, diagnostics: diagnostics } : undefined; + } + JSDocParser.parseIsolatedJSDocComment = parseIsolatedJSDocComment; + function parseJSDocComment(parent, start, length) { + var comment = parseJSDocCommentWorker(start, length); + if (comment) { + fixupParentReferences(comment); + comment.parent = parent; + } + return comment; + } + JSDocParser.parseJSDocComment = parseJSDocComment; + function parseJSDocCommentWorker(start, length) { + var content = sourceText; + start = start || 0; + var end = length === undefined ? content.length : start + length; + length = end - start; + ts.Debug.assert(start >= 0); + ts.Debug.assert(start <= end); + ts.Debug.assert(end <= content.length); + var tags; + var pos; + // NOTE(cyrusn): This is essentially a handwritten scanner for JSDocComments. I + // considered using an actual Scanner, but this would complicate things. The + // scanner would need to know it was in a Doc Comment. Otherwise, it would then + // produce comments *inside* the doc comment. In the end it was just easier to + // write a simple scanner rather than go that route. + if (length >= "/** */".length) { + if (content.charCodeAt(start) === 47 /* slash */ && + content.charCodeAt(start + 1) === 42 /* asterisk */ && + content.charCodeAt(start + 2) === 42 /* asterisk */ && + content.charCodeAt(start + 3) !== 42 /* asterisk */) { + // Initially we can parse out a tag. We also have seen a starting asterisk. + // This is so that /** * @type */ doesn't parse. + var canParseTag = true; + var seenAsterisk = true; + for (pos = start + "/**".length; pos < end;) { + var ch = content.charCodeAt(pos); + pos++; + if (ch === 64 /* at */ && canParseTag) { + parseTag(); + // Once we parse out a tag, we cannot keep parsing out tags on this line. + canParseTag = false; + continue; + } + if (ts.isLineBreak(ch)) { + // After a line break, we can parse a tag, and we haven't seen as asterisk + // on the next line yet. + canParseTag = true; + seenAsterisk = false; + continue; + } + if (ts.isWhiteSpace(ch)) { + // Whitespace doesn't affect any of our parsing. + continue; + } + // Ignore the first asterisk on a line. + if (ch === 42 /* asterisk */) { + if (seenAsterisk) { + // If we've already seen an asterisk, then we can no longer parse a tag + // on this line. + canParseTag = false; + } + seenAsterisk = true; + continue; + } + // Anything else is doc comment text. We can't do anything with it. Because it + // wasn't a tag, we can no longer parse a tag on this line until we hit the next + // line break. + canParseTag = false; + } + } + } + return createJSDocComment(); + function createJSDocComment() { + if (!tags) { + return undefined; + } + var result = createNode(245 /* JSDocComment */, start); + result.tags = tags; + return finishNode(result, end); + } + function skipWhitespace() { + while (pos < end && ts.isWhiteSpace(content.charCodeAt(pos))) { + pos++; + } + } + function parseTag() { + ts.Debug.assert(content.charCodeAt(pos - 1) === 64 /* at */); + var atToken = createNode(52 /* AtToken */, pos - 1); + atToken.end = pos; + var startPos = pos; + var tagName = scanIdentifier(); + if (!tagName) { + return; + } + var tag = handleTag(atToken, tagName) || handleUnknownTag(atToken, tagName); + addTag(tag); + } + function handleTag(atToken, tagName) { + if (tagName) { + switch (tagName.text) { + case "param": + return handleParamTag(atToken, tagName); + case "return": + case "returns": + return handleReturnTag(atToken, tagName); + case "template": + return handleTemplateTag(atToken, tagName); + case "type": + return handleTypeTag(atToken, tagName); + } + } + return undefined; + } + function handleUnknownTag(atToken, tagName) { + var result = createNode(246 /* JSDocTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + return finishNode(result, pos); + } + function addTag(tag) { + if (tag) { + if (!tags) { + tags = []; + tags.pos = tag.pos; + } + tags.push(tag); + tags.end = tag.end; + } + } + function tryParseTypeExpression() { + skipWhitespace(); + if (content.charCodeAt(pos) !== 123 /* openBrace */) { + return undefined; + } + var typeExpression = parseJSDocTypeExpression(pos, end - pos); + pos = typeExpression.end; + return typeExpression; + } + function handleParamTag(atToken, tagName) { + var typeExpression = tryParseTypeExpression(); + skipWhitespace(); + var name; + var isBracketed; + if (content.charCodeAt(pos) === 91 /* openBracket */) { + pos++; + skipWhitespace(); + name = scanIdentifier(); + isBracketed = true; + } + else { + name = scanIdentifier(); + } + if (!name) { + parseErrorAtPosition(pos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var preName, postName; + if (typeExpression) { + postName = name; + } + else { + preName = name; + } + if (!typeExpression) { + typeExpression = tryParseTypeExpression(); + } + var result = createNode(247 /* JSDocParameterTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.preParameterName = preName; + result.typeExpression = typeExpression; + result.postParameterName = postName; + result.isBracketed = isBracketed; + return finishNode(result, pos); + } + function handleReturnTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 248 /* JSDocReturnTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(248 /* JSDocReturnTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTypeTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 249 /* JSDocTypeTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var result = createNode(249 /* JSDocTypeTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeExpression = tryParseTypeExpression(); + return finishNode(result, pos); + } + function handleTemplateTag(atToken, tagName) { + if (ts.forEach(tags, function (t) { return t.kind === 250 /* JSDocTemplateTag */; })) { + parseErrorAtPosition(tagName.pos, pos - tagName.pos, ts.Diagnostics._0_tag_already_specified, tagName.text); + } + var typeParameters = []; + typeParameters.pos = pos; + while (true) { + skipWhitespace(); + var startPos = pos; + var name_6 = scanIdentifier(); + if (!name_6) { + parseErrorAtPosition(startPos, 0, ts.Diagnostics.Identifier_expected); + return undefined; + } + var typeParameter = createNode(129 /* TypeParameter */, name_6.pos); + typeParameter.name = name_6; + finishNode(typeParameter, pos); + typeParameters.push(typeParameter); + skipWhitespace(); + if (content.charCodeAt(pos) !== 44 /* comma */) { + break; + } + pos++; + } + typeParameters.end = pos; + var result = createNode(250 /* JSDocTemplateTag */, atToken.pos); + result.atToken = atToken; + result.tagName = tagName; + result.typeParameters = typeParameters; + return finishNode(result, pos); + } + function scanIdentifier() { + var startPos = pos; + for (; pos < end; pos++) { + var ch = content.charCodeAt(pos); + if (pos === startPos && ts.isIdentifierStart(ch, 2 /* Latest */)) { + continue; + } + else if (pos > startPos && ts.isIdentifierPart(ch, 2 /* Latest */)) { + continue; + } + break; + } + if (startPos === pos) { + return undefined; + } + var result = createNode(65 /* Identifier */, startPos); + result.text = content.substring(startPos, pos); + return finishNode(result, pos); + } + } + JSDocParser.parseJSDocCommentWorker = parseJSDocCommentWorker; + })(JSDocParser = Parser.JSDocParser || (Parser.JSDocParser = {})); })(Parser || (Parser = {})); var IncrementalParser; (function (IncrementalParser) { @@ -10379,7 +11270,12 @@ var ts; } // Ditch any existing LS children we may have created. This way we can avoid // moving them forward. - node._children = undefined; + if (node._children) { + node._children = undefined; + } + if (node.jsDocComment) { + node.jsDocComment = undefined; + } node.pos += delta; node.end += delta; if (aggressiveChecks && shouldCheckNode(node)) { @@ -10839,13 +11735,15 @@ var ts; var undefinedType = createIntrinsicType(32 /* Undefined */ | 262144 /* ContainsUndefinedOrNull */, "undefined"); var nullType = createIntrinsicType(64 /* Null */ | 262144 /* ContainsUndefinedOrNull */, "null"); var unknownType = createIntrinsicType(1 /* Any */, "unknown"); + var circularType = createIntrinsicType(1 /* Any */, "__circular__"); var emptyObjectType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + var emptyGenericType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); + emptyGenericType.instantiations = {}; var anyFunctionType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); var noConstraintType = 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 = {}; - var globalArraySymbol; var globalESSymbolConstructorSymbol; var globalObjectType; var globalFunctionType; @@ -10857,6 +11755,8 @@ var ts; var globalTemplateStringsArrayType; var globalESSymbolType; var globalIterableType; + var globalIteratorType; + var globalIterableIteratorType; var anyArrayType; var getGlobalClassDecoratorType; var getGlobalParameterDecoratorType; @@ -11083,7 +11983,15 @@ var ts; // Locals of a source file are not in scope (because they get merged into the global symbol table) if (location.locals && !isGlobalSourceFile(location)) { if (result = getSymbol(location.locals, name, meaning)) { - break loop; + // Type parameters of a function are in scope in the entire function declaration, including the parameter + // list and return type. However, local types are only in scope in the function body. + if (!(meaning & 793056 /* Type */) || + !(result.flags & (793056 /* Type */ & ~262144 /* TypeParameter */)) || + !ts.isFunctionLike(location) || + lastLocation === location.body) { + break loop; + } + result = undefined; } } switch (location.kind) { @@ -11385,15 +12293,15 @@ var ts; var moduleSymbol = resolveExternalModuleName(node, node.moduleSpecifier); var targetSymbol = resolveESModuleSymbol(moduleSymbol, node.moduleSpecifier); if (targetSymbol) { - var name_4 = specifier.propertyName || specifier.name; - if (name_4.text) { - var symbolFromModule = getExportOfModule(targetSymbol, name_4.text); - var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_4.text); + var name_7 = specifier.propertyName || specifier.name; + if (name_7.text) { + var symbolFromModule = getExportOfModule(targetSymbol, name_7.text); + var symbolFromVariable = getPropertyOfVariable(targetSymbol, name_7.text); var symbol = symbolFromModule && symbolFromVariable ? combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) : symbolFromModule || symbolFromVariable; if (!symbol) { - error(name_4, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_4)); + error(name_7, ts.Diagnostics.Module_0_has_no_exported_member_1, getFullyQualifiedName(moduleSymbol), ts.declarationNameToString(name_7)); } return symbol; } @@ -12150,17 +13058,54 @@ var ts; writeType(types[i], union ? 64 /* InElementType */ : 0 /* None */); } } + function writeSymbolTypeReference(symbol, typeArguments, pos, end) { + // Unnamed function expressions, arrow functions, and unnamed class expressions have reserved names that + // we don't want to display + if (!isReservedMemberName(symbol.name)) { + buildSymbolDisplay(symbol, writer, enclosingDeclaration, 793056 /* Type */); + } + if (pos < end) { + writePunctuation(writer, 24 /* LessThanToken */); + writeType(typeArguments[pos++], 0 /* None */); + while (pos < end) { + writePunctuation(writer, 23 /* CommaToken */); + writeSpace(writer); + writeType(typeArguments[pos++], 0 /* None */); + } + writePunctuation(writer, 25 /* GreaterThanToken */); + } + } function writeTypeReference(type, flags) { + var typeArguments = type.typeArguments; if (type.target === globalArrayType && !(flags & 1 /* WriteArrayAsGenericType */)) { - writeType(type.typeArguments[0], 64 /* InElementType */); + writeType(typeArguments[0], 64 /* InElementType */); writePunctuation(writer, 18 /* OpenBracketToken */); writePunctuation(writer, 19 /* CloseBracketToken */); } else { - buildSymbolDisplay(type.target.symbol, writer, enclosingDeclaration, 793056 /* Type */); - writePunctuation(writer, 24 /* LessThanToken */); - writeTypeList(type.typeArguments, false); - writePunctuation(writer, 25 /* GreaterThanToken */); + // Write the type reference in the format f.g.C where A and B are type arguments + // for outer type parameters, and f and g are the respective declaring containers of those + // type parameters. + var outerTypeParameters = type.target.outerTypeParameters; + var i = 0; + if (outerTypeParameters) { + var length_1 = outerTypeParameters.length; + while (i < length_1) { + // Find group of type arguments for type parameters with the same declaring container. + var start = i; + var parent_3 = getParentSymbolOfTypeParameter(outerTypeParameters[i]); + do { + i++; + } while (i < length_1 && getParentSymbolOfTypeParameter(outerTypeParameters[i]) === parent_3); + // When type parameters are their own type arguments for the whole group (i.e. we have + // the default outer type arguments), we don't show the group. + if (!ts.rangeEquals(outerTypeParameters, typeArguments, start, i)) { + writeSymbolTypeReference(parent_3, typeArguments, start, i); + writePunctuation(writer, 20 /* DotToken */); + } + } + } + writeSymbolTypeReference(type.symbol, typeArguments, i, typeArguments.length); } } function writeTupleType(type) { @@ -12348,7 +13293,7 @@ var ts; function buildTypeParameterDisplayFromSymbol(symbol, writer, enclosingDeclaraiton, flags) { var targetSymbol = getTargetSymbol(symbol); if (targetSymbol.flags & 32 /* Class */ || targetSymbol.flags & 64 /* Interface */) { - buildDisplayForTypeParametersAndDelimiters(getTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); + buildDisplayForTypeParametersAndDelimiters(getLocalTypeParametersOfClassOrInterface(symbol), writer, enclosingDeclaraiton, flags); } } function buildTypeParameterDisplay(tp, writer, enclosingDeclaration, flags, typeStack) { @@ -12521,14 +13466,14 @@ var ts; case 201 /* FunctionDeclaration */: case 205 /* EnumDeclaration */: case 209 /* ImportEqualsDeclaration */: - var parent_2 = getDeclarationContainer(node); + var parent_4 = getDeclarationContainer(node); // If the node is not exported or it is not ambient module element (except import declaration) if (!(ts.getCombinedNodeFlags(node) & 1 /* Export */) && - !(node.kind !== 209 /* ImportEqualsDeclaration */ && parent_2.kind !== 228 /* SourceFile */ && ts.isInAmbientContext(parent_2))) { - return isGlobalSourceFile(parent_2); + !(node.kind !== 209 /* ImportEqualsDeclaration */ && parent_4.kind !== 228 /* SourceFile */ && ts.isInAmbientContext(parent_4))) { + return isGlobalSourceFile(parent_4); } // Exported members/ambient module elements (exception import declaration) are visible if parent is visible - return isDeclarationVisible(parent_2); + return isDeclarationVisible(parent_4); case 133 /* PropertyDeclaration */: case 132 /* PropertySignature */: case 137 /* GetAccessor */: @@ -12679,14 +13624,14 @@ var ts; var type; if (pattern.kind === 151 /* ObjectBindingPattern */) { // Use explicitly specified property name ({ p: xxx } form), or otherwise the implied name ({ p } form) - var name_5 = declaration.propertyName || declaration.name; + var name_8 = declaration.propertyName || declaration.name; // Use type of the specified property, or otherwise, for a numeric name, the type of the numeric index signature, // or otherwise the type of the string index signature. - type = getTypeOfPropertyOfType(parentType, name_5.text) || - isNumericLiteralName(name_5.text) && getIndexTypeOfType(parentType, 1 /* Number */) || + type = getTypeOfPropertyOfType(parentType, name_8.text) || + isNumericLiteralName(name_8.text) && getIndexTypeOfType(parentType, 1 /* Number */) || getIndexTypeOfType(parentType, 0 /* String */); if (!type) { - error(name_5, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_5)); + error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(parentType), ts.declarationNameToString(name_8)); return unknownType; } } @@ -12731,7 +13676,7 @@ var ts; // checkRightHandSideOfForOf will return undefined if the for-of expression type was // missing properties/signatures required to get its iteratedType (like // [Symbol.iterator] or next). This may be because we accessed properties from anyType, - // or it may have led to an error inside getIteratedType. + // or it may have led to an error inside getElementTypeOfIterable. return checkRightHandSideOfForOf(declaration.parent.parent.expression) || anyType; } if (ts.isBindingPattern(declaration.parent)) { @@ -13024,29 +13969,65 @@ var ts; return target === checkBase || ts.forEach(getBaseTypes(target), check); } } - // Return combined list of type parameters from all declarations of a class or interface. Elsewhere we check they're all - // the same, but even if they're not we still need the complete list to ensure instantiations supply type arguments - // for all type parameters. - function getTypeParametersOfClassOrInterface(symbol) { - var result; - ts.forEach(symbol.declarations, function (node) { - if (node.kind === 203 /* InterfaceDeclaration */ || node.kind === 202 /* ClassDeclaration */) { - var declaration = node; - if (declaration.typeParameters && declaration.typeParameters.length) { - ts.forEach(declaration.typeParameters, function (node) { - var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(node)); - if (!result) { - result = [tp]; - } - else if (!ts.contains(result, tp)) { - result.push(tp); - } - }); + // Appends the type parameters given by a list of declarations to a set of type parameters and returns the resulting set. + // The function allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set + // in-place and returns the same array. + function appendTypeParameters(typeParameters, declarations) { + for (var _i = 0; _i < declarations.length; _i++) { + var declaration = declarations[_i]; + var tp = getDeclaredTypeOfTypeParameter(getSymbolOfNode(declaration)); + if (!typeParameters) { + typeParameters = [tp]; + } + else if (!ts.contains(typeParameters, tp)) { + typeParameters.push(tp); + } + } + return typeParameters; + } + // Appends the outer type parameters of a node to a set of type parameters and returns the resulting set. The function + // allocates a new array if the input type parameter set is undefined, but otherwise it modifies the set in-place and + // returns the same array. + function appendOuterTypeParameters(typeParameters, node) { + while (true) { + node = node.parent; + if (!node) { + return typeParameters; + } + if (node.kind === 202 /* ClassDeclaration */ || node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */ || node.kind === 135 /* MethodDeclaration */ || + node.kind === 164 /* ArrowFunction */) { + var declarations = node.typeParameters; + if (declarations) { + return appendTypeParameters(appendOuterTypeParameters(typeParameters, node), declarations); } } - }); + } + } + // The outer type parameters are those defined by enclosing generic classes, methods, or functions. + function getOuterTypeParametersOfClassOrInterface(symbol) { + var kind = symbol.flags & 32 /* Class */ ? 202 /* ClassDeclaration */ : 203 /* InterfaceDeclaration */; + return appendOuterTypeParameters(undefined, ts.getDeclarationOfKind(symbol, kind)); + } + // The local type parameters are the combined set of type parameters from all declarations of the class or interface. + function getLocalTypeParametersOfClassOrInterface(symbol) { + var result; + for (var _i = 0, _a = symbol.declarations; _i < _a.length; _i++) { + var node = _a[_i]; + if (node.kind === 203 /* InterfaceDeclaration */ || node.kind === 202 /* ClassDeclaration */) { + var declaration = node; + if (declaration.typeParameters) { + result = appendTypeParameters(result, declaration.typeParameters); + } + } + } return result; } + // The full set of type parameters for a generic class or interface type consists of its outer type parameters plus + // its locally declared type parameters. + function getTypeParametersOfClassOrInterface(symbol) { + return ts.concatenate(getOuterTypeParametersOfClassOrInterface(symbol), getLocalTypeParametersOfClassOrInterface(symbol)); + } function getBaseTypes(type) { var typeWithBaseTypes = type; if (!typeWithBaseTypes.baseTypes) { @@ -13113,10 +14094,13 @@ var ts; if (!links.declaredType) { var kind = symbol.flags & 32 /* Class */ ? 1024 /* Class */ : 2048 /* Interface */; var type = links.declaredType = createObjectType(kind, symbol); - var typeParameters = getTypeParametersOfClassOrInterface(symbol); - if (typeParameters) { + var outerTypeParameters = getOuterTypeParametersOfClassOrInterface(symbol); + var localTypeParameters = getLocalTypeParametersOfClassOrInterface(symbol); + if (outerTypeParameters || localTypeParameters) { type.flags |= 4096 /* Reference */; - type.typeParameters = typeParameters; + type.typeParameters = ts.concatenate(outerTypeParameters, localTypeParameters); + type.outerTypeParameters = outerTypeParameters; + type.localTypeParameters = localTypeParameters; type.instantiations = {}; type.instantiations[getTypeListId(type.typeParameters)] = type; type.target = type; @@ -13294,12 +14278,12 @@ var ts; return ts.map(baseSignatures, function (baseSignature) { var signature = baseType.flags & 4096 /* Reference */ ? getSignatureInstantiation(baseSignature, baseType.typeArguments) : cloneSignature(baseSignature); - signature.typeParameters = classType.typeParameters; + signature.typeParameters = classType.localTypeParameters; signature.resolvedReturnType = classType; return signature; }); } - return [createSignature(undefined, classType.typeParameters, emptyArray, classType, 0, false, false)]; + return [createSignature(undefined, classType.localTypeParameters, emptyArray, classType, 0, false, false)]; } function createTupleTypeMemberSymbols(memberTypes) { var members = {}; @@ -13627,7 +14611,7 @@ var ts; var links = getNodeLinks(declaration); if (!links.resolvedSignature) { var classType = declaration.kind === 136 /* Constructor */ ? getDeclaredTypeOfClassOrInterface(declaration.parent.symbol) : undefined; - var typeParameters = classType ? classType.typeParameters : + var typeParameters = classType ? classType.localTypeParameters : declaration.typeParameters ? getTypeParametersFromDeclaration(declaration.typeParameters) : undefined; var parameters = []; var hasStringLiterals = false; @@ -13814,6 +14798,9 @@ var ts; } return type.constraint === noConstraintType ? undefined : type.constraint; } + function getParentSymbolOfTypeParameter(typeParameter) { + return getSymbolOfNode(ts.getDeclarationOfKind(typeParameter.symbol, 129 /* TypeParameter */).parent); + } function getTypeListId(types) { switch (types.length) { case 1: @@ -13919,12 +14906,21 @@ var ts; else { type = getDeclaredTypeOfSymbol(symbol); if (type.flags & (1024 /* Class */ | 2048 /* Interface */) && type.flags & 4096 /* Reference */) { - var typeParameters = type.typeParameters; - if (node.typeArguments && node.typeArguments.length === typeParameters.length) { - type = createTypeReference(type, ts.map(node.typeArguments, getTypeFromTypeNode)); + // In a type reference, the outer type parameters of the referenced class or interface are automatically + // supplied as type arguments and the type reference only specifies arguments for the local type parameters + // of the class or interface. + var localTypeParameters = type.localTypeParameters; + var expectedTypeArgCount = localTypeParameters ? localTypeParameters.length : 0; + var typeArgCount = node.typeArguments ? node.typeArguments.length : 0; + if (typeArgCount === expectedTypeArgCount) { + // When no type arguments are expected we already have the right type because all outer type parameters + // have themselves as default type arguments. + if (typeArgCount) { + type = createTypeReference(type, ts.concatenate(type.outerTypeParameters, ts.map(node.typeArguments, getTypeFromTypeNode))); + } } else { - error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1 /* WriteArrayAsGenericType */), typeParameters.length); + error(node, ts.Diagnostics.Generic_type_0_requires_1_type_argument_s, typeToString(type, undefined, 1 /* WriteArrayAsGenericType */), expectedTypeArgCount); type = undefined; } } @@ -13966,16 +14962,16 @@ var ts; } } if (!symbol) { - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } var type = getDeclaredTypeOfSymbol(symbol); if (!(type.flags & 48128 /* ObjectType */)) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_be_a_class_or_interface_type, symbol.name); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } if ((type.typeParameters ? type.typeParameters.length : 0) !== arity) { error(getTypeDeclaration(symbol), ts.Diagnostics.Global_type_0_must_have_1_type_parameter_s, symbol.name, arity); - return emptyObjectType; + return arity ? emptyGenericType : emptyObjectType; } return type; } @@ -13995,15 +14991,20 @@ var ts; function getGlobalESSymbolConstructorSymbol() { return globalESSymbolConstructorSymbol || (globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol")); } + /** + * Instantiates a global type that is generic with some element type, and returns that instantiation. + */ + function createTypeFromGenericGlobalType(genericGlobalType, elementType) { + return genericGlobalType !== emptyGenericType ? createTypeReference(genericGlobalType, [elementType]) : emptyObjectType; + } function createIterableType(elementType) { - return globalIterableType !== emptyObjectType ? createTypeReference(globalIterableType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalIterableType, elementType); + } + function createIterableIteratorType(elementType) { + return createTypeFromGenericGlobalType(globalIterableIteratorType, elementType); } function createArrayType(elementType) { - // globalArrayType will be undefined if we get here during creation of the Array type. This for example happens if - // user code augments the Array type with call or construct signatures that have an array type as the return type. - // We instead use globalArraySymbol to obtain the (not yet fully constructed) Array type. - var arrayType = globalArrayType || getDeclaredTypeOfSymbol(globalArraySymbol); - return arrayType !== emptyObjectType ? createTypeReference(arrayType, [elementType]) : emptyObjectType; + return createTypeFromGenericGlobalType(globalArrayType, elementType); } function getTypeFromArrayTypeNode(node) { var links = getNodeLinks(node); @@ -14058,17 +15059,7 @@ var ts; } return false; } - // Since removeSubtypes checks the subtype relation, and the subtype relation on a union - // may attempt to reduce a union, it is possible that removeSubtypes could be called - // recursively on the same set of types. The removeSubtypesStack is used to track which - // sets of types are currently undergoing subtype reduction. - var removeSubtypesStack = []; function removeSubtypes(types) { - var typeListId = getTypeListId(types); - if (removeSubtypesStack.lastIndexOf(typeListId) >= 0) { - return; - } - removeSubtypesStack.push(typeListId); var i = types.length; while (i > 0) { i--; @@ -14076,7 +15067,6 @@ var ts; types.splice(i, 1); } } - removeSubtypesStack.pop(); } function containsAnyType(types) { for (var _i = 0; _i < types.length; _i++) { @@ -14128,10 +15118,20 @@ var ts; } return type; } + // Subtype reduction is basically an optimization we do to avoid excessively large union types, which take longer + // to process and look strange in quick info and error messages. Semantically there is no difference between the + // reduced type and the type itself. So, when we detect a circularity we simply say that the reduced type is the + // type itself. function getReducedTypeOfUnionType(type) { - // If union type was created without subtype reduction, perform the deferred reduction now if (!type.reducedType) { - type.reducedType = getUnionType(type.types, false); + type.reducedType = circularType; + var reducedType = getUnionType(type.types, false); + if (type.reducedType === circularType) { + type.reducedType = reducedType; + } + } + else if (type.reducedType === circularType) { + type.reducedType = type; } return type.reducedType; } @@ -14323,6 +15323,18 @@ var ts; return result; } function instantiateAnonymousType(type, mapper) { + // If this type has already been instantiated using this mapper, returned the cached result. This guards against + // infinite instantiations of cyclic types, e.g. "var x: { a: T, b: typeof x };" + if (mapper.mappings) { + var cached = mapper.mappings[type.id]; + if (cached) { + return cached; + } + } + else { + mapper.mappings = {}; + } + // Instantiate the given type using the given mapper and cache the result var result = createObjectType(32768 /* Anonymous */, type.symbol); result.properties = instantiateList(getPropertiesOfObjectType(type), mapper, instantiateSymbol); result.members = createSymbolTable(result.properties); @@ -14334,6 +15346,7 @@ var ts; result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); + mapper.mappings[type.id] = result; return result; } function instantiateType(type, mapper) { @@ -14342,7 +15355,7 @@ var ts; return mapper(type); } if (type.flags & 32768 /* Anonymous */) { - return type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) ? + return type.symbol && type.symbol.flags & (16 /* Function */ | 8192 /* Method */ | 32 /* Class */ | 2048 /* TypeLiteral */ | 4096 /* ObjectLiteral */) ? instantiateAnonymousType(type, mapper) : type; } if (type.flags & 4096 /* Reference */) { @@ -15617,10 +16630,10 @@ var ts; // Resolve location from top down towards node if it is a context sensitive expression // That helps in making sure not assigning types as any when resolved out of order var containerNodes = []; - for (var parent_3 = node.parent; parent_3; parent_3 = parent_3.parent) { - if ((ts.isExpression(parent_3) || ts.isObjectLiteralMethod(node)) && - isContextSensitive(parent_3)) { - containerNodes.unshift(parent_3); + for (var parent_5 = node.parent; parent_5; parent_5 = parent_5.parent) { + if ((ts.isExpression(parent_5) || ts.isObjectLiteralMethod(node)) && + isContextSensitive(parent_5)) { + containerNodes.unshift(parent_5); } } ts.forEach(containerNodes, function (node) { getTypeOfNode(node); }); @@ -16105,22 +17118,40 @@ var ts; return undefined; } function getContextualTypeForReturnExpression(node) { + var func = ts.getContainingFunction(node); + if (func && !func.asteriskToken) { + return getContextualReturnType(func); + } + return undefined; + } + function getContextualTypeForYieldOperand(node) { var func = ts.getContainingFunction(node); if (func) { - // If the containing function has a return type annotation, is a constructor, or is a get accessor whose - // corresponding set accessor has a type annotation, return statements in the function are contextually typed - if (func.type || func.kind === 136 /* Constructor */ || func.kind === 137 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(func.symbol, 138 /* SetAccessor */))) { - return getReturnTypeOfSignature(getSignatureFromDeclaration(func)); - } - // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature - // and that call signature is non-generic, return statements are contextually typed by the return type of the signature - var signature = getContextualSignatureForFunctionLikeDeclaration(func); - if (signature) { - return getReturnTypeOfSignature(signature); + var contextualReturnType = getContextualReturnType(func); + if (contextualReturnType) { + return node.asteriskToken + ? contextualReturnType + : getElementTypeOfIterableIterator(contextualReturnType); } } return undefined; } + function getContextualReturnType(functionDecl) { + // If the containing function has a return type annotation, is a constructor, or is a get accessor whose + // corresponding set accessor has a type annotation, return statements in the function are contextually typed + if (functionDecl.type || + functionDecl.kind === 136 /* Constructor */ || + functionDecl.kind === 137 /* GetAccessor */ && getSetAccessorTypeAnnotationNode(ts.getDeclarationOfKind(functionDecl.symbol, 138 /* SetAccessor */))) { + return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl)); + } + // Otherwise, if the containing function is contextually typed by a function type with exactly one call signature + // and that call signature is non-generic, return statements are contextually typed by the return type of the signature + var signature = getContextualSignatureForFunctionLikeDeclaration(functionDecl); + if (signature) { + return getReturnTypeOfSignature(signature); + } + return undefined; + } // In a typed function call, an argument or substitution expression is contextually typed by the type of the corresponding parameter. function getContextualTypeForArgument(callTarget, arg) { var args = getEffectiveCallArguments(callTarget); @@ -16242,7 +17273,7 @@ var ts; var index = ts.indexOf(arrayLiteral.elements, node); return getTypeOfPropertyOfContextualType(type, "" + index) || getIndexTypeOfContextualType(type, 1 /* Number */) - || (languageVersion >= 2 /* ES6 */ ? checkIteratedType(type, undefined) : undefined); + || (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(type, undefined) : undefined); } return undefined; } @@ -16272,6 +17303,8 @@ var ts; case 164 /* ArrowFunction */: case 192 /* ReturnStatement */: return getContextualTypeForReturnExpression(node); + case 173 /* YieldExpression */: + return getContextualTypeForYieldOperand(parent); case 158 /* CallExpression */: case 159 /* NewExpression */: return getContextualTypeForArgument(parent, node); @@ -16308,8 +17341,10 @@ var ts; return node.kind === 163 /* FunctionExpression */ || node.kind === 164 /* ArrowFunction */; } function getContextualSignatureForFunctionLikeDeclaration(node) { - // Only function expressions and arrow functions are contextually typed. - return isFunctionExpressionOrArrowFunction(node) ? getContextualSignature(node) : undefined; + // Only function expressions, arrow functions, and object literal methods are contextually typed. + return isFunctionExpressionOrArrowFunction(node) || ts.isObjectLiteralMethod(node) + ? getContextualSignature(node) + : undefined; } // Return the contextual signature for a given expression node. A contextual type provides a // contextual signature if it has a single call signature and if that call signature is non-generic. @@ -16419,7 +17454,7 @@ var ts; // if there is no index type / iterated type. var restArrayType = checkExpression(e.expression, contextualMapper); var restElementType = getIndexTypeOfType(restArrayType, 1 /* Number */) || - (languageVersion >= 2 /* ES6 */ ? checkIteratedType(restArrayType, undefined) : undefined); + (languageVersion >= 2 /* ES6 */ ? getElementTypeOfIterable(restArrayType, undefined) : undefined); if (restElementType) { elementTypes.push(restElementType); } @@ -16707,15 +17742,15 @@ var ts; // - Otherwise, if IndexExpr is of type Any, the String or Number primitive type, or an enum type, the property access is of type Any. // See if we can index as a property. if (node.argumentExpression) { - var name_6 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); - if (name_6 !== undefined) { - var prop = getPropertyOfType(objectType, name_6); + var name_9 = getPropertyNameForIndexedAccess(node.argumentExpression, indexType); + if (name_9 !== undefined) { + var prop = getPropertyOfType(objectType, name_9); if (prop) { getNodeLinks(node).resolvedSymbol = prop; return getTypeOfSymbol(prop); } else if (isConstEnum) { - error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_6, symbolToString(objectType.symbol)); + error(node.argumentExpression, ts.Diagnostics.Property_0_does_not_exist_on_const_enum_1, name_9, symbolToString(objectType.symbol)); return unknownType; } } @@ -16836,13 +17871,13 @@ var ts; for (var _i = 0; _i < signatures.length; _i++) { var signature = signatures[_i]; var symbol = signature.declaration && getSymbolOfNode(signature.declaration); - var parent_4 = signature.declaration && signature.declaration.parent; + var parent_6 = signature.declaration && signature.declaration.parent; if (!lastSymbol || symbol === lastSymbol) { - if (lastParent && parent_4 === lastParent) { + if (lastParent && parent_6 === lastParent) { index++; } else { - lastParent = parent_4; + lastParent = parent_6; index = cutoffIndex; } } @@ -16850,7 +17885,7 @@ var ts; // current declaration belongs to a different symbol // set cutoffIndex so re-orderings in the future won't change result set from 0 to cutoffIndex index = cutoffIndex = result.length; - lastParent = parent_4; + lastParent = parent_6; } lastSymbol = symbol; // specialized signatures always need to be placed before non-specialized signatures regardless @@ -17335,10 +18370,10 @@ var ts; return resolveCall(node, callSignatures, candidatesOutArray); } function resolveNewExpression(node, candidatesOutArray) { - if (node.arguments && languageVersion < 2 /* ES6 */) { + if (node.arguments && languageVersion < 1 /* ES5 */) { var spreadIndex = getSpreadArgumentIndex(node.arguments); if (spreadIndex >= 0) { - error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_6_and_higher); + error(node.arguments[spreadIndex], ts.Diagnostics.Spread_operator_in_new_expressions_is_only_available_when_targeting_ECMAScript_5_and_higher); } } var expressionType = checkExpression(node.expression); @@ -17354,7 +18389,7 @@ var ts; // If ConstructExpr's apparent type(section 3.8.1) is an object type with one or // more construct signatures, the expression is processed in the same manner as a // function call, but using the construct signatures as the initial set of candidate - // signatures for overload resolution.The result type of the function call becomes + // signatures for overload resolution. The result type of the function call becomes // the result type of the operation. expressionType = getApparentType(expressionType); if (expressionType === unknownType) { @@ -17490,17 +18525,39 @@ var ts; type = checkExpressionCached(func.body, contextualMapper); } else { - // Aggregate the types of expressions within all the return statements. - var types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); - if (types.length === 0) { - return voidType; + var types; + var funcIsGenerator = !!func.asteriskToken; + if (funcIsGenerator) { + types = checkAndAggregateYieldOperandTypes(func.body, contextualMapper); + if (types.length === 0) { + var iterableIteratorAny = createIterableIteratorType(anyType); + if (compilerOptions.noImplicitAny) { + error(func.asteriskToken, ts.Diagnostics.Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type, typeToString(iterableIteratorAny)); + } + return iterableIteratorAny; + } } - // When return statements are contextually typed we allow the return type to be a union type. Otherwise we require the - // return expressions to have a best common supertype. + else { + types = checkAndAggregateReturnExpressionTypes(func.body, contextualMapper); + if (types.length === 0) { + return voidType; + } + } + // When yield/return statements are contextually typed we allow the return type to be a union type. + // Otherwise we require the yield/return expressions to have a best common supertype. type = contextualSignature ? getUnionType(types) : getCommonSupertype(types); if (!type) { - error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); - return unknownType; + if (funcIsGenerator) { + error(func, ts.Diagnostics.No_best_common_type_exists_among_yield_expressions); + return createIterableIteratorType(unknownType); + } + else { + error(func, ts.Diagnostics.No_best_common_type_exists_among_return_expressions); + return unknownType; + } + } + if (funcIsGenerator) { + type = createIterableIteratorType(type); } } if (!contextualSignature) { @@ -17508,7 +18565,23 @@ var ts; } return getWidenedType(type); } - /// Returns a set of types relating to every return expression relating to a function block. + function checkAndAggregateYieldOperandTypes(body, contextualMapper) { + var aggregatedTypes = []; + ts.forEachYieldExpression(body, function (yieldExpression) { + var expr = yieldExpression.expression; + if (expr) { + var type = checkExpressionCached(expr, contextualMapper); + if (yieldExpression.asteriskToken) { + // A yield* expression effectively yields everything that its operand yields + type = checkElementTypeOfIterable(type, yieldExpression.expression); + } + if (!ts.contains(aggregatedTypes, type)) { + aggregatedTypes.push(type); + } + } + }); + return aggregatedTypes; + } function checkAndAggregateReturnExpressionTypes(body, contextualMapper) { var aggregatedTypes = []; ts.forEachReturnStatement(body, function (returnStatement) { @@ -17677,8 +18750,8 @@ var ts; var index = n.argumentExpression; var symbol = findSymbol(n.expression); if (symbol && index && index.kind === 8 /* StringLiteral */) { - var name_7 = index.text; - var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_7); + var name_10 = index.text; + var prop = getPropertyOfType(getTypeOfSymbol(symbol), name_10); return prop && (prop.flags & 3 /* Variable */) !== 0 && (getDeclarationFlagsFromSymbol(prop) & 8192 /* Const */) !== 0; } return false; @@ -17836,16 +18909,16 @@ var ts; var p = properties[_i]; if (p.kind === 225 /* PropertyAssignment */ || p.kind === 226 /* ShorthandPropertyAssignment */) { // TODO(andersh): Computed property support - var name_8 = p.name; + var name_11 = p.name; var type = sourceType.flags & 1 /* Any */ ? sourceType : - getTypeOfPropertyOfType(sourceType, name_8.text) || - isNumericLiteralName(name_8.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || + getTypeOfPropertyOfType(sourceType, name_11.text) || + isNumericLiteralName(name_11.text) && getIndexTypeOfType(sourceType, 1 /* Number */) || getIndexTypeOfType(sourceType, 0 /* String */); if (type) { - checkDestructuringAssignment(p.initializer || name_8, type); + checkDestructuringAssignment(p.initializer || name_11, type); } else { - error(name_8, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_8)); + error(name_11, ts.Diagnostics.Type_0_has_no_property_1_and_no_string_index_signature, typeToString(sourceType), ts.declarationNameToString(name_11)); } } else { @@ -18095,14 +19168,53 @@ var ts; error(node, ts.Diagnostics.Operator_0_cannot_be_applied_to_types_1_and_2, ts.tokenToString(node.operatorToken.kind), typeToString(leftType), typeToString(rightType)); } } + function isYieldExpressionInClass(node) { + var current = node; + var parent = node.parent; + while (parent) { + if (ts.isFunctionLike(parent) && current === parent.body) { + return false; + } + else if (current.kind === 202 /* ClassDeclaration */ || current.kind === 175 /* ClassExpression */) { + return true; + } + current = parent; + parent = parent.parent; + } + return false; + } function checkYieldExpression(node) { // Grammar checking - if (!(node.parserContextFlags & 4 /* Yield */)) { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration); + if (!(node.parserContextFlags & 4 /* Yield */) || isYieldExpressionInClass(node)) { + grammarErrorOnFirstToken(node, ts.Diagnostics.A_yield_expression_is_only_allowed_in_a_generator_body); } - else { - grammarErrorOnFirstToken(node, ts.Diagnostics.yield_expressions_are_not_currently_supported); + if (node.expression) { + var func = ts.getContainingFunction(node); + // If the user's code is syntactically correct, the func should always have a star. After all, + // we are in a yield context. + if (func && func.asteriskToken) { + var expressionType = checkExpressionCached(node.expression, undefined); + var expressionElementType; + var nodeIsYieldStar = !!node.asteriskToken; + if (nodeIsYieldStar) { + expressionElementType = checkElementTypeOfIterable(expressionType, node.expression); + } + // There is no point in doing an assignability check if the function + // has no explicit return type because the return type is directly computed + // from the yield expressions. + if (func.type) { + var signatureElementType = getElementTypeOfIterableIterator(getTypeFromTypeNode(func.type)) || anyType; + if (nodeIsYieldStar) { + checkTypeAssignableTo(expressionElementType, signatureElementType, node.expression, undefined); + } + else { + checkTypeAssignableTo(expressionType, signatureElementType, node.expression, undefined); + } + } + } } + // Both yield and yield* expressions have type 'any' + return anyType; } function checkConditionalExpression(node, contextualMapper) { checkExpression(node.condition); @@ -18273,8 +19385,7 @@ var ts; case 176 /* OmittedExpression */: return undefinedType; case 173 /* YieldExpression */: - checkYieldExpression(node); - return unknownType; + return checkYieldExpression(node); } return unknownType; } @@ -18318,6 +19429,14 @@ var ts; error(node, ts.Diagnostics.A_rest_parameter_must_be_of_an_array_type); } } + function isSyntacticallyValidGenerator(node) { + if (!node.asteriskToken || !node.body) { + return false; + } + return node.kind === 135 /* MethodDeclaration */ || + node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */; + } function checkSignatureDeclaration(node) { // Grammar checking if (node.kind === 141 /* IndexSignature */) { @@ -18345,6 +19464,25 @@ var ts; break; } } + if (node.type) { + if (languageVersion >= 2 /* ES6 */ && isSyntacticallyValidGenerator(node)) { + var returnType = getTypeFromTypeNode(node.type); + if (returnType === voidType) { + error(node.type, ts.Diagnostics.A_generator_cannot_have_a_void_type_annotation); + } + else { + var generatorElementType = getElementTypeOfIterableIterator(returnType) || anyType; + var iterableIteratorInstantiation = createIterableIteratorType(generatorElementType); + // Naively, one could check that IterableIterator is assignable to the return type annotation. + // However, that would not catch the error in the following case. + // + // interface BadGenerator extends Iterable, Iterator { } + // function* g(): BadGenerator { } // Iterable and Iterator have different types! + // + checkTypeAssignableTo(iterableIteratorInstantiation, returnType, node.type); + } + } + } } checkSpecializedSignatureDeclaration(node); } @@ -18984,7 +20122,6 @@ var ts; function checkFunctionDeclaration(node) { if (produceDiagnostics) { checkFunctionLikeDeclaration(node) || - checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarFunctionName(node.name) || checkGrammarForGenerator(node); checkCollisionWithCapturedSuperVariable(node, node.name); @@ -19027,10 +20164,18 @@ var ts; if (node.type && !isAccessor(node.kind) && !node.asteriskToken) { checkIfNonVoidFunctionHasReturnExpressionsOrSingleThrowStatment(node, getTypeFromTypeNode(node.type)); } - // Report an implicit any error if there is no body, no explicit return type, and node is not a private method - // in an ambient context - if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !node.type && !isPrivateWithinAmbient(node)) { - reportImplicitAnyError(node, anyType); + if (produceDiagnostics && !node.type) { + // Report an implicit any error if there is no body, no explicit return type, and node is not a private method + // in an ambient context + if (compilerOptions.noImplicitAny && ts.nodeIsMissing(node.body) && !isPrivateWithinAmbient(node)) { + reportImplicitAnyError(node, anyType); + } + if (node.asteriskToken && ts.nodeIsPresent(node.body)) { + // A generator with a body and no type annotation can still cause errors. It can error if the + // yielded values have no common supertype, or it can give an implicit any error if it has no + // yielded values. The only way to trigger these errors is to try checking its return type. + getReturnTypeOfSignature(getSignatureFromDeclaration(node)); + } } } function checkBlock(node) { @@ -19191,8 +20336,8 @@ var ts; // otherwise if variable has an initializer - show error that initialization will fail // since LHS will be block scoped name instead of function scoped if (!namesShareScope) { - var name_9 = symbolToString(localDeclarationSymbol); - error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_9, name_9); + var name_12 = symbolToString(localDeclarationSymbol); + error(node, ts.Diagnostics.Cannot_initialize_outer_scoped_variable_0_in_the_same_scope_as_block_scoped_declaration_1, name_12, name_12); } } } @@ -19302,7 +20447,7 @@ var ts; } function checkVariableStatement(node) { // Grammar checking - checkGrammarDecorators(node) || checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); + checkGrammarDecorators(node) || checkGrammarModifiers(node) || checkGrammarVariableDeclarationList(node.declarationList) || checkGrammarForDisallowedLetOrConstStatement(node); ts.forEach(node.declarationList.declarations, checkSourceElement); } function checkGrammarDisallowedModifiersInBlockOrObjectLiteralExpression(node) { @@ -19392,7 +20537,7 @@ var ts; // iteratedType will be undefined if the rightType was missing properties/signatures // required to get its iteratedType (like [Symbol.iterator] or next). This may be // because we accessed properties from anyType, or it may have led to an error inside - // getIteratedType. + // getElementTypeOfIterable. if (iteratedType) { checkTypeAssignableTo(iteratedType, leftType, varExpr, undefined); } @@ -19458,7 +20603,7 @@ var ts; return inputType; } if (languageVersion >= 2 /* ES6 */) { - return checkIteratedType(inputType, errorNode) || anyType; + return checkElementTypeOfIterable(inputType, errorNode); } if (allowStringInput) { return checkElementTypeOfArrayOrString(inputType, errorNode); @@ -19475,88 +20620,127 @@ var ts; /** * When errorNode is undefined, it means we should not report any errors. */ - function checkIteratedType(iterable, errorNode) { - ts.Debug.assert(languageVersion >= 2 /* ES6 */); - var iteratedType = getIteratedType(iterable, errorNode); + function checkElementTypeOfIterable(iterable, errorNode) { + var elementType = getElementTypeOfIterable(iterable, errorNode); // Now even though we have extracted the iteratedType, we will have to validate that the type // passed in is actually an Iterable. - if (errorNode && iteratedType) { - checkTypeAssignableTo(iterable, createIterableType(iteratedType), errorNode); + if (errorNode && elementType) { + checkTypeAssignableTo(iterable, createIterableType(elementType), errorNode); } - return iteratedType; - function getIteratedType(iterable, errorNode) { - // We want to treat type as an iterable, and get the type it is an iterable of. The iterable - // must have the following structure (annotated with the names of the variables below): - // - // { // iterable - // [Symbol.iterator]: { // iteratorFunction - // (): { // iterator - // next: { // iteratorNextFunction - // (): { // iteratorNextResult - // value: T // iteratorNextValue - // } - // } - // } - // } - // } - // - // T is the type we are after. At every level that involves analyzing return types - // of signatures, we union the return types of all the signatures. - // - // Another thing to note is that at any step of this process, we could run into a dead end, - // meaning either the property is missing, or we run into the anyType. If either of these things - // happens, we return undefined to signal that we could not find the iterated type. If a property - // is missing, and the previous step did not result in 'any', then we also give an error if the - // caller requested it. Then the caller can decide what to do in the case where there is no iterated - // type. This is different from returning anyType, because that would signify that we have matched the - // whole pattern and that T (above) is 'any'. - if (allConstituentTypesHaveKind(iterable, 1 /* Any */)) { - return undefined; - } + return elementType || anyType; + } + /** + * We want to treat type as an iterable, and get the type it is an iterable of. The iterable + * must have the following structure (annotated with the names of the variables below): + * + * { // iterable + * [Symbol.iterator]: { // iteratorFunction + * (): Iterator + * } + * } + * + * T is the type we are after. At every level that involves analyzing return types + * of signatures, we union the return types of all the signatures. + * + * Another thing to note is that at any step of this process, we could run into a dead end, + * meaning either the property is missing, or we run into the anyType. If either of these things + * happens, we return undefined to signal that we could not find the iterated type. If a property + * is missing, and the previous step did not result in 'any', then we also give an error if the + * caller requested it. Then the caller can decide what to do in the case where there is no iterated + * type. This is different from returning anyType, because that would signify that we have matched the + * whole pattern and that T (above) is 'any'. + */ + function getElementTypeOfIterable(type, errorNode) { + if (type.flags & 1 /* Any */) { + return undefined; + } + var typeAsIterable = type; + if (!typeAsIterable.iterableElementType) { // As an optimization, if the type is instantiated directly using the globalIterableType (Iterable), // then just grab its type argument. - if ((iterable.flags & 4096 /* Reference */) && iterable.target === globalIterableType) { - return iterable.typeArguments[0]; + if ((type.flags & 4096 /* Reference */) && type.target === globalIterableType) { + typeAsIterable.iterableElementType = type.typeArguments[0]; } - var iteratorFunction = getTypeOfPropertyOfType(iterable, ts.getPropertyNameForKnownSymbolName("iterator")); - if (iteratorFunction && allConstituentTypesHaveKind(iteratorFunction, 1 /* Any */)) { - return undefined; - } - var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; - if (iteratorFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + else { + var iteratorFunction = getTypeOfPropertyOfType(type, ts.getPropertyNameForKnownSymbolName("iterator")); + if (iteratorFunction && iteratorFunction.flags & 1 /* Any */) { + return undefined; } - return undefined; - } - var iterator = getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iterator, 1 /* Any */)) { - return undefined; - } - var iteratorNextFunction = getTypeOfPropertyOfType(iterator, "next"); - if (iteratorNextFunction && allConstituentTypesHaveKind(iteratorNextFunction, 1 /* Any */)) { - return undefined; - } - var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; - if (iteratorNextFunctionSignatures.length === 0) { - if (errorNode) { - error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + var iteratorFunctionSignatures = iteratorFunction ? getSignaturesOfType(iteratorFunction, 0 /* Call */) : emptyArray; + if (iteratorFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.Type_must_have_a_Symbol_iterator_method_that_returns_an_iterator); + } + return undefined; } - return undefined; + typeAsIterable.iterableElementType = getElementTypeOfIterator(getUnionType(ts.map(iteratorFunctionSignatures, getReturnTypeOfSignature)), errorNode); } - var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); - if (allConstituentTypesHaveKind(iteratorNextResult, 1 /* Any */)) { - return undefined; - } - var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); - if (!iteratorNextValue) { - if (errorNode) { - error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); - } - return undefined; - } - return iteratorNextValue; } + return typeAsIterable.iterableElementType; + } + /** + * This function has very similar logic as getElementTypeOfIterable, except that it operates on + * Iterators instead of Iterables. Here is the structure: + * + * { // iterator + * next: { // iteratorNextFunction + * (): { // iteratorNextResult + * value: T // iteratorNextValue + * } + * } + * } + * + */ + function getElementTypeOfIterator(type, errorNode) { + if (type.flags & 1 /* Any */) { + return undefined; + } + var typeAsIterator = type; + if (!typeAsIterator.iteratorElementType) { + // As an optimization, if the type is instantiated directly using the globalIteratorType (Iterator), + // then just grab its type argument. + if ((type.flags & 4096 /* Reference */) && type.target === globalIteratorType) { + typeAsIterator.iteratorElementType = type.typeArguments[0]; + } + else { + var iteratorNextFunction = getTypeOfPropertyOfType(type, "next"); + if (iteratorNextFunction && iteratorNextFunction.flags & 1 /* Any */) { + return undefined; + } + var iteratorNextFunctionSignatures = iteratorNextFunction ? getSignaturesOfType(iteratorNextFunction, 0 /* Call */) : emptyArray; + if (iteratorNextFunctionSignatures.length === 0) { + if (errorNode) { + error(errorNode, ts.Diagnostics.An_iterator_must_have_a_next_method); + } + return undefined; + } + var iteratorNextResult = getUnionType(ts.map(iteratorNextFunctionSignatures, getReturnTypeOfSignature)); + if (iteratorNextResult.flags & 1 /* Any */) { + return undefined; + } + var iteratorNextValue = getTypeOfPropertyOfType(iteratorNextResult, "value"); + if (!iteratorNextValue) { + if (errorNode) { + error(errorNode, ts.Diagnostics.The_type_returned_by_the_next_method_of_an_iterator_must_have_a_value_property); + } + return undefined; + } + typeAsIterator.iteratorElementType = iteratorNextValue; + } + } + return typeAsIterator.iteratorElementType; + } + function getElementTypeOfIterableIterator(type) { + if (type.flags & 1 /* Any */) { + return undefined; + } + // As an optimization, if the type is instantiated directly using the globalIterableIteratorType (IterableIterator), + // then just grab its type argument. + if ((type.flags & 4096 /* Reference */) && type.target === globalIterableIteratorType) { + return type.typeArguments[0]; + } + return getElementTypeOfIterable(type, undefined) || + getElementTypeOfIterator(type, undefined); } /** * This function does the following steps: @@ -19637,19 +20821,24 @@ var ts; if (func) { var returnType = getReturnTypeOfSignature(getSignatureFromDeclaration(func)); var exprType = checkExpressionCached(node.expression); + if (func.asteriskToken) { + // A generator does not need its return expressions checked against its return type. + // Instead, the yield expressions are checked against the element type. + // TODO: Check return expressions of generators when return type tracking is added + // for generators. + return; + } if (func.kind === 138 /* SetAccessor */) { error(node.expression, ts.Diagnostics.Setters_cannot_return_a_value); } - else { - if (func.kind === 136 /* Constructor */) { - if (!isTypeAssignableTo(exprType, returnType)) { - error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); - } - } - else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { - checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + else if (func.kind === 136 /* Constructor */) { + if (!isTypeAssignableTo(exprType, returnType)) { + error(node.expression, ts.Diagnostics.Return_type_of_constructor_signature_must_be_assignable_to_the_instance_type_of_the_class); } } + else if (func.type || isGetAccessorWithAnnotatatedSetAccessor(func)) { + checkTypeAssignableTo(exprType, returnType, node.expression, undefined); + } } } } @@ -19870,9 +21059,6 @@ var ts; function checkClassDeclaration(node) { checkGrammarDeclarationNameInStrictMode(node); // Grammar checking - if (node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { - grammarErrorOnNode(node, ts.Diagnostics.class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration); - } if (!node.name && !(node.flags & 256 /* Default */)) { grammarErrorOnFirstToken(node, ts.Diagnostics.A_class_declaration_without_the_default_modifier_must_have_a_name); } @@ -20978,87 +22164,6 @@ var ts; } return node.parent && node.parent.kind === 177 /* ExpressionWithTypeArguments */; } - function isTypeNode(node) { - if (142 /* FirstTypeNode */ <= node.kind && node.kind <= 150 /* LastTypeNode */) { - return true; - } - switch (node.kind) { - case 112 /* AnyKeyword */: - case 120 /* NumberKeyword */: - case 122 /* StringKeyword */: - case 113 /* BooleanKeyword */: - case 123 /* SymbolKeyword */: - return true; - case 99 /* VoidKeyword */: - return node.parent.kind !== 167 /* VoidExpression */; - case 8 /* StringLiteral */: - // Specialized signatures can have string literals as their parameters' type names - return node.parent.kind === 130 /* Parameter */; - case 177 /* ExpressionWithTypeArguments */: - return true; - // Identifiers and qualified names may be type nodes, depending on their context. Climb - // above them to find the lowest container - case 65 /* Identifier */: - // If the identifier is the RHS of a qualified name, then it's a type iff its parent is. - if (node.parent.kind === 127 /* QualifiedName */ && node.parent.right === node) { - node = node.parent; - } - else if (node.parent.kind === 156 /* PropertyAccessExpression */ && node.parent.name === node) { - node = node.parent; - } - // fall through - case 127 /* QualifiedName */: - case 156 /* PropertyAccessExpression */: - // At this point, node is either a qualified name or an identifier - ts.Debug.assert(node.kind === 65 /* Identifier */ || node.kind === 127 /* QualifiedName */ || node.kind === 156 /* PropertyAccessExpression */, "'node' was expected to be a qualified name, identifier or property access in 'isTypeNode'."); - var parent_5 = node.parent; - if (parent_5.kind === 145 /* TypeQuery */) { - return false; - } - // Do not recursively call isTypeNode on the parent. In the example: - // - // let a: A.B.C; - // - // Calling isTypeNode would consider the qualified name A.B a type node. Only C or - // A.B.C is a type node. - if (142 /* FirstTypeNode */ <= parent_5.kind && parent_5.kind <= 150 /* LastTypeNode */) { - return true; - } - switch (parent_5.kind) { - case 177 /* ExpressionWithTypeArguments */: - return true; - case 129 /* TypeParameter */: - return node === parent_5.constraint; - case 133 /* PropertyDeclaration */: - case 132 /* PropertySignature */: - case 130 /* Parameter */: - case 199 /* VariableDeclaration */: - return node === parent_5.type; - case 201 /* FunctionDeclaration */: - case 163 /* FunctionExpression */: - case 164 /* ArrowFunction */: - case 136 /* Constructor */: - case 135 /* MethodDeclaration */: - case 134 /* MethodSignature */: - case 137 /* GetAccessor */: - case 138 /* SetAccessor */: - return node === parent_5.type; - case 139 /* CallSignature */: - case 140 /* ConstructSignature */: - case 141 /* IndexSignature */: - return node === parent_5.type; - case 161 /* TypeAssertionExpression */: - return node === parent_5.type; - case 158 /* CallExpression */: - case 159 /* NewExpression */: - return parent_5.typeArguments && ts.indexOf(parent_5.typeArguments, node) >= 0; - case 160 /* TaggedTemplateExpression */: - // TODO (drosen): TaggedTemplateExpressions may eventually support type arguments. - return false; - } - } - return false; - } function getLeftSideOfImportEqualsOrExportAssignment(nodeOnRightSide) { while (nodeOnRightSide.parent.kind === 127 /* QualifiedName */) { nodeOnRightSide = nodeOnRightSide.parent; @@ -21201,7 +22306,7 @@ var ts; // We cannot answer semantic questions within a with block, do not proceed any further return unknownType; } - if (isTypeNode(node)) { + if (ts.isTypeNode(node)) { return getTypeFromTypeNode(node); } if (ts.isExpression(node)) { @@ -21255,9 +22360,9 @@ var ts; function getRootSymbols(symbol) { if (symbol.flags & 268435456 /* UnionProperty */) { var symbols = []; - var name_10 = symbol.name; + var name_13 = symbol.name; ts.forEach(getSymbolLinks(symbol).unionType.types, function (t) { - symbols.push(getPropertyOfType(t, name_10)); + symbols.push(getPropertyOfType(t, name_13)); }); return symbols; } @@ -21712,8 +22817,7 @@ var ts; getSymbolLinks(unknownSymbol).type = unknownType; globals[undefinedSymbol.name] = undefinedSymbol; // Initialize special types - globalArraySymbol = getGlobalTypeSymbol("Array"); - globalArrayType = getTypeOfGlobalSymbol(globalArraySymbol, 1); + globalArrayType = getGlobalType("Array", 1); globalObjectType = getGlobalType("Object"); globalFunctionType = getGlobalType("Function"); globalStringType = getGlobalType("String"); @@ -21731,6 +22835,8 @@ var ts; globalESSymbolType = getGlobalType("Symbol"); globalESSymbolConstructorSymbol = getGlobalValueSymbol("Symbol"); globalIterableType = getGlobalType("Iterable", 1); + globalIteratorType = getGlobalType("Iterator", 1); + globalIterableIteratorType = getGlobalType("IterableIterator", 1); } else { globalTemplateStringsArrayType = unknownType; @@ -21739,6 +22845,9 @@ var ts; // a global Symbol already, particularly if it is a class. globalESSymbolType = createAnonymousType(undefined, emptySymbols, emptyArray, emptyArray, undefined, undefined); globalESSymbolConstructorSymbol = undefined; + globalIterableType = emptyGenericType; + globalIteratorType = emptyGenericType; + globalIterableIteratorType = emptyGenericType; } anyArrayType = createArrayType(anyType); } @@ -21763,20 +22872,20 @@ var ts; if (impotClause.namedBindings) { var nameBindings = impotClause.namedBindings; if (nameBindings.kind === 212 /* NamespaceImport */) { - var name_11 = nameBindings.name; - if (isReservedWordInStrictMode(name_11)) { - var nameText = ts.declarationNameToString(name_11); - return grammarErrorOnNode(name_11, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_14 = nameBindings.name; + if (isReservedWordInStrictMode(name_14)) { + var nameText = ts.declarationNameToString(name_14); + return grammarErrorOnNode(name_14, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } else if (nameBindings.kind === 213 /* NamedImports */) { var reportError = false; for (var _i = 0, _a = nameBindings.elements; _i < _a.length; _i++) { var element = _a[_i]; - var name_12 = element.name; - if (isReservedWordInStrictMode(name_12)) { - var nameText = ts.declarationNameToString(name_12); - reportError = reportError || grammarErrorOnNode(name_12, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); + var name_15 = element.name; + if (isReservedWordInStrictMode(name_15)) { + var nameText = ts.declarationNameToString(name_15); + reportError = reportError || grammarErrorOnNode(name_15, ts.Diagnostics.Identifier_expected_0_is_a_reserved_word_in_strict_mode, nameText); } } return reportError; @@ -21902,19 +23011,28 @@ var ts; case 135 /* MethodDeclaration */: case 134 /* MethodSignature */: case 141 /* IndexSignature */: - case 202 /* ClassDeclaration */: - case 203 /* InterfaceDeclaration */: case 206 /* ModuleDeclaration */: - case 205 /* EnumDeclaration */: - case 181 /* VariableStatement */: - case 201 /* FunctionDeclaration */: - case 204 /* TypeAliasDeclaration */: case 210 /* ImportDeclaration */: case 209 /* ImportEqualsDeclaration */: case 216 /* ExportDeclaration */: case 215 /* ExportAssignment */: case 130 /* Parameter */: break; + case 202 /* ClassDeclaration */: + case 203 /* InterfaceDeclaration */: + case 181 /* VariableStatement */: + case 201 /* FunctionDeclaration */: + case 204 /* TypeAliasDeclaration */: + if (node.modifiers && node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; + case 205 /* EnumDeclaration */: + if (node.modifiers && (node.modifiers.length > 1 || node.modifiers[0].kind !== 70 /* ConstKeyword */) && + node.parent.kind !== 207 /* ModuleBlock */ && node.parent.kind !== 228 /* SourceFile */) { + return grammarErrorOnFirstToken(node, ts.Diagnostics.Modifiers_cannot_appear_here); + } + break; default: return false; } @@ -22012,9 +23130,6 @@ var ts; else if ((node.kind === 210 /* ImportDeclaration */ || node.kind === 209 /* ImportEqualsDeclaration */) && flags & 2 /* Ambient */) { return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_import_declaration, "declare"); } - else if (node.kind === 203 /* InterfaceDeclaration */ && flags & 2 /* Ambient */) { - return grammarErrorOnNode(lastDeclare, ts.Diagnostics.A_declare_modifier_cannot_be_used_with_an_interface_declaration, "declare"); - } else if (node.kind === 130 /* Parameter */ && (flags & 112 /* AccessibilityModifier */) && ts.isBindingPattern(node.name)) { return grammarErrorOnNode(node, ts.Diagnostics.A_parameter_property_may_not_be_a_binding_pattern); } @@ -22231,7 +23346,18 @@ var ts; } function checkGrammarForGenerator(node) { if (node.asteriskToken) { - return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_currently_supported); + ts.Debug.assert(node.kind === 201 /* FunctionDeclaration */ || + node.kind === 163 /* FunctionExpression */ || + node.kind === 135 /* MethodDeclaration */); + if (ts.isInAmbientContext(node)) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_not_allowed_in_an_ambient_context); + } + if (!node.body) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.An_overload_signature_cannot_be_declared_as_a_generator); + } + if (languageVersion < 2 /* ES6 */) { + return grammarErrorOnNode(node.asteriskToken, ts.Diagnostics.Generators_are_only_available_when_targeting_ECMAScript_6_or_higher); + } } } function checkGrammarFunctionName(name) { @@ -22252,11 +23378,11 @@ var ts; var inStrictMode = (node.parserContextFlags & 1 /* StrictMode */) !== 0; for (var _i = 0, _a = node.properties; _i < _a.length; _i++) { var prop = _a[_i]; - var name_13 = prop.name; + var name_16 = prop.name; if (prop.kind === 176 /* OmittedExpression */ || - name_13.kind === 128 /* ComputedPropertyName */) { + name_16.kind === 128 /* ComputedPropertyName */) { // If the name is not a ComputedPropertyName, the grammar checking will skip it - checkGrammarComputedPropertyName(name_13); + checkGrammarComputedPropertyName(name_16); continue; } // ECMA-262 11.1.5 Object Initialiser @@ -22271,8 +23397,8 @@ var ts; if (prop.kind === 225 /* PropertyAssignment */ || prop.kind === 226 /* ShorthandPropertyAssignment */) { // Grammar checking for computedPropertName and shorthandPropertyAssignment checkGrammarForInvalidQuestionMark(prop, prop.questionToken, ts.Diagnostics.An_object_member_cannot_be_declared_optional); - if (name_13.kind === 7 /* NumericLiteral */) { - checkGrammarNumericLiteral(name_13); + if (name_16.kind === 7 /* NumericLiteral */) { + checkGrammarNumericLiteral(name_16); } currentKind = Property; } @@ -22288,26 +23414,26 @@ var ts; else { ts.Debug.fail("Unexpected syntax kind:" + prop.kind); } - if (!ts.hasProperty(seen, name_13.text)) { - seen[name_13.text] = currentKind; + if (!ts.hasProperty(seen, name_16.text)) { + seen[name_16.text] = currentKind; } else { - var existingKind = seen[name_13.text]; + var existingKind = seen[name_16.text]; if (currentKind === Property && existingKind === Property) { if (inStrictMode) { - grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); + grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_properties_with_the_same_name_in_strict_mode); } } else if ((currentKind & GetOrSetAccessor) && (existingKind & GetOrSetAccessor)) { if (existingKind !== GetOrSetAccessor && currentKind !== existingKind) { - seen[name_13.text] = currentKind | existingKind; + seen[name_16.text] = currentKind | existingKind; } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_multiple_get_Slashset_accessors_with_the_same_name); } } else { - return grammarErrorOnNode(name_13, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); + return grammarErrorOnNode(name_16, ts.Diagnostics.An_object_literal_cannot_have_property_and_accessor_with_the_same_name); } } } @@ -23168,9 +24294,9 @@ var ts; } var count = 0; while (true) { - var name_14 = baseName + "_" + (++count); - if (!ts.hasProperty(currentSourceFile.identifiers, name_14)) { - return name_14; + var name_17 = baseName + "_" + (++count); + if (!ts.hasProperty(currentSourceFile.identifiers, name_17)) { + return name_17; } } } @@ -24379,9 +25505,9 @@ var ts; tempFlags++; // Skip over 'i' and 'n' if (count !== 8 && count !== 13) { - var name_15 = count < 26 ? "_" + String.fromCharCode(97 /* a */ + count) : "_" + (count - 26); - if (isUniqueName(name_15)) { - return name_15; + var name_18 = count < 26 ? "_" + String.fromCharCode(97 /* a */ + count) : "_" + (count - 26); + if (isUniqueName(name_18)) { + return name_18; } } } @@ -24414,9 +25540,9 @@ var ts; } function generateNameForModuleOrEnum(node) { if (node.name.kind === 65 /* Identifier */) { - var name_16 = node.name.text; + var name_19 = node.name.text; // Use module/enum name itself if it is unique, otherwise make a unique variation - assignGeneratedName(node, isUniqueLocalName(name_16, node) ? name_16 : makeUniqueName(name_16)); + assignGeneratedName(node, isUniqueLocalName(name_19, node) ? name_19 : makeUniqueName(name_19)); } } function generateNameForImportOrExportDeclaration(node) { @@ -24635,8 +25761,8 @@ var ts; // Child scopes are always shown with a dot (even if they have no name), // unless it is a computed property. Then it is shown with brackets, // but the brackets are included in the name. - var name_17 = node.name; - if (!name_17 || name_17.kind !== 128 /* ComputedPropertyName */) { + var name_20 = node.name; + if (!name_20 || name_20.kind !== 128 /* ComputedPropertyName */) { scopeName = "." + scopeName; } scopeName = sourceMapData.sourceMapNames[parentIndex] + scopeName; @@ -24665,10 +25791,10 @@ var ts; node.kind === 205 /* EnumDeclaration */) { // Declaration and has associated name use it if (node.name) { - var name_18 = node.name; + var name_21 = node.name; // For computed property names, the text will include the brackets - scopeName = name_18.kind === 128 /* ComputedPropertyName */ - ? ts.getTextOfNode(name_18) + scopeName = name_21.kind === 128 /* ComputedPropertyName */ + ? ts.getTextOfNode(name_21) : node.name.text; } recordScopeNameStart(scopeName); @@ -25369,16 +26495,16 @@ var ts; } return true; } - function emitListWithSpread(elements, alwaysCopy, multiLine, trailingComma) { + function emitListWithSpread(elements, needsUniqueCopy, multiLine, trailingComma, useConcat) { var pos = 0; var group = 0; var length = elements.length; while (pos < length) { // Emit using the pattern .concat(, , ...) - if (group === 1) { + if (group === 1 && useConcat) { write(".concat("); } - else if (group > 1) { + else if (group > 0) { write(", "); } var e = elements[pos]; @@ -25386,7 +26512,7 @@ var ts; e = e.expression; emitParenthesizedIf(e, group === 0 && needsParenthesisForPropertyAccessOrInvocation(e)); pos++; - if (pos === length && group === 0 && alwaysCopy && e.kind !== 154 /* ArrayLiteralExpression */) { + if (pos === length && group === 0 && needsUniqueCopy && e.kind !== 154 /* ArrayLiteralExpression */) { write(".slice()"); } } @@ -25409,7 +26535,9 @@ var ts; group++; } if (group > 1) { - write(")"); + if (useConcat) { + write(")"); + } } } function isSpreadElementExpression(node) { @@ -25427,7 +26555,7 @@ var ts; } else { emitListWithSpread(elements, true, (node.flags & 512 /* MultiLine */) !== 0, - /*trailingComma*/ elements.hasTrailingComma); + /*trailingComma*/ elements.hasTrailingComma, true); } } function emitObjectLiteralBody(node, numElements) { @@ -25798,7 +26926,7 @@ var ts; write("void 0"); } write(", "); - emitListWithSpread(node.arguments, false, false, false); + emitListWithSpread(node.arguments, false, false, false, true); write(")"); } function emitCallExpression(node) { @@ -25832,11 +26960,42 @@ var ts; } function emitNewExpression(node) { write("new "); - emit(node.expression); - if (node.arguments) { + // Spread operator logic can be supported in new expressions in ES5 using a combination + // of Function.prototype.bind() and Function.prototype.apply(). + // + // Example: + // + // var arguments = [1, 2, 3, 4, 5]; + // new Array(...arguments); + // + // Could be transpiled into ES5: + // + // var arguments = [1, 2, 3, 4, 5]; + // new (Array.bind.apply(Array, [void 0].concat(arguments))); + // + // `[void 0]` is the first argument which represents `thisArg` to the bind method above. + // And `thisArg` will be set to the return value of the constructor when instantiated + // with the new operator — regardless of any value we set `thisArg` to. Thus, we set it + // to an undefined, `void 0`. + if (languageVersion === 1 /* ES5 */ && + node.arguments && + hasSpreadElement(node.arguments)) { write("("); - emitCommaList(node.arguments); - write(")"); + var target = emitCallTarget(node.expression); + write(".bind.apply("); + emit(target); + write(", [void 0].concat("); + emitListWithSpread(node.arguments, false, false, false, false); + write(")))"); + write("()"); + } + else { + emit(node.expression); + if (node.arguments) { + write("("); + emitCommaList(node.arguments); + write(")"); + } } } function emitTaggedTemplateExpression(node) { @@ -26136,9 +27295,10 @@ var ts; write(")"); emitEmbeddedStatement(node.statement); } - /* Returns true if start of variable declaration list was emitted. - * Return false if nothing was written - this can happen for source file level variable declarations - * in system modules - such variable declarations are hoisted. + /** + * Returns true if start of variable declaration list was emitted. + * Returns false if nothing was written - this can happen for source file level variable declarations + * in system modules where such variable declarations are hoisted. */ function tryEmitStartOfVariableDeclarationList(decl, startPos) { if (shouldHoistVariable(decl, true)) { @@ -26901,15 +28061,35 @@ var ts; ts.forEach(node.declarationList.declarations, emitExportVariableAssignments); } } + function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node) { + // If we're not exporting the variables, there's nothing special here. + // Always emit comments for these nodes. + if (!(node.flags & 1 /* Export */)) { + return true; + } + // If we are exporting, but it's a top-level ES6 module exports, + // we'll emit the declaration list verbatim, so emit comments too. + if (isES6ExportedDeclaration(node)) { + return true; + } + // Otherwise, only emit if we have at least one initializer present. + for (var _a = 0, _b = node.declarationList.declarations; _a < _b.length; _a++) { + var declaration = _b[_a]; + if (declaration.initializer) { + return true; + } + } + return false; + } function emitParameter(node) { if (languageVersion < 2 /* ES6 */) { if (ts.isBindingPattern(node.name)) { - var name_19 = createTempVariable(0 /* Auto */); + var name_22 = createTempVariable(0 /* Auto */); if (!tempParameters) { tempParameters = []; } - tempParameters.push(name_19); - emit(name_19); + tempParameters.push(name_22); + emit(name_22); } else { emit(node.name); @@ -28577,8 +29757,8 @@ var ts; // export { x, y } for (var _c = 0, _d = node.exportClause.elements; _c < _d.length; _c++) { var specifier = _d[_c]; - var name_20 = (specifier.propertyName || specifier.name).text; - (exportSpecifiers[name_20] || (exportSpecifiers[name_20] = [])).push(specifier); + var name_23 = (specifier.propertyName || specifier.name).text; + (exportSpecifiers[name_23] || (exportSpecifiers[name_23] = [])).push(specifier); } } break; @@ -28778,12 +29958,12 @@ var ts; var seen = {}; for (var i = 0; i < hoistedVars.length; ++i) { var local = hoistedVars[i]; - var name_21 = local.kind === 65 /* Identifier */ + var name_24 = local.kind === 65 /* Identifier */ ? local : local.name; - if (name_21) { + if (name_24) { // do not emit duplicate entries (in case of declaration merging) in the list of hoisted variables - var text = ts.unescapeIdentifier(name_21.text); + var text = ts.unescapeIdentifier(name_24.text); if (ts.hasProperty(seen, text)) { continue; } @@ -28862,15 +30042,15 @@ var ts; } if (node.kind === 199 /* VariableDeclaration */ || node.kind === 153 /* BindingElement */) { if (shouldHoistVariable(node, false)) { - var name_22 = node.name; - if (name_22.kind === 65 /* Identifier */) { + var name_25 = node.name; + if (name_25.kind === 65 /* Identifier */) { if (!hoistedVars) { hoistedVars = []; } - hoistedVars.push(name_22); + hoistedVars.push(name_25); } else { - ts.forEachChild(name_22, visit); + ts.forEachChild(name_25, visit); } } return; @@ -29329,6 +30509,8 @@ var ts; case 204 /* TypeAliasDeclaration */: case 215 /* ExportAssignment */: return false; + case 181 /* VariableStatement */: + return shouldEmitLeadingAndTrailingCommentsForVariableStatement(node); case 206 /* ModuleDeclaration */: // Only emit the leading/trailing comments for a module if we're actually // emitting the module as well. @@ -30710,28 +31892,28 @@ var ts; switch (n.kind) { case 180 /* Block */: if (!ts.isFunctionBlock(n)) { - var parent_6 = n.parent; + var parent_7 = n.parent; var openBrace = ts.findChildOfKind(n, 14 /* OpenBraceToken */, sourceFile); var closeBrace = ts.findChildOfKind(n, 15 /* CloseBraceToken */, sourceFile); // Check if the block is standalone, or 'attached' to some parent statement. // If the latter, we want to collaps the block, but consider its hint span // to be the entire span of the parent. - if (parent_6.kind === 185 /* DoStatement */ || - parent_6.kind === 188 /* ForInStatement */ || - parent_6.kind === 189 /* ForOfStatement */ || - parent_6.kind === 187 /* ForStatement */ || - parent_6.kind === 184 /* IfStatement */ || - parent_6.kind === 186 /* WhileStatement */ || - parent_6.kind === 193 /* WithStatement */ || - parent_6.kind === 224 /* CatchClause */) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + if (parent_7.kind === 185 /* DoStatement */ || + parent_7.kind === 188 /* ForInStatement */ || + parent_7.kind === 189 /* ForOfStatement */ || + parent_7.kind === 187 /* ForStatement */ || + parent_7.kind === 184 /* IfStatement */ || + parent_7.kind === 186 /* WhileStatement */ || + parent_7.kind === 193 /* WithStatement */ || + parent_7.kind === 224 /* CatchClause */) { + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } - if (parent_6.kind === 197 /* TryStatement */) { + if (parent_7.kind === 197 /* TryStatement */) { // Could be the try-block, or the finally-block. - var tryStatement = parent_6; + var tryStatement = parent_7; if (tryStatement.tryBlock === n) { - addOutliningSpan(parent_6, openBrace, closeBrace, autoCollapse(n)); + addOutliningSpan(parent_7, openBrace, closeBrace, autoCollapse(n)); break; } else if (tryStatement.finallyBlock === n) { @@ -30798,12 +31980,12 @@ var ts; ts.forEach(program.getSourceFiles(), function (sourceFile) { cancellationToken.throwIfCancellationRequested(); var nameToDeclarations = sourceFile.getNamedDeclarations(); - for (var name_23 in nameToDeclarations) { - var declarations = ts.getProperty(nameToDeclarations, name_23); + for (var name_26 in nameToDeclarations) { + var declarations = ts.getProperty(nameToDeclarations, name_26); if (declarations) { // First do a quick check to see if the name of the declaration matches the // last portion of the (possibly) dotted name they're searching for. - var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_23); + var matches = patternMatcher.getMatchesForLastSegmentOfPattern(name_26); if (!matches) { continue; } @@ -30816,14 +31998,14 @@ var ts; if (!containers) { return undefined; } - matches = patternMatcher.getMatches(containers, name_23); + matches = patternMatcher.getMatches(containers, name_26); if (!matches) { continue; } } var fileName = sourceFile.fileName; var matchKind = bestMatchKind(matches); - rawItems.push({ name: name_23, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); + rawItems.push({ name: name_26, fileName: fileName, matchKind: matchKind, isCaseSensitive: allMatchesAreCaseSensitive(matches), declaration: declaration }); } } } @@ -31206,9 +32388,9 @@ var ts; case 199 /* VariableDeclaration */: case 153 /* BindingElement */: var variableDeclarationNode; - var name_24; + var name_27; if (node.kind === 153 /* BindingElement */) { - name_24 = node.name; + name_27 = node.name; variableDeclarationNode = node; // binding elements are added only for variable declarations // bubble up to the containing variable declaration @@ -31220,16 +32402,16 @@ var ts; else { ts.Debug.assert(!ts.isBindingPattern(node.name)); variableDeclarationNode = node; - name_24 = node.name; + name_27 = node.name; } if (ts.isConst(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.constElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.constElement); } else if (ts.isLet(variableDeclarationNode)) { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.letElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.letElement); } else { - return createItem(node, getTextOfNode(name_24), ts.ScriptElementKind.variableElement); + return createItem(node, getTextOfNode(name_27), ts.ScriptElementKind.variableElement); } case 136 /* Constructor */: return createItem(node, "constructor", ts.ScriptElementKind.constructorImplementationElement); @@ -32751,7 +33933,7 @@ var ts; // for the position of the relevant node (or comma). var syntaxList = ts.forEach(node.parent.getChildren(), function (c) { // find syntax list that covers the span of the node - if (c.kind === 229 /* SyntaxList */ && c.pos <= node.pos && c.end >= node.end) { + if (c.kind === 251 /* SyntaxList */ && c.pos <= node.pos && c.end >= node.end) { return c; } }); @@ -33158,10 +34340,6 @@ var ts; }); } ts.signatureToDisplayParts = signatureToDisplayParts; - function isJavaScript(fileName) { - return ts.fileExtensionIs(fileName, ".js"); - } - ts.isJavaScript = isJavaScript; })(ts || (ts = {})); /// /// @@ -33644,7 +34822,7 @@ var ts; this.NoSpaceBeforeOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 18 /* OpenBracketToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.NoSpaceAfterOpenBracket = new formatting.Rule(formatting.RuleDescriptor.create3(18 /* OpenBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.NoSpaceBeforeCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 19 /* CloseBracketToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); - this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19 /* CloseBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); + this.NoSpaceAfterCloseBracket = new formatting.Rule(formatting.RuleDescriptor.create3(19 /* CloseBracketToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsNotBeforeBlockInFunctionDeclarationContext), 8 /* Delete */)); // Place a space before open brace in a function declaration this.FunctionOpenBraceLeftTokenRange = formatting.Shared.TokenRange.AnyIncludingMultilineComments; this.SpaceBeforeOpenBraceInFunction = new formatting.Rule(formatting.RuleDescriptor.create2(this.FunctionOpenBraceLeftTokenRange, 14 /* OpenBraceToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclContext, Rules.IsBeforeBlockContext, Rules.IsNotFormatOnEnter, Rules.IsSameLineTokenOrBeforeMultilineBlockContext), 2 /* Space */), 1 /* CanDeleteNewLines */); @@ -33727,6 +34905,10 @@ var ts; this.SpaceBeforeAt = new formatting.Rule(formatting.RuleDescriptor.create2(formatting.Shared.TokenRange.Any, 52 /* AtToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 2 /* Space */)); this.NoSpaceAfterAt = new formatting.Rule(formatting.RuleDescriptor.create3(52 /* AtToken */, formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext), 8 /* Delete */)); this.SpaceAfterDecorator = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.Any, formatting.Shared.TokenRange.FromTokens([65 /* Identifier */, 78 /* ExportKeyword */, 73 /* DefaultKeyword */, 69 /* ClassKeyword */, 109 /* StaticKeyword */, 108 /* PublicKeyword */, 106 /* PrivateKeyword */, 107 /* ProtectedKeyword */, 116 /* GetKeyword */, 121 /* SetKeyword */, 18 /* OpenBracketToken */, 35 /* AsteriskToken */])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsEndOfDecoratorContextOnSameLine), 2 /* Space */)); + this.NoSpaceBetweenFunctionKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(83 /* FunctionKeyword */, 35 /* AsteriskToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 8 /* Delete */)); + this.SpaceAfterStarInGeneratorDeclaration = new formatting.Rule(formatting.RuleDescriptor.create3(35 /* AsteriskToken */, formatting.Shared.TokenRange.FromTokens([65 /* Identifier */, 16 /* OpenParenToken */])), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsFunctionDeclarationOrFunctionExpressionContext), 2 /* Space */)); + this.NoSpaceBetweenYieldKeywordAndStar = new formatting.Rule(formatting.RuleDescriptor.create1(110 /* YieldKeyword */, 35 /* AsteriskToken */), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 8 /* Delete */)); + this.SpaceBetweenYieldOrYieldStarAndOperand = new formatting.Rule(formatting.RuleDescriptor.create4(formatting.Shared.TokenRange.FromTokens([110 /* YieldKeyword */, 35 /* AsteriskToken */]), formatting.Shared.TokenRange.Any), formatting.RuleOperation.create2(new formatting.RuleOperationContext(Rules.IsSameLineTokenContext, Rules.IsYieldOrYieldStarWithOperand), 2 /* Space */)); // These rules are higher in priority than user-configurable rules. this.HighPriorityCommonRules = [ @@ -33744,7 +34926,9 @@ var ts; this.NoSpaceAfterCloseBrace, this.SpaceAfterOpenBrace, this.SpaceBeforeCloseBrace, this.NewLineBeforeCloseBraceInBlockContext, this.SpaceAfterCloseBrace, this.SpaceBetweenCloseBraceAndElse, this.SpaceBetweenCloseBraceAndWhile, this.NoSpaceBetweenEmptyBraceBrackets, + this.NoSpaceBetweenFunctionKeywordAndStar, this.SpaceAfterStarInGeneratorDeclaration, this.SpaceAfterFunctionInFuncDecl, this.NewLineAfterOpenBraceInBlockContext, this.SpaceAfterGetSetInMember, + this.NoSpaceBetweenYieldKeywordAndStar, this.SpaceBetweenYieldOrYieldStarAndOperand, this.NoSpaceBetweenReturnAndSemicolon, this.SpaceAfterCertainKeywords, this.SpaceAfterLetConstInVariableDeclaration, @@ -33816,9 +35000,9 @@ var ts; } Rules.prototype.getRuleName = function (rule) { var o = this; - for (var name_25 in o) { - if (o[name_25] === rule) { - return name_25; + for (var name_28 in o) { + if (o[name_28] === rule) { + return name_28; } } throw new Error("Unknown rule"); @@ -33937,6 +35121,9 @@ var ts; } return false; }; + Rules.IsFunctionDeclarationOrFunctionExpressionContext = function (context) { + return context.contextNode.kind === 201 /* FunctionDeclaration */ || context.contextNode.kind === 163 /* FunctionExpression */; + }; Rules.IsTypeScriptDeclWithBlockContext = function (context) { return Rules.NodeIsTypeScriptDeclWithBlockContext(context.contextNode); }; @@ -34001,6 +35188,9 @@ var ts; Rules.IsSameLineTokenContext = function (context) { return context.TokensAreOnSameLine(); }; + Rules.IsNotBeforeBlockInFunctionDeclarationContext = function (context) { + return !Rules.IsFunctionDeclContext(context) && !Rules.IsBeforeBlockContext(context); + }; Rules.IsEndOfDecoratorContextOnSameLine = function (context) { return context.TokensAreOnSameLine() && context.contextNode.decorators && @@ -34055,6 +35245,9 @@ var ts; Rules.IsVoidOpContext = function (context) { return context.currentTokenSpan.kind === 99 /* VoidKeyword */ && context.currentTokenParent.kind === 167 /* VoidExpression */; }; + Rules.IsYieldOrYieldStarWithOperand = function (context) { + return context.contextNode.kind === 173 /* YieldExpression */ && context.contextNode.expression !== undefined; + }; return Rules; })(); formatting.Rules = Rules; @@ -35759,7 +36952,7 @@ var ts; return pos; }; NodeObject.prototype.createSyntaxList = function (nodes) { - var list = createNode(229 /* SyntaxList */, nodes.pos, nodes.end, 1024 /* Synthetic */, this); + var list = createNode(251 /* SyntaxList */, nodes.pos, nodes.end, 1024 /* Synthetic */, this); list._children = []; var pos = nodes.pos; for (var _i = 0; _i < nodes.length; _i++) { @@ -36557,9 +37750,9 @@ var ts; return false; } // If the parent is not sourceFile or module block it is local variable - for (var parent_7 = declaration.parent; !ts.isFunctionBlock(parent_7); parent_7 = parent_7.parent) { + for (var parent_8 = declaration.parent; !ts.isFunctionBlock(parent_8); parent_8 = parent_8.parent) { // Reached source file or module block - if (parent_7.kind === 228 /* SourceFile */ || parent_7.kind === 207 /* ModuleBlock */) { + if (parent_8.kind === 228 /* SourceFile */ || parent_8.kind === 207 /* ModuleBlock */) { return false; } } @@ -37537,7 +38730,7 @@ var ts; return true; } if (parameter.questionToken) { - diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics.can_only_be_used_in_a_ts_file)); + diagnostics.push(ts.createDiagnosticForNode(parameter.questionToken, ts.Diagnostics._0_can_only_be_used_in_a_ts_file, '?')); return true; } if (parameter.type) { @@ -37919,12 +39112,12 @@ var ts; function getContainingObjectLiteralApplicableForCompletion(previousToken) { // The locations in an object literal expression that are applicable for completion are property name definition locations. if (previousToken) { - var parent_8 = previousToken.parent; + var parent_9 = previousToken.parent; switch (previousToken.kind) { case 14 /* OpenBraceToken */: // let x = { | case 23 /* CommaToken */: - if (parent_8 && parent_8.kind === 155 /* ObjectLiteralExpression */) { - return parent_8; + if (parent_9 && parent_9.kind === 155 /* ObjectLiteralExpression */) { + return parent_9; } break; } @@ -38107,10 +39300,10 @@ var ts; for (var _i = 0, _a = program.getSourceFiles(); _i < _a.length; _i++) { var sourceFile = _a[_i]; var nameTable = getNameTable(sourceFile); - for (var name_26 in nameTable) { - if (!allNames[name_26]) { - allNames[name_26] = name_26; - var displayName = getCompletionEntryDisplayName(name_26, target, true); + for (var name_29 in nameTable) { + if (!allNames[name_29]) { + allNames[name_29] = name_29; + var displayName = getCompletionEntryDisplayName(name_29, target, true); if (displayName) { var entry = { name: displayName, @@ -39018,19 +40211,19 @@ var ts; function getThrowStatementOwner(throwStatement) { var child = throwStatement; while (child.parent) { - var parent_9 = child.parent; - if (ts.isFunctionBlock(parent_9) || parent_9.kind === 228 /* SourceFile */) { - return parent_9; + var parent_10 = child.parent; + if (ts.isFunctionBlock(parent_10) || parent_10.kind === 228 /* SourceFile */) { + return parent_10; } // A throw-statement is only owned by a try-statement if the try-statement has // a catch clause, and if the throw-statement occurs within the try block. - if (parent_9.kind === 197 /* TryStatement */) { - var tryStatement = parent_9; + if (parent_10.kind === 197 /* TryStatement */) { + var tryStatement = parent_10; if (tryStatement.tryBlock === child && tryStatement.catchClause) { return child; } } - child = parent_9; + child = parent_10; } return undefined; } @@ -39992,19 +41185,19 @@ var ts; if (isNameOfPropertyAssignment(node)) { var objectLiteral = node.parent.parent; var contextualType = typeChecker.getContextualType(objectLiteral); - var name_27 = node.text; + var name_30 = node.text; if (contextualType) { if (contextualType.flags & 16384 /* Union */) { // This is a union type, first see if the property we are looking for is a union property (i.e. exists in all types) // if not, search the constituent types for the property - var unionProperty = contextualType.getProperty(name_27); + var unionProperty = contextualType.getProperty(name_30); if (unionProperty) { return [unionProperty]; } else { var result_4 = []; ts.forEach(contextualType.types, function (t) { - var symbol = t.getProperty(name_27); + var symbol = t.getProperty(name_30); if (symbol) { result_4.push(symbol); } @@ -40013,7 +41206,7 @@ var ts; } } else { - var symbol_1 = contextualType.getProperty(name_27); + var symbol_1 = contextualType.getProperty(name_30); if (symbol_1) { return [symbol_1]; } @@ -40994,7 +42187,7 @@ var ts; var lastEnd = 0; for (var i = 0, n = dense.length; i < n; i += 3) { var start = dense[i]; - var length_1 = dense[i + 1]; + var length_2 = dense[i + 1]; var type = dense[i + 2]; // Make a whitespace entry between the last item and this one. if (lastEnd >= 0) { @@ -41003,8 +42196,8 @@ var ts; entries.push({ length: whitespaceLength_1, classification: TokenClass.Whitespace }); } } - entries.push({ length: length_1, classification: convertClassification(type) }); - lastEnd = start + length_1; + entries.push({ length: length_2, classification: convertClassification(type) }); + lastEnd = start + length_2; } var whitespaceLength = text.length - lastEnd; if (whitespaceLength > 0) {