Commit 1ac09655 authored by jwolfe's avatar jwolfe Committed by Commit bot

Allow trailing commas in function parameter lists

Add a flag harmony_trailing_commas_in_parameters that allows trailing
commas in function parameter declaration lists and function call
parameter lists. Trailing commas are allowed in parenthetical lists like
`(a, b, c,)` only if the next token is `=>`, thereby making it an arrow
function declaration. Only 1 trailing comma is allowed, not `(a,,)`. A
trailing comma must follow a non-rest parameter, so `(,)` and `(...a,)`
are still SyntaxErrors. However, a trailing comma is allowed after a
spread parameter, e.g. `a(...b,);`.

Add parser tests for all of the above.

BUG=v8:5051
LOG=y

Review-Url: https://codereview.chromium.org/2094463002
Cr-Commit-Position: refs/heads/master@{#37355}
parent fa5cb207
......@@ -2697,6 +2697,7 @@ EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(icu_case_mapping)
#endif
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_async_await)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_restrictive_generators)
EMPTY_INITIALIZE_GLOBAL_FOR_FEATURE(harmony_trailing_commas)
void InstallPublicSymbol(Factory* factory, Handle<Context> native_context,
const char* name, Handle<Symbol> value) {
......@@ -3275,6 +3276,7 @@ bool Genesis::InstallExperimentalNatives() {
static const char* harmony_async_await_natives[] = {
"native harmony-async-await.js", nullptr};
static const char* harmony_restrictive_generators_natives[] = {nullptr};
static const char* harmony_trailing_commas_natives[] = {nullptr};
for (int i = ExperimentalNatives::GetDebuggerCount();
i < ExperimentalNatives::GetBuiltinsCount(); i++) {
......
......@@ -208,7 +208,9 @@ DEFINE_IMPLICATION(es_staging, move_object_start)
"harmony restrictions on generator declarations") \
V(harmony_regexp_named_captures, "harmony regexp named captures") \
V(harmony_regexp_property, "harmony unicode regexp property classes") \
V(harmony_for_in, "harmony for-in syntax")
V(harmony_for_in, "harmony for-in syntax") \
V(harmony_trailing_commas, \
"harmony trailing commas in function parameter lists")
// Features that are complete (but still behind --harmony/es-staging flag).
#define HARMONY_STAGED_BASE(V) \
......
......@@ -198,7 +198,8 @@ class ParserBase : public Traits {
allow_harmony_for_in_(false),
allow_harmony_function_sent_(false),
allow_harmony_async_await_(false),
allow_harmony_restrictive_generators_(false) {}
allow_harmony_restrictive_generators_(false),
allow_harmony_trailing_commas_(false) {}
#define ALLOW_ACCESSORS(name) \
bool allow_##name() const { return allow_##name##_; } \
......@@ -219,6 +220,7 @@ class ParserBase : public Traits {
ALLOW_ACCESSORS(harmony_function_sent);
ALLOW_ACCESSORS(harmony_async_await);
ALLOW_ACCESSORS(harmony_restrictive_generators);
ALLOW_ACCESSORS(harmony_trailing_commas);
SCANNER_ACCESSORS(harmony_exponentiation_operator);
#undef SCANNER_ACCESSORS
......@@ -1189,6 +1191,7 @@ class ParserBase : public Traits {
bool allow_harmony_function_sent_;
bool allow_harmony_async_await_;
bool allow_harmony_restrictive_generators_;
bool allow_harmony_trailing_commas_;
};
template <class Traits>
......@@ -1700,7 +1703,11 @@ typename ParserBase<Traits>::ExpressionT ParserBase<Traits>::ParseExpression(
}
Consume(Token::COMMA);
bool is_rest = false;
if (peek() == Token::ELLIPSIS) {
if (allow_harmony_trailing_commas() && peek() == Token::RPAREN &&
PeekAhead() == Token::ARROW) {
// a trailing comma is allowed at the end of an arrow parameter list
break;
} else if (peek() == Token::ELLIPSIS) {
// 'x, y, ...z' in CoverParenthesizedExpressionAndArrowParameterList only
// as the formal parameters of'(x, y, ...z) => foo', and is not itself a
// valid expression or binding pattern.
......@@ -2196,6 +2203,10 @@ typename Traits::Type::ExpressionList ParserBase<Traits>::ParseArguments(
done = (peek() != Token::COMMA);
if (!done) {
Next();
if (allow_harmony_trailing_commas() && peek() == Token::RPAREN) {
// allow trailing comma
done = true;
}
}
}
Scanner::Location location = scanner_->location();
......@@ -3204,24 +3215,21 @@ void ParserBase<Traits>::ParseFormalParameter(
template <class Traits>
void ParserBase<Traits>::ParseFormalParameterList(
FormalParametersT* parameters, ExpressionClassifier* classifier, bool* ok) {
// FormalParameters[Yield,GeneratorParameter] :
// FormalParameters[Yield] :
// [empty]
// FormalParameterList[?Yield, ?GeneratorParameter]
//
// FormalParameterList[Yield,GeneratorParameter] :
// FunctionRestParameter[?Yield]
// FormalsList[?Yield, ?GeneratorParameter]
// FormalsList[?Yield, ?GeneratorParameter] , FunctionRestParameter[?Yield]
// FormalParameterList[?Yield]
// FormalParameterList[?Yield] ,
// FormalParameterList[?Yield] , FunctionRestParameter[?Yield]
//
// FormalsList[Yield,GeneratorParameter] :
// FormalParameter[?Yield, ?GeneratorParameter]
// FormalsList[?Yield, ?GeneratorParameter] ,
// FormalParameter[?Yield,?GeneratorParameter]
// FormalParameterList[Yield] :
// FormalParameter[?Yield]
// FormalParameterList[?Yield] , FormalParameter[?Yield]
DCHECK_EQ(0, parameters->Arity());
if (peek() != Token::RPAREN) {
do {
while (true) {
if (parameters->Arity() > Code::kMaxArguments) {
ReportMessage(MessageTemplate::kTooManyParameters);
*ok = false;
......@@ -3230,16 +3238,22 @@ void ParserBase<Traits>::ParseFormalParameterList(
parameters->has_rest = Check(Token::ELLIPSIS);
ParseFormalParameter(parameters, classifier, ok);
if (!*ok) return;
} while (!parameters->has_rest && Check(Token::COMMA));
if (parameters->has_rest) {
parameters->is_simple = false;
classifier->RecordNonSimpleParameter();
if (peek() == Token::COMMA) {
ReportMessageAt(scanner()->peek_location(),
MessageTemplate::kParamAfterRest);
*ok = false;
return;
if (parameters->has_rest) {
parameters->is_simple = false;
classifier->RecordNonSimpleParameter();
if (peek() == Token::COMMA) {
ReportMessageAt(scanner()->peek_location(),
MessageTemplate::kParamAfterRest);
*ok = false;
return;
}
break;
}
if (!Check(Token::COMMA)) break;
if (allow_harmony_trailing_commas() && peek() == Token::RPAREN) {
// allow the trailing comma
break;
}
}
}
......
......@@ -815,6 +815,7 @@ Parser::Parser(ParseInfo* info)
FLAG_harmony_exponentiation_operator);
set_allow_harmony_async_await(FLAG_harmony_async_await);
set_allow_harmony_restrictive_generators(FLAG_harmony_restrictive_generators);
set_allow_harmony_trailing_commas(FLAG_harmony_trailing_commas);
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
++feature) {
use_counts_[feature] = 0;
......@@ -4876,6 +4877,7 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser(
SET_ALLOW(harmony_exponentiation_operator);
SET_ALLOW(harmony_restrictive_declarations);
SET_ALLOW(harmony_async_await);
SET_ALLOW(harmony_trailing_commas);
#undef SET_ALLOW
}
PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction(
......
......@@ -1511,6 +1511,7 @@ enum ParserFlag {
kAllowHarmonyForIn,
kAllowHarmonyAsyncAwait,
kAllowHarmonyRestrictiveGenerators,
kAllowHarmonyTrailingCommas,
};
enum ParserSyncTestResult {
......@@ -1535,6 +1536,8 @@ void SetParserFlags(i::ParserBase<Traits>* parser,
flags.Contains(kAllowHarmonyAsyncAwait));
parser->set_allow_harmony_restrictive_generators(
flags.Contains(kAllowHarmonyRestrictiveGenerators));
parser->set_allow_harmony_trailing_commas(
flags.Contains(kAllowHarmonyTrailingCommas));
}
......@@ -7840,3 +7843,107 @@ TEST(NoDuplicateAsyncFunctionInBlock) {
RunParserSyncTest(top_level_context_data, error_data, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
}
TEST(TrailingCommasInParameters) {
// clang-format off
const char* context_data[][2] = {
{ "", "" },
{ "'use strict';", "" },
{ "function foo() {", "}" },
{ "function foo() {'use strict';", "}" },
{ NULL, NULL }
};
const char* data[] = {
" function a(b,) {}",
" function* a(b,) {}",
"(function a(b,) {});",
"(function* a(b,) {});",
"(function (b,) {});",
"(function* (b,) {});",
" function a(b,c,d,) {}",
" function* a(b,c,d,) {}",
"(function a(b,c,d,) {});",
"(function* a(b,c,d,) {});",
"(function (b,c,d,) {});",
"(function* (b,c,d,) {});",
"(b,) => {};",
"(b,c,d,) => {};",
"a(1,);",
"a(1,2,3,);",
"a(...[],);",
"a(1, 2, ...[],);",
"a(...[], 2, ...[],);",
NULL
};
// clang-format on
static const ParserFlag always_flags[] = {kAllowHarmonyTrailingCommas};
RunParserSyncTest(context_data, data, kSuccess, NULL, 0, always_flags,
arraysize(always_flags));
}
TEST(TrailingCommasInParametersErrors) {
// clang-format off
const char* context_data[][2] = {
{ "", "" },
{ "'use strict';", "" },
{ "function foo() {", "}" },
{ "function foo() {'use strict';", "}" },
{ NULL, NULL }
};
const char* data[] = {
// too many trailing commas
" function a(b,,) {}",
" function* a(b,,) {}",
"(function a(b,,) {});",
"(function* a(b,,) {});",
"(function (b,,) {});",
"(function* (b,,) {});",
" function a(b,c,d,,) {}",
" function* a(b,c,d,,) {}",
"(function a(b,c,d,,) {});",
"(function* a(b,c,d,,) {});",
"(function (b,c,d,,) {});",
"(function* (b,c,d,,) {});",
"(b,,) => {};",
"(b,c,d,,) => {};",
"a(1,,);",
"a(1,2,3,,);",
// only a trailing comma and no parameters
" function a1(,) {}",
" function* a2(,) {}",
"(function a3(,) {});",
"(function* a4(,) {});",
"(function (,) {});",
"(function* (,) {});",
"(,) => {};",
"a1(,);",
// no trailing commas after rest parameter declaration
" function a(...b,) {}",
" function* a(...b,) {}",
"(function a(...b,) {});",
"(function* a(...b,) {});",
"(function (...b,) {});",
"(function* (...b,) {});",
" function a(b, c, ...d,) {}",
" function* a(b, c, ...d,) {}",
"(function a(b, c, ...d,) {});",
"(function* a(b, c, ...d,) {});",
"(function (b, c, ...d,) {});",
"(function* (b, c, ...d,) {});",
"(...b,) => {};",
"(b, c, ...d,) => {};",
// parenthesized trailing comma without arrow is still an error
"(,);",
"(a,);",
"(a,b,c,);",
NULL
};
// clang-format on
static const ParserFlag always_flags[] = {kAllowHarmonyTrailingCommas};
RunParserSyncTest(context_data, data, kError, NULL, 0, always_flags,
arraysize(always_flags));
}
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --harmony-trailing-commas
function f1(a,) {}
function f2(a,b,) {}
function f3(a,b,c,) {}
assertEquals(1, f1.length);
assertEquals(2, f2.length);
assertEquals(3, f3.length);
function* g1(a,) {}
function* g2(a,b,) {}
function* g3(a,b,c,) {}
assertEquals(1, g1.length);
assertEquals(2, g2.length);
assertEquals(3, g3.length);
assertEquals(1, (function(a,) {}).length);
assertEquals(2, (function(a,b,) {}).length);
assertEquals(3, (function(a,b,c,) {}).length);
assertEquals(1, (function*(a,) {}).length);
assertEquals(2, (function*(a,b,) {}).length);
assertEquals(3, (function*(a,b,c,) {}).length);
assertEquals(1, ((a,) => {}).length);
assertEquals(2, ((a,b,) => {}).length);
assertEquals(3, ((a,b,c,) => {}).length);
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