[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}
This commit is contained in:
caitpotter88 2015-03-10 12:24:49 -07:00 committed by Commit bot
parent 040225a315
commit f950ddf581
2 changed files with 35 additions and 1 deletions

View File

@ -538,6 +538,7 @@ class ParserBase : public Traits {
}
void ReportUnexpectedToken(Token::Value token);
void ReportUnexpectedTokenAt(Scanner::Location location, Token::Value token);
// Recursive descent functions:
@ -1681,7 +1682,13 @@ ParserBase<Traits>::FunctionState::~FunctionState() {
template<class Traits>
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
switch (token) {
@ -2849,6 +2856,15 @@ typename ParserBase<Traits>::ExpressionT
ParserBase<Traits>::ParseArrowFunctionLiteral(int start_pos,
ExpressionT params_ast,
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);
typename Traits::Type::StatementList body;
int num_parameters = -1;

View File

@ -5564,3 +5564,21 @@ TEST(StrongForIn) {
RunParserSyncTest(strong_context_data, data, kError, NULL, 0, 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));
}