summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-10-19 00:29:17 +0100
committerAndreas Kling <kling@serenityos.org>2020-10-19 11:31:55 +0200
commite898c988737b012134bb4ba7e12c1db9eb48714e (patch)
treedd433e446f6ff16089a6d5ab71dfa2042b28dfec
parent965d952ff32e3b87b860e7d5fd488755332665e1 (diff)
downloadserenity-e898c988737b012134bb4ba7e12c1db9eb48714e.zip
LibJS: Don't parse arrow function with newline between ) and =>
If there's a newline between the closing paren and arrow it's not a valid arrow function, ASI should kick in instead (it'll then fail with "Unexpected token Arrow")
-rw-r--r--Libraries/LibJS/Parser.cpp4
-rw-r--r--Libraries/LibJS/Tests/functions/arrow-functions.js1
2 files changed, 5 insertions, 0 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index a612ff59e2..b725982005 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -353,6 +353,10 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
return nullptr;
parameters.append({ consume().value(), {} });
}
+ // If there's a newline between the closing paren and arrow it's not a valid arrow function,
+ // ASI should kick in instead (it'll then fail with "Unexpected token Arrow")
+ if (m_parser_state.m_current_token.trivia().contains('\n'))
+ return nullptr;
if (!match(TokenType::Arrow))
return nullptr;
consume();
diff --git a/Libraries/LibJS/Tests/functions/arrow-functions.js b/Libraries/LibJS/Tests/functions/arrow-functions.js
index cb741e2a17..6af8790514 100644
--- a/Libraries/LibJS/Tests/functions/arrow-functions.js
+++ b/Libraries/LibJS/Tests/functions/arrow-functions.js
@@ -160,4 +160,5 @@ test("syntax errors", () => {
expect("(a b) => {}").not.toEval();
expect("(a ...b) => {}").not.toEval();
expect("(a = 1 = 2) => {}").not.toEval();
+ expect("()\n=> {}").not.toEval();
});