From b50c5ec623e6313402fa82bf917b37bd0d1624df Mon Sep 17 00:00:00 2001 From: Dunqing Date: Mon, 8 Jan 2024 15:07:43 +0800 Subject: [PATCH] fix(parser): unexpected ts type annotation in get/set (#1942) fix: https://github.com/oxc-project/oxc/issues/1939 --- crates/oxc_parser/src/js/class.rs | 4 ++++ tasks/coverage/misc/fail/oxc-1942-1.ts | 5 +++++ tasks/coverage/misc/fail/oxc-1942-2.ts | 3 +++ tasks/coverage/parser_misc.snap | 18 +++++++++++++++++- 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tasks/coverage/misc/fail/oxc-1942-1.ts create mode 100644 tasks/coverage/misc/fail/oxc-1942-2.ts diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index 15835ce44..1172ae0ca 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -294,6 +294,10 @@ impl<'a> Parser<'a> { } Ok(definition) } else { + // getter and setter has no ts type annotation + if !kind.is_method() { + return Err(self.unexpected()); + } let definition = self.parse_class_property_definition( span, key, diff --git a/tasks/coverage/misc/fail/oxc-1942-1.ts b/tasks/coverage/misc/fail/oxc-1942-1.ts new file mode 100644 index 000000000..9924545d0 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-1942-1.ts @@ -0,0 +1,5 @@ +class Foo { + get x: () => { + return 5; + } +} \ No newline at end of file diff --git a/tasks/coverage/misc/fail/oxc-1942-2.ts b/tasks/coverage/misc/fail/oxc-1942-2.ts new file mode 100644 index 000000000..f02bec397 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-1942-2.ts @@ -0,0 +1,3 @@ +class Foo { + set x: (v: number) => {} +} \ No newline at end of file diff --git a/tasks/coverage/parser_misc.snap b/tasks/coverage/parser_misc.snap index 9d35285c9..d37d0f43b 100644 --- a/tasks/coverage/parser_misc.snap +++ b/tasks/coverage/parser_misc.snap @@ -1,12 +1,28 @@ parser_misc Summary: AST Parsed : 9/9 (100.00%) Positive Passed: 9/9 (100.00%) -Negative Passed: 3/3 (100.00%) +Negative Passed: 5/5 (100.00%) × Unexpected token ╭─[fail/oxc-169.js:1:1] 1 │ 1<(V=82< ╰──── + × Unexpected token + ╭─[fail/oxc-1942-1.ts:1:1] + 1 │ class Foo { + 2 │ get x: () => { + · ─ + 3 │ return 5; + ╰──── + + × Unexpected token + ╭─[fail/oxc-1942-2.ts:1:1] + 1 │ class Foo { + 2 │ set x: (v: number) => {} + · ─ + 3 │ } + ╰──── + × Empty parenthesized expression ╭─[fail/oxc-232.js:1:1] 1 │ x = (/* a */)