refactor: add recoverable error on accessorIsGenerator (#11921)

* refactor: add recoverable error on accessorIsGenerator

* Update packages/babel-parser/src/parser/error-message.js

Co-authored-by: Brian Ng <bng412@gmail.com>

* Apply suggestions from code review

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
Huáng Jùnliàng
2020-08-05 17:52:13 -04:00
committed by GitHub
parent c0f6f0394d
commit cd577eedfd
4 changed files with 91 additions and 3 deletions

View File

@@ -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:

View File

@@ -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);
}
}
@@ -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,