commit 4e708e94e26574cef4a2a1a80d2eb2abf14c1db7
parent a0ef4f658721c0d58fe9da46fc7aab62e69f5bc7
Author: Charlie Gordon <github@chqrlie.org>
Date: Sun, 5 May 2024 19:54:47 +0200
Improve class parser (#289)
- accept `class P { async = 1 }}`
- accept `class P { static = 1 }}` etc.
- Fixes #261
Diffstat:
2 files changed, 13 insertions(+), 5 deletions(-)
diff --git a/quickjs/quickjs.c b/quickjs/quickjs.c
@@ -22568,7 +22568,7 @@ static int __exception js_parse_property_name(JSParseState *s,
goto fail1;
if (s->token.val == ':' || s->token.val == ',' ||
s->token.val == '}' || s->token.val == '(' ||
- s->token.val == '=' ) {
+ s->token.val == '=') {
is_non_reserved_ident = TRUE;
goto ident_found;
}
@@ -22584,7 +22584,8 @@ static int __exception js_parse_property_name(JSParseState *s,
if (next_token(s))
goto fail1;
if (s->token.val == ':' || s->token.val == ',' ||
- s->token.val == '}' || s->token.val == '(') {
+ s->token.val == '}' || s->token.val == '(' ||
+ s->token.val == '=') {
is_non_reserved_ident = TRUE;
goto ident_found;
}
@@ -23268,7 +23269,12 @@ static __exception int js_parse_class(JSParseState *s, BOOL is_class_expr,
goto fail;
continue;
}
- is_static = (s->token.val == TOK_STATIC);
+ is_static = FALSE;
+ if (s->token.val == TOK_STATIC) {
+ int next = peek_token(s, TRUE);
+ if (!(next == ';' || next == '}' || next == '(' || next == '='))
+ is_static = TRUE;
+ }
prop_type = -1;
if (is_static) {
if (next_token(s))
diff --git a/quickjs/tests/test_language.js b/quickjs/tests/test_language.js
@@ -335,11 +335,13 @@ function test_class()
assert(S.x === 42);
assert(S.y === 42);
assert(S.z === 42);
-
+
class P {
- get = () => "123"
+ get = () => "123";
+ static() { return 42; }
}
assert(new P().get() === "123");
+ assert(new P().static() === 42);
};
function test_template()