]> git.kaiwu.me - quickjs.git/commitdiff
Improve class parser (#289)
authorCharlie Gordon <github@chqrlie.org>
Sun, 5 May 2024 17:54:47 +0000 (19:54 +0200)
committerGitHub <noreply@github.com>
Sun, 5 May 2024 17:54:47 +0000 (19:54 +0200)
- accept `class P { async = 1 }}`
- accept `class P { static = 1 }}` etc.
- Fixes #261

quickjs.c
tests/test_language.js

index 798d5090e9a47e495b22fae8df1c9f1d4d7fce11..3c1c326280730407a74127f0db37e28d204b799b 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -22381,7 +22381,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;
             }
@@ -22397,7 +22397,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;
             }
@@ -23081,7 +23082,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))
index f00c4be0b28150af90330371e708f14b23c06506..7e98d7a817e71aa3e706733bc5b62d24b3605ff3 100644 (file)
@@ -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()