From c64dce042ada49c3ecd0debbb5188d49299a7956 Mon Sep 17 00:00:00 2001 From: Sam Zhou Date: Wed, 27 Mar 2024 19:06:36 -0700 Subject: [PATCH] [flow][estree] Change estree output of readonly type operator Summary: [Prettier has not used it in any meaningful ways yet](https://github.com/search?q=repo%3Aprettier%2Fprettier+++ReadOnlyTypeAnnotation&type=code), since there is no visitor key for it in hermes parser, because the parsing support is not in hermes parser. This is the only downstream uses of flow-parser that we care about, so I think it's fine to change it to unify with the type operator used by render types, to make future changes in this space easier and consistent. Once the render type prettier support is upstreamed, we should do the same for keyof. Changelog: [parser] Previously Flow outputs `ReadOnlyTypeAnnotation` in parser output for annotations like `readonly string[]`. Now we output it as `TypeOperator` with `"operator": "readonly"`. Reviewed By: jbrown215 Differential Revision: D54989485 fbshipit-source-id: 76b868a817825e0a467b283344ee1c4f83170e0f --- packages/flow-parser/test/custom_ast_types.js | 4 ---- src/parser/estree_translator.ml | 2 +- .../ts_syntax/invalid_readonly_type.tree.json | 5 +++-- .../test/flow/ts_syntax/readonly_type.tree.json | 15 +++++++++------ 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/flow-parser/test/custom_ast_types.js b/packages/flow-parser/test/custom_ast_types.js index c29dc953928..6ce2cd382f4 100644 --- a/packages/flow-parser/test/custom_ast_types.js +++ b/packages/flow-parser/test/custom_ast_types.js @@ -249,10 +249,6 @@ function custom_ast_types(fork) { .bases('FlowType') .build('argument') .field('argument', def('FlowType')); - def('ReadOnlyTypeAnnotation') - .bases('FlowType') - .build('argument') - .field('argument', def('FlowType')); def('TypeParameter').field('usesExtendsBound', or(undefined, Boolean)); def('Variance').field( 'kind', diff --git a/src/parser/estree_translator.ml b/src/parser/estree_translator.ml index 7f87ab58036..22852c0ce25 100644 --- a/src/parser/estree_translator.ml +++ b/src/parser/estree_translator.ml @@ -1958,7 +1958,7 @@ with type t = Impl.t = struct loc [("operator", string operator); ("typeAnnotation", _type operand)] and read_only_type (loc, { Type.ReadOnly.argument; comments }) = - node ?comments "ReadOnlyTypeAnnotation" loc [("argument", _type argument)] + flow_type_operator loc comments "readonly" argument and tuple_type (loc, { Type.Tuple.elements; comments }) = node ?comments diff --git a/src/parser/test/flow/ts_syntax/invalid_readonly_type.tree.json b/src/parser/test/flow/ts_syntax/invalid_readonly_type.tree.json index 426b2a44dd0..4e7233cd844 100644 --- a/src/parser/test/flow/ts_syntax/invalid_readonly_type.tree.json +++ b/src/parser/test/flow/ts_syntax/invalid_readonly_type.tree.json @@ -23,10 +23,11 @@ }, "typeParameters":null, "right":{ - "type":"ReadOnlyTypeAnnotation", + "type":"TypeOperator", "loc":{"source":null,"start":{"line":1,"column":9},"end":{"line":1,"column":17}}, "range":[9,17], - "argument":{ + "operator":"readonly", + "typeAnnotation":{ "type":"AnyTypeAnnotation", "loc":{"source":null,"start":{"line":2,"column":0},"end":{"line":2,"column":0}}, "range":[18,18] diff --git a/src/parser/test/flow/ts_syntax/readonly_type.tree.json b/src/parser/test/flow/ts_syntax/readonly_type.tree.json index b19957ffaa2..2fb0392d7d2 100644 --- a/src/parser/test/flow/ts_syntax/readonly_type.tree.json +++ b/src/parser/test/flow/ts_syntax/readonly_type.tree.json @@ -17,10 +17,11 @@ }, "typeParameters":null, "right":{ - "type":"ReadOnlyTypeAnnotation", + "type":"TypeOperator", "loc":{"source":null,"start":{"line":1,"column":9},"end":{"line":1,"column":34}}, "range":[9,34], - "argument":{ + "operator":"readonly", + "typeAnnotation":{ "type":"TupleTypeAnnotation", "loc":{"source":null,"start":{"line":1,"column":18},"end":{"line":1,"column":34}}, "range":[18,34], @@ -53,10 +54,11 @@ }, "typeParameters":null, "right":{ - "type":"ReadOnlyTypeAnnotation", + "type":"TypeOperator", "loc":{"source":null,"start":{"line":2,"column":9},"end":{"line":2,"column":26}}, "range":[45,62], - "argument":{ + "operator":"readonly", + "typeAnnotation":{ "type":"ArrayTypeAnnotation", "loc":{"source":null,"start":{"line":2,"column":18},"end":{"line":2,"column":26}}, "range":[54,62], @@ -82,10 +84,11 @@ }, "typeParameters":null, "right":{ - "type":"ReadOnlyTypeAnnotation", + "type":"TypeOperator", "loc":{"source":null,"start":{"line":3,"column":9},"end":{"line":3,"column":24}}, "range":[73,88], - "argument":{ + "operator":"readonly", + "typeAnnotation":{ "type":"NumberTypeAnnotation", "loc":{"source":null,"start":{"line":3,"column":18},"end":{"line":3,"column":24}}, "range":[82,88]