Skip to content

Commit 5de4ea5

Browse files
SamChou19815facebook-github-bot
authored andcommitted
[private-fields] Fix ESTree translator output to match latest ESTree spec
Summary: As a first step to fix ESTree translator output for private properties, we should unify it with public properties according to the latest [estree spec](https://github.com/estree/estree/blob/master/es2022.md). Changes: - Both private and public fields have type `PropertyDefinition`. - `key` of private fields are now `PrivateIdentifier` instead of `Identifier` - `computed` field of private fields are always present and always false Reviewed By: gkz Differential Revision: D30306603 fbshipit-source-id: 9ea947a69c6578bf2b6fe527f137f44076b412d5
1 parent faec817 commit 5de4ea5

File tree

81 files changed

+503
-548
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+503
-548
lines changed

packages/flow-parser/test/custom_ast_types.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ export default function (fork) {
6666
.field("bigint", String);
6767

6868
/////////
69-
// es-proposal
69+
// es2022
7070
/////////
71-
def("ClassPrivateProperty")
72-
.field("key", def("Identifier"))
71+
def("PrivateIdentifier")
72+
.bases("Expression", "Pattern")
73+
.field("name", String);
74+
75+
def("PropertyDefinition")
76+
.bases("ClassProperty");
77+
78+
def("ClassBody")
79+
.field("body", [or(def("MethodDefinition"), def("PropertyDefinition"))]);
7380
}

packages/flow-parser/test/esprima_test_runner.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ function handleSpecialObjectCompare(esprima, flow, env) {
365365
break;
366366
case 'ObjectTypeProperty':
367367
case 'ObjectTypeIndexer':
368-
case 'ClassProperty':
368+
case 'PropertyDefinition':
369369
delete flow.variance;
370370
break;
371371
case 'DeclareModule':
@@ -470,6 +470,7 @@ function compare(esprima, flow, env) {
470470
if (!ast_types.namedTypes[flow.type]) {
471471
env.diff("Unknown AST type", "known type", flow.type);
472472
} else if (!ast_types.namedTypes[flow.type].check(flow, true)) {
473+
throw new Error(`${ast_types.namedTypes[flow.type]} ${JSON.stringify(flow)}`);
473474
env.ast_types_error();
474475
}
475476
}

packages/flow-remove-types/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ var removeFlowVisitor = {
197197
}
198198
},
199199

200-
ClassProperty: function(context, node) {
200+
PropertyDefinition: function(context, node) {
201201
if (node.declare || (context.ignoreUninitializedFields && !node.value)) {
202202
return removeNode(context, node);
203203
}

src/parser/estree_translator.ml

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,16 @@ with type t = Impl.t = struct
737737
?comments
738738
loc
739739
[("name", string name); ("typeAnnotation", null); ("optional", bool false)]
740-
and private_name (loc, { PrivateName.id; comments }) =
741-
node ?comments "PrivateName" loc [("id", identifier id)]
740+
and private_identifier
741+
( loc,
742+
{ PrivateName.id = (_, { Identifier.name; comments = identifier_comments }); comments } )
743+
=
744+
let comments = Flow_ast_utils.merge_comments ~outer:comments ~inner:identifier_comments in
745+
node
746+
?comments
747+
"PrivateIdentifier"
748+
loc
749+
[("name", string name); ("typeAnnotation", null); ("optional", bool false)]
742750
and pattern_identifier
743751
loc { Pattern.Identifier.name = (_, { Identifier.name; comments }); annot; optional } =
744752
node
@@ -939,7 +947,7 @@ with type t = Impl.t = struct
939947
match key with
940948
| Literal lit -> (literal lit, false, comments)
941949
| Identifier id -> (identifier id, false, comments)
942-
| PrivateName name -> (private_name name, false, comments)
950+
| PrivateName name -> (private_identifier name, false, comments)
943951
| Computed (_, { ComputedKey.expression = expr; comments = computed_comments }) ->
944952
( expression expr,
945953
true,
@@ -966,27 +974,19 @@ with type t = Impl.t = struct
966974
("decorators", array_of_list class_decorator decorators);
967975
]
968976
and class_private_field
969-
( loc,
970-
{
971-
Class.PrivateField.key = (_, { PrivateName.id; comments = key_comments });
972-
value;
973-
annot;
974-
static;
975-
variance = variance_;
976-
comments;
977-
} ) =
977+
(loc, { Class.PrivateField.key; value; annot; static; variance = variance_; comments }) =
978978
let (value, declare) =
979979
match value with
980980
| Class.Property.Declared -> (None, true)
981981
| Class.Property.Uninitialized -> (None, false)
982982
| Class.Property.Initialized x -> (Some x, false)
983983
in
984-
let comments = Flow_ast_utils.merge_comments ~outer:comments ~inner:key_comments in
985984
let props =
986985
[
987-
("key", identifier id);
986+
("key", private_identifier key);
988987
("value", option expression value);
989988
("typeAnnotation", hint type_annotation annot);
989+
("computed", bool false);
990990
("static", bool static);
991991
("variance", option variance variance_);
992992
]
@@ -996,7 +996,7 @@ with type t = Impl.t = struct
996996
else
997997
[]
998998
in
999-
node ?comments "ClassPrivateProperty" loc props
999+
node ?comments "PropertyDefinition" loc props
10001000
and class_property
10011001
(loc, { Class.Property.key; value; annot; static; variance = variance_; comments }) =
10021002
let (key, computed, comments) =
@@ -1030,7 +1030,7 @@ with type t = Impl.t = struct
10301030
else
10311031
[]
10321032
in
1033-
node ?comments "ClassProperty" loc props
1033+
node ?comments "PropertyDefinition" loc props
10341034
and enum_declaration (loc, { Statement.EnumDeclaration.id; body; comments }) =
10351035
let open Statement.EnumDeclaration in
10361036
let enum_body =
@@ -1887,7 +1887,7 @@ with type t = Impl.t = struct
18871887
let (property, computed) =
18881888
match property with
18891889
| Expression.Member.PropertyIdentifier id -> (identifier id, false)
1890-
| Expression.Member.PropertyPrivateName name -> (private_name name, false)
1890+
| Expression.Member.PropertyPrivateName name -> (private_identifier name, false)
18911891
| Expression.Member.PropertyExpression expr -> (expression expr, true)
18921892
in
18931893
[("object", expression _object); ("property", property); ("computed", bool computed)]

src/parser/test/esprima_tests.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,8 +2569,28 @@ module.exports = {
25692569
},
25702570
},
25712571
},
2572-
'class Foo { [1 + 1]: string; }',
2573-
'class Foo { 123:string; }',
2572+
{
2573+
content: 'class Foo { [1 + 1]: string; }',
2574+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2575+
expected_differences: {
2576+
'root.body.0.body.body.0.type': {
2577+
type: 'Wrong string',
2578+
expected: 'ClassProperty',
2579+
actual: 'PropertyDefinition'
2580+
}
2581+
}
2582+
},
2583+
{
2584+
content: 'class Foo { 123:string; }',
2585+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2586+
expected_differences: {
2587+
'root.body.0.body.body.0.type': {
2588+
type: 'Wrong string',
2589+
expected: 'ClassProperty',
2590+
actual: 'PropertyDefinition'
2591+
}
2592+
}
2593+
},
25742594
{
25752595
content: 'class Foo { "bar"<T>() { } }',
25762596
explanation: "Esprima-fb doesn't include params in " +
@@ -2588,10 +2608,60 @@ module.exports = {
25882608
},
25892609
},
25902610
},
2591-
'class Foo { "prop1":string; }',
2592-
'class Foo { [prop1]: string; }' ,
2593-
'class Foo { prop1:string; prop2:number; }',
2594-
'class Foo { static prop1:string; prop2:number; }',
2611+
{
2612+
content: 'class Foo { "prop1":string; }',
2613+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2614+
expected_differences: {
2615+
'root.body.0.body.body.0.type': {
2616+
type: 'Wrong string',
2617+
expected: 'ClassProperty',
2618+
actual: 'PropertyDefinition'
2619+
}
2620+
}
2621+
},
2622+
{
2623+
content: 'class Foo { [prop1]: string; }',
2624+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2625+
expected_differences: {
2626+
'root.body.0.body.body.0.type': {
2627+
type: 'Wrong string',
2628+
expected: 'ClassProperty',
2629+
actual: 'PropertyDefinition'
2630+
}
2631+
}
2632+
},
2633+
{
2634+
content: 'class Foo { prop1:string; prop2:number; }',
2635+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2636+
expected_differences: {
2637+
'root.body.0.body.body.0.type': {
2638+
type: 'Wrong string',
2639+
expected: 'ClassProperty',
2640+
actual: 'PropertyDefinition'
2641+
},
2642+
'root.body.0.body.body.1.type': {
2643+
type: 'Wrong string',
2644+
expected: 'ClassProperty',
2645+
actual: 'PropertyDefinition'
2646+
}
2647+
}
2648+
},
2649+
{
2650+
content: 'class Foo { static prop1:string; prop2:number; }',
2651+
explanation: 'Esprima-fb returns ClassProperty, but Flow returns PropertyDefinition',
2652+
expected_differences: {
2653+
'root.body.0.body.body.0.type': {
2654+
type: 'Wrong string',
2655+
expected: 'ClassProperty',
2656+
actual: 'PropertyDefinition'
2657+
},
2658+
'root.body.0.body.body.1.type': {
2659+
type: 'Wrong string',
2660+
expected: 'ClassProperty',
2661+
actual: 'PropertyDefinition'
2662+
}
2663+
},
2664+
},
25952665
{
25962666
content: 'class Foo {set fooProp(value:number){}}',
25972667
explanation: "Esprima-fb doesn't include params in " +

src/parser/test/flow/async_await/migrated_0005.tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"range":[12,30],
2222
"body":[
2323
{
24-
"type":"ClassProperty",
24+
"type":"PropertyDefinition",
2525
"loc":{"source":null,"start":{"line":1,"column":14},"end":{"line":1,"column":28}},
2626
"range":[14,28],
2727
"key":{

src/parser/test/flow/class_method_kinds/multiple_constructors.tree.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
"range":[74,115],
137137
"body":[
138138
{
139-
"type":"ClassProperty",
139+
"type":"PropertyDefinition",
140140
"loc":{"source":null,"start":{"line":7,"column":2},"end":{"line":7,"column":28}},
141141
"range":[78,104],
142142
"key":{
@@ -188,7 +188,7 @@
188188
"range":[127,161],
189189
"body":[
190190
{
191-
"type":"ClassProperty",
191+
"type":"PropertyDefinition",
192192
"loc":{"source":null,"start":{"line":11,"column":2},"end":{"line":11,"column":21}},
193193
"range":[131,150],
194194
"key":{
@@ -337,7 +337,7 @@
337337
"range":[255,286],
338338
"body":[
339339
{
340-
"type":"ClassProperty",
340+
"type":"PropertyDefinition",
341341
"loc":{"source":null,"start":{"line":20,"column":2},"end":{"line":20,"column":19}},
342342
"range":[259,276],
343343
"key":{
@@ -389,7 +389,7 @@
389389
"range":[303,342],
390390
"body":[
391391
{
392-
"type":"ClassProperty",
392+
"type":"PropertyDefinition",
393393
"loc":{"source":null,"start":{"line":24,"column":2},"end":{"line":24,"column":26}},
394394
"range":[307,331],
395395
"key":{

src/parser/test/flow/class_properties/async.tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"range":[8,19],
2222
"body":[
2323
{
24-
"type":"ClassProperty",
24+
"type":"PropertyDefinition",
2525
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":7}},
2626
"range":[12,17],
2727
"key":{

src/parser/test/flow/class_properties/async_annotated.tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"range":[8,28],
2222
"body":[
2323
{
24-
"type":"ClassProperty",
24+
"type":"PropertyDefinition",
2525
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":16}},
2626
"range":[12,26],
2727
"key":{

src/parser/test/flow/class_properties/async_asi.tree.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"range":[8,43],
2222
"body":[
2323
{
24-
"type":"ClassProperty",
24+
"type":"PropertyDefinition",
2525
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":7}},
2626
"range":[12,17],
2727
"key":{

0 commit comments

Comments
 (0)