Skip to content

Commit

Permalink
Merge pull request #3949 from masatake/javascript--static-block
Browse files Browse the repository at this point in the history
JavaScript: skip static initialization blocks
  • Loading branch information
masatake authored Mar 9, 2024
2 parents 45f200c + c433018 commit 38fd8e3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions Units/parser-javascript.r/js-static-block.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--sort=no
7 changes: 7 additions & 0 deletions Units/parser-javascript.r/js-static-block.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a input.js /^class a {$/;" c
b input.js /^ static { class b {} }$/;" c class:a
fld0 input.js /^ fld0;$/;" M class:a
c input.js /^ static { class c {} }$/;" c class:a
fld1 input.js /^ static fld1;$/;" M class:a
d input.js /^ static { class d {} }$/;" c class:a
fld2 input.js /^ static fld2 = 1;$/;" M class:a
10 changes: 10 additions & 0 deletions Units/parser-javascript.r/js-static-block.d/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Derived from #3948 submitted by @cpardotortosa
class a {
static { class b {} }
fld0;
static { class c {} }
static fld1;
static { class d {} }
static fld2 = 1;
}
a.b
1 change: 1 addition & 0 deletions Units/parser-javascript.r/js-static-block.d/validator
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node
25 changes: 23 additions & 2 deletions parsers/jscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -1966,13 +1966,22 @@ static bool parseMethods (tokenInfo *const token, int class_index,
* field1 = 1
* The parser extracts field0 as a method because the left value
* is a function (kind propagation), and field1 as a field.
*
* static methods and static initialization blocks
* - ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks
*
* static func() {}
* static {}
* static prop;
* static prop = val;
*/

bool dont_read = false;
do
{
bool is_setter = false;
bool is_getter = false;
bool is_static = false; /* For recognizing static {...} block. */

if (!dont_read)
readToken (token);
Expand All @@ -1985,6 +1994,8 @@ static bool parseMethods (tokenInfo *const token, int class_index,

if (isKeyword (token, KEYWORD_async))
readToken (token);
else if (isKeyword (token, KEYWORD_static))
is_static = true;
else if (isType (token, TOKEN_KEYWORD) &&
(isKeyword (token, KEYWORD_get) || isKeyword (token, KEYWORD_set)))
{
Expand Down Expand Up @@ -2014,8 +2025,9 @@ static bool parseMethods (tokenInfo *const token, int class_index,
continue;
}

if (! isType (token, TOKEN_KEYWORD) &&
! isType (token, TOKEN_SEMICOLON))
if ((! isType (token, TOKEN_KEYWORD) &&
! isType (token, TOKEN_SEMICOLON))
|| is_static)
{
bool is_generator = false;
bool is_shorthand = false; /* ES6 shorthand syntax */
Expand Down Expand Up @@ -2190,6 +2202,15 @@ static bool parseMethods (tokenInfo *const token, int class_index,

vStringDelete (signature);
}
else if (is_static)
{
if (isType (token, TOKEN_OPEN_CURLY))
/* static initialization block */
parseBlock (token, class_index);
else
dont_read = true;
continue;
}
else
{
bool is_property = isType (token, TOKEN_COMMA);
Expand Down

0 comments on commit 38fd8e3

Please sign in to comment.