mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
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.
This commit is contained in:
parent
3142c4a4fd
commit
419bce6915
Notes:
sideshowbarker
2024-07-19 06:56:26 +09:00
Author: https://github.com/mattco98
Commit: 419bce6915
Pull-request: https://github.com/SerenityOS/serenity/pull/2126
Issue: https://github.com/SerenityOS/serenity/issues/2124
2 changed files with 4 additions and 1 deletions
|
@ -295,7 +295,7 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
|
||||||
} else if (match(TokenType::Identifier)) {
|
} else if (match(TokenType::Identifier)) {
|
||||||
auto parameter_name = consume(TokenType::Identifier).value();
|
auto parameter_name = consume(TokenType::Identifier).value();
|
||||||
RefPtr<Expression> default_value;
|
RefPtr<Expression> default_value;
|
||||||
if (match(TokenType::Equals)) {
|
if (expect_parens && match(TokenType::Equals)) {
|
||||||
consume(TokenType::Equals);
|
consume(TokenType::Equals);
|
||||||
default_value = parse_expression(0);
|
default_value = parse_expression(0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,9 @@ try {
|
||||||
let getNumber = () => 42;
|
let getNumber = () => 42;
|
||||||
assert(getNumber() === 42);
|
assert(getNumber() === 42);
|
||||||
|
|
||||||
|
getNumber = () => 99;
|
||||||
|
assert(getNumber() === 99);
|
||||||
|
|
||||||
let add = (a, b) => a + b;
|
let add = (a, b) => a + b;
|
||||||
assert(add(2, 3) === 5);
|
assert(add(2, 3) === 5);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue