Fix private property parsing in Flow (#8340)

* Fix private property parsing in Flow

* Flow tests updated

* Fix type error

* Appropriate name was given to test folder

* Fix

* Empty

* Correct type annotation

* Add required changes in generator package

* Add required changes in flow-strip-types
This commit is contained in:
kalenikalexander 2018-08-02 09:38:58 +03:00 committed by Nicolò Ribaudo
parent b8f9ebf638
commit 5c728ea609
18 changed files with 559 additions and 2 deletions

View File

@ -1,5 +1,5 @@
MAKEFLAGS = -j1
FLOW_COMMIT = 395e045c18d537fcbbc552a96ef2cdcd70b4ab52
FLOW_COMMIT = bea8b83f50f597454941d2a7ecef6e93a881e576
TEST262_COMMIT = f90a52b39609a620c0854e0bd0b3a906c930fd17
# Fix color output until TravisCI fixes https://github.com/travis-ci/travis-ci/issues/7967

View File

@ -124,6 +124,7 @@ export function ClassPrivateProperty(node: Object) {
this.space();
}
this.print(node.key, node);
this.print(node.typeAnnotation, node);
if (node.value) {
this.space();
this.token("=");

View File

@ -72,6 +72,10 @@ class Foo {
static prop1: string;
prop2: number;
}
class Foo {
#prop1: string;
prop2: number;
}
var x: number | string = 4;
class Array { concat(items:number | string) {}; }
var x: () => number | () => string = fn;

View File

@ -0,0 +1 @@
{ "plugins": ["classPrivateProperties", "flow"] }

View File

@ -168,6 +168,11 @@ class Foo {
prop2: number;
}
class Foo {
#prop1: string;
prop2: number;
}
var x: number | string = 4;
class Array {

View File

@ -1917,6 +1917,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return super.parseClassProperty(node);
}
parseClassPrivateProperty(
node: N.ClassPrivateProperty,
): N.ClassPrivateProperty {
if (this.match(tt.colon)) {
node.typeAnnotation = this.flowParseTypeAnnotation();
}
return super.parseClassPrivateProperty(node);
}
// determine whether or not we're currently in the position where a class method would appear
isClassMethod(): boolean {
return this.isRelational("<") || super.isClassMethod();

View File

@ -717,6 +717,7 @@ export type ClassPrivateProperty = NodeBase & {
value: ?Expression, // TODO: Not in spec that this is nullable.
static: boolean,
computed: false,
typeAnnotation?: ?TypeAnnotation, // TODO: Not in spec
};
export type OptClassDeclaration = ClassBase &

View File

@ -0,0 +1,4 @@
class A {
#prop1: string;
#prop2: number = value;
}

View File

@ -0,0 +1,261 @@
{
"type": "File",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 55,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 12,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 17
}
},
"static": false,
"key": {
"type": "PrivateName",
"start": 12,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
}
},
"id": {
"type": "Identifier",
"start": 13,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "prop1"
},
"name": "prop1"
}
},
"variance": null,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 18,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 16
}
},
"typeAnnotation": {
"type": "StringTypeAnnotation",
"start": 20,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 16
}
}
}
},
"value": null
},
{
"type": "ClassPrivateProperty",
"start": 30,
"end": 53,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 25
}
},
"static": false,
"key": {
"type": "PrivateName",
"start": 30,
"end": 36,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
},
"id": {
"type": "Identifier",
"start": 31,
"end": 36,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 8
},
"identifierName": "prop2"
},
"name": "prop2"
}
},
"variance": null,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 36,
"end": 44,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 16
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 38,
"end": 44,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 16
}
}
}
},
"value": {
"type": "Identifier",
"start": 47,
"end": 52,
"loc": {
"start": {
"line": 3,
"column": 19
},
"end": {
"line": 3,
"column": 24
},
"identifierName": "value"
},
"name": "value"
}
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
class A {
#prop1: string;
#prop2: number;
}

View File

@ -0,0 +1,245 @@
{
"type": "File",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start": 0,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 6,
"end": 7,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 7
},
"identifierName": "A"
},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start": 8,
"end": 47,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ClassPrivateProperty",
"start": 12,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 17
}
},
"static": false,
"key": {
"type": "PrivateName",
"start": 12,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 8
}
},
"id": {
"type": "Identifier",
"start": 13,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 8
},
"identifierName": "prop1"
},
"name": "prop1"
}
},
"variance": null,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 18,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 16
}
},
"typeAnnotation": {
"type": "StringTypeAnnotation",
"start": 20,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 16
}
}
}
},
"value": null
},
{
"type": "ClassPrivateProperty",
"start": 30,
"end": 45,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 17
}
},
"static": false,
"key": {
"type": "PrivateName",
"start": 30,
"end": 36,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 8
}
},
"id": {
"type": "Identifier",
"start": 31,
"end": 36,
"loc": {
"start": {
"line": 3,
"column": 3
},
"end": {
"line": 3,
"column": 8
},
"identifierName": "prop2"
},
"name": "prop2"
}
},
"variance": null,
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 36,
"end": 44,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 16
}
},
"typeAnnotation": {
"type": "NumberTypeAnnotation",
"start": 38,
"end": 44,
"loc": {
"start": {
"line": 3,
"column": 10
},
"end": {
"line": 3,
"column": 16
}
}
}
},
"value": null
}
]
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,4 @@
{
"sourceType": "module",
"plugins": [ "flow", "classPrivateProperties" ]
}

View File

@ -76,6 +76,11 @@ export default declare(api => {
if (!path.node.value) path.remove();
},
ClassPrivateProperty(path) {
if (skipStrip) return;
path.node.typeAnnotation = null;
},
Class(path) {
if (skipStrip) return;
path.node.implements = null;

View File

@ -69,6 +69,10 @@ class Foo10 {
static prop1: string;
prop2: number;
}
class Foo11 {
#prop1: string;
#prop2: number;
}
var x: number | string = 4;
class Array { concat(items:number | string) {}; }
var x: () => number | () => string = fn;

View File

@ -0,0 +1 @@
{ "plugins": [ "transform-flow-strip-types", "syntax-class-properties" ] }

View File

@ -123,6 +123,11 @@ class Foo9 {}
class Foo10 {}
class Foo11 {
#prop1;
#prop2;
}
var x = 4;
class Array {

View File

@ -15,6 +15,7 @@ async_await/migrated_0020.js
async_await/migrated_0024.js
async_await/migrated_0027.js
async_generators/migrated_0007.js
catch/optional_catch_binding.js
class_properties/migrated_0000.js
class_properties/migrated_0005.js
class_properties/migrated_0011.js
@ -23,7 +24,8 @@ class_properties/migrated_0021.js
class_properties/migrated_0026.js
decorators/migrated_0003.js
decorators/migrated_0007.js
private_class_properties/valid.js
private_class_properties/multiple.js
private_class_properties/super.js
types/annotations/migrated_0001.js
types/annotations_in_comments_invalid/migrated_0003.js
types/annotations/void_is_reserved_param.js

View File

@ -115,6 +115,7 @@ const options = {
"flowComments",
"jsx",
"objectRestSpread",
"classPrivateProperties",
],
sourceType: "module",
ranges: true,