From 9138251dc43e2d06965a5c10feab3b84e2fe868a Mon Sep 17 00:00:00 2001 From: George Zahariev Date: Fri, 20 Dec 2024 12:51:53 -0800 Subject: [PATCH] [flow][refactor] Pull out `string_of_variable_kind` function for re-use Summary: Pull out `string_of_variable_kind` function for re-use. Changelog: [internal] Reviewed By: SamChou19815 Differential Revision: D67502320 ------------------------------------------------------------------------ (from d93582fdb364287bf1bfcf6bcd8b915285ffb6db) fbshipit-source-id: f5a07275123ef62f13e17804ac188d4e702fc0e7 --- src/parser/estree_translator.ml | 11 +++-------- src/parser/flow_ast_utils.ml | 5 +++++ src/parser/flow_ast_utils.mli | 2 ++ src/parser_utils/output/js_layout_generator.ml | 6 +----- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/parser/estree_translator.ml b/src/parser/estree_translator.ml index 979cb27921b..986e24def0d 100644 --- a/src/parser/estree_translator.ml +++ b/src/parser/estree_translator.ml @@ -772,7 +772,7 @@ with type t = Impl.t = struct let (loc, _) = id in node "MatchIdentifierPattern" loc [("id", identifier id)] and match_binding_pattern (loc, { MatchPattern.BindingPattern.kind; id; comments }) = - let kind = variable_kind kind in + let kind = Flow_ast_utils.string_of_variable_kind kind in node ?comments "MatchBindingPattern" loc [("id", identifier id); ("kind", string kind)] and match_rest_pattern (loc, { MatchPattern.RestPattern.argument; comments }) = node ?comments "MatchRestPattern" loc [("argument", option match_binding_pattern argument)] @@ -915,7 +915,7 @@ with type t = Impl.t = struct [("body", statement_list body)] and declare_variable (loc, { Statement.DeclareVariable.id; annot; kind; comments }) = let id_loc = Loc.btwn (fst id) (fst annot) in - let kind = variable_kind kind in + let kind = Flow_ast_utils.string_of_variable_kind kind in node ?comments "DeclareVariable" @@ -1687,13 +1687,8 @@ with type t = Impl.t = struct "TaggedTemplateExpression" loc [("tag", expression tag); ("quasi", template_literal quasi)] - and variable_kind kind = - match kind with - | Variable.Var -> "var" - | Variable.Let -> "let" - | Variable.Const -> "const" and variable_declaration (loc, { Statement.VariableDeclaration.kind; declarations; comments }) = - let kind = variable_kind kind in + let kind = Flow_ast_utils.string_of_variable_kind kind in node ?comments "VariableDeclaration" diff --git a/src/parser/flow_ast_utils.ml b/src/parser/flow_ast_utils.ml index 014ca19b785..bc8d0c2d8a2 100644 --- a/src/parser/flow_ast_utils.ml +++ b/src/parser/flow_ast_utils.ml @@ -113,6 +113,11 @@ let rec match_pattern_has_binding = | (_, OrPattern { OrPattern.patterns; _ }) -> List.exists match_pattern_has_binding patterns | (_, AsPattern _) -> true +let string_of_variable_kind = function + | Variable.Var -> "var" + | Variable.Let -> "let" + | Variable.Const -> "const" + let partition_directives statements = let open Statement in let rec helper directives = function diff --git a/src/parser/flow_ast_utils.mli b/src/parser/flow_ast_utils.mli index 4433a5d3462..cfdd8678eed 100644 --- a/src/parser/flow_ast_utils.mli +++ b/src/parser/flow_ast_utils.mli @@ -24,6 +24,8 @@ val pattern_has_binding : ('m, 't) Flow_ast.Pattern.t -> bool val match_pattern_has_binding : ('m, 't) Flow_ast.MatchPattern.t -> bool +val string_of_variable_kind : Flow_ast.Variable.kind -> string + val partition_directives : (Loc.t, Loc.t) Flow_ast.Statement.t list -> (Loc.t, Loc.t) Flow_ast.Statement.t list * (Loc.t, Loc.t) Flow_ast.Statement.t list diff --git a/src/parser_utils/output/js_layout_generator.ml b/src/parser_utils/output/js_layout_generator.ml index d334774ac95..c8beb2309dd 100644 --- a/src/parser_utils/output/js_layout_generator.ml +++ b/src/parser_utils/output/js_layout_generator.ml @@ -1735,11 +1735,7 @@ and template_literal ~opts { Ast.Expression.TemplateLiteral.quasis; expressions; in fuse [Atom "`"; fuse (List.mapi template_element quasis); Atom "`"] -and variable_kind kind = - match kind with - | Ast.Variable.Var -> Atom "var" - | Ast.Variable.Let -> Atom "let" - | Ast.Variable.Const -> Atom "const" +and variable_kind kind = Atom (Flow_ast_utils.string_of_variable_kind kind) and variable_declaration ?(ctxt = normal_context)