diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 6c0de5314747..e054410f81bc 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2791,7 +2791,8 @@ export default (superClass: Class): Class => method: N.ObjectMethod | N.ClassMethod, ): number { const baseCount = super.getGetterSetterExpectedParamCount(method); - const firstParam = method.params[0]; + const params = this.getObjectOrClassMethodParams(method); + const firstParam = params[0]; const hasContextParam = firstParam && firstParam.type === "Identifier" && diff --git a/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/input.ts b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/input.ts new file mode 100644 index 000000000000..3a8931bb5fba --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/input.ts @@ -0,0 +1,4 @@ +({ set x(foo) {} }); +({ get x() { return 1 } }); +({ set x() {} }); +({ get x(x) {} }); diff --git a/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json new file mode 100644 index 000000000000..284b2c96b440 --- /dev/null +++ b/packages/babel-parser/test/fixtures/estree/typescript/getter-setter/output.json @@ -0,0 +1,203 @@ +{ + "type": "File", + "start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":18}}, + "errors": [ + "SyntaxError: setter must have exactly one formal parameter (3:3)", + "SyntaxError: getter must not have any formal parameters (4:3)" + ], + "program": { + "type": "Program", + "start":0,"end":85,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":18}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":20,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":20}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":18,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":18}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "properties": [ + { + "type": "Property", + "start":3,"end":16,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":16}}, + "method": false, + "key": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "kind": "set", + "value": { + "type": "FunctionExpression", + "start":8,"end":16,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":16}}, + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + } + ], + "body": { + "type": "BlockStatement", + "start":14,"end":16,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":16}}, + "body": [] + } + }, + "shorthand": false + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":21,"end":48,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":27}}, + "expression": { + "type": "ObjectExpression", + "start":22,"end":46,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":25}}, + "extra": { + "parenthesized": true, + "parenStart": 21 + }, + "properties": [ + { + "type": "Property", + "start":24,"end":44,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":23}}, + "method": false, + "key": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":8},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "kind": "get", + "value": { + "type": "FunctionExpression", + "start":29,"end":44,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":23}}, + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":32,"end":44,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":23}}, + "body": [ + { + "type": "ReturnStatement", + "start":34,"end":42,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":21}}, + "argument": { + "type": "Literal", + "start":41,"end":42,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":21}}, + "value": 1, + "raw": "1" + } + } + ] + } + }, + "shorthand": false + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":49,"end":66,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":17}}, + "expression": { + "type": "ObjectExpression", + "start":50,"end":64,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":15}}, + "extra": { + "parenthesized": true, + "parenStart": 49 + }, + "properties": [ + { + "type": "Property", + "start":52,"end":62,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":13}}, + "method": false, + "key": { + "type": "Identifier", + "start":56,"end":57,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":8},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "kind": "set", + "value": { + "type": "FunctionExpression", + "start":57,"end":62,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":13}}, + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":60,"end":62,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":13}}, + "body": [] + } + }, + "shorthand": false + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":67,"end":85,"loc":{"start":{"line":4,"column":0},"end":{"line":4,"column":18}}, + "expression": { + "type": "ObjectExpression", + "start":68,"end":83,"loc":{"start":{"line":4,"column":1},"end":{"line":4,"column":16}}, + "extra": { + "parenthesized": true, + "parenStart": 67 + }, + "properties": [ + { + "type": "Property", + "start":70,"end":81,"loc":{"start":{"line":4,"column":3},"end":{"line":4,"column":14}}, + "method": false, + "key": { + "type": "Identifier", + "start":74,"end":75,"loc":{"start":{"line":4,"column":7},"end":{"line":4,"column":8},"identifierName":"x"}, + "name": "x" + }, + "computed": false, + "kind": "get", + "value": { + "type": "FunctionExpression", + "start":75,"end":81,"loc":{"start":{"line":4,"column":8},"end":{"line":4,"column":14}}, + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "start":76,"end":77,"loc":{"start":{"line":4,"column":9},"end":{"line":4,"column":10},"identifierName":"x"}, + "name": "x" + } + ], + "body": { + "type": "BlockStatement", + "start":79,"end":81,"loc":{"start":{"line":4,"column":12},"end":{"line":4,"column":14}}, + "body": [] + } + }, + "shorthand": false + } + ] + } + } + ] + } +} \ No newline at end of file