diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 71ce823720..35da903e7a 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -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", diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 0724db5570..513d0f3a94 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -2344,4 +2344,17 @@ export default (superClass: Class): Class => 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; + } }; diff --git a/packages/babel-parser/test/fixtures/typescript/function/getter-setter/input.js b/packages/babel-parser/test/fixtures/typescript/function/getter-setter/input.js new file mode 100644 index 0000000000..2a14042c1f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/getter-setter/input.js @@ -0,0 +1,6 @@ +const g = { + get m(this: {}) {} +}; +const s = { + set m(this: {}, value) {} +}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/function/getter-setter/output.json b/packages/babel-parser/test/fixtures/typescript/function/getter-setter/output.json new file mode 100644 index 0000000000..ba424efc68 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/function/getter-setter/output.json @@ -0,0 +1,396 @@ +{ + "type": "File", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 78, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 2 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 35, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "g" + }, + "name": "g" + }, + "init": { + "type": "ObjectExpression", + "start": 10, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectMethod", + "start": 14, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 18, + "end": 19, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 20, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 16 + }, + "identifierName": "this" + }, + "name": "this", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 24, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 26, + "end": 28, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 16 + } + }, + "members": [] + } + } + } + ], + "body": { + "type": "BlockStatement", + "start": 30, + "end": 32, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 20 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "kind": "const" + }, + { + "type": "VariableDeclaration", + "start": 36, + "end": 78, + "loc": { + "start": { + "line": 4, + "column": 0 + }, + "end": { + "line": 6, + "column": 2 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 42, + "end": 77, + "loc": { + "start": { + "line": 4, + "column": 6 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 42, + "end": 43, + "loc": { + "start": { + "line": 4, + "column": 6 + }, + "end": { + "line": 4, + "column": 7 + }, + "identifierName": "s" + }, + "name": "s" + }, + "init": { + "type": "ObjectExpression", + "start": 46, + "end": 77, + "loc": { + "start": { + "line": 4, + "column": 10 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "properties": [ + { + "type": "ObjectMethod", + "start": 50, + "end": 75, + "loc": { + "start": { + "line": 5, + "column": 2 + }, + "end": { + "line": 5, + "column": 27 + } + }, + "method": false, + "key": { + "type": "Identifier", + "start": 54, + "end": 55, + "loc": { + "start": { + "line": 5, + "column": 6 + }, + "end": { + "line": 5, + "column": 7 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 56, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 16 + }, + "identifierName": "this" + }, + "name": "this", + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 60, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 12 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "typeAnnotation": { + "type": "TSTypeLiteral", + "start": 62, + "end": 64, + "loc": { + "start": { + "line": 5, + "column": 14 + }, + "end": { + "line": 5, + "column": 16 + } + }, + "members": [] + } + } + }, + { + "type": "Identifier", + "start": 66, + "end": 71, + "loc": { + "start": { + "line": 5, + "column": 18 + }, + "end": { + "line": 5, + "column": 23 + }, + "identifierName": "value" + }, + "name": "value" + } + ], + "body": { + "type": "BlockStatement", + "start": 73, + "end": 75, + "loc": { + "start": { + "line": 5, + "column": 25 + }, + "end": { + "line": 5, + "column": 27 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/input.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/input.js index 420e204814..bcc3eb3624 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/input.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/input.js @@ -5,3 +5,9 @@ const o = { class C { m(this: {}) {} } +const g = { + get m(this: {}) {} +}; +const s = { + set m(this: {}, value: {}) {} +}; \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/output.js b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/output.js index ded864e231..24b0164ac9 100644 --- a/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/output.js +++ b/packages/babel-plugin-transform-typescript/test/fixtures/function/this-parameter/output.js @@ -9,3 +9,12 @@ class C { m() {} } + +const g = { + get m() {} + +}; +const s = { + set m(value) {} + +}; \ No newline at end of file