Skip to content

Commit

Permalink
JavaScript: extract name in { ..., name, ...}, a shorthand for { ...,…
Browse files Browse the repository at this point in the history
… name: name, ...}

Signed-off-by: Masatake YAMATO <[email protected]>
  • Loading branch information
masatake committed Nov 17, 2023
1 parent a23eaf4 commit 68ee390
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ f input-1.js /^function f({ u, x }) {$/;" f signature:({ u, x })
anonymousObjectf91cef720105 input-1.js /^f({u: 1, x: 2})$/;" v
u input-1.js /^f({u: 1, x: 2})$/;" p variable:anonymousObjectf91cef720105
x input-1.js /^f({u: 1, x: 2})$/;" p variable:anonymousObjectf91cef720105
f input-2.js /^function f(x, y, z) {$/;" f signature:(x, y, z)
anonymousObjectf91d7bd30105 input-2.js /^f({x, y, z})$/;" v
x input-2.js /^f({x, y, z})$/;" p variable:anonymousObjectf91d7bd30105
y input-2.js /^f({x, y, z})$/;" p variable:anonymousObjectf91d7bd30105
z input-2.js /^f({x, y, z})$/;" p variable:anonymousObjectf91d7bd30105
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function f(x, y, z) {
return x + y + z
}

x = 1
y = 1
z = 1
f({x, y, z})
54 changes: 50 additions & 4 deletions parsers/jscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -2120,13 +2120,45 @@ static bool parseMethods (tokenInfo *const token, int class_index,

is_shorthand = isType (token, TOKEN_OPEN_PAREN);
bool can_be_field = isType (token, TOKEN_EQUAL_SIGN);
if ( isType (token, TOKEN_COLON) || can_be_field || is_shorthand )
/* is_comma is for handling
*
* { ..., name, ... }
*
* . This is shorthand of
*
* { ... ,name: name, ... }
*
* .
* In this case, the token variables point to:
*
* { ..., name, ... }
* name-------^ ^
* token----------+
*
*/
bool is_comma = ((!is_es6_class && isType (token, TOKEN_CLOSE_CURLY)) || isType (token, TOKEN_COMMA));
if ( isType (token, TOKEN_COLON) || is_comma || can_be_field || is_shorthand )
{
tokenInfo * comma = NULL;
if (! is_shorthand)
{
readToken (token);
if (isKeyword (token, KEYWORD_async))
if (is_comma)
{
comma = newToken ();
copyToken (comma, token, true);
copyToken (token, name, true);
/*
* { ..., name, ... }
* token -----^ ^
* comma ---------+
*/
}
else
{
readToken (token);
if (isKeyword (token, KEYWORD_async))
readToken (token);
}
}

vString * signature = vStringNew ();
Expand Down Expand Up @@ -2218,7 +2250,19 @@ static bool parseMethods (tokenInfo *const token, int class_index,
else
{
copyToken (saved_token, token, true);
readToken (token);
if (comma)
{
copyToken(token, comma, true);
deleteToken (comma);
comma = NULL;
/*
* { ..., name, ... }
* ^
* token ---------+
*/
}
else
readToken (token);
}
}
deleteToken (saved_token);
Expand All @@ -2235,6 +2279,8 @@ static bool parseMethods (tokenInfo *const token, int class_index,
}

vStringDelete (signature);
if (comma)
deleteToken (comma);

Check warning on line 2283 in parsers/jscript.c

View check run for this annotation

Codecov / codecov/patch

parsers/jscript.c#L2283

Added line #L2283 was not covered by tests
}
else
{
Expand Down

0 comments on commit 68ee390

Please sign in to comment.