Skip to content

Commit 38fd8e3

Browse files
authored
Merge pull request universal-ctags#3949 from masatake/javascript--static-block
JavaScript: skip static initialization blocks
2 parents 45f200c + c433018 commit 38fd8e3

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--sort=no
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a input.js /^class a {$/;" c
2+
b input.js /^ static { class b {} }$/;" c class:a
3+
fld0 input.js /^ fld0;$/;" M class:a
4+
c input.js /^ static { class c {} }$/;" c class:a
5+
fld1 input.js /^ static fld1;$/;" M class:a
6+
d input.js /^ static { class d {} }$/;" c class:a
7+
fld2 input.js /^ static fld2 = 1;$/;" M class:a
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Derived from #3948 submitted by @cpardotortosa
2+
class a {
3+
static { class b {} }
4+
fld0;
5+
static { class c {} }
6+
static fld1;
7+
static { class d {} }
8+
static fld2 = 1;
9+
}
10+
a.b
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node

parsers/jscript.c

+23-2
Original file line numberDiff line numberDiff line change
@@ -1966,13 +1966,22 @@ static bool parseMethods (tokenInfo *const token, int class_index,
19661966
* field1 = 1
19671967
* The parser extracts field0 as a method because the left value
19681968
* is a function (kind propagation), and field1 as a field.
1969+
*
1970+
* static methods and static initialization blocks
1971+
* - ref. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Static_initialization_blocks
1972+
*
1973+
* static func() {}
1974+
* static {}
1975+
* static prop;
1976+
* static prop = val;
19691977
*/
19701978

19711979
bool dont_read = false;
19721980
do
19731981
{
19741982
bool is_setter = false;
19751983
bool is_getter = false;
1984+
bool is_static = false; /* For recognizing static {...} block. */
19761985

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

19861995
if (isKeyword (token, KEYWORD_async))
19871996
readToken (token);
1997+
else if (isKeyword (token, KEYWORD_static))
1998+
is_static = true;
19881999
else if (isType (token, TOKEN_KEYWORD) &&
19892000
(isKeyword (token, KEYWORD_get) || isKeyword (token, KEYWORD_set)))
19902001
{
@@ -2014,8 +2025,9 @@ static bool parseMethods (tokenInfo *const token, int class_index,
20142025
continue;
20152026
}
20162027

2017-
if (! isType (token, TOKEN_KEYWORD) &&
2018-
! isType (token, TOKEN_SEMICOLON))
2028+
if ((! isType (token, TOKEN_KEYWORD) &&
2029+
! isType (token, TOKEN_SEMICOLON))
2030+
|| is_static)
20192031
{
20202032
bool is_generator = false;
20212033
bool is_shorthand = false; /* ES6 shorthand syntax */
@@ -2190,6 +2202,15 @@ static bool parseMethods (tokenInfo *const token, int class_index,
21902202

21912203
vStringDelete (signature);
21922204
}
2205+
else if (is_static)
2206+
{
2207+
if (isType (token, TOKEN_OPEN_CURLY))
2208+
/* static initialization block */
2209+
parseBlock (token, class_index);
2210+
else
2211+
dont_read = true;
2212+
continue;
2213+
}
21932214
else
21942215
{
21952216
bool is_property = isType (token, TOKEN_COMMA);

0 commit comments

Comments
 (0)