[TS] Allow context type annotation on getters/setters (#9641)

* Allow context type annotation on getters/setters

* Extract getAccessorsExpectedParamCount
This commit is contained in:
Matt Tingen
2019-03-06 16:54:42 -05:00
committed by Nicolò Ribaudo
parent fba5655a44
commit e53be4b387
6 changed files with 441 additions and 2 deletions

View File

@@ -1564,10 +1564,16 @@ export default class ExpressionParser extends LValParser {
);
}
getGetterSetterExpectedParamCount(
method: N.ObjectMethod | N.ClassMethod,
): number {
return method.kind === "get" ? 0 : 1;
}
// 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 = method.kind === "get" ? 0 : 1;
const paramCount = this.getGetterSetterExpectedParamCount(method);
const start = method.start;
if (method.params.length !== paramCount) {
if (method.kind === "get") {
@@ -1577,7 +1583,10 @@ export default class ExpressionParser extends LValParser {
}
}
if (method.kind === "set" && method.params[0].type === "RestElement") {
if (
method.kind === "set" &&
method.params[method.params.length - 1].type === "RestElement"
) {
this.raise(
start,
"setter function argument must not be a rest parameter",

View File

@@ -2344,4 +2344,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (typeArguments) node.typeParameters = typeArguments;
return super.jsxParseOpeningElementAfterName(node);
}
getGetterSetterExpectedParamCount(
method: N.ObjectMethod | N.ClassMethod,
): number {
const baseCount = super.getGetterSetterExpectedParamCount(method);
const firstParam = method.params[0];
const hasContextParam =
firstParam &&
firstParam.type === "Identifier" &&
firstParam.name === "this";
return hasContextParam ? baseCount + 1 : baseCount;
}
};