Skip to content

Commit

Permalink
don't treat get() {} a getter in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed May 5, 2024
1 parent 25d269b commit 5d7c32b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
21 changes: 18 additions & 3 deletions crates/dash_parser/src/stmt.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions crates/dash_vm/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);

0 comments on commit 5d7c32b

Please sign in to comment.