[TS] Allow context type annotation on getters/setters (#9641)
* Allow context type annotation on getters/setters * Extract getAccessorsExpectedParamCount
This commit is contained in:
committed by
Nicolò Ribaudo
parent
fba5655a44
commit
e53be4b387
@@ -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",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
6
packages/babel-parser/test/fixtures/typescript/function/getter-setter/input.js
vendored
Normal file
6
packages/babel-parser/test/fixtures/typescript/function/getter-setter/input.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
const g = {
|
||||
get m(this: {}) {}
|
||||
};
|
||||
const s = {
|
||||
set m(this: {}, value) {}
|
||||
};
|
||||
396
packages/babel-parser/test/fixtures/typescript/function/getter-setter/output.json
vendored
Normal file
396
packages/babel-parser/test/fixtures/typescript/function/getter-setter/output.json
vendored
Normal file
@@ -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": []
|
||||
}
|
||||
}
|
||||
@@ -5,3 +5,9 @@ const o = {
|
||||
class C {
|
||||
m(this: {}) {}
|
||||
}
|
||||
const g = {
|
||||
get m(this: {}) {}
|
||||
};
|
||||
const s = {
|
||||
set m(this: {}, value: {}) {}
|
||||
};
|
||||
@@ -9,3 +9,12 @@ class C {
|
||||
m() {}
|
||||
|
||||
}
|
||||
|
||||
const g = {
|
||||
get m() {}
|
||||
|
||||
};
|
||||
const s = {
|
||||
set m(value) {}
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user