diff --git a/crates/dash_parser/src/stmt.rs b/crates/dash_parser/src/stmt.rs index ed30a92a..1134513b 100644 --- a/crates/dash_parser/src/stmt.rs +++ b/crates/dash_parser/src/stmt.rs @@ -1,3 +1,4 @@ +use dash_middle::interner::sym; use dash_middle::lexer::token::{TokenType, VARIABLE_TYPES}; use dash_middle::parser::error::Error; use dash_middle::parser::expr::{Expr, ExprKind}; @@ -90,8 +91,8 @@ impl<'a, 'interner> Parser<'a, 'interner> { false => Asyncness::No, }; let is_generator = self.expect_token_type_and_skip(&[TokenType::Star], false); - // FIXME: this breaks `class V { get }`, which is valid -- needs backtracking - let property_kind = if self.expect_token_type_and_skip(&[TokenType::Get], false) { + + let mut property_kind = if self.expect_token_type_and_skip(&[TokenType::Get], false) { Kind::Getter(self.previous().unwrap().span) } else if self.expect_token_type_and_skip(&[TokenType::Set], false) { Kind::Setter(self.previous().unwrap().span) @@ -104,7 +105,21 @@ impl<'a, 'interner> Parser<'a, 'interner> { self.expect_token_type_and_skip(&[TokenType::RightSquareBrace], true); ClassMemberKey::Computed(expr) } else { - ClassMemberKey::Named(self.expect_identifier_or_reserved_kw(true)?) + // HACK: if we have `get` + `(`, it is not a getter + // change `Kind::Getter` to `Kind::Normal` and treat it as a "get" named method (same for set) + if self.current().is_some_and(|tok| tok.ty == TokenType::LeftParen) + && matches!(property_kind, Kind::Getter(_) | Kind::Setter(_)) + { + let key = match property_kind { + Kind::Getter(_) => sym::get, + Kind::Setter(_) => sym::set, + Kind::Normal => unreachable!(), + }; + property_kind = Kind::Normal; + ClassMemberKey::Named(key) + } else { + ClassMemberKey::Named(self.expect_identifier_or_reserved_kw(true)?) + } }; let is_method = self.expect_token_type_and_skip(&[TokenType::LeftParen], false); diff --git a/crates/dash_vm/src/test/mod.rs b/crates/dash_vm/src/test/mod.rs index 43a24ccf..e2e9e35d 100644 --- a/crates/dash_vm/src/test/mod.rs +++ b/crates/dash_vm/src/test/mod.rs @@ -425,6 +425,24 @@ simple_test!( class C6 { foo(n) { return n * n; }; static bar() { return 42 } } assert(new C6().foo(4) === 16); assert(C6.bar() === 42); + + class C7 { + *[Symbol.iterator]() { + yield 42; + } + } + const gen = new C7()[Symbol.iterator](); + assert(gen.next().value === 42); + assert(gen.next().done); + + class C8 { + get() { return 1 } + set() { return 2 } + get a() { return 3 } + } + assert(new C8().get() === 1); + assert(new C8().set() === 2); + assert(new C8().a === 3); "#, Value::undefined() );