[es6] Fix default parameters in arrow functions
R=adamk@chromium.org, wingo@igalia.com BUG=v8:811 LOG=N Review URL: https://codereview.chromium.org/1314543005 Cr-Commit-Position: refs/heads/master@{#30338}
This commit is contained in:
parent
032a35ffb6
commit
ff932fe8f4
@ -3904,6 +3904,7 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
|
||||
parameters->is_simple = !is_rest && expr->IsVariableProxy();
|
||||
}
|
||||
|
||||
Expression* initializer = nullptr;
|
||||
if (expr->IsVariableProxy()) {
|
||||
// When the formal parameter was originally seen, it was parsed as a
|
||||
// VariableProxy and recorded as unresolved in the scope. Here we undo that
|
||||
@ -3911,18 +3912,12 @@ void ParserTraits::ParseArrowFunctionFormalParameters(
|
||||
// patterns; for patterns that happens uniformly in
|
||||
// PatternRewriter::VisitVariableProxy).
|
||||
parser_->scope_->RemoveUnresolved(expr->AsVariableProxy());
|
||||
}
|
||||
|
||||
Expression* initializer = nullptr;
|
||||
if (!is_rest && parser_->allow_harmony_default_parameters() &&
|
||||
parser_->Check(Token::ASSIGN)) {
|
||||
ExpressionClassifier init_classifier;
|
||||
initializer =
|
||||
parser_->ParseAssignmentExpression(true, &init_classifier, ok);
|
||||
if (!*ok) return;
|
||||
parser_->ValidateExpression(&init_classifier, ok);
|
||||
if (!*ok) return;
|
||||
parameters->is_simple = false;
|
||||
} else if (expr->IsAssignment()) {
|
||||
Assignment* assignment = expr->AsAssignment();
|
||||
DCHECK(parser_->allow_harmony_default_parameters());
|
||||
DCHECK(!assignment->is_compound());
|
||||
initializer = assignment->value();
|
||||
expr = assignment->target();
|
||||
}
|
||||
|
||||
AddFormalParameter(parameters, expr, initializer, is_rest);
|
||||
|
@ -1426,6 +1426,7 @@ enum ParserFlag {
|
||||
kAllowLazy,
|
||||
kAllowNatives,
|
||||
kAllowHarmonyArrowFunctions,
|
||||
kAllowHarmonyDefaultParameters,
|
||||
kAllowHarmonyRestParameters,
|
||||
kAllowHarmonySloppy,
|
||||
kAllowHarmonySloppyLet,
|
||||
@ -1451,6 +1452,8 @@ void SetParserFlags(i::ParserBase<Traits>* parser,
|
||||
parser->set_allow_natives(flags.Contains(kAllowNatives));
|
||||
parser->set_allow_harmony_arrow_functions(
|
||||
flags.Contains(kAllowHarmonyArrowFunctions));
|
||||
parser->set_allow_harmony_default_parameters(
|
||||
flags.Contains(kAllowHarmonyDefaultParameters));
|
||||
parser->set_allow_harmony_rest_parameters(
|
||||
flags.Contains(kAllowHarmonyRestParameters));
|
||||
parser->set_allow_harmony_spreadcalls(
|
||||
@ -3730,10 +3733,30 @@ TEST(NoErrorsArrowFunctions) {
|
||||
|
||||
// Arrow has more precedence, this is the same as: foo ? bar : (baz = {})
|
||||
"foo ? bar : baz => {}",
|
||||
|
||||
// Arrows with non-simple parameters.
|
||||
"({a}) => {}",
|
||||
"(x = 9) => {}",
|
||||
"(x, y = 9) => {}",
|
||||
"(x = 9, y) => {}",
|
||||
"(x, y = 9, z) => {}",
|
||||
"(x, y = 9, z = 8) => {}",
|
||||
"(...a) => {}",
|
||||
"(x, ...a) => {}",
|
||||
"(x = 9, ...a) => {}",
|
||||
"(x, y = 9, ...a) => {}",
|
||||
"(x, y = 9, {b}, z = 8, ...a) => {}",
|
||||
// TODO(wingo, rossberg): This is not accepted right now.
|
||||
// "({a} = {}) => {}",
|
||||
// "([x] = []) => {}",
|
||||
"({a = 42}) => {}",
|
||||
"([x = 0]) => {}",
|
||||
NULL
|
||||
};
|
||||
|
||||
static const ParserFlag always_flags[] = {kAllowHarmonyArrowFunctions};
|
||||
static const ParserFlag always_flags[] = {
|
||||
kAllowHarmonyArrowFunctions, kAllowHarmonyDefaultParameters,
|
||||
kAllowHarmonyRestParameters, kAllowHarmonyDestructuring};
|
||||
RunParserSyncTest(context_data, statement_data, kSuccess, NULL, 0,
|
||||
always_flags, arraysize(always_flags));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user