Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Commit

Permalink
Merge pull request #55 from vkurchatkin/gen-getset
Browse files Browse the repository at this point in the history
flow: allow generic method with name get or set
  • Loading branch information
sebmck committed Jun 24, 2016
2 parents cde17b3 + f0c7660 commit f30f194
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/parser/statement.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/flow.js
Expand Up @@ -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);
}
};
});
}
7 changes: 7 additions & 0 deletions test/fixtures/flow/type-annotations/104/actual.js
@@ -0,0 +1,7 @@
class Foo {
get<T>() {}
}

class Bar {
set<T>() {}
}
310 changes: 310 additions & 0 deletions test/fixtures/flow/type-annotations/104/expected.json
@@ -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": []
}
}

0 comments on commit f30f194

Please sign in to comment.