flow: allow generic method with name get or set

This commit is contained in:
Vladimir Kurchatkin 2016-06-24 19:47:30 +03:00
parent cde17b33bd
commit d5f75cb2f0
3 changed files with 323 additions and 1 deletions

View File

@ -658,6 +658,7 @@ pp.parseClassBody = function (node) {
let isGenerator = this.eat(tt.star);
let isGetSet = false;
let isAsync = false;
let isGeneric = false;
this.parsePropertyName(method);
@ -692,9 +693,13 @@ pp.parseClassBody = function (node) {
if (!method.computed) {
let { key } = method;
if (this.hasPlugin("flow") && this.isRelational("<")) {
isGeneric = true;
}
// handle get/set methods
// eg. class Foo { get bar() {} set bar() {} }
if (!isAsync && !isGenerator && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
if (!isAsync && !isGenerator && !isGeneric && key.type === "Identifier" && !this.match(tt.parenL) && (key.name === "get" || key.name === "set")) {
isGetSet = true;
method.kind = key.name;
key = this.parsePropertyName(method);

View File

@ -0,0 +1,7 @@
class Foo {
get<T>() {}
}
class Bar {
set<T>() {}
}

View File

@ -0,0 +1,310 @@
{
"type": "File",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 56,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"sourceType": "module",
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 9
}
},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 10,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 10
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 14,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 13
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 14,
"end": 17,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 5
}
},
"name": "get"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 17,
"end": 20,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 8
}
},
"params": [
{
"type": "TypeParameter",
"start": 18,
"end": 19,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 7
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 23,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 13
}
},
"body": [],
"directives": []
}
}
]
}
},
{
"type": "ClassDeclaration",
"start": 29,
"end": 56,
"loc": {
"start": {
"line": 5,
"column": 0
},
"end": {
"line": 7,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 35,
"end": 38,
"loc": {
"start": {
"line": 5,
"column": 6
},
"end": {
"line": 5,
"column": 9
}
},
"name": "Bar"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 39,
"end": 56,
"loc": {
"start": {
"line": 5,
"column": 10
},
"end": {
"line": 7,
"column": 1
}
},
"body": [
{
"type": "ClassMethod",
"start": 43,
"end": 54,
"loc": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 13
}
},
"computed": false,
"key": {
"type": "Identifier",
"start": 43,
"end": 46,
"loc": {
"start": {
"line": 6,
"column": 2
},
"end": {
"line": 6,
"column": 5
}
},
"name": "set"
},
"static": false,
"kind": "method",
"typeParameters": {
"type": "TypeParameterDeclaration",
"start": 46,
"end": 49,
"loc": {
"start": {
"line": 6,
"column": 5
},
"end": {
"line": 6,
"column": 8
}
},
"params": [
{
"type": "TypeParameter",
"start": 47,
"end": 48,
"loc": {
"start": {
"line": 6,
"column": 6
},
"end": {
"line": 6,
"column": 7
}
},
"name": "T"
}
]
},
"id": null,
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 52,
"end": 54,
"loc": {
"start": {
"line": 6,
"column": 11
},
"end": {
"line": 6,
"column": 13
}
},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}