From 997df23a84fd829189500b11e9ddb3c41a4a021e Mon Sep 17 00:00:00 2001 From: Zheyu Zhang Date: Wed, 24 Jan 2024 17:09:59 +0800 Subject: [PATCH] fix(js_parser): correctly parse type arguments in expression (#1645) --- CHANGELOG.md | 13 +++-- .../src/syntax/typescript/types.rs | 13 ++++- .../ok/ts_type_arguments_like_expression.rast | 52 +++++++++++++++++++ .../ok/ts_type_arguments_like_expression.ts | 1 + .../src/content/docs/internals/changelog.mdx | 13 +++-- 5 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.rast create mode 100644 crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index b6abc676419c..1f2cd4489f51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,8 +42,6 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom describe.skip("test", () => {}); it.skip("test", () => {}); ``` -<<<<<<< HEAD -======= - Add the rule [noFocusedTests](https://biomejs.dev/linter/rules/no-focused-tests), to disallow skipped tests: @@ -52,7 +50,6 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom it.only("test", () => {}); ``` ->>>>>>> fd3de977d1 (feat(linter): new rule noFocusedTests (#1641)) ### Parser ## 1.5.3 (2024-01-22) @@ -133,6 +130,16 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom Contributed by @magic-akari +- Correctly parse type arguments in expression([#1184](https://github.com/biomejs/biome/issues/1184)). + + The following code is now correctly parsed in typescript: + + ```ts + 0 < (0 >= 1); + ``` + + Contributed by @ah-yu + ### Website #### New diff --git a/crates/biome_js_parser/src/syntax/typescript/types.rs b/crates/biome_js_parser/src/syntax/typescript/types.rs index 9aa38da380de..4b09528168bb 100644 --- a/crates/biome_js_parser/src/syntax/typescript/types.rs +++ b/crates/biome_js_parser/src/syntax/typescript/types.rs @@ -2004,9 +2004,20 @@ pub(crate) fn parse_ts_type_arguments_in_expression( return Absent; } + // test ts ts_type_arguments_like_expression + // 0 < (0 >= 1); try_parse(p, |p| { p.re_lex(JsReLexContext::TypeArgumentLessThan); - let arguments = parse_ts_type_arguments_impl(p, TypeContext::default(), false); + let m = p.start(); + p.bump(T![<]); + + if p.at(T![>]) { + p.error(expected_ts_type_parameter(p, p.cur_range())); + } + TypeArgumentsList::new(TypeContext::default(), false).parse_list(p); + p.re_lex(JsReLexContext::BinaryOperator); + p.expect(T![>]); + let arguments = m.complete(p, TS_TYPE_ARGUMENTS); if p.last() == Some(T![>]) && can_follow_type_arguments_in_expr(p, context) { Ok(Present(arguments)) diff --git a/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.rast b/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.rast new file mode 100644 index 000000000000..de3aacba23b2 --- /dev/null +++ b/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.rast @@ -0,0 +1,52 @@ +JsModule { + bom_token: missing (optional), + interpreter_token: missing (optional), + directives: JsDirectiveList [], + items: JsModuleItemList [ + JsExpressionStatement { + expression: JsBinaryExpression { + left: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@0..2 "0" [] [Whitespace(" ")], + }, + operator_token: L_ANGLE@2..4 "<" [] [Whitespace(" ")], + right: JsParenthesizedExpression { + l_paren_token: L_PAREN@4..5 "(" [] [], + expression: JsBinaryExpression { + left: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@5..7 "0" [] [Whitespace(" ")], + }, + operator_token: GTEQ@7..10 ">=" [] [Whitespace(" ")], + right: JsNumberLiteralExpression { + value_token: JS_NUMBER_LITERAL@10..11 "1" [] [], + }, + }, + r_paren_token: R_PAREN@11..12 ")" [] [], + }, + }, + semicolon_token: SEMICOLON@12..13 ";" [] [], + }, + ], + eof_token: EOF@13..14 "" [Newline("\n")] [], +} + +0: JS_MODULE@0..14 + 0: (empty) + 1: (empty) + 2: JS_DIRECTIVE_LIST@0..0 + 3: JS_MODULE_ITEM_LIST@0..13 + 0: JS_EXPRESSION_STATEMENT@0..13 + 0: JS_BINARY_EXPRESSION@0..12 + 0: JS_NUMBER_LITERAL_EXPRESSION@0..2 + 0: JS_NUMBER_LITERAL@0..2 "0" [] [Whitespace(" ")] + 1: L_ANGLE@2..4 "<" [] [Whitespace(" ")] + 2: JS_PARENTHESIZED_EXPRESSION@4..12 + 0: L_PAREN@4..5 "(" [] [] + 1: JS_BINARY_EXPRESSION@5..11 + 0: JS_NUMBER_LITERAL_EXPRESSION@5..7 + 0: JS_NUMBER_LITERAL@5..7 "0" [] [Whitespace(" ")] + 1: GTEQ@7..10 ">=" [] [Whitespace(" ")] + 2: JS_NUMBER_LITERAL_EXPRESSION@10..11 + 0: JS_NUMBER_LITERAL@10..11 "1" [] [] + 2: R_PAREN@11..12 ")" [] [] + 1: SEMICOLON@12..13 ";" [] [] + 4: EOF@13..14 "" [Newline("\n")] [] diff --git a/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.ts b/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.ts new file mode 100644 index 000000000000..078b37128e86 --- /dev/null +++ b/crates/biome_js_parser/test_data/inline/ok/ts_type_arguments_like_expression.ts @@ -0,0 +1 @@ +0 < (0 >= 1); diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index 639f0efa5cc6..f92292ff8c30 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -48,8 +48,6 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom describe.skip("test", () => {}); it.skip("test", () => {}); ``` -<<<<<<< HEAD -======= - Add the rule [noFocusedTests](https://biomejs.dev/linter/rules/no-focused-tests), to disallow skipped tests: @@ -58,7 +56,6 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom it.only("test", () => {}); ``` ->>>>>>> fd3de977d1 (feat(linter): new rule noFocusedTests (#1641)) ### Parser ## 1.5.3 (2024-01-22) @@ -139,6 +136,16 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom Contributed by @magic-akari +- Correctly parse type arguments in expression([#1184](https://github.com/biomejs/biome/issues/1184)). + + The following code is now correctly parsed in typescript: + + ```ts + 0 < (0 >= 1); + ``` + + Contributed by @ah-yu + ### Website #### New