Merge pull request #55 from vkurchatkin/gen-getset

flow: allow generic method with name get or set
This commit is contained in:
Sebastian McKenzie 2016-06-24 18:24:27 +01:00 committed by GitHub
commit f30f1942b6
4 changed files with 332 additions and 1 deletions

View File

@ -621,6 +621,10 @@ pp.isClassProperty = function () {
return this.match(tt.eq) || this.isLineTerminator();
};
pp.isClassMutatorStarter = function () {
return false;
};
pp.parseClassBody = function (node) {
// class bodies are implicitly strict
let oldStrict = this.state.strict;
@ -694,7 +698,7 @@ pp.parseClassBody = function (node) {
// 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 && !this.isClassMutatorStarter() && 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

@ -1075,4 +1075,14 @@ export default function (instance) {
return inner.call(this, node);
};
});
instance.extend("isClassMutatorStarter", function (inner) {
return function () {
if (this.isRelational("<")) {
return true;
} else {
return inner.call(this);
}
};
});
}

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": []
}
}