Fix invalid setter parse (#12076)

* Fix invalid `setter` parse

* estree

Co-authored-by: Brian Ng <bng412@gmail.com>
This commit is contained in:
fisker Cheung
2020-09-19 16:03:35 +08:00
committed by GitHub
parent ae18f9c0d9
commit 18d13d0032
6 changed files with 123 additions and 18 deletions

View File

@@ -1795,12 +1795,20 @@ export default class ExpressionParser extends LValParser {
return method.kind === "get" ? 0 : 1;
}
// This exists so we can override within the ESTree plugin
getObjectOrClassMethodParams(method: N.ObjectMethod | N.ClassMethod) {
return method.params;
}
// get methods aren't allowed to have any parameters
// set methods must have exactly 1 parameter which is not a rest parameter
checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void {
const paramCount = this.getGetterSetterExpectedParamCount(method);
const params = this.getObjectOrClassMethodParams(method);
const start = method.start;
if (method.params.length !== paramCount) {
if (params.length !== paramCount) {
if (method.kind === "get") {
this.raise(start, Errors.BadGetterArity);
} else {
@@ -1810,7 +1818,7 @@ export default class ExpressionParser extends LValParser {
if (
method.kind === "set" &&
method.params[method.params.length - 1].type === "RestElement"
params[params.length - 1]?.type === "RestElement"
) {
this.raise(start, Errors.BadSetterRestParameter);
}

View File

@@ -105,22 +105,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void {
const prop = ((method: any): N.EstreeProperty | N.EstreeMethodDefinition);
const paramCount = prop.kind === "get" ? 0 : 1;
const start = prop.start;
if (prop.value.params.length !== paramCount) {
if (method.kind === "get") {
this.raise(start, Errors.BadGetterArity);
} else {
this.raise(start, Errors.BadSetterArity);
}
} else if (
prop.kind === "set" &&
prop.value.params[0].type === "RestElement"
) {
this.raise(start, Errors.BadSetterRestParameter);
}
getObjectOrClassMethodParams(method: N.ObjectMethod | N.ClassMethod) {
return ((method: any): N.EstreeProperty | N.EstreeMethodDefinition).value
.params;
}
checkLVal(