diff --git a/crates/rome_js_parser/src/syntax/expr.rs b/crates/rome_js_parser/src/syntax/expr.rs index 0489ed6ef7c..00c06eb39eb 100644 --- a/crates/rome_js_parser/src/syntax/expr.rs +++ b/crates/rome_js_parser/src/syntax/expr.rs @@ -244,7 +244,12 @@ fn parse_assignment_expression_or_higher_base( p: &mut Parser, context: ExpressionContext, ) -> ParsedSyntax { - if p.state.in_generator() && p.at(T![yield]) { + // test reparse_yield_as_identifier + // // SCRIPT + // function foo() { yield *bar; } + // function bar() { yield; } + // function baz() { yield } + if p.at(T![yield]) && (p.state.in_generator() || is_nth_at_expression(p, 1)) { return Present(parse_yield_expression(p, context)); } @@ -347,25 +352,45 @@ fn is_assign_token(kind: JsSyntaxKind) -> bool { // } fn parse_yield_expression(p: &mut Parser, context: ExpressionContext) -> CompletedMarker { let m = p.start(); + let yield_range = p.cur_range(); p.expect(T![yield]); + // test yield_in_generator_function + // function* foo() { yield 10; } + // function* foo() { yield *bar; } + // function* foo() { yield; } if !is_semi(p, 0) && (p.at(T![*]) || is_at_expression(p)) { let argument = p.start(); p.eat(T![*]); parse_assignment_expression_or_higher(p, context.and_object_expression_allowed(true)).ok(); - argument.complete(p, JS_YIELD_ARGUMENT); } let mut yield_expr = m.complete(p, JS_YIELD_EXPRESSION); - if !p.state.is_top_level() && !p.state.in_function() { + // test_err yield_at_top_level_module + // yield 10; + + // test_err yield_at_top_level_script + // // SCRIPT + // yield 10; + + // test_err yield_in_non_generator_function_script + // // SCRIPT + // function foo() { yield bar; } + // function foo() { yield 10; } + + // test_err yield_in_non_generator_function_module + // function foo() { yield; } + // function foo() { yield foo; } + // function foo() { yield *foo; } + if !(p.state.in_generator() && p.state.in_function()) { // test_err yield_expr_in_parameter_initializer // function* test(a = yield "test") {} // function test2(a = yield "test") {} p.error(p.err_builder( "`yield` is only allowed within generator functions.", - yield_expr.range(p), + yield_range, )); yield_expr.change_to_unknown(p); } diff --git a/crates/rome_js_parser/test_data/inline/err/function_decl_err.rast b/crates/rome_js_parser/test_data/inline/err/function_decl_err.rast index ef63eb6ee74..297a187394d 100644 --- a/crates/rome_js_parser/test_data/inline/err/function_decl_err.rast +++ b/crates/rome_js_parser/test_data/inline/err/function_decl_err.rast @@ -134,21 +134,17 @@ JsModule { JsExpressionStatement { expression: JsUnknownExpression { items: [ - JsUnknown { - items: [ - IDENT@119..126 "yield" [Newline("\n")] [Whitespace(" ")], - ], + YIELD_KW@119..126 "yield" [Newline("\n")] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@126..130 "foo3" [] [], + }, + }, }, ], }, - semicolon_token: missing (optional), - }, - JsExpressionStatement { - expression: JsIdentifierExpression { - name: JsReferenceIdentifier { - value_token: IDENT@126..130 "foo3" [] [], - }, - }, semicolon_token: SEMICOLON@130..131 ";" [] [], }, JsUnknownStatement { @@ -358,17 +354,16 @@ JsModule { 1: JS_DIRECTIVE_LIST@118..118 2: JS_STATEMENT_LIST@118..118 3: R_CURLY@118..119 "}" [] [] - 7: JS_EXPRESSION_STATEMENT@119..126 - 0: JS_UNKNOWN_EXPRESSION@119..126 - 0: JS_UNKNOWN@119..126 - 0: IDENT@119..126 "yield" [Newline("\n")] [Whitespace(" ")] - 1: (empty) - 8: JS_EXPRESSION_STATEMENT@126..131 - 0: JS_IDENTIFIER_EXPRESSION@126..130 - 0: JS_REFERENCE_IDENTIFIER@126..130 - 0: IDENT@126..130 "foo3" [] [] + 7: JS_EXPRESSION_STATEMENT@119..131 + 0: JS_UNKNOWN_EXPRESSION@119..130 + 0: YIELD_KW@119..126 "yield" [Newline("\n")] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@126..130 + 0: (empty) + 1: JS_IDENTIFIER_EXPRESSION@126..130 + 0: JS_REFERENCE_IDENTIFIER@126..130 + 0: IDENT@126..130 "foo3" [] [] 1: SEMICOLON@130..131 ";" [] [] - 9: JS_UNKNOWN_STATEMENT@131..159 + 8: JS_UNKNOWN_STATEMENT@131..159 0: FUNCTION_KW@131..141 "function" [Newline("\n")] [Whitespace(" ")] 1: JS_IDENTIFIER_BINDING@141..146 0: IDENT@141..146 "test2" [] [] @@ -385,7 +380,7 @@ JsModule { 1: JS_DIRECTIVE_LIST@158..158 2: JS_STATEMENT_LIST@158..158 3: R_CURLY@158..159 "}" [] [] - 10: JS_FUNCTION_DECLARATION@159..183 + 9: JS_FUNCTION_DECLARATION@159..183 0: (empty) 1: FUNCTION_KW@159..169 "function" [Newline("\n")] [Whitespace(" ")] 2: (empty) @@ -408,7 +403,7 @@ JsModule { 1: JS_DIRECTIVE_LIST@182..182 2: JS_STATEMENT_LIST@182..182 3: R_CURLY@182..183 "}" [] [] - 11: JS_FUNCTION_DECLARATION@183..207 + 10: JS_FUNCTION_DECLARATION@183..207 0: (empty) 1: FUNCTION_KW@183..193 "function" [Newline("\n")] [Whitespace(" ")] 2: (empty) @@ -512,7 +507,7 @@ function_decl_err.js:6:17 parse ━━━━━━━━━━━━━━━━ -- function_decl_err.js:8:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Illegal use of reserved keyword `yield` as an identifier in strict mode + × `yield` is only allowed within generator functions. 6 │ async function *() {} 7 │ function *foo2() {} @@ -521,36 +516,6 @@ function_decl_err.js:8:1 parse ━━━━━━━━━━━━━━━━ 9 │ function test2(): number {} 10 │ function foo4(await) {} --- -function_decl_err.js:8:7 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × Expected a semicolon or an implicit semicolon after a statement, but found none - - 6 │ async function *() {} - 7 │ function *foo2() {} - > 8 │ yield foo3; - │ ^^^^ - 9 │ function test2(): number {} - 10 │ function foo4(await) {} - - i An explicit or implicit semicolon is expected here... - - 6 │ async function *() {} - 7 │ function *foo2() {} - > 8 │ yield foo3; - │ ^^^^ - 9 │ function test2(): number {} - 10 │ function foo4(await) {} - - i ...Which is required to end this statement - - 6 │ async function *() {} - 7 │ function *foo2() {} - > 8 │ yield foo3; - │ ^^^^^^^^^^ - 9 │ function test2(): number {} - 10 │ function foo4(await) {} - -- function_decl_err.js:9:17 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ diff --git a/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.js b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.js new file mode 100644 index 00000000000..278011d9343 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.js @@ -0,0 +1 @@ +yield 10; diff --git a/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.rast b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.rast new file mode 100644 index 00000000000..bce77d9cc9e --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_module.rast @@ -0,0 +1,46 @@ +JsModule { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + items: JsModuleItemList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@0..6 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@6..8 "10" [] [], + }, + }, + ], + }, + semicolon_token: SEMICOLON@8..9 ";" [] [], + }, + ], + eof_token: EOF@9..10 "" [Newline("\n")] [], +} + +0: JS_MODULE@0..10 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_MODULE_ITEM_LIST@0..9 + 0: JS_EXPRESSION_STATEMENT@0..9 + 0: JS_UNKNOWN_EXPRESSION@0..8 + 0: YIELD_KW@0..6 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@6..8 + 0: (empty) + 1: JS_NUMBER_LITERAL_EXPRESSION@6..8 + 0: JS_NUMBER_LITERAL@6..8 "10" [] [] + 1: SEMICOLON@8..9 ";" [] [] + 3: EOF@9..10 "" [Newline("\n")] [] +-- +yield_at_top_level_module.js:1:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + > 1 │ yield 10; + │ ^^^^^ + 2 │ + +-- +yield 10; diff --git a/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.js b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.js new file mode 100644 index 00000000000..7b6720f0e30 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.js @@ -0,0 +1,2 @@ +// SCRIPT +yield 10; diff --git a/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.rast b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.rast new file mode 100644 index 00000000000..65d1f656361 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_at_top_level_script.rast @@ -0,0 +1,48 @@ +JsScript { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@0..16 "yield" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@16..18 "10" [] [], + }, + }, + ], + }, + semicolon_token: SEMICOLON@18..19 ";" [] [], + }, + ], + eof_token: EOF@19..20 "" [Newline("\n")] [], +} + +0: JS_SCRIPT@0..20 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_STATEMENT_LIST@0..19 + 0: JS_EXPRESSION_STATEMENT@0..19 + 0: JS_UNKNOWN_EXPRESSION@0..18 + 0: YIELD_KW@0..16 "yield" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@16..18 + 0: (empty) + 1: JS_NUMBER_LITERAL_EXPRESSION@16..18 + 0: JS_NUMBER_LITERAL@16..18 "10" [] [] + 1: SEMICOLON@18..19 ";" [] [] + 3: EOF@19..20 "" [Newline("\n")] [] +-- +yield_at_top_level_script.js:2:1 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + 1 │ // SCRIPT + > 2 │ yield 10; + │ ^^^^^ + 3 │ + +-- +// SCRIPT +yield 10; diff --git a/crates/rome_js_parser/test_data/inline/err/yield_expr_in_parameter_initializer.rast b/crates/rome_js_parser/test_data/inline/err/yield_expr_in_parameter_initializer.rast index 3828dfcac3f..f1a10b2d5fe 100644 --- a/crates/rome_js_parser/test_data/inline/err/yield_expr_in_parameter_initializer.rast +++ b/crates/rome_js_parser/test_data/inline/err/yield_expr_in_parameter_initializer.rast @@ -66,21 +66,17 @@ JsModule { eq_token: EQ@53..55 "=" [] [Whitespace(" ")], expression: JsUnknownExpression { items: [ - JsUnknown { - items: [ - IDENT@55..61 "yield" [] [Whitespace(" ")], - ], + YIELD_KW@55..61 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsStringLiteralExpression { + value_token: JS_STRING_LITERAL@61..67 "\"test\"" [] [], + }, }, ], }, }, }, - missing separator, - JsUnknownParameter { - items: [ - JS_STRING_LITERAL@61..67 "\"test\"" [] [], - ], - }, ], r_paren_token: R_PAREN@67..69 ")" [] [Whitespace(" ")], }, @@ -140,19 +136,19 @@ JsModule { 5: JS_PARAMETERS@50..69 0: L_PAREN@50..51 "(" [] [] 1: JS_PARAMETER_LIST@51..67 - 0: JS_FORMAL_PARAMETER@51..61 + 0: JS_FORMAL_PARAMETER@51..67 0: JS_IDENTIFIER_BINDING@51..53 0: IDENT@51..53 "a" [] [Whitespace(" ")] 1: (empty) 2: (empty) - 3: JS_INITIALIZER_CLAUSE@53..61 + 3: JS_INITIALIZER_CLAUSE@53..67 0: EQ@53..55 "=" [] [Whitespace(" ")] - 1: JS_UNKNOWN_EXPRESSION@55..61 - 0: JS_UNKNOWN@55..61 - 0: IDENT@55..61 "yield" [] [Whitespace(" ")] - 1: (empty) - 2: JS_UNKNOWN_PARAMETER@61..67 - 0: JS_STRING_LITERAL@61..67 "\"test\"" [] [] + 1: JS_UNKNOWN_EXPRESSION@55..67 + 0: YIELD_KW@55..61 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@61..67 + 0: (empty) + 1: JS_STRING_LITERAL_EXPRESSION@61..67 + 0: JS_STRING_LITERAL@61..67 "\"test\"" [] [] 2: R_PAREN@67..69 ")" [] [Whitespace(" ")] 6: (empty) 7: JS_FUNCTION_BODY@69..71 @@ -167,32 +163,20 @@ yield_expr_in_parameter_initializer.js:1:20 parse ━━━━━━━━━━ × `yield` is only allowed within generator functions. > 1 │ function* test(a = yield "test") {} - │ ^^^^^^^^^^^^ + │ ^^^^^ 2 │ function test2(a = yield "test") {} 3 │ -- yield_expr_in_parameter_initializer.js:2:20 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - × Illegal use of reserved keyword `yield` as an identifier in strict mode + × `yield` is only allowed within generator functions. 1 │ function* test(a = yield "test") {} > 2 │ function test2(a = yield "test") {} │ ^^^^^ 3 │ --- -yield_expr_in_parameter_initializer.js:2:26 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected `,` but instead found `"test"` - - 1 │ function* test(a = yield "test") {} - > 2 │ function test2(a = yield "test") {} - │ ^^^^^^ - 3 │ - - i Remove "test" - -- function* test(a = yield "test") {} function test2(a = yield "test") {} diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.js b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.js new file mode 100644 index 00000000000..1b003485b49 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.js @@ -0,0 +1,2 @@ +function foo() { yield bar; } +function foo() { yield 10; } diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.rast b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.rast new file mode 100644 index 00000000000..8010ac580a7 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function.rast @@ -0,0 +1,161 @@ +JsModule { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + items: JsModuleItemList [ + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@0..9 "function" [] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@9..12 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@12..13 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@13..15 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@15..17 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@17..23 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@23..26 "bar" [] [], + }, + }, + }, + ], + }, + semicolon_token: SEMICOLON@26..28 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@28..29 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@29..39 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@39..42 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@42..43 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@43..45 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@45..47 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@47..53 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@53..55 "10" [] [], + }, + }, + ], + }, + semicolon_token: SEMICOLON@55..57 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@57..58 "}" [] [], + }, + }, + ], + eof_token: EOF@58..59 "" [Newline("\n")] [], +} + +0: JS_MODULE@0..59 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_MODULE_ITEM_LIST@0..58 + 0: JS_FUNCTION_DECLARATION@0..29 + 0: (empty) + 1: FUNCTION_KW@0..9 "function" [] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@9..12 + 0: IDENT@9..12 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@12..15 + 0: L_PAREN@12..13 "(" [] [] + 1: JS_PARAMETER_LIST@13..13 + 2: R_PAREN@13..15 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@15..29 + 0: L_CURLY@15..17 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@17..17 + 2: JS_STATEMENT_LIST@17..28 + 0: JS_EXPRESSION_STATEMENT@17..28 + 0: JS_UNKNOWN_EXPRESSION@17..26 + 0: YIELD_KW@17..23 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@23..26 + 0: (empty) + 1: JS_IDENTIFIER_EXPRESSION@23..26 + 0: JS_REFERENCE_IDENTIFIER@23..26 + 0: IDENT@23..26 "bar" [] [] + 1: SEMICOLON@26..28 ";" [] [Whitespace(" ")] + 3: R_CURLY@28..29 "}" [] [] + 1: JS_FUNCTION_DECLARATION@29..58 + 0: (empty) + 1: FUNCTION_KW@29..39 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@39..42 + 0: IDENT@39..42 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@42..45 + 0: L_PAREN@42..43 "(" [] [] + 1: JS_PARAMETER_LIST@43..43 + 2: R_PAREN@43..45 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@45..58 + 0: L_CURLY@45..47 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@47..47 + 2: JS_STATEMENT_LIST@47..57 + 0: JS_EXPRESSION_STATEMENT@47..57 + 0: JS_UNKNOWN_EXPRESSION@47..55 + 0: YIELD_KW@47..53 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@53..55 + 0: (empty) + 1: JS_NUMBER_LITERAL_EXPRESSION@53..55 + 0: JS_NUMBER_LITERAL@53..55 "10" [] [] + 1: SEMICOLON@55..57 ";" [] [Whitespace(" ")] + 3: R_CURLY@57..58 "}" [] [] + 3: EOF@58..59 "" [Newline("\n")] [] +-- +yield_in_non_generator_function.js:1:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + > 1 │ function foo() { yield bar; } + │ ^^^^^ + 2 │ function foo() { yield 10; } + 3 │ + +-- +yield_in_non_generator_function.js:2:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + 1 │ function foo() { yield bar; } + > 2 │ function foo() { yield 10; } + │ ^^^^^ + 3 │ + +-- +function foo() { yield bar; } +function foo() { yield 10; } diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.js b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.js new file mode 100644 index 00000000000..186578bc739 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.js @@ -0,0 +1,3 @@ +function foo() { yield; } +function foo() { yield foo; } +function foo() { yield *foo; } diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.rast b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.rast new file mode 100644 index 00000000000..3d203687868 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_module.rast @@ -0,0 +1,238 @@ +JsModule { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + items: JsModuleItemList [ + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@0..9 "function" [] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@9..12 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@12..13 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@13..15 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@15..17 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + JsUnknown { + items: [ + IDENT@17..22 "yield" [] [], + ], + }, + ], + }, + semicolon_token: SEMICOLON@22..24 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@24..25 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@25..35 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@35..38 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@38..39 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@39..41 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@41..43 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@43..49 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@49..52 "foo" [] [], + }, + }, + }, + ], + }, + semicolon_token: SEMICOLON@52..54 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@54..55 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@55..65 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@65..68 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@68..69 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@69..71 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@71..73 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsBinaryExpression { + left: JsUnknownExpression { + items: [ + JsUnknown { + items: [ + IDENT@73..79 "yield" [] [Whitespace(" ")], + ], + }, + ], + }, + operator_token: STAR@79..80 "*" [] [], + right: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@80..83 "foo" [] [], + }, + }, + }, + semicolon_token: SEMICOLON@83..85 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@85..86 "}" [] [], + }, + }, + ], + eof_token: EOF@86..87 "" [Newline("\n")] [], +} + +0: JS_MODULE@0..87 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_MODULE_ITEM_LIST@0..86 + 0: JS_FUNCTION_DECLARATION@0..25 + 0: (empty) + 1: FUNCTION_KW@0..9 "function" [] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@9..12 + 0: IDENT@9..12 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@12..15 + 0: L_PAREN@12..13 "(" [] [] + 1: JS_PARAMETER_LIST@13..13 + 2: R_PAREN@13..15 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@15..25 + 0: L_CURLY@15..17 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@17..17 + 2: JS_STATEMENT_LIST@17..24 + 0: JS_EXPRESSION_STATEMENT@17..24 + 0: JS_UNKNOWN_EXPRESSION@17..22 + 0: JS_UNKNOWN@17..22 + 0: IDENT@17..22 "yield" [] [] + 1: SEMICOLON@22..24 ";" [] [Whitespace(" ")] + 3: R_CURLY@24..25 "}" [] [] + 1: JS_FUNCTION_DECLARATION@25..55 + 0: (empty) + 1: FUNCTION_KW@25..35 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@35..38 + 0: IDENT@35..38 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@38..41 + 0: L_PAREN@38..39 "(" [] [] + 1: JS_PARAMETER_LIST@39..39 + 2: R_PAREN@39..41 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@41..55 + 0: L_CURLY@41..43 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@43..43 + 2: JS_STATEMENT_LIST@43..54 + 0: JS_EXPRESSION_STATEMENT@43..54 + 0: JS_UNKNOWN_EXPRESSION@43..52 + 0: YIELD_KW@43..49 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@49..52 + 0: (empty) + 1: JS_IDENTIFIER_EXPRESSION@49..52 + 0: JS_REFERENCE_IDENTIFIER@49..52 + 0: IDENT@49..52 "foo" [] [] + 1: SEMICOLON@52..54 ";" [] [Whitespace(" ")] + 3: R_CURLY@54..55 "}" [] [] + 2: JS_FUNCTION_DECLARATION@55..86 + 0: (empty) + 1: FUNCTION_KW@55..65 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@65..68 + 0: IDENT@65..68 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@68..71 + 0: L_PAREN@68..69 "(" [] [] + 1: JS_PARAMETER_LIST@69..69 + 2: R_PAREN@69..71 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@71..86 + 0: L_CURLY@71..73 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@73..73 + 2: JS_STATEMENT_LIST@73..85 + 0: JS_EXPRESSION_STATEMENT@73..85 + 0: JS_BINARY_EXPRESSION@73..83 + 0: JS_UNKNOWN_EXPRESSION@73..79 + 0: JS_UNKNOWN@73..79 + 0: IDENT@73..79 "yield" [] [Whitespace(" ")] + 1: STAR@79..80 "*" [] [] + 2: JS_IDENTIFIER_EXPRESSION@80..83 + 0: JS_REFERENCE_IDENTIFIER@80..83 + 0: IDENT@80..83 "foo" [] [] + 1: SEMICOLON@83..85 ";" [] [Whitespace(" ")] + 3: R_CURLY@85..86 "}" [] [] + 3: EOF@86..87 "" [Newline("\n")] [] +-- +yield_in_non_generator_function_module.js:1:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Illegal use of reserved keyword `yield` as an identifier in strict mode + + > 1 │ function foo() { yield; } + │ ^^^^^ + 2 │ function foo() { yield foo; } + 3 │ function foo() { yield *foo; } + +-- +yield_in_non_generator_function_module.js:2:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + 1 │ function foo() { yield; } + > 2 │ function foo() { yield foo; } + │ ^^^^^ + 3 │ function foo() { yield *foo; } + 4 │ + +-- +yield_in_non_generator_function_module.js:3:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × Illegal use of reserved keyword `yield` as an identifier in strict mode + + 1 │ function foo() { yield; } + 2 │ function foo() { yield foo; } + > 3 │ function foo() { yield *foo; } + │ ^^^^^ + 4 │ + +-- +function foo() { yield; } +function foo() { yield foo; } +function foo() { yield *foo; } diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.js b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.js new file mode 100644 index 00000000000..2981e40a339 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.js @@ -0,0 +1,3 @@ +// SCRIPT +function foo() { yield bar; } +function foo() { yield 10; } diff --git a/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.rast b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.rast new file mode 100644 index 00000000000..03f3c8163a6 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/err/yield_in_non_generator_function_script.rast @@ -0,0 +1,164 @@ +JsScript { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + statements: JsStatementList [ + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@0..19 "function" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@19..22 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@22..23 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@23..25 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@25..27 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@27..33 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@33..36 "bar" [] [], + }, + }, + }, + ], + }, + semicolon_token: SEMICOLON@36..38 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@38..39 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@39..49 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@49..52 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@52..53 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@53..55 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@55..57 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsUnknownExpression { + items: [ + YIELD_KW@57..63 "yield" [] [Whitespace(" ")], + JsYieldArgument { + star_token: missing (optional), + expression: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@63..65 "10" [] [], + }, + }, + ], + }, + semicolon_token: SEMICOLON@65..67 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@67..68 "}" [] [], + }, + }, + ], + eof_token: EOF@68..69 "" [Newline("\n")] [], +} + +0: JS_SCRIPT@0..69 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_STATEMENT_LIST@0..68 + 0: JS_FUNCTION_DECLARATION@0..39 + 0: (empty) + 1: FUNCTION_KW@0..19 "function" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@19..22 + 0: IDENT@19..22 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@22..25 + 0: L_PAREN@22..23 "(" [] [] + 1: JS_PARAMETER_LIST@23..23 + 2: R_PAREN@23..25 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@25..39 + 0: L_CURLY@25..27 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@27..27 + 2: JS_STATEMENT_LIST@27..38 + 0: JS_EXPRESSION_STATEMENT@27..38 + 0: JS_UNKNOWN_EXPRESSION@27..36 + 0: YIELD_KW@27..33 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@33..36 + 0: (empty) + 1: JS_IDENTIFIER_EXPRESSION@33..36 + 0: JS_REFERENCE_IDENTIFIER@33..36 + 0: IDENT@33..36 "bar" [] [] + 1: SEMICOLON@36..38 ";" [] [Whitespace(" ")] + 3: R_CURLY@38..39 "}" [] [] + 1: JS_FUNCTION_DECLARATION@39..68 + 0: (empty) + 1: FUNCTION_KW@39..49 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@49..52 + 0: IDENT@49..52 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@52..55 + 0: L_PAREN@52..53 "(" [] [] + 1: JS_PARAMETER_LIST@53..53 + 2: R_PAREN@53..55 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@55..68 + 0: L_CURLY@55..57 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@57..57 + 2: JS_STATEMENT_LIST@57..67 + 0: JS_EXPRESSION_STATEMENT@57..67 + 0: JS_UNKNOWN_EXPRESSION@57..65 + 0: YIELD_KW@57..63 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@63..65 + 0: (empty) + 1: JS_NUMBER_LITERAL_EXPRESSION@63..65 + 0: JS_NUMBER_LITERAL@63..65 "10" [] [] + 1: SEMICOLON@65..67 ";" [] [Whitespace(" ")] + 3: R_CURLY@67..68 "}" [] [] + 3: EOF@68..69 "" [Newline("\n")] [] +-- +yield_in_non_generator_function_script.js:2:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + 1 │ // SCRIPT + > 2 │ function foo() { yield bar; } + │ ^^^^^ + 3 │ function foo() { yield 10; } + 4 │ + +-- +yield_in_non_generator_function_script.js:3:18 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + × `yield` is only allowed within generator functions. + + 1 │ // SCRIPT + 2 │ function foo() { yield bar; } + > 3 │ function foo() { yield 10; } + │ ^^^^^ + 4 │ + +-- +// SCRIPT +function foo() { yield bar; } +function foo() { yield 10; } diff --git a/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.js b/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.js new file mode 100644 index 00000000000..8c772216cd5 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.js @@ -0,0 +1,4 @@ +// SCRIPT +function foo() { yield *bar; } +function bar() { yield; } +function baz() { yield } diff --git a/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.rast b/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.rast new file mode 100644 index 00000000000..7b609ed1024 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/ok/reparse_yield_as_identifier.rast @@ -0,0 +1,182 @@ +JsScript { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + statements: JsStatementList [ + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@0..19 "function" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@19..22 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@22..23 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@23..25 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@25..27 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsBinaryExpression { + left: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@27..33 "yield" [] [Whitespace(" ")], + }, + }, + operator_token: STAR@33..34 "*" [] [], + right: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@34..37 "bar" [] [], + }, + }, + }, + semicolon_token: SEMICOLON@37..39 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@39..40 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@40..50 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@50..53 "bar" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@53..54 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@54..56 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@56..58 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@58..63 "yield" [] [], + }, + }, + semicolon_token: SEMICOLON@63..65 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@65..66 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@66..76 "function" [Newline("\n")] [Whitespace(" ")], + star_token: missing (optional), + id: JsIdentifierBinding { + name_token: IDENT@76..79 "baz" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@79..80 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@80..82 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@82..84 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@84..90 "yield" [] [Whitespace(" ")], + }, + }, + semicolon_token: missing (optional), + }, + ], + r_curly_token: R_CURLY@90..91 "}" [] [], + }, + }, + ], + eof_token: EOF@91..92 "" [Newline("\n")] [], +} + +0: JS_SCRIPT@0..92 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_STATEMENT_LIST@0..91 + 0: JS_FUNCTION_DECLARATION@0..40 + 0: (empty) + 1: FUNCTION_KW@0..19 "function" [Comments("// SCRIPT"), Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@19..22 + 0: IDENT@19..22 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@22..25 + 0: L_PAREN@22..23 "(" [] [] + 1: JS_PARAMETER_LIST@23..23 + 2: R_PAREN@23..25 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@25..40 + 0: L_CURLY@25..27 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@27..27 + 2: JS_STATEMENT_LIST@27..39 + 0: JS_EXPRESSION_STATEMENT@27..39 + 0: JS_BINARY_EXPRESSION@27..37 + 0: JS_IDENTIFIER_EXPRESSION@27..33 + 0: JS_REFERENCE_IDENTIFIER@27..33 + 0: IDENT@27..33 "yield" [] [Whitespace(" ")] + 1: STAR@33..34 "*" [] [] + 2: JS_IDENTIFIER_EXPRESSION@34..37 + 0: JS_REFERENCE_IDENTIFIER@34..37 + 0: IDENT@34..37 "bar" [] [] + 1: SEMICOLON@37..39 ";" [] [Whitespace(" ")] + 3: R_CURLY@39..40 "}" [] [] + 1: JS_FUNCTION_DECLARATION@40..66 + 0: (empty) + 1: FUNCTION_KW@40..50 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@50..53 + 0: IDENT@50..53 "bar" [] [] + 4: (empty) + 5: JS_PARAMETERS@53..56 + 0: L_PAREN@53..54 "(" [] [] + 1: JS_PARAMETER_LIST@54..54 + 2: R_PAREN@54..56 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@56..66 + 0: L_CURLY@56..58 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@58..58 + 2: JS_STATEMENT_LIST@58..65 + 0: JS_EXPRESSION_STATEMENT@58..65 + 0: JS_IDENTIFIER_EXPRESSION@58..63 + 0: JS_REFERENCE_IDENTIFIER@58..63 + 0: IDENT@58..63 "yield" [] [] + 1: SEMICOLON@63..65 ";" [] [Whitespace(" ")] + 3: R_CURLY@65..66 "}" [] [] + 2: JS_FUNCTION_DECLARATION@66..91 + 0: (empty) + 1: FUNCTION_KW@66..76 "function" [Newline("\n")] [Whitespace(" ")] + 2: (empty) + 3: JS_IDENTIFIER_BINDING@76..79 + 0: IDENT@76..79 "baz" [] [] + 4: (empty) + 5: JS_PARAMETERS@79..82 + 0: L_PAREN@79..80 "(" [] [] + 1: JS_PARAMETER_LIST@80..80 + 2: R_PAREN@80..82 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@82..91 + 0: L_CURLY@82..84 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@84..84 + 2: JS_STATEMENT_LIST@84..90 + 0: JS_EXPRESSION_STATEMENT@84..90 + 0: JS_IDENTIFIER_EXPRESSION@84..90 + 0: JS_REFERENCE_IDENTIFIER@84..90 + 0: IDENT@84..90 "yield" [] [Whitespace(" ")] + 1: (empty) + 3: R_CURLY@90..91 "}" [] [] + 3: EOF@91..92 "" [Newline("\n")] [] diff --git a/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.js b/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.js new file mode 100644 index 00000000000..e8877feb2b3 --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.js @@ -0,0 +1,3 @@ +function* foo() { yield 10; } +function* foo() { yield *bar; } +function* foo() { yield; } diff --git a/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.rast b/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.rast new file mode 100644 index 00000000000..005a639e2dd --- /dev/null +++ b/crates/rome_js_parser/test_data/inline/ok/yield_in_generator_function.rast @@ -0,0 +1,185 @@ +JsModule { + interpreter_token: missing (optional), + directives: JsDirectiveList [], + items: JsModuleItemList [ + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@0..8 "function" [] [], + star_token: STAR@8..10 "*" [] [Whitespace(" ")], + id: JsIdentifierBinding { + name_token: IDENT@10..13 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@13..14 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@14..16 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@16..18 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsYieldExpression { + yield_token: YIELD_KW@18..24 "yield" [] [Whitespace(" ")], + argument: JsYieldArgument { + star_token: missing (optional), + expression: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@24..26 "10" [] [], + }, + }, + }, + semicolon_token: SEMICOLON@26..28 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@28..29 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@29..38 "function" [Newline("\n")] [], + star_token: STAR@38..40 "*" [] [Whitespace(" ")], + id: JsIdentifierBinding { + name_token: IDENT@40..43 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@43..44 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@44..46 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@46..48 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsYieldExpression { + yield_token: YIELD_KW@48..54 "yield" [] [Whitespace(" ")], + argument: JsYieldArgument { + star_token: STAR@54..55 "*" [] [], + expression: JsIdentifierExpression { + name: JsReferenceIdentifier { + value_token: IDENT@55..58 "bar" [] [], + }, + }, + }, + }, + semicolon_token: SEMICOLON@58..60 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@60..61 "}" [] [], + }, + }, + JsFunctionDeclaration { + async_token: missing (optional), + function_token: FUNCTION_KW@61..70 "function" [Newline("\n")] [], + star_token: STAR@70..72 "*" [] [Whitespace(" ")], + id: JsIdentifierBinding { + name_token: IDENT@72..75 "foo" [] [], + }, + type_parameters: missing (optional), + parameters: JsParameters { + l_paren_token: L_PAREN@75..76 "(" [] [], + items: JsParameterList [], + r_paren_token: R_PAREN@76..78 ")" [] [Whitespace(" ")], + }, + return_type_annotation: missing (optional), + body: JsFunctionBody { + l_curly_token: L_CURLY@78..80 "{" [] [Whitespace(" ")], + directives: JsDirectiveList [], + statements: JsStatementList [ + JsExpressionStatement { + expression: JsYieldExpression { + yield_token: YIELD_KW@80..85 "yield" [] [], + argument: missing (optional), + }, + semicolon_token: SEMICOLON@85..87 ";" [] [Whitespace(" ")], + }, + ], + r_curly_token: R_CURLY@87..88 "}" [] [], + }, + }, + ], + eof_token: EOF@88..89 "" [Newline("\n")] [], +} + +0: JS_MODULE@0..89 + 0: (empty) + 1: JS_DIRECTIVE_LIST@0..0 + 2: JS_MODULE_ITEM_LIST@0..88 + 0: JS_FUNCTION_DECLARATION@0..29 + 0: (empty) + 1: FUNCTION_KW@0..8 "function" [] [] + 2: STAR@8..10 "*" [] [Whitespace(" ")] + 3: JS_IDENTIFIER_BINDING@10..13 + 0: IDENT@10..13 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@13..16 + 0: L_PAREN@13..14 "(" [] [] + 1: JS_PARAMETER_LIST@14..14 + 2: R_PAREN@14..16 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@16..29 + 0: L_CURLY@16..18 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@18..18 + 2: JS_STATEMENT_LIST@18..28 + 0: JS_EXPRESSION_STATEMENT@18..28 + 0: JS_YIELD_EXPRESSION@18..26 + 0: YIELD_KW@18..24 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@24..26 + 0: (empty) + 1: JS_NUMBER_LITERAL_EXPRESSION@24..26 + 0: JS_NUMBER_LITERAL@24..26 "10" [] [] + 1: SEMICOLON@26..28 ";" [] [Whitespace(" ")] + 3: R_CURLY@28..29 "}" [] [] + 1: JS_FUNCTION_DECLARATION@29..61 + 0: (empty) + 1: FUNCTION_KW@29..38 "function" [Newline("\n")] [] + 2: STAR@38..40 "*" [] [Whitespace(" ")] + 3: JS_IDENTIFIER_BINDING@40..43 + 0: IDENT@40..43 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@43..46 + 0: L_PAREN@43..44 "(" [] [] + 1: JS_PARAMETER_LIST@44..44 + 2: R_PAREN@44..46 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@46..61 + 0: L_CURLY@46..48 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@48..48 + 2: JS_STATEMENT_LIST@48..60 + 0: JS_EXPRESSION_STATEMENT@48..60 + 0: JS_YIELD_EXPRESSION@48..58 + 0: YIELD_KW@48..54 "yield" [] [Whitespace(" ")] + 1: JS_YIELD_ARGUMENT@54..58 + 0: STAR@54..55 "*" [] [] + 1: JS_IDENTIFIER_EXPRESSION@55..58 + 0: JS_REFERENCE_IDENTIFIER@55..58 + 0: IDENT@55..58 "bar" [] [] + 1: SEMICOLON@58..60 ";" [] [Whitespace(" ")] + 3: R_CURLY@60..61 "}" [] [] + 2: JS_FUNCTION_DECLARATION@61..88 + 0: (empty) + 1: FUNCTION_KW@61..70 "function" [Newline("\n")] [] + 2: STAR@70..72 "*" [] [Whitespace(" ")] + 3: JS_IDENTIFIER_BINDING@72..75 + 0: IDENT@72..75 "foo" [] [] + 4: (empty) + 5: JS_PARAMETERS@75..78 + 0: L_PAREN@75..76 "(" [] [] + 1: JS_PARAMETER_LIST@76..76 + 2: R_PAREN@76..78 ")" [] [Whitespace(" ")] + 6: (empty) + 7: JS_FUNCTION_BODY@78..88 + 0: L_CURLY@78..80 "{" [] [Whitespace(" ")] + 1: JS_DIRECTIVE_LIST@80..80 + 2: JS_STATEMENT_LIST@80..87 + 0: JS_EXPRESSION_STATEMENT@80..87 + 0: JS_YIELD_EXPRESSION@80..85 + 0: YIELD_KW@80..85 "yield" [] [] + 1: (empty) + 1: SEMICOLON@85..87 ";" [] [Whitespace(" ")] + 3: R_CURLY@87..88 "}" [] [] + 3: EOF@88..89 "" [Newline("\n")] []