From e047a1b89a4b26c2fd56b6306144b8f3057ac368 Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 17 Oct 2016 22:55:50 +0300 Subject: [PATCH] Flow: Allow class properties to be named `static` (#184) Related: #158 --- src/plugins/flow.js | 8 +- .../class-properties/named-static/actual.js | 3 + .../named-static/expected.json | 157 ++++++++++++++++++ 3 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/flow/class-properties/named-static/actual.js create mode 100644 test/fixtures/flow/class-properties/named-static/expected.json diff --git a/src/plugins/flow.js b/src/plugins/flow.js index e248c0f90f..e86b8455c4 100644 --- a/src/plugins/flow.js +++ b/src/plugins/flow.js @@ -360,7 +360,7 @@ pp.flowParseObjectType = function (allowStatic, allowExact) { let optional = false; let startPos = this.state.start, startLoc = this.state.startLoc; node = this.startNode(); - if (allowStatic && this.isContextual("static")) { + if (allowStatic && this.isContextual("static") && this.lookahead().type !== tt.colon) { this.next(); isStatic = true; } @@ -376,11 +376,7 @@ pp.flowParseObjectType = function (allowStatic, allowExact) { } nodeStart.callProperties.push(this.flowParseObjectTypeCallProperty(node, allowStatic)); } else { - if (isStatic && this.match(tt.colon)) { - propertyKey = this.parseIdentifier(); - } else { - propertyKey = this.flowParseObjectPropertyKey(); - } + propertyKey = this.flowParseObjectPropertyKey(); if (this.isRelational("<") || this.match(tt.parenL)) { // This is a method property if (variance) { diff --git a/test/fixtures/flow/class-properties/named-static/actual.js b/test/fixtures/flow/class-properties/named-static/actual.js new file mode 100644 index 0000000000..f3ad590669 --- /dev/null +++ b/test/fixtures/flow/class-properties/named-static/actual.js @@ -0,0 +1,3 @@ +declare class A { + static: T; +} diff --git a/test/fixtures/flow/class-properties/named-static/expected.json b/test/fixtures/flow/class-properties/named-static/expected.json new file mode 100644 index 0000000000..e3d5623c98 --- /dev/null +++ b/test/fixtures/flow/class-properties/named-static/expected.json @@ -0,0 +1,157 @@ +{ + "type": "File", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "module", + "body": [ + { + "type": "DeclareClass", + "start": 0, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 14, + "end": 15, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 1, + "column": 15 + }, + "identifierName": "A" + }, + "name": "A" + }, + "typeParameters": null, + "extends": [], + "mixins": [], + "body": { + "type": "ObjectTypeAnnotation", + "start": 16, + "end": 32, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "callProperties": [], + "properties": [ + { + "type": "ObjectTypeProperty", + "start": 20, + "end": 30, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "key": { + "type": "Identifier", + "start": 20, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + }, + "identifierName": "static" + }, + "name": "static" + }, + "value": { + "type": "GenericTypeAnnotation", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "typeParameters": null, + "id": { + "type": "Identifier", + "start": 28, + "end": 29, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 2, + "column": 11 + }, + "identifierName": "T" + }, + "name": "T" + } + }, + "optional": false, + "static": false, + "variance": null + } + ], + "indexers": [], + "exact": false + } + } + ], + "directives": [] + } +} \ No newline at end of file