From 68ee390032f927557e20cccdfa25b43d933e24a2 Mon Sep 17 00:00:00 2001 From: Masatake YAMATO Date: Fri, 13 Jan 2023 08:32:58 +0900 Subject: [PATCH] JavaScript: extract name in { ..., name, ...}, a shorthand for { ..., name: name, ...} Signed-off-by: Masatake YAMATO --- .../js-destructural-binding.d/expected.tags | 5 ++ .../js-destructural-binding.d/input-2.js | 8 +++ parsers/jscript.c | 54 +++++++++++++++++-- 3 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 Units/parser-javascript.r/js-destructural-binding.d/input-2.js diff --git a/Units/parser-javascript.r/js-destructural-binding.d/expected.tags b/Units/parser-javascript.r/js-destructural-binding.d/expected.tags index aeb61943da..a9294b92f3 100644 --- a/Units/parser-javascript.r/js-destructural-binding.d/expected.tags +++ b/Units/parser-javascript.r/js-destructural-binding.d/expected.tags @@ -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 diff --git a/Units/parser-javascript.r/js-destructural-binding.d/input-2.js b/Units/parser-javascript.r/js-destructural-binding.d/input-2.js new file mode 100644 index 0000000000..d024f3bbca --- /dev/null +++ b/Units/parser-javascript.r/js-destructural-binding.d/input-2.js @@ -0,0 +1,8 @@ +function f(x, y, z) { + return x + y + z +} + +x = 1 +y = 1 +z = 1 +f({x, y, z}) diff --git a/parsers/jscript.c b/parsers/jscript.c index 4e2be9179d..d069ad67a8 100644 --- a/parsers/jscript.c +++ b/parsers/jscript.c @@ -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 (); @@ -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); @@ -2235,6 +2279,8 @@ static bool parseMethods (tokenInfo *const token, int class_index, } vStringDelete (signature); + if (comma) + deleteToken (comma); } else {