Skip to content

Commit 675679a

Browse files
committed
Merge pull request #3031 from Microsoft/generators
Basic support for generators as iterators
2 parents 57f3e04 + 171b385 commit 675679a

File tree

349 files changed

+4060
-643
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

349 files changed

+4060
-643
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "typescript",
33
"author": "Microsoft Corp.",
44
"homepage": "http://typescriptlang.org/",
5-
"version": "1.5.2",
5+
"version": "1.5.3",
66
"license": "Apache-2.0",
77
"description": "TypeScript is a language for application scale JavaScript development",
88
"keywords": [

src/compiler/checker.ts

Lines changed: 367 additions & 220 deletions
Large diffs are not rendered by default.

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ module ts {
120120
Unterminated_template_literal: { code: 1160, category: DiagnosticCategory.Error, key: "Unterminated template literal." },
121121
Unterminated_regular_expression_literal: { code: 1161, category: DiagnosticCategory.Error, key: "Unterminated regular expression literal." },
122122
An_object_member_cannot_be_declared_optional: { code: 1162, category: DiagnosticCategory.Error, key: "An object member cannot be declared optional." },
123-
yield_expression_must_be_contained_within_a_generator_declaration: { code: 1163, category: DiagnosticCategory.Error, key: "'yield' expression must be contained_within a generator declaration." },
123+
A_yield_expression_is_only_allowed_in_a_generator_body: { code: 1163, category: DiagnosticCategory.Error, key: "A 'yield' expression is only allowed in a generator body." },
124124
Computed_property_names_are_not_allowed_in_enums: { code: 1164, category: DiagnosticCategory.Error, key: "Computed property names are not allowed in enums." },
125125
A_computed_property_name_in_an_ambient_context_must_directly_refer_to_a_built_in_symbol: { code: 1165, category: DiagnosticCategory.Error, key: "A computed property name in an ambient context must directly refer to a built-in symbol." },
126126
A_computed_property_name_in_a_class_property_declaration_must_directly_refer_to_a_built_in_symbol: { code: 1166, category: DiagnosticCategory.Error, key: "A computed property name in a class property declaration must directly refer to a built-in symbol." },
@@ -174,6 +174,9 @@ module ts {
174174
Type_expected_0_is_a_reserved_word_in_strict_mode: { code: 1215, category: DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode" },
175175
Type_expected_0_is_a_reserved_word_in_strict_mode_Class_definitions_are_automatically_in_strict_mode: { code: 1216, category: DiagnosticCategory.Error, key: "Type expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode." },
176176
Export_assignment_is_not_supported_when_module_flag_is_system: { code: 1218, category: DiagnosticCategory.Error, key: "Export assignment is not supported when '--module' flag is 'system'." },
177+
Generators_are_only_available_when_targeting_ECMAScript_6_or_higher: { code: 1219, category: DiagnosticCategory.Error, key: "Generators are only available when targeting ECMAScript 6 or higher." },
178+
Generators_are_not_allowed_in_an_ambient_context: { code: 1220, category: DiagnosticCategory.Error, key: "Generators are not allowed in an ambient context." },
179+
An_overload_signature_cannot_be_declared_as_a_generator: { code: 1221, category: DiagnosticCategory.Error, key: "An overload signature cannot be declared as a generator." },
177180
_0_tag_already_specified: { code: 1219, category: DiagnosticCategory.Error, key: "'{0}' tag already specified." },
178181
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
179182
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
@@ -366,6 +369,8 @@ module ts {
366369
A_rest_element_cannot_contain_a_binding_pattern: { code: 2501, category: DiagnosticCategory.Error, key: "A rest element cannot contain a binding pattern." },
367370
_0_is_referenced_directly_or_indirectly_in_its_own_type_annotation: { code: 2502, category: DiagnosticCategory.Error, key: "'{0}' is referenced directly or indirectly in its own type annotation." },
368371
Cannot_find_namespace_0: { code: 2503, category: DiagnosticCategory.Error, key: "Cannot find namespace '{0}'." },
372+
No_best_common_type_exists_among_yield_expressions: { code: 2504, category: DiagnosticCategory.Error, key: "No best common type exists among yield expressions." },
373+
A_generator_cannot_have_a_void_type_annotation: { code: 2505, category: DiagnosticCategory.Error, key: "A generator cannot have a 'void' type annotation." },
369374
Import_declaration_0_is_using_private_name_1: { code: 4000, category: DiagnosticCategory.Error, key: "Import declaration '{0}' is using private name '{1}'." },
370375
Type_parameter_0_of_exported_class_has_or_is_using_private_name_1: { code: 4002, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported class has or is using private name '{1}'." },
371376
Type_parameter_0_of_exported_interface_has_or_is_using_private_name_1: { code: 4004, category: DiagnosticCategory.Error, key: "Type parameter '{0}' of exported interface has or is using private name '{1}'." },
@@ -523,6 +528,7 @@ module ts {
523528
_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: 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." },
524529
_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: 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." },
525530
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: 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." },
531+
Generator_implicitly_has_type_0_because_it_does_not_yield_any_values_Consider_supplying_a_return_type: { code: 7025, category: DiagnosticCategory.Error, key: "Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type." },
526532
You_cannot_rename_this_element: { code: 8000, category: DiagnosticCategory.Error, key: "You cannot rename this element." },
527533
You_cannot_rename_elements_that_are_defined_in_the_standard_TypeScript_library: { code: 8001, category: DiagnosticCategory.Error, key: "You cannot rename elements that are defined in the standard TypeScript library." },
528534
import_can_only_be_used_in_a_ts_file: { code: 8002, category: DiagnosticCategory.Error, key: "'import ... =' can only be used in a .ts file." },
@@ -540,8 +546,6 @@ module ts {
540546
enum_declarations_can_only_be_used_in_a_ts_file: { code: 8015, category: DiagnosticCategory.Error, key: "'enum declarations' can only be used in a .ts file." },
541547
type_assertion_expressions_can_only_be_used_in_a_ts_file: { code: 8016, category: DiagnosticCategory.Error, key: "'type assertion expressions' can only be used in a .ts file." },
542548
decorators_can_only_be_used_in_a_ts_file: { code: 8017, category: DiagnosticCategory.Error, key: "'decorators' can only be used in a .ts file." },
543-
yield_expressions_are_not_currently_supported: { code: 9000, category: DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." },
544-
Generators_are_not_currently_supported: { code: 9001, category: DiagnosticCategory.Error, key: "Generators are not currently supported." },
545549
Only_identifiers_Slashqualified_names_with_optional_type_arguments_are_currently_supported_in_a_class_extends_clauses: { code: 9002, category: DiagnosticCategory.Error, key: "Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses." },
546550
class_expressions_are_not_currently_supported: { code: 9003, category: DiagnosticCategory.Error, key: "'class' expressions are not currently supported." },
547551
class_declarations_are_only_supported_directly_inside_a_module_or_as_a_top_level_declaration: { code: 9004, category: DiagnosticCategory.Error, key: "'class' declarations are only supported directly inside a module or as a top level declaration." },

src/compiler/diagnosticMessages.json

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@
467467
"category": "Error",
468468
"code": 1162
469469
},
470-
"'yield' expression must be contained_within a generator declaration.": {
470+
"A 'yield' expression is only allowed in a generator body.": {
471471
"category": "Error",
472472
"code": 1163
473473
},
@@ -682,7 +682,21 @@
682682
"Export assignment is not supported when '--module' flag is 'system'.": {
683683
"category": "Error",
684684
"code": 1218
685+
},
686+
"Generators are only available when targeting ECMAScript 6 or higher.": {
687+
"category": "Error",
688+
"code": 1219
685689
},
690+
"Generators are not allowed in an ambient context.": {
691+
"category": "Error",
692+
"code": 1220
693+
},
694+
"An overload signature cannot be declared as a generator.": {
695+
"category": "Error",
696+
"code": 1221
697+
},
698+
699+
686700
"'{0}' tag already specified.": {
687701
"category": "Error",
688702
"code": 1219
@@ -1442,15 +1456,23 @@
14421456
"A rest element cannot contain a binding pattern.": {
14431457
"category": "Error",
14441458
"code": 2501
1445-
},
1459+
},
14461460
"'{0}' is referenced directly or indirectly in its own type annotation.": {
14471461
"category": "Error",
14481462
"code": 2502
1449-
},
1463+
},
14501464
"Cannot find namespace '{0}'.": {
14511465
"category": "Error",
14521466
"code": 2503
1467+
},
1468+
"No best common type exists among yield expressions.": {
1469+
"category": "Error",
1470+
"code": 2504
14531471
},
1472+
"A generator cannot have a 'void' type annotation.": {
1473+
"category": "Error",
1474+
"code": 2505
1475+
},
14541476

14551477
"Import declaration '{0}' is using private name '{1}'.": {
14561478
"category": "Error",
@@ -2083,6 +2105,10 @@
20832105
"category": "Error",
20842106
"code": 7024
20852107
},
2108+
"Generator implicitly has type '{0}' because it does not yield any values. Consider supplying a return type.": {
2109+
"category": "Error",
2110+
"code": 7025
2111+
},
20862112
"You cannot rename this element.": {
20872113
"category": "Error",
20882114
"code": 8000
@@ -2152,14 +2178,6 @@
21522178
"code": 8017
21532179
},
21542180

2155-
"'yield' expressions are not currently supported.": {
2156-
"category": "Error",
2157-
"code": 9000
2158-
},
2159-
"Generators are not currently supported.": {
2160-
"category": "Error",
2161-
"code": 9001
2162-
},
21632181
"Only identifiers/qualified-names with optional type arguments are currently supported in a class 'extends' clauses.": {
21642182
"category": "Error",
21652183
"code": 9002

src/compiler/parser.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4143,7 +4143,20 @@ module ts {
41434143
property.name = name;
41444144
property.questionToken = questionToken;
41454145
property.type = parseTypeAnnotation();
4146-
property.initializer = allowInAnd(parseNonParameterInitializer);
4146+
4147+
// For instance properties specifically, since they are evaluated inside the constructor,
4148+
// we do *not * want to parse yield expressions, so we specifically turn the yield context
4149+
// off. The grammar would look something like this:
4150+
//
4151+
// MemberVariableDeclaration[Yield]:
4152+
// AccessibilityModifier_opt PropertyName TypeAnnotation_opt Initialiser_opt[In];
4153+
// AccessibilityModifier_opt static_opt PropertyName TypeAnnotation_opt Initialiser_opt[In, ?Yield];
4154+
//
4155+
// The checker may still error in the static case to explicitly disallow the yield expression.
4156+
property.initializer = modifiers && modifiers.flags & NodeFlags.Static
4157+
? allowInAnd(parseNonParameterInitializer)
4158+
: doOutsideOfContext(ParserContextFlags.Yield | ParserContextFlags.DisallowIn, parseNonParameterInitializer);
4159+
41474160
parseSemicolon();
41484161
return finishNode(property);
41494162
}

src/compiler/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ module ts {
693693

694694
export interface YieldExpression extends Expression {
695695
asteriskToken?: Node;
696-
expression: Expression;
696+
expression?: Expression;
697697
}
698698

699699
export interface BinaryExpression extends Expression {
@@ -1676,6 +1676,13 @@ module ts {
16761676
numberIndexType: Type; // Numeric index type
16771677
}
16781678

1679+
// Just a place to cache element types of iterables and iterators
1680+
/* @internal */
1681+
export interface IterableOrIteratorType extends ObjectType, UnionType {
1682+
iterableElementType?: Type;
1683+
iteratorElementType?: Type;
1684+
}
1685+
16791686
// Type parameters (TypeFlags.TypeParameter)
16801687
export interface TypeParameter extends Type {
16811688
constraint: Type; // Constraint

0 commit comments

Comments
 (0)