From 23eb1d188f2505652ecac26ce3b6a2964057cad9 Mon Sep 17 00:00:00 2001 From: Daiki Nishikawa Date: Fri, 30 Dec 2022 03:18:33 +0900 Subject: [PATCH] fix(rome_js_parser): parse "async" as class member property correctly (#4112) --- .../js/classes/keyword-property/async.js.snap | 98 ------------------- .../keyword-property/static-async.js.snap | 98 ------------------- crates/rome_js_parser/src/syntax/class.rs | 2 + 3 files changed, 2 insertions(+), 196 deletions(-) delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/async.js.snap delete mode 100644 crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/static-async.js.snap diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/async.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/async.js.snap deleted file mode 100644 index fa202a2a8d3..00000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/async.js.snap +++ /dev/null @@ -1,98 +0,0 @@ ---- -source: crates/rome_formatter_test/src/snapshot_builder.rs -info: js/classes/keyword-property/async.js ---- - -# Input - -```js -class A { - async; - foo() {}; -} - -class B { - async = 1; - foo() {} -} - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -1,9 +1,10 @@ - class A { -- async; -+ async - foo() {} - } - - class B { -- async = 1; -+ async -+ = 1 - foo() {} - } -``` - -# Output - -```js -class A { - async - foo() {} -} - -class B { - async - = 1 - foo() {} -} -``` - -# Errors -``` -async.js:2:8 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an identifier, a string literal, a number literal, a private field name, or a computed name but instead found ';' - - 1 │ class A { - > 2 │ async; - │ ^ - 3 │ foo() {}; - 4 │ } - - i Expected an identifier, a string literal, a number literal, a private field name, or a computed name here - - 1 │ class A { - > 2 │ async; - │ ^ - 3 │ foo() {}; - 4 │ } - -async.js:7:9 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an identifier, a string literal, a number literal, a private field name, or a computed name but instead found '=' - - 6 │ class B { - > 7 │ async = 1; - │ ^ - 8 │ foo() {} - 9 │ } - - i Expected an identifier, a string literal, a number literal, a private field name, or a computed name here - - 6 │ class B { - > 7 │ async = 1; - │ ^ - 8 │ foo() {} - 9 │ } - - -``` - - diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/static-async.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/static-async.js.snap deleted file mode 100644 index 5cba305fc59..00000000000 --- a/crates/rome_js_formatter/tests/specs/prettier/js/classes/keyword-property/static-async.js.snap +++ /dev/null @@ -1,98 +0,0 @@ ---- -source: crates/rome_formatter_test/src/snapshot_builder.rs -info: js/classes/keyword-property/static-async.js ---- - -# Input - -```js -class A { - static async; - foo() {} -} - -class B { - static async = 1; - foo() {} -} - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Rome -@@ -1,9 +1,10 @@ - class A { -- static async; -+ static async - foo() {} - } - - class B { -- static async = 1; -+ static async -+ = 1 - foo() {} - } -``` - -# Output - -```js -class A { - static async - foo() {} -} - -class B { - static async - = 1 - foo() {} -} -``` - -# Errors -``` -static-async.js:2:15 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an identifier, a string literal, a number literal, a private field name, or a computed name but instead found ';' - - 1 │ class A { - > 2 │ static async; - │ ^ - 3 │ foo() {} - 4 │ } - - i Expected an identifier, a string literal, a number literal, a private field name, or a computed name here - - 1 │ class A { - > 2 │ static async; - │ ^ - 3 │ foo() {} - 4 │ } - -static-async.js:7:16 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - - × expected an identifier, a string literal, a number literal, a private field name, or a computed name but instead found '=' - - 6 │ class B { - > 7 │ static async = 1; - │ ^ - 8 │ foo() {} - 9 │ } - - i Expected an identifier, a string literal, a number literal, a private field name, or a computed name here - - 6 │ class B { - > 7 │ static async = 1; - │ ^ - 8 │ foo() {} - 9 │ } - - -``` - - diff --git a/crates/rome_js_parser/src/syntax/class.rs b/crates/rome_js_parser/src/syntax/class.rs index 4ce22d23118..d041803660f 100644 --- a/crates/rome_js_parser/src/syntax/class.rs +++ b/crates/rome_js_parser/src/syntax/class.rs @@ -594,6 +594,8 @@ fn parse_class_member_impl( // Seems like we're at an async method if p.at(T![async]) && !p.nth_at(1, T![?]) + && !p.nth_at(1, T![;]) + && !p.nth_at(1, T![=]) && !is_at_method_class_member(p, 1) && !p.has_nth_preceding_line_break(1) {