Commit f950ddf5 authored by caitpotter88's avatar caitpotter88 Committed by Commit bot

[parser] parse arrow function only if no linefeed before =>

BUG=v8:3954
LOG=N
R=arv@chromium.org

Review URL: https://codereview.chromium.org/987203003

Cr-Commit-Position: refs/heads/master@{#27122}
parent 040225a3
...@@ -538,6 +538,7 @@ class ParserBase : public Traits { ...@@ -538,6 +538,7 @@ class ParserBase : public Traits {
} }
void ReportUnexpectedToken(Token::Value token); void ReportUnexpectedToken(Token::Value token);
void ReportUnexpectedTokenAt(Scanner::Location location, Token::Value token);
// Recursive descent functions: // Recursive descent functions:
...@@ -1681,7 +1682,13 @@ ParserBase<Traits>::FunctionState::~FunctionState() { ...@@ -1681,7 +1682,13 @@ ParserBase<Traits>::FunctionState::~FunctionState() {
template<class Traits> template<class Traits>
void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) { void ParserBase<Traits>::ReportUnexpectedToken(Token::Value token) {
Scanner::Location source_location = scanner()->location(); return ReportUnexpectedTokenAt(scanner_->location(), token);
}
template<class Traits>
void ParserBase<Traits>::ReportUnexpectedTokenAt(
Scanner::Location source_location, Token::Value token) {
// Four of the tokens are treated specially // Four of the tokens are treated specially
switch (token) { switch (token) {
...@@ -2849,6 +2856,15 @@ typename ParserBase<Traits>::ExpressionT ...@@ -2849,6 +2856,15 @@ typename ParserBase<Traits>::ExpressionT
ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos, ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos,
ExpressionT params_ast, ExpressionT params_ast,
bool* ok) { bool* ok) {
if (peek() == Token::ARROW && scanner_->HasAnyLineTerminatorBeforeNext()) {
// ASI inserts `;` after arrow parameters if a line terminator is found.
// `=> ...` is never a valid expression, so report as syntax error.
// If next token is not `=>`, it's a syntax error anyways.
ReportUnexpectedTokenAt(scanner_->peek_location(), Token::ARROW);
*ok = false;
return this->EmptyExpression();
}
Scope* scope = this->NewScope(scope_, ARROW_SCOPE); Scope* scope = this->NewScope(scope_, ARROW_SCOPE);
typename Traits::Type::StatementList body; typename Traits::Type::StatementList body;
int num_parameters = -1; int num_parameters = -1;
......
...@@ -5564,3 +5564,21 @@ TEST(StrongForIn) { ...@@ -5564,3 +5564,21 @@ TEST(StrongForIn) {
RunParserSyncTest(strong_context_data, data, kError, NULL, 0, always_flags, RunParserSyncTest(strong_context_data, data, kError, NULL, 0, always_flags,
arraysize(always_flags)); arraysize(always_flags));
} }
TEST(ArrowFunctionASIErrors) {
const char* context_data[][2] = {{"'use strict';", ""}, {"", ""},
{NULL, NULL}};
const char* data[] = {
"(a\n=> a)(1)",
"(a/*\n*/=> a)(1)",
"((a)\n=> a)(1)",
"((a)/*\n*/=> a)(1)",
"((a, b)\n=> a + b)(1, 2)",
"((a, b)/*\n*/=> a + b)(1, 2)",
NULL};
static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions};
RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
arraysize(always_flags));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment