Skip to content

Commit b874e3d

Browse files
ajkleinCommit bot
authored andcommitted
Treat yield expressions as an AssignmentPattern error
They were already treated as a BindingPattern error; this patch simply replaces that call with one marking them as both a binding and assignment error, and adds parsing tests for both cases. BUG=v8:4707 LOG=n Review URL: https://codereview.chromium.org/1632303002 Cr-Commit-Position: refs/heads/master@{#33528}
1 parent 7068caf commit b874e3d

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/parsing/parser-base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2108,7 +2108,8 @@ ParserBase<Traits>::ParseYieldExpression(ExpressionClassifier* classifier,
21082108
// YieldExpression ::
21092109
// 'yield' ([no line terminator] '*'? AssignmentExpression)?
21102110
int pos = peek_position();
2111-
BindingPatternUnexpectedToken(classifier);
2111+
classifier->RecordPatternError(scanner()->peek_location(),
2112+
MessageTemplate::kInvalidDestructuringTarget);
21122113
FormalParameterInitializerUnexpectedToken(classifier);
21132114
Expect(Token::YIELD, CHECK_OK);
21142115
ExpressionT generator_object =

test/cctest/test-parsing.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,7 @@ TEST(ErrorsYieldSloppy) {
23082308

23092309

23102310
TEST(NoErrorsGenerator) {
2311+
// clang-format off
23112312
const char* context_data[][2] = {
23122313
{ "function * gen() {", "}" },
23132314
{ "(function * gen() {", "})" },
@@ -2345,6 +2346,8 @@ TEST(NoErrorsGenerator) {
23452346
// Yield is still a valid key in object literals.
23462347
"({ yield: 1 })",
23472348
"({ get yield() { } })",
2349+
// And in assignment pattern computed properties
2350+
"({ [yield]: x } = { })",
23482351
// Yield without RHS.
23492352
"yield;",
23502353
"yield",
@@ -2362,12 +2365,17 @@ TEST(NoErrorsGenerator) {
23622365
"yield\nfor (;;) {}",
23632366
NULL
23642367
};
2368+
// clang-format on
23652369

2366-
RunParserSyncTest(context_data, statement_data, kSuccess);
2370+
static const ParserFlag always_flags[] = {
2371+
kAllowHarmonyDestructuringAssignment};
2372+
RunParserSyncTest(context_data, statement_data, kSuccess, nullptr, 0,
2373+
always_flags, arraysize(always_flags));
23672374
}
23682375

23692376

23702377
TEST(ErrorsYieldGenerator) {
2378+
// clang-format off
23712379
const char* context_data[][2] = {
23722380
{ "function * gen() {", "}" },
23732381
{ "\"use strict\"; function * gen() {", "}" },
@@ -2408,8 +2416,19 @@ TEST(ErrorsYieldGenerator) {
24082416
"yield\n{yield: 42}",
24092417
"yield /* comment */\n {yield: 42}",
24102418
"yield //comment\n {yield: 42}",
2419+
// Destructuring binding and assignment are both disallowed
2420+
"var [yield] = [42];",
2421+
"var {foo: yield} = {a: 42};",
2422+
"[yield] = [42];",
2423+
"({a: yield} = {a: 42});",
2424+
// Also disallow full yield expressions on LHS
2425+
"var [yield 24] = [42];",
2426+
"var {foo: yield 24} = {a: 42};",
2427+
"[yield 24] = [42];",
2428+
"({a: yield 24} = {a: 42});",
24112429
NULL
24122430
};
2431+
// clang-format on
24132432

24142433
RunParserSyncTest(context_data, statement_data, kError);
24152434
}

0 commit comments

Comments
 (0)