From d5f75cb2f0759c0feec464231a8af03caa7feeaa Mon Sep 17 00:00:00 2001 From: Vladimir Kurchatkin Date: Fri, 24 Jun 2016 19:47:30 +0300 Subject: [PATCH] flow: allow generic method with name get or set --- src/parser/statement.js | 7 +- .../flow/type-annotations/104/actual.js | 7 + .../flow/type-annotations/104/expected.json | 310 ++++++++++++++++++ 3 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/flow/type-annotations/104/actual.js create mode 100644 test/fixtures/flow/type-annotations/104/expected.json diff --git a/src/parser/statement.js b/src/parser/statement.js index 291e5a66d5..e81a437b83 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -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); diff --git a/test/fixtures/flow/type-annotations/104/actual.js b/test/fixtures/flow/type-annotations/104/actual.js new file mode 100644 index 0000000000..2c1ead8048 --- /dev/null +++ b/test/fixtures/flow/type-annotations/104/actual.js @@ -0,0 +1,7 @@ +class Foo { + get() {} +} + +class Bar { + set() {} +} diff --git a/test/fixtures/flow/type-annotations/104/expected.json b/test/fixtures/flow/type-annotations/104/expected.json new file mode 100644 index 0000000000..d1bef6ae76 --- /dev/null +++ b/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": [] + } +}