Skip to content

Commit

Permalink
JavaScript: don't analyze inside parameter lists
Browse files Browse the repository at this point in the history
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
  • Loading branch information
masatake committed Jan 12, 2023
1 parent 759a4ab commit 3b52348
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--sort=no
--fields=+S
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,10 @@ url input.js /^ url: '\/en-US\/docs\/Tools\/Scratchpad'$/;" p variable:metadata
metadata input.js /^const metadata = {$/;" v
englishTitle input.js /^ title: englishTitle, \/\/ rename$/;" v
localeTitle input.js /^ title: localeTitle, \/\/ rename$/;" v
userDisplayName input-0.js /^function userDisplayName({displayName: dname}) {$/;" f signature:({displayName: dname})
whois input-0.js /^function whois({displayName, fullName: {firstName: name}}) {$/;" f signature:({displayName, fullName: {firstName: name}})
drawChart input-0.js /^function drawChart({size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}) {$/;" f signature:({size = 'big', coords = {x: 0, y: 0}, radius = 25} = {})
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
13 changes: 13 additions & 0 deletions Units/parser-javascript.r/js-destructural-binding.d/input-0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Derrived from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
function userDisplayName({displayName: dname}) {
return dname;
}

function whois({displayName, fullName: {firstName: name}}) {
return `${displayName} is ${name}`;
}

function drawChart({size = 'big', coords = {x: 0, y: 0}, radius = 25} = {}) {
console.log(size, coords, radius);
// do some chart drawing
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function f({ u, x }) {
return (u + x)
}

f({u: 1, x: 2})
72 changes: 52 additions & 20 deletions parsers/jscript.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ static const keywordTable JsKeywordTable [] = {

/* Recursive functions */
static void readTokenFull (tokenInfo *const token, bool include_newlines, vString *const repr);
static void skipArgumentList (tokenInfo *const token, bool include_newlines, vString *const repr);
static void skipArgumentList (tokenInfo *const token, bool include_newlines);
static void skipParameterList (tokenInfo *const token, bool include_newlines, vString *const repr);
static bool parseFunction (tokenInfo *const token, tokenInfo *const name, const bool is_inside_class);
static bool parseBlock (tokenInfo *const token, int parent_scope);
static bool parseMethods (tokenInfo *const token, int class_index, const bool is_es6_class);
Expand Down Expand Up @@ -1284,7 +1285,10 @@ static void skipBabelDecorator (tokenInfo *token, bool include_newlines, vString
if (isType (token, TOKEN_OPEN_PAREN))
{
/* @(complex ? dec1 : dec2) */
skipArgumentList (token, include_newlines, repr);
if (repr)
skipParameterList (token, include_newlines, repr);
else
skipArgumentList (token, include_newlines);
TRACE_PRINT ("found @(...) style decorator");
}
else if (isType (token, TOKEN_IDENTIFIER))
Expand All @@ -1307,7 +1311,10 @@ static void skipBabelDecorator (tokenInfo *token, bool include_newlines, vString
found_period = true;
else if (isType (token, TOKEN_OPEN_PAREN))
{
skipArgumentList (token, include_newlines, repr);
if (repr)
skipParameterList (token, include_newlines, repr);
else
skipArgumentList (token, include_newlines);
TRACE_PRINT("found @foo(...) style decorator");
break;
}
Expand Down Expand Up @@ -1384,20 +1391,17 @@ static int parseMethodsInAnonymousObject (tokenInfo *const token)
return index;
}

static void skipArgumentList (tokenInfo *const token, bool include_newlines, vString *const repr)
static void skipArgumentList (tokenInfo *const token, bool include_newlines)
{
TRACE_ENTER();

if (isType (token, TOKEN_OPEN_PAREN)) /* arguments? */
{
int nest_level = 1;
if (repr)
vStringPut (repr, '(');

tokenType prev_token_type = token->type;
while (nest_level > 0 && ! isType (token, TOKEN_EOF))
{
readTokenFull (token, false, repr);
readToken (token);
if (isType (token, TOKEN_OPEN_PAREN))
nest_level++;
else if (isType (token, TOKEN_CLOSE_PAREN))
Expand All @@ -1419,6 +1423,30 @@ static void skipArgumentList (tokenInfo *const token, bool include_newlines, vSt
TRACE_LEAVE();
}

static void skipParameterList (tokenInfo *const token, bool include_newlines, vString *const repr)
{
TRACE_ENTER_TEXT("repr = %p", repr);

Assert (repr);
if (isType (token, TOKEN_OPEN_PAREN)) /* parameter? */
{
int nest_level = 1;
if (repr)
vStringPut (repr, '(');

while (nest_level > 0 && ! isType (token, TOKEN_EOF))
{
readTokenFull (token, false, repr);
if (isType (token, TOKEN_OPEN_PAREN))
nest_level++;
else if (isType (token, TOKEN_CLOSE_PAREN))
nest_level--;
}
readTokenFull (token, include_newlines, NULL);
}
TRACE_LEAVE();
}

static void skipArrayList (tokenInfo *const token, bool include_newlines)
{
/*
Expand Down Expand Up @@ -1496,7 +1524,7 @@ static bool findCmdTerm (tokenInfo *const token, bool include_newlines, bool inc
readTokenFull (token, include_newlines, NULL);
}
else if ( isType (token, TOKEN_OPEN_PAREN) )
skipArgumentList(token, include_newlines, NULL);
skipArgumentList(token, include_newlines);
else if ( isType (token, TOKEN_OPEN_SQUARE) )
skipArrayList(token, include_newlines);
else
Expand Down Expand Up @@ -1524,7 +1552,7 @@ static void parseSwitch (tokenInfo *const token)

if (isType (token, TOKEN_OPEN_PAREN))
{
skipArgumentList(token, false, NULL);
skipArgumentList(token, false);
}

if (isType (token, TOKEN_OPEN_CURLY))
Expand Down Expand Up @@ -1563,7 +1591,7 @@ static bool parseLoop (tokenInfo *const token)
readToken(token);

if (isType (token, TOKEN_OPEN_PAREN))
skipArgumentList(token, false, NULL);
skipArgumentList(token, false);

if (isType (token, TOKEN_OPEN_CURLY))
parseBlock (token, CORK_NIL);
Expand All @@ -1587,7 +1615,7 @@ static bool parseLoop (tokenInfo *const token)
readToken(token);

if (isType (token, TOKEN_OPEN_PAREN))
skipArgumentList(token, true, NULL);
skipArgumentList(token, true);

if (! isType (token, TOKEN_SEMICOLON))
{
Expand Down Expand Up @@ -1657,7 +1685,7 @@ static bool parseIf (tokenInfo *const token)
}

if (isType (token, TOKEN_OPEN_PAREN))
skipArgumentList(token, false, NULL);
skipArgumentList(token, false);

if (isType (token, TOKEN_OPEN_CURLY))
parseBlock (token, CORK_NIL);
Expand Down Expand Up @@ -1775,7 +1803,7 @@ static bool parseFunction (tokenInfo *const token, tokenInfo *const lhs_name, co
readToken (token);

if ( isType (token, TOKEN_OPEN_PAREN) )
skipArgumentList(token, false, signature);
skipParameterList(token, false, signature);

if ( isType (token, TOKEN_OPEN_CURLY) )
{
Expand Down Expand Up @@ -2086,7 +2114,7 @@ static bool parseMethods (tokenInfo *const token, int class_index,
}
if ( isType (token, TOKEN_OPEN_PAREN) )
{
skipArgumentList(token, false, signature);
skipParameterList(token, false, signature);
}

function:
Expand Down Expand Up @@ -2135,7 +2163,7 @@ static bool parseMethods (tokenInfo *const token, int class_index,
else if (isType (token, TOKEN_OPEN_PAREN))
{
vStringClear (signature);
skipArgumentList (token, false, signature);
skipParameterList (token, false, signature);
}
else if (isType (token, TOKEN_OPEN_SQUARE))
{
Expand Down Expand Up @@ -2386,8 +2414,12 @@ static bool parsePrototype (tokenInfo *const name, tokenInfo *const token, state
! isType (method_body_token, TOKEN_EOF))
{
if ( isType (method_body_token, TOKEN_OPEN_PAREN) )
skipArgumentList(method_body_token, false,
vStringLength (signature) == 0 ? signature : NULL);
{
if (vStringIsEmpty (signature))
skipParameterList(method_body_token, false, signature);
else
skipArgumentList(method_body_token, false);
}
else
readToken (method_body_token);
}
Expand Down Expand Up @@ -2600,7 +2632,7 @@ static bool parseStatementRHS (tokenInfo *const name, tokenInfo *const token, st
readToken (token);

if ( isType (token, TOKEN_OPEN_PAREN) )
skipArgumentList(token, true, NULL);
skipArgumentList(token, true);

if (isType (token, TOKEN_SEMICOLON) && token->nestLevel == 0)
{
Expand Down Expand Up @@ -2992,7 +3024,7 @@ static bool parseStatement (tokenInfo *const token, bool is_inside_class)
readTokenFull (token, true, NULL);

if ( isType (token, TOKEN_OPEN_PAREN) )
skipArgumentList(token, false, NULL);
skipArgumentList(token, false);

if ( isType (token, TOKEN_OPEN_SQUARE) )
skipArrayList(token, false);
Expand Down

0 comments on commit 3b52348

Please sign in to comment.