Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add recoverable error on accessorIsGenerator #11921

Merged
merged 3 commits into from Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -3,6 +3,7 @@

// The Errors key follows https://cs.chromium.org/chromium/src/v8/src/common/message-template.h unless it does not exist
export const ErrorMessages = Object.freeze({
AccessorIsGenerator: "A %0ter cannot be a generator",
ArgumentsDisallowedInInitializer:
"'arguments' is not allowed in class field initializer",
AsyncFunctionInSingleStatementContext:
Expand Down
8 changes: 5 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -1757,8 +1757,11 @@ export default class ExpressionParser extends LValParser {
// set PropertyName[?Yield, ?Await] ( PropertySetParameterList ) { FunctionBody[~Yield, ~Await] }
if (keyName === "get" || keyName === "set") {
isAccessor = true;
isGenerator = this.eat(tt.star); // tt.star is allowed in `maybeAsyncOrAccessorProp`, we will throw in `parseObjectMethod` later
prop.kind = keyName;
if (this.match(tt.star)) {
this.raise(this.state.pos, Errors.AccessorIsGenerator, keyName);
this.next();
}
this.parsePropertyName(prop, /* isPrivateNameAllowed */ false);
}
}
Expand Down Expand Up @@ -1813,8 +1816,7 @@ export default class ExpressionParser extends LValParser {
isAccessor: boolean,
): ?N.ObjectMethod {
if (isAccessor) {
// isAccessor implies isAsync: false, isPattern: false
if (isGenerator) this.unexpected();
// isAccessor implies isAsync: false, isPattern: false, isGenerator: false
this.parseMethod(
prop,
/* isGenerator */ false,
Expand Down
@@ -0,0 +1,4 @@
({
get *iterator() { },
set *iterator(iter) { }
})
@@ -0,0 +1,81 @@
{
"type": "File",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"errors": [
"SyntaxError: A getter can not be a generator (2:9)",
"SyntaxError: A setter can not be a generator (3:9)"
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
],
"program": {
"type": "Program",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":58,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"expression": {
"type": "ObjectExpression",
"start":1,"end":57,"loc":{"start":{"line":1,"column":1},"end":{"line":4,"column":1}},
"properties": [
{
"type": "ObjectMethod",
"start":7,"end":26,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":23}},
"method": false,
"key": {
"type": "Identifier",
"start":12,"end":20,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":17},"identifierName":"iterator"},
"name": "iterator"
},
"computed": false,
"kind": "get",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":23,"end":26,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":23}},
"body": [],
"directives": []
}
},
{
"type": "ObjectMethod",
"start":32,"end":55,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":27}},
"method": false,
"key": {
"type": "Identifier",
"start":37,"end":45,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":17},"identifierName":"iterator"},
"name": "iterator"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":46,"end":50,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":22},"identifierName":"iter"},
"name": "iter"
}
],
"body": {
"type": "BlockStatement",
"start":52,"end":55,"loc":{"start":{"line":3,"column":24},"end":{"line":3,"column":27}},
"body": [],
"directives": []
}
}
],
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
}
],
"directives": []
}
}