diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-05-05 18:05:50 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-06 12:21:29 +0200 |
commit | 419bce69154bd540811705eaf6518d9d14c734e9 (patch) | |
tree | 9be375350bb0fd04a813f6bb61e36c8abb1b6f70 | |
parent | 3142c4a4fd840c7a77fbe1647abba8dabd9d7ebb (diff) | |
download | serenity-419bce69154bd540811705eaf6518d9d14c734e9.zip |
LibJS: Fix syntax error for arrow function non-decl variable assignment
A regression was introduced in dc9b4da where the parser would
incorrectly parse the assignment of arrow functions to (non-declaration)
variables. For example, consider:
a = () => {}
Because the parser was aware of default parameters, in
try_parse_arrow_function, the equals sign would be interpreted as a
default argument, leading to incorrect parsing of the overall
expression. Also resulted in some funny behavior
(a = () => {} => {} worked just fine!).
The simple fix is to only look for default parameters if the arrow
function is required to have parenthesis.
-rw-r--r-- | Libraries/LibJS/Parser.cpp | 2 | ||||
-rw-r--r-- | Libraries/LibJS/Tests/arrow-functions.js | 3 |
2 files changed, 4 insertions, 1 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp index a999b4b325..a665ec2872 100644 --- a/Libraries/LibJS/Parser.cpp +++ b/Libraries/LibJS/Parser.cpp @@ -295,7 +295,7 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe } else if (match(TokenType::Identifier)) { auto parameter_name = consume(TokenType::Identifier).value(); RefPtr<Expression> default_value; - if (match(TokenType::Equals)) { + if (expect_parens && match(TokenType::Equals)) { consume(TokenType::Equals); default_value = parse_expression(0); } diff --git a/Libraries/LibJS/Tests/arrow-functions.js b/Libraries/LibJS/Tests/arrow-functions.js index 56537de34c..6c9f7eadaf 100644 --- a/Libraries/LibJS/Tests/arrow-functions.js +++ b/Libraries/LibJS/Tests/arrow-functions.js @@ -4,6 +4,9 @@ try { let getNumber = () => 42; assert(getNumber() === 42); + getNumber = () => 99; + assert(getNumber() === 99); + let add = (a, b) => a + b; assert(add(2, 3) === 5); |