diff --git a/src/parser/statement.js b/src/parser/statement.js index 291e5a66d5..d20e8c5bde 100644 --- a/src/parser/statement.js +++ b/src/parser/statement.js @@ -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); diff --git a/src/plugins/flow.js b/src/plugins/flow.js index 9d0279ae99..4bcf405632 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -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); + } + }; + }); } 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": [] + } +}