Skip to content

Commit 1b29b51

Browse files
Merge pull request #1268 from Microsoft/generators
Report errors for using yield/generators right now.
2 parents 44e6bcf + d37368e commit 1b29b51

File tree

73 files changed

+259
-445
lines changed

Some content is hidden

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

73 files changed

+259
-445
lines changed

Diff for: src/compiler/diagnosticInformationMap.generated.ts

+2
Original file line numberDiff line numberDiff line change
@@ -414,5 +414,7 @@ module ts {
414414
_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." },
415415
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." },
416416
You_cannot_rename_this_element: { code: 8000, category: DiagnosticCategory.Error, key: "You cannot rename this element." },
417+
yield_expressions_are_not_currently_supported: { code: 9000, category: DiagnosticCategory.Error, key: "'yield' expressions are not currently supported." },
418+
generators_are_not_currently_supported: { code: 9001, category: DiagnosticCategory.Error, key: "'generators' are not currently supported." },
417419
};
418420
}

Diff for: src/compiler/diagnosticMessages.json

+8
Original file line numberDiff line numberDiff line change
@@ -1654,5 +1654,13 @@
16541654
"You cannot rename this element.": {
16551655
"category": "Error",
16561656
"code": 8000
1657+
},
1658+
"'yield' expressions are not currently supported.": {
1659+
"category": "Error",
1660+
"code": 9000
1661+
},
1662+
"'generators' are not currently supported.": {
1663+
"category": "Error",
1664+
"code": 9001
16571665
}
16581666
}

Diff for: src/compiler/parser.ts

+45-42
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,15 @@ module ts {
11551155
return false;
11561156
}
11571157

1158+
function parseOptionalToken(t: SyntaxKind): Node {
1159+
if (token === t) {
1160+
var node = createNode(t);
1161+
nextToken();
1162+
return finishNode(node);
1163+
}
1164+
return undefined;
1165+
}
1166+
11581167
function canParseSemicolon() {
11591168
// If there's a real semicolon, then we can always parse it out.
11601169
if (token === SyntaxKind.SemicolonToken) {
@@ -2202,10 +2211,7 @@ module ts {
22022211

22032212
if (!scanner.hasPrecedingLineBreak() &&
22042213
(token === SyntaxKind.AsteriskToken || isStartOfExpression())) {
2205-
if (parseOptional(SyntaxKind.AsteriskToken)) {
2206-
node.flags = NodeFlags.YieldStar;
2207-
}
2208-
2214+
node.asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
22092215
node.expression = parseAssignmentExpression();
22102216
return finishNode(node);
22112217
}
@@ -2255,7 +2261,7 @@ module ts {
22552261
}
22562262
else {
22572263
// If not, we're probably better off bailing out and returning a bogus function expression.
2258-
return makeFunctionExpression(SyntaxKind.ArrowFunction, pos, /* name */ undefined, sig, createMissingNode());
2264+
return makeFunctionExpression(SyntaxKind.ArrowFunction, pos, /*asteriskToken:*/ undefined, /*name:*/ undefined, sig, createMissingNode());
22592265
}
22602266
}
22612267

@@ -2395,7 +2401,7 @@ module ts {
23952401
body = parseAssignmentExpression();
23962402
}
23972403

2398-
return makeFunctionExpression(SyntaxKind.ArrowFunction, pos, /* name */ undefined, sig, body);
2404+
return makeFunctionExpression(SyntaxKind.ArrowFunction, pos, /*asteriskToken:*/ undefined, /*name:*/ undefined, sig, body);
23992405
}
24002406

24012407
function parseConditionalExpression(): Expression {
@@ -2731,26 +2737,23 @@ module ts {
27312737

27322738
function parsePropertyAssignment(): Declaration {
27332739
var nodePos = scanner.getStartPos();
2734-
var isGenerator = parseOptional(SyntaxKind.AsteriskToken);
2740+
var asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
27352741
var tokenIsIdentifier = isIdentifier();
27362742
var nameToken = token;
27372743
var propertyName = parsePropertyName();
27382744
var node: Declaration;
2739-
if (isGenerator || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
2745+
if (asteriskToken || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
27402746
node = <PropertyDeclaration>createNode(SyntaxKind.PropertyAssignment, nodePos);
27412747
node.name = propertyName;
2742-
if (isGenerator) {
2743-
node.flags |= NodeFlags.Generator;
2744-
}
2745-
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator);
2748+
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ !!asteriskToken);
27462749

2747-
var body = parseFunctionBlock(isGenerator, /* ignoreMissingOpenBrace */ false);
2750+
var body = parseFunctionBlock(!!asteriskToken, /* ignoreMissingOpenBrace */ false);
27482751
// do not propagate property name as name for function expression
27492752
// for scenarios like
27502753
// var x = 1;
27512754
// var y = { x() { } }
27522755
// otherwise this will bring y.x into the scope of x which is incorrect
2753-
(<PropertyDeclaration>node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body);
2756+
(<PropertyDeclaration>node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, asteriskToken, undefined, sig, body);
27542757
return finishNode(node);
27552758
}
27562759

@@ -2808,24 +2811,21 @@ module ts {
28082811

28092812
var pos = getNodePos();
28102813
parseExpected(SyntaxKind.FunctionKeyword);
2811-
var isGenerator = parseOptional(SyntaxKind.AsteriskToken);
2812-
var name = isGenerator ? doInYieldContext(parseOptionalIdentifier) : parseOptionalIdentifier();
2813-
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator);
2814+
var asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
2815+
var name = asteriskToken ? doInYieldContext(parseOptionalIdentifier) : parseOptionalIdentifier();
2816+
var sig = parseSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ !!asteriskToken);
28142817

2815-
var body = parseFunctionBlock(/*allowYield:*/ isGenerator, /* ignoreMissingOpenBrace */ false);
2816-
return makeFunctionExpression(SyntaxKind.FunctionExpression, pos, name, sig, body, isGenerator ? NodeFlags.Generator : undefined);
2818+
var body = parseFunctionBlock(/*allowYield:*/ !!asteriskToken, /* ignoreMissingOpenBrace */ false);
2819+
return makeFunctionExpression(SyntaxKind.FunctionExpression, pos, asteriskToken, name, sig, body);
28172820
}
28182821

28192822
function parseOptionalIdentifier() {
28202823
return isIdentifier() ? parseIdentifier() : undefined;
28212824
}
28222825

2823-
function makeFunctionExpression(kind: SyntaxKind, pos: number, name: Identifier, sig: ParsedSignature, body: Node, flags?: NodeFlags): FunctionExpression {
2826+
function makeFunctionExpression(kind: SyntaxKind, pos: number, asteriskToken: Node, name: Identifier, sig: ParsedSignature, body: Node): FunctionExpression {
28242827
var node = <FunctionExpression>createNode(kind, pos);
2825-
if (flags) {
2826-
node.flags = flags;
2827-
}
2828-
2828+
node.asteriskToken = asteriskToken;
28292829
node.name = name;
28302830
node.typeParameters = sig.typeParameters;
28312831
node.parameters = sig.parameters;
@@ -3292,14 +3292,10 @@ module ts {
32923292
var node = <FunctionLikeDeclaration>createNode(SyntaxKind.FunctionDeclaration, fullStart);
32933293
setModifiers(node, modifiers);
32943294
parseExpected(SyntaxKind.FunctionKeyword);
3295-
var isGenerator = parseOptional(SyntaxKind.AsteriskToken);
3296-
if (isGenerator) {
3297-
node.flags |= NodeFlags.Generator;
3298-
}
3299-
3295+
node.asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
33003296
node.name = parseIdentifier();
3301-
fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator, node);
3302-
node.body = parseFunctionBlockOrSemicolon(isGenerator);
3297+
fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ !!node.asteriskToken, node);
3298+
node.body = parseFunctionBlockOrSemicolon(!!node.asteriskToken);
33033299
return finishNode(node);
33043300
}
33053301

@@ -3314,27 +3310,24 @@ module ts {
33143310

33153311
function parsePropertyMemberDeclaration(fullStart: number, modifiers: ModifiersArray): Declaration {
33163312
var flags = modifiers ? modifiers.flags : 0;
3317-
var isGenerator = parseOptional(SyntaxKind.AsteriskToken);
3318-
if (isGenerator) {
3319-
flags |= NodeFlags.Generator;
3320-
}
3321-
3313+
var asteriskToken = parseOptionalToken(SyntaxKind.AsteriskToken);
33223314
var name = parsePropertyName();
33233315
if (parseOptional(SyntaxKind.QuestionToken)) {
33243316
// Note: this is not legal as per the grammar. But we allow it in the parser and
33253317
// report an error in the grammar checker.
33263318
flags |= NodeFlags.QuestionMark;
33273319
}
33283320

3329-
if (isGenerator || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
3321+
if (asteriskToken || token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
33303322
var method = <MethodDeclaration>createNode(SyntaxKind.Method, fullStart);
33313323
setModifiers(method, modifiers);
33323324
if (flags) {
33333325
method.flags = flags;
33343326
}
3327+
method.asteriskToken = asteriskToken;
33353328
method.name = name;
3336-
fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ isGenerator, method);
3337-
method.body = parseFunctionBlockOrSemicolon(isGenerator);
3329+
fillSignature(SyntaxKind.CallSignature, SyntaxKind.ColonToken, /* returnTokenRequired */ false, /*yieldAndGeneratorParameterContext:*/ !!asteriskToken, method);
3330+
method.body = parseFunctionBlockOrSemicolon(!!asteriskToken);
33383331
return finishNode(method);
33393332
}
33403333
else {
@@ -4302,12 +4295,20 @@ module ts {
43024295
function checkFunctionDeclaration(node: FunctionLikeDeclaration) {
43034296
return checkAnyParsedSignature(node) ||
43044297
checkFunctionName(node.name) ||
4305-
checkForBodyInAmbientContext(node.body, /*isConstructor:*/ false);
4298+
checkForBodyInAmbientContext(node.body, /*isConstructor:*/ false) ||
4299+
checkForGenerator(node);
4300+
}
4301+
4302+
function checkForGenerator(node: FunctionLikeDeclaration) {
4303+
if (node.asteriskToken) {
4304+
return grammarErrorOnNode(node.asteriskToken, Diagnostics.generators_are_not_currently_supported);
4305+
}
43064306
}
43074307

43084308
function checkFunctionExpression(node: FunctionExpression) {
43094309
return checkAnyParsedSignature(node) ||
4310-
checkFunctionName(node.name);
4310+
checkFunctionName(node.name) ||
4311+
checkForGenerator(node);
43114312
}
43124313

43134314
function checkFunctionName(name: Node) {
@@ -4386,7 +4387,8 @@ module ts {
43864387
function checkMethod(node: MethodDeclaration) {
43874388
return checkAnyParsedSignature(node) ||
43884389
checkForBodyInAmbientContext(node.body, /*isConstructor:*/ false) ||
4389-
(node.parent.kind === SyntaxKind.ClassDeclaration && checkForInvalidQuestionMark(node, Diagnostics.A_class_member_cannot_be_declared_optional));
4390+
(node.parent.kind === SyntaxKind.ClassDeclaration && checkForInvalidQuestionMark(node, Diagnostics.A_class_member_cannot_be_declared_optional)) ||
4391+
checkForGenerator(node);
43904392
}
43914393

43924394
function checkForBodyInAmbientContext(body: Block | Expression, isConstructor: boolean): boolean {
@@ -4963,6 +4965,7 @@ module ts {
49634965
if (!(node.parserContextFlags & ParserContextFlags.Yield)) {
49644966
return grammarErrorOnFirstToken(node, Diagnostics.yield_expression_must_be_contained_within_a_generator_declaration);
49654967
}
4968+
return grammarErrorOnFirstToken(node, Diagnostics.yield_expressions_are_not_currently_supported);
49664969
}
49674970
}
49684971

Diff for: src/compiler/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ module ts {
271271
Let = 0x00000800, // Variable declaration
272272
Const = 0x00001000, // Variable declaration
273273
OctalLiteral = 0x00002000,
274-
Generator = 0x00004000,
275-
YieldStar = 0x00008000,
276274

277275
Modifier = Export | Ambient | Public | Private | Protected | Static,
278276
AccessibilityModifier = Public | Private | Protected,
@@ -377,6 +375,7 @@ module ts {
377375
* FunctionExpression
378376
*/
379377
export interface FunctionLikeDeclaration extends Declaration, ParsedSignature {
378+
asteriskToken?: Node;
380379
body?: Block | Expression;
381380
}
382381

@@ -442,6 +441,7 @@ module ts {
442441
}
443442

444443
export interface YieldExpression extends Expression {
444+
asteriskToken?: Node;
445445
expression: Expression;
446446
}
447447

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration10_es6.ts(1,10): error TS9001: 'generators' are not currently supported.
2+
3+
4+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration10_es6.ts (1 errors) ====
5+
function * foo(a = yield => yield) {
6+
~
7+
!!! error TS9001: 'generators' are not currently supported.
8+
}

Diff for: tests/baselines/reference/FunctionDeclaration10_es6.js

-8
This file was deleted.

Diff for: tests/baselines/reference/FunctionDeclaration10_es6.types

-8
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration11_es6.ts(1,10): error TS9001: 'generators' are not currently supported.
2+
3+
4+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration11_es6.ts (1 errors) ====
5+
function * yield() {
6+
~
7+
!!! error TS9001: 'generators' are not currently supported.
8+
}

Diff for: tests/baselines/reference/FunctionDeclaration11_es6.js

-7
This file was deleted.

Diff for: tests/baselines/reference/FunctionDeclaration11_es6.types

-4
This file was deleted.

Diff for: tests/baselines/reference/FunctionDeclaration13_es6.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration13_es6.ts(1,10): error TS9001: 'generators' are not currently supported.
12
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration13_es6.ts(3,11): error TS2304: Cannot find name 'yield'.
23

34

4-
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration13_es6.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration13_es6.ts (2 errors) ====
56
function * foo() {
7+
~
8+
!!! error TS9001: 'generators' are not currently supported.
69
// Legal to use 'yield' in a type context.
710
var v: yield;
811
~~~~~

Diff for: tests/baselines/reference/FunctionDeclaration13_es6.js

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration1_es6.ts(1,10): error TS9001: 'generators' are not currently supported.
2+
3+
4+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration1_es6.ts (1 errors) ====
5+
function * foo() {
6+
~
7+
!!! error TS9001: 'generators' are not currently supported.
8+
}

Diff for: tests/baselines/reference/FunctionDeclaration1_es6.js

-7
This file was deleted.

Diff for: tests/baselines/reference/FunctionDeclaration1_es6.types

-4
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration6_es6.ts(1,9): error TS9001: 'generators' are not currently supported.
12
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration6_es6.ts(1,18): error TS2304: Cannot find name 'yield'.
23

34

4-
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration6_es6.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration6_es6.ts (2 errors) ====
56
function*foo(a = yield) {
7+
~
8+
!!! error TS9001: 'generators' are not currently supported.
69
~~~~~
710
!!! error TS2304: Cannot find name 'yield'.
811
}

Diff for: tests/baselines/reference/FunctionDeclaration6_es6.js

-8
This file was deleted.

Diff for: tests/baselines/reference/FunctionDeclaration7_es6.errors.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration7_es6.ts(1,9): error TS9001: 'generators' are not currently supported.
12
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration7_es6.ts(3,20): error TS2304: Cannot find name 'yield'.
23

34

4-
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration7_es6.ts (1 errors) ====
5+
==== tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration7_es6.ts (2 errors) ====
56
function*bar() {
7+
~
8+
!!! error TS9001: 'generators' are not currently supported.
69
// 'yield' here is an identifier, and not a yield expression.
710
function*foo(a = yield) {
811
~~~~~

0 commit comments

Comments
 (0)