Skip to content

Commit

Permalink
Allow context type annotation on getters/setters
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-tingen committed Mar 6, 2019
1 parent 349c0d4 commit 238598c
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -1552,15 +1552,25 @@ export default class ExpressionParser extends LValParser {
checkGetterSetterParams(method: N.ObjectMethod | N.ClassMethod): void {
const paramCount = method.kind === "get" ? 0 : 1;
const start = method.start;
if (method.params.length !== paramCount) {
// Allow `this` type annotation in TypeScript
if (
method.params.length === paramCount + 1 &&
method.params[0].type === "Identifier" &&
method.params[0].name === "this"
) {
this.expectPlugin("typescript");
} else if (method.params.length !== paramCount) {
if (method.kind === "get") {
this.raise(start, "getter must not have any formal parameters");
} else {
this.raise(start, "setter must have exactly one formal parameter");
}
}

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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const g = {
get m(this: {}) {}
};
const s = {
set m(this: {}, value) {}
};

0 comments on commit 238598c

Please sign in to comment.