From 7d0924821f0f907e2ed1483c5942f429d2a0135a Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 8 Nov 2026 18:03:17 +0000 Subject: [PATCH] Drop the Flow parser's comment-moving AST mapper classes Flow moves trailing comments off nodes it has already built with an AST mapper class (Flow_ast_mapper, 336 methods) and two subclasses in Comment_attachment. OCaml creates classes when their module is initialized, so every compiler process built their method tables: about 206,000 of the compiler's ~275,000 startup words, although ReScript only parses %raw code with the Flow parser and never reads trailing comments (only the leading comment of a %raw literal, which the parser collects without the mapper). The removers now return the node unchanged; what they kept is the consumption of comments, which decides where later comments attach. Flow_ast_mapper and both classes go. The Flow parser's startup allocation, measured by linking it into an otherwise empty program, drops from 227,172 to 20,669 words. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gLDo8mBJKpNE25WAwT Signed-off-by: Christoph Knittel --- changes/flow-parser-startup.polish.md | 1 + .../flow_parser/parser/comment_attachment.ml | 904 +--- .../flow_parser/parser/declaration_parser.ml | 3 - compiler/flow_parser/parser/dune | 1 - .../flow_parser/parser/expression_parser.ml | 7 +- .../flow_parser/parser/flow_ast_mapper.ml | 4033 ----------------- compiler/flow_parser/parser/object_parser.ml | 28 +- .../flow_parser/parser/statement_parser.ml | 90 +- compiler/flow_parser/parser/type_parser.ml | 15 +- 9 files changed, 107 insertions(+), 4975 deletions(-) create mode 100644 changes/flow-parser-startup.polish.md delete mode 100644 compiler/flow_parser/parser/flow_ast_mapper.ml diff --git a/changes/flow-parser-startup.polish.md b/changes/flow-parser-startup.polish.md new file mode 100644 index 00000000000..3ba310b644d --- /dev/null +++ b/changes/flow-parser-startup.polish.md @@ -0,0 +1 @@ +- Start the compiler with less work: the Flow parser for `%raw` code no longer creates AST mapper classes at startup. diff --git a/compiler/flow_parser/parser/comment_attachment.ml b/compiler/flow_parser/parser/comment_attachment.ml index 3d0b283a536..6602a66a217 100644 --- a/compiler/flow_parser/parser/comment_attachment.ml +++ b/compiler/flow_parser/parser/comment_attachment.ml @@ -5,876 +5,70 @@ * LICENSE file in the root directory of this source tree. *) +(* Trailing comments of a node the parser has already built. Flow moves such + comments off the inner node with an AST mapper class. ReScript never reads + trailing comments, and creating the mapper classes was most of the + compiler's startup work, so the nodes are returned unchanged. What is kept + is the consumption of the comments, which decides where later comments + attach. *) + +(* The parsers that open this module use Ast for Flow_ast. *) module Ast = Flow_ast -open Flow_ast open Parser_env -let id = Flow_ast_mapper.id - -let map_loc = Flow_ast_mapper.map_loc - -let map_opt = Flow_ast_mapper.map_opt - -let id_list_last (map : 'a -> 'a) (lst : 'a list) : 'a list = - match List.rev lst with - | [] -> lst - | hd :: tl -> - let hd' = map hd in - if hd == hd' then - lst - else - List.rev (hd' :: tl) - -(* Mapper that removes all trailing comments that appear after a given position in an AST node *) -class ['loc] trailing_comments_remover ~after_pos = - object (this) - inherit ['loc] Flow_ast_mapper.mapper - - method! syntax comments = - let open Syntax in - let { trailing; _ } = comments in - let trailing' = - List.filter (fun (loc, _) -> Loc.(pos_cmp loc.start after_pos < 0)) trailing - in - if List.length trailing = List.length trailing' then - comments - else - { comments with trailing = trailing' } - - method! array _loc expr = - let open Ast.Expression.Array in - let { comments; _ } = expr in - id this#syntax_opt comments expr (fun comments' -> { expr with comments = comments' }) - - method! array_type t = - let open Ast.Type.Array in - let { comments; _ } = t in - id this#syntax_opt comments t (fun comments' -> { t with comments = comments' }) - - method! assignment _loc expr = - let open Ast.Expression.Assignment in - let { right; comments; _ } = expr in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if right == right' && comments == comments' then - expr - else - { expr with right = right'; comments = comments' } - - method! binary _loc expr = - let open Ast.Expression.Binary in - let { right; comments; _ } = expr in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if right == right' && comments == comments' then - expr - else - { expr with right = right'; comments = comments' } - - method! block _loc stmt = - let open Ast.Statement.Block in - let { comments; _ } = stmt in - id this#syntax_opt comments stmt (fun comments' -> { stmt with comments = comments' }) - - method! call _annot expr = - let open Ast.Expression.Call in - let { arguments; comments; _ } = expr in - let arguments' = this#arg_list arguments in - let comments' = this#syntax_opt comments in - if arguments == arguments' && comments == comments' then - expr - else - { expr with arguments = arguments'; comments = comments' } - - method! arg_list arg_list = - let open Ast.Expression.ArgList in - let (loc, { arguments; comments }) = arg_list in - id this#syntax_opt comments arg_list (fun comments' -> - (loc, { arguments; comments = comments' }) - ) - - method! call_type_args targs = - let open Ast.Expression.CallTypeArgs in - let (loc, { arguments; comments }) = targs in - id this#syntax_opt comments targs (fun comments' -> (loc, { arguments; comments = comments' })) - - method! class_ _loc cls = - let open Ast.Class in - let { body; comments; _ } = cls in - let body' = this#class_body body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - cls - else - { cls with body = body'; comments = comments' } - - method! class_body body = - let open Ast.Class.Body in - let (loc, { body = _body; comments }) = body in - id this#syntax_opt comments body (fun comments' -> - (loc, { body = _body; comments = comments' }) - ) - - method! class_extends _loc extends = - let open Ast.Class.Extends in - let { expr; targs; _ } = extends in - if targs = None then - id this#expression expr extends (fun expr' -> { extends with expr = expr' }) - else - id (map_opt this#type_args) targs extends (fun targs' -> { extends with targs = targs' }) - - method! class_implements implements = - let open Ast.Class.Implements in - let (loc, { interfaces; comments }) = implements in - id (id_list_last this#class_implements_interface) interfaces implements (fun interfaces' -> - (loc, { interfaces = interfaces'; comments }) - ) - - method! class_implements_interface interface = - let open Ast.Class.Implements.Interface in - let (loc, { id = id_; targs }) = interface in - if targs = None then - id this#generic_identifier_type id_ interface (fun id' -> (loc, { id = id'; targs })) - else - id (map_opt this#type_args) targs interface (fun targs' -> - (loc, { id = id_; targs = targs' }) - ) - - method! component_declaration _loc component = - let open Ast.Statement.ComponentDeclaration in - let { body; comments; _ } = component in - let body' = map_opt this#component_body body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - component - else - { component with body = body'; comments = comments' } - - method! component_params (loc, params) = - let open Ast.Statement.ComponentDeclaration.Params in - let { comments; _ } = params in - id this#syntax_opt comments (loc, params) (fun comments' -> - (loc, { params with comments = comments' }) - ) - - method! computed_key key = - let open Ast.ComputedKey in - let (loc, { expression; comments }) = key in - id this#syntax_opt comments key (fun comments' -> (loc, { expression; comments = comments' })) - - method! conditional _loc expr = - let open Ast.Expression.Conditional in - let { alternate; comments; _ } = expr in - let alternate' = this#expression alternate in - let comments' = this#syntax_opt comments in - if alternate == alternate' && comments == comments' then - expr - else - { expr with alternate = alternate'; comments = comments' } - - method! function_ _loc func = - let open Ast.Function in - let { body; comments; _ } = func in - let body' = this#function_body_any body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - func - else - { func with body = body'; comments = comments' } - - method! function_params (loc, params) = - let open Ast.Function.Params in - let { comments; _ } = params in - id this#syntax_opt comments (loc, params) (fun comments' -> - (loc, { params with comments = comments' }) - ) - - method! function_type func = - let open Ast.Type.Function in - let { return; comments; _ } = func in - let return' = this#function_type_return_annotation return in - let comments' = this#syntax_opt comments in - if return == return' && comments == comments' then - func - else - { func with return = return'; comments = comments' } - - method! generic_identifier_type git = - let open Ast.Type.Generic.Identifier in - match git with - | Unqualified i -> id this#identifier i git (fun i -> Unqualified i) - | Qualified (loc, ({ id; _ } as qualified)) -> - let id' = this#identifier id in - if id == id' then - git - else - Qualified (loc, { qualified with id = id' }) - | ImportTypeAnnot _ -> git - - method! import expr = - let open Ast.Expression.Import in - let { comments; _ } = expr in - id this#syntax_opt comments expr (fun comments' -> { expr with comments = comments' }) - - method! interface_type _loc t = - let open Ast.Type.Interface in - let { body; comments; _ } = t in - let body' = - let (bloc, b) = body in - id this#object_type b body (fun b' -> (bloc, b')) - in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - t - else - { t with body = body'; comments = comments' } - - method! intersection_type _loc t = - let { Ast.Type.Intersection.types = (t0, t1, ts); comments } = t in - let (t1', ts') = - match ts with - | [] -> (this#type_ t1, []) - | _ -> (t1, id_list_last this#type_ ts) - in - let comments' = this#syntax_opt comments in - if t1 == t1' && ts == ts' && comments == comments' then - t - else - { Ast.Type.Intersection.types = (t0, t1', ts'); comments = comments' } - - method! jsx_element _loc elem = - let open Ast.JSX in - let { comments; _ } = elem in - id this#syntax_opt comments elem (fun comments' -> { elem with comments = comments' }) - - method! jsx_fragment _loc frag = - let open Ast.JSX in - let { frag_comments = comments; _ } = frag in - id this#syntax_opt comments frag (fun comments' -> { frag with frag_comments = comments' }) - - method! logical _loc expr = - let open Ast.Expression.Logical in - let { right; comments; _ } = expr in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if right == right' && comments == comments' then - expr - else - { expr with right = right'; comments = comments' } - - method! new_ _loc expr = - let open Ast.Expression.New in - let { callee; targs; arguments; comments } = expr in - let comments' = this#syntax_opt comments in - match (targs, arguments) with - (* new Callee() *) - | (_, Some _) -> - let arguments' = map_opt this#arg_list arguments in - if arguments == arguments' && comments == comments' then - expr - else - { expr with arguments = arguments'; comments = comments' } - (* new Callee *) - | (Some _, _) -> - let targs' = map_opt this#call_type_args targs in - if targs == targs' && comments == comments' then - expr - else - { expr with targs = targs'; comments = comments' } - (* new Callee *) - | (None, None) -> - let callee' = this#expression callee in - if callee == callee' && comments == comments' then - expr - else - { expr with callee = callee'; comments = comments' } - - method! member _loc expr = - let open Ast.Expression.Member in - let { property; comments; _ } = expr in - let property' = this#member_property property in - let comments' = this#syntax_opt comments in - if property == property' && comments == comments' then - expr - else - { expr with property = property'; comments = comments' } - - method! object_ _loc expr = - let open Ast.Expression.Object in - let { comments; _ } = expr in - id this#syntax_opt comments expr (fun comments' -> { expr with comments = comments' }) - - method! object_type obj = - let open Ast.Type.Object in - let { comments; _ } = obj in - id this#syntax_opt comments obj (fun comments' -> { obj with comments = comments' }) - - method! predicate pred = - let open Ast.Type.Predicate in - let (loc, { kind; comments }) = pred in - id this#syntax_opt comments pred (fun comments' -> (loc, { kind; comments = comments' })) - - method! sequence _loc expr = - let open Ast.Expression.Sequence in - let { expressions; comments } = expr in - let expressions' = id_list_last this#expression expressions in - let comments' = this#syntax_opt comments in - if expressions == expressions' && comments == comments' then - expr - else - { expressions = expressions'; comments = comments' } - - method! template_literal _loc expr = - let open Ast.Expression.TemplateLiteral in - let { comments; _ } = expr in - id this#syntax_opt comments expr (fun comments' -> { expr with comments = comments' }) - - method! tuple_type t = - let open Ast.Type.Tuple in - let { comments; _ } = t in - id this#syntax_opt comments t (fun comments' -> { t with comments = comments' }) - - method! type_cast _loc expr = - let open Ast.Expression.TypeCast in - let { comments; _ } = expr in - id this#syntax_opt comments expr (fun comments' -> { expr with comments = comments' }) - - method! type_params ~kind:_ tparams = - let open Ast.Type.TypeParams in - let (loc, { params; comments }) = tparams in - id this#syntax_opt comments tparams (fun comments' -> (loc, { params; comments = comments' })) - - method! union_type _loc t = - let { Ast.Type.Union.types = (t0, t1, ts); comments } = t in - let (t1', ts') = - match ts with - | [] -> (this#type_ t1, []) - | _ -> (t1, id_list_last this#type_ ts) - in - let comments' = this#syntax_opt comments in - if t1 == t1' && ts == ts' && comments == comments' then - t - else - { Ast.Type.Union.types = (t0, t1', ts'); comments = comments' } - - method! variable_declarator ~kind decl = - let open Ast.Statement.VariableDeclaration.Declarator in - let (loc, { id = ident; init }) = decl in - match init with - | None -> - id (this#variable_declarator_pattern ~kind) ident decl (fun ident' -> - (loc, { id = ident'; init }) - ) - | Some init -> - id this#expression init decl (fun init' -> (loc, { id = ident; init = Some init' })) - end - type trailing_and_remover_result = { - trailing: Loc.t Comment.t list; - remove_trailing: 'a. 'a -> (Loc.t trailing_comments_remover -> 'a -> 'a) -> 'a; + trailing: Loc.t Flow_ast.Comment.t list; + remove_trailing: 'a. 'a -> 'a; } -(* Returns a remover function which removes comments beginning after the previous token. - No trailing comments are returned, since all comments since the last loc should be removed. *) -let trailing_and_remover_after_last_loc : Parser_env.env -> trailing_and_remover_result = - fun env -> - let open Loc in - let remover = - match Parser_env.last_loc env with - | None -> None - | Some _ when not (Peek.has_eaten_comments env) -> None - | Some last_loc -> - Parser_env.consume_comments_until env last_loc._end; - let remover = new trailing_comments_remover ~after_pos:last_loc._end in - Some remover - in - { - trailing = []; - remove_trailing = - (fun node f -> - match remover with - | None -> node - | Some remover -> f remover node); - } +let unchanged node = node + +(* Consumes the comments since the previous token: none are trailing. *) +let trailing_and_remover_after_last_loc env = + (match Parser_env.last_loc env with + | Some last_loc when Peek.has_eaten_comments env -> + Parser_env.consume_comments_until env last_loc.Loc._end + | _ -> ()); + { trailing = []; remove_trailing = unchanged } -(* Consumes and returns comments on the same line as the previous token. Also returns a remover - function which can be used to remove comments beginning after the previous token's line. *) -let trailing_and_remover_after_last_line : Parser_env.env -> trailing_and_remover_result = - fun env -> - let open Loc in - let (trailing, remover) = +(* Consumes and returns the comments on the same line as the previous token. *) +let trailing_and_remover_after_last_line env = + let trailing = match Parser_env.last_loc env with - | None -> ([], None) - | Some _ when not (Peek.has_eaten_comments env) -> (Eat.comments_until_next_line env, None) + | None -> [] + | Some _ when not (Peek.has_eaten_comments env) -> Eat.comments_until_next_line env | Some last_loc -> - Parser_env.consume_comments_until env last_loc._end; - let trailing = Eat.comments_until_next_line env in - let next_line_start = { line = last_loc._end.line + 1; column = 0 } in - let remover = new trailing_comments_remover ~after_pos:next_line_start in - (trailing, Some remover) + Parser_env.consume_comments_until env last_loc.Loc._end; + Eat.comments_until_next_line env in - { - trailing; - remove_trailing = - (fun node f -> - match remover with - | None -> node - | Some remover -> f remover node); - } + { trailing; remove_trailing = unchanged } -let trailing_and_remover : Parser_env.env -> trailing_and_remover_result = - fun env -> +let trailing_and_remover env = if Peek.is_line_terminator env then trailing_and_remover_after_last_line env else trailing_and_remover_after_last_loc env -let id_remove_trailing env id = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing id (fun remover id -> remover#identifier id) - -let expression_remove_trailing env expr = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing expr (fun remover expr -> remover#expression expr) - -let block_remove_trailing env block = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing block (fun remover (loc, str) -> (loc, remover#block loc str)) - -let type_params_remove_trailing env ~kind tparams = - match tparams with - | None -> None - | Some tparams -> - let { remove_trailing; _ } = trailing_and_remover env in - Some (remove_trailing tparams (fun remover tparams -> remover#type_params ~kind tparams)) - -let type_remove_trailing env ty = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing ty (fun remover ty -> remover#type_ ty) - -let type_annotation_hint_remove_trailing env annot = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing annot (fun remover annot -> remover#type_annotation_hint annot) - -let component_renders_annotation_remove_trailing env annot = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing annot (fun remover annot -> remover#component_renders_annotation annot) - -let return_annotation_remove_trailing env annot = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing annot (fun remover annot -> remover#function_return_annotation annot) +let remove_trailing env node = + ignore (trailing_and_remover env); + node -let function_params_remove_trailing env params = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing params (fun remover params -> remover#function_params params) - -let component_params_remove_trailing env params = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing params (fun remover params -> remover#component_params params) - -let component_type_params_remove_trailing env params = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing params (fun remover params -> remover#component_type_params params) - -let predicate_remove_trailing env pred = - match pred with +let remove_trailing_opt env = function | None -> None - | Some pred -> - let { remove_trailing; _ } = trailing_and_remover env in - Some (remove_trailing pred (fun remover pred -> remover#predicate pred)) - -let object_key_remove_trailing env key = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing key (fun remover key -> remover#object_key key) - -let generic_type_remove_trailing env ty = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing ty (fun remover ty -> - let (tyloc, t) = ty in - id remover#generic_type t ty (fun t' -> (tyloc, t')) - ) - -let generic_type_list_remove_trailing env extends = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing extends (fun remover extends -> - id_list_last - (fun ty -> - let (tyloc, t) = ty in - id remover#generic_type t ty (fun t' -> (tyloc, t'))) - extends - ) - -let class_implements_remove_trailing env implements = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing implements (fun remover impl -> remover#class_implements impl) - -let string_literal_remove_trailing env str = - let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing str (fun remover (loc, str) -> (loc, remover#string_literal str)) - -let statement_add_comments - ((loc, stmt) : (Loc.t, Loc.t) Statement.t) (comments : (Loc.t, unit) Syntax.t option) : - (Loc.t, Loc.t) Statement.t = - let open Statement in - let merge_comments inner = Flow_ast_utils.merge_comments ~inner ~outer:comments in - let merge_comments_with_internal inner = - Flow_ast_utils.merge_comments_with_internal ~inner ~outer:comments - in - ( loc, - match stmt with - | Block ({ Block.comments; _ } as s) -> - Block { s with Block.comments = merge_comments_with_internal comments } - | Break ({ Break.comments; _ } as s) -> - Break { s with Break.comments = merge_comments comments } - | ClassDeclaration ({ Class.comments; _ } as s) -> - ClassDeclaration { s with Class.comments = merge_comments comments } - | ComponentDeclaration ({ ComponentDeclaration.comments; _ } as s) -> - ComponentDeclaration { s with ComponentDeclaration.comments = merge_comments comments } - | Continue ({ Continue.comments; _ } as s) -> - Continue { s with Continue.comments = merge_comments comments } - | Debugger { Debugger.comments } -> Debugger { Debugger.comments = merge_comments comments } - | DeclareClass ({ DeclareClass.comments; _ } as s) -> - DeclareClass { s with DeclareClass.comments = merge_comments comments } - | DeclareComponent ({ DeclareComponent.comments; _ } as s) -> - DeclareComponent { s with DeclareComponent.comments = merge_comments comments } - | DeclareEnum ({ EnumDeclaration.comments; _ } as s) -> - DeclareEnum { s with EnumDeclaration.comments = merge_comments comments } - | DeclareExportDeclaration ({ DeclareExportDeclaration.comments; _ } as s) -> - DeclareExportDeclaration - { s with DeclareExportDeclaration.comments = merge_comments comments } - | DeclareFunction ({ DeclareFunction.comments; _ } as s) -> - DeclareFunction { s with DeclareFunction.comments = merge_comments comments } - | DeclareInterface ({ Interface.comments; _ } as s) -> - DeclareInterface { s with Interface.comments = merge_comments comments } - | DeclareModule ({ DeclareModule.comments; _ } as s) -> - DeclareModule { s with DeclareModule.comments = merge_comments comments } - | DeclareModuleExports ({ DeclareModuleExports.comments; _ } as s) -> - DeclareModuleExports { s with DeclareModuleExports.comments = merge_comments comments } - | DeclareNamespace ({ DeclareNamespace.comments; _ } as s) -> - DeclareNamespace { s with DeclareNamespace.comments = merge_comments comments } - | DeclareTypeAlias ({ TypeAlias.comments; _ } as s) -> - DeclareTypeAlias { s with TypeAlias.comments = merge_comments comments } - | DeclareOpaqueType ({ OpaqueType.comments; _ } as s) -> - DeclareOpaqueType { s with OpaqueType.comments = merge_comments comments } - | DeclareVariable ({ DeclareVariable.comments; _ } as s) -> - DeclareVariable { s with DeclareVariable.comments = merge_comments comments } - | DoWhile ({ DoWhile.comments; _ } as s) -> - DoWhile { s with DoWhile.comments = merge_comments comments } - | Empty { Empty.comments } -> Empty { Empty.comments = merge_comments comments } - | EnumDeclaration ({ EnumDeclaration.comments; _ } as s) -> - EnumDeclaration { s with EnumDeclaration.comments = merge_comments comments } - | ExportDefaultDeclaration ({ ExportDefaultDeclaration.comments; _ } as s) -> - ExportDefaultDeclaration - { s with ExportDefaultDeclaration.comments = merge_comments comments } - | ExportNamedDeclaration ({ ExportNamedDeclaration.comments; _ } as s) -> - ExportNamedDeclaration { s with ExportNamedDeclaration.comments = merge_comments comments } - | ExportAssignment ({ ExportAssignment.comments; _ } as s) -> - ExportAssignment { s with ExportAssignment.comments = merge_comments comments } - | NamespaceExportDeclaration ({ NamespaceExportDeclaration.comments; _ } as s) -> - NamespaceExportDeclaration - { s with NamespaceExportDeclaration.comments = merge_comments comments } - | Expression ({ Expression.comments; _ } as s) -> - Expression { s with Expression.comments = merge_comments comments } - | For ({ For.comments; _ } as s) -> For { s with For.comments = merge_comments comments } - | ForIn ({ ForIn.comments; _ } as s) -> - ForIn { s with ForIn.comments = merge_comments comments } - | ForOf ({ ForOf.comments; _ } as s) -> - ForOf { s with ForOf.comments = merge_comments comments } - | FunctionDeclaration ({ Function.comments; _ } as s) -> - FunctionDeclaration { s with Function.comments = merge_comments comments } - | If ({ If.comments; _ } as s) -> If { s with If.comments = merge_comments comments } - | ImportDeclaration ({ ImportDeclaration.comments; _ } as s) -> - ImportDeclaration { s with ImportDeclaration.comments = merge_comments comments } - | ImportEqualsDeclaration ({ ImportEqualsDeclaration.comments; _ } as s) -> - ImportEqualsDeclaration { s with ImportEqualsDeclaration.comments = merge_comments comments } - | InterfaceDeclaration ({ Interface.comments; _ } as s) -> - InterfaceDeclaration { s with Interface.comments = merge_comments comments } - | Labeled ({ Labeled.comments; _ } as s) -> - Labeled { s with Labeled.comments = merge_comments comments } - | Match ({ Match.comments; _ } as s) -> - Match { s with Match.comments = merge_comments comments } - | RecordDeclaration ({ RecordDeclaration.comments; _ } as s) -> - RecordDeclaration { s with RecordDeclaration.comments = merge_comments comments } - | Return ({ Return.comments; _ } as s) -> - Return { s with Return.comments = merge_comments comments } - | Switch ({ Switch.comments; _ } as s) -> - Switch { s with Switch.comments = merge_comments comments } - | Throw ({ Throw.comments; _ } as s) -> - Throw { s with Throw.comments = merge_comments comments } - | Try ({ Try.comments; _ } as s) -> Try { s with Try.comments = merge_comments comments } - | TypeAlias ({ TypeAlias.comments; _ } as s) -> - TypeAlias { s with TypeAlias.comments = merge_comments comments } - | OpaqueType ({ OpaqueType.comments; _ } as s) -> - OpaqueType { s with OpaqueType.comments = merge_comments comments } - | VariableDeclaration ({ VariableDeclaration.comments; _ } as s) -> - VariableDeclaration { s with VariableDeclaration.comments = merge_comments comments } - | While ({ While.comments; _ } as s) -> - While { s with While.comments = merge_comments comments } - | With ({ With.comments; _ } as s) -> With { s with With.comments = merge_comments comments } - ) - -(* Collects the first leading and last trailing comment on an AST node or its children. - The first leading comment is the first attached comment that begins before the given node's loc, - and the last trailing comment is the last attached comment that begins after the given node's loc. *) -class ['loc] comment_bounds_collector ~loc = - object (this) - inherit ['loc] Flow_ast_mapper.mapper - - val mutable first_leading = None - - val mutable last_trailing = None - - method comment_bounds = (first_leading, last_trailing) - - method collect_comments : 'internal. ('loc, 'internal) Syntax.t -> unit = - function - | { Syntax.leading; trailing; _ } -> - List.iter this#visit_leading_comment leading; - List.iter this#visit_trailing_comment trailing - - method collect_comments_opt = - function - | None -> () - | Some comments -> this#collect_comments comments - - method visit_leading_comment ((comment_loc, _) as comment) = - let open Loc in - match first_leading with - | None -> if pos_cmp comment_loc.start loc.start < 0 then first_leading <- Some comment - | Some (current_first_loc, _) -> - if pos_cmp comment_loc.start current_first_loc.start < 0 then first_leading <- Some comment - - method visit_trailing_comment ((comment_loc, _) as comment) = - let open Loc in - match last_trailing with - | None -> if pos_cmp comment_loc.start loc._end >= 0 then last_trailing <- Some comment - | Some (current_last_loc, _) -> - if pos_cmp current_last_loc.start comment_loc.start < 0 then last_trailing <- Some comment - - method! syntax comments = - this#collect_comments comments; - comments - - method! block _loc block = - let { Statement.Block.comments; _ } = block in - this#collect_comments_opt comments; - block - end - -(* Given an AST node and a function to collect all its comments, return the first leading - and last trailing comment on the node. *) -let comment_bounds loc node f = - let collector = new comment_bounds_collector ~loc in - ignore (f collector node); - collector#comment_bounds - -(* Expand node's loc to include its attached comments *) -let expand_loc_with_comment_bounds loc (first_leading, last_trailing) = - let open Loc in - let start = - match first_leading with - | None -> loc - | Some (first_leading_loc, _) -> first_leading_loc - in - let _end = - match last_trailing with - | None -> loc - | Some (last_trailing_loc, _) -> last_trailing_loc - in - btwn start _end - -(* Remove the trailing comment bound if it is a line comment *) -let comment_bounds_without_trailing_line_comment (leading, trailing) = - match trailing with - | Some (_, { Ast.Comment.kind = Ast.Comment.Line; _ }) -> (leading, None) - | _ -> (leading, trailing) - -let collect_without_trailing_line_comment collector = - comment_bounds_without_trailing_line_comment collector#comment_bounds - -(* Return the first leading and last trailing comment of a statement *) -let statement_comment_bounds ((loc, _) as stmt : (Loc.t, Loc.t) Statement.t) : - Loc.t Comment.t option * Loc.t Comment.t option = - let collector = new comment_bounds_collector ~loc in - ignore (collector#statement stmt); - collector#comment_bounds - -let expression_comment_bounds ((loc, _) as expr) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#expression expr); - collector#comment_bounds - -let type_comment_bounds ((loc, _) as ty) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#type_ ty); - collector#comment_bounds - -let block_comment_bounds (loc, block) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#block loc block); - collector#comment_bounds - -let object_property_comment_bounds property = - let open Ast.Expression.Object in - let collector = - match property with - | Property ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_property p); - collector - | SpreadProperty ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#spread_property p); - collector - in - collect_without_trailing_line_comment collector - -let object_type_property_comment_bounds property = - let open Ast.Type.Object in - let collector = - match property with - | Property ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_property_type p); - collector - | SpreadProperty ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_spread_property_type p); - collector - | Indexer ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_indexer_property_type p); - collector - | InternalSlot ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_internal_slot_property_type p); - collector - | CallProperty ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_call_property_type p); - collector - | MappedType ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_mapped_type_property p); - collector - | PrivateField ((loc, _) as p) -> - let collector = new comment_bounds_collector ~loc in - ignore (collector#object_private_field_type p); - collector - in - collect_without_trailing_line_comment collector - -let object_pattern_property_comment_bounds loc property = - let collector = new comment_bounds_collector ~loc in - ignore (collector#pattern_object_p property); - collect_without_trailing_line_comment collector - -let match_expression_case_comment_bounds (loc, case) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#match_case ~on_case_body:collector#expression (loc, case)); - collector#comment_bounds - -let match_statement_case_comment_bounds (loc, case) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#match_case ~on_case_body:collector#statement (loc, case)); - collector#comment_bounds - -let switch_case_comment_bounds (loc, case) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#switch_case (loc, case)); - collector#comment_bounds - -let function_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_param (loc, param)); - collect_without_trailing_line_comment collector - -let function_rest_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_rest_param (loc, param)); - collect_without_trailing_line_comment collector - -let function_this_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_this_param (loc, param)); - collect_without_trailing_line_comment collector - -let function_type_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_param_type (loc, param)); - collect_without_trailing_line_comment collector - -let function_type_rest_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_rest_param_type (loc, param)); - collect_without_trailing_line_comment collector - -let function_type_this_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_this_param_type (loc, param)); - collect_without_trailing_line_comment collector - -let component_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#component_param (loc, param)); - collect_without_trailing_line_comment collector - -let component_rest_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#component_rest_param (loc, param)); - collect_without_trailing_line_comment collector - -let component_type_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#component_type_param (loc, param)); - collect_without_trailing_line_comment collector - -let component_type_rest_param_comment_bounds (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#component_type_rest_param (loc, param)); - collect_without_trailing_line_comment collector - -let array_element_comment_bounds loc element = - let collector = new comment_bounds_collector ~loc in - ignore (collector#array_element element); - collect_without_trailing_line_comment collector - -let array_pattern_element_comment_bounds loc element = - let collector = new comment_bounds_collector ~loc in - ignore (collector#pattern_array_e element); - collect_without_trailing_line_comment collector - -let expression_or_spread_comment_bounds loc expr_or_spread = - let collector = new comment_bounds_collector ~loc in - ignore (collector#expression_or_spread expr_or_spread); - collect_without_trailing_line_comment collector - -let call_type_arg_comment_bounds loc arg = - let collector = new comment_bounds_collector ~loc in - ignore (collector#call_type_arg arg); - collect_without_trailing_line_comment collector - -let type_param_comment_bounds ~kind (loc, param) = - let collector = new comment_bounds_collector ~loc in - ignore (collector#type_param ~kind (loc, param)); - collect_without_trailing_line_comment collector - -let function_body_comment_bounds body = - let loc = - match body with - | Ast.Function.BodyBlock (loc, _) -> loc - | Ast.Function.BodyExpression (loc, _) -> loc - in - let collector = new comment_bounds_collector ~loc in - ignore (collector#function_body_any body); - collector#comment_bounds - -let if_alternate_statement_comment_bounds loc alternate = - let collector = new comment_bounds_collector ~loc in - ignore (collector#if_alternate_statement loc alternate); - collector#comment_bounds - -let member_property_comment_bounds loc property = - let collector = new comment_bounds_collector ~loc in - ignore (collector#member_property property); - collector#comment_bounds + | Some node -> Some (remove_trailing env node) + +let id_remove_trailing = remove_trailing +let expression_remove_trailing = remove_trailing +let block_remove_trailing = remove_trailing +let type_params_remove_trailing = remove_trailing_opt +let type_remove_trailing = remove_trailing +let component_renders_annotation_remove_trailing = remove_trailing +let return_annotation_remove_trailing = remove_trailing +let function_params_remove_trailing = remove_trailing +let component_params_remove_trailing = remove_trailing +let component_type_params_remove_trailing = remove_trailing +let predicate_remove_trailing = remove_trailing_opt +let object_key_remove_trailing = remove_trailing +let generic_type_list_remove_trailing = remove_trailing +let class_implements_remove_trailing = remove_trailing +let string_literal_remove_trailing = remove_trailing diff --git a/compiler/flow_parser/parser/declaration_parser.ml b/compiler/flow_parser/parser/declaration_parser.ml index 992c4c4b80e..b3c247a219c 100644 --- a/compiler/flow_parser/parser/declaration_parser.ml +++ b/compiler/flow_parser/parser/declaration_parser.ml @@ -495,7 +495,6 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Parser_common.TYPE) : let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.DeclareFunctionTP (Type.type_params env) in let id = @@ -525,7 +524,6 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Parser_common.TYPE) : let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.DeclareFunctionTP (Type.type_params env) in (tparams, Some id) @@ -849,7 +847,6 @@ module Declaration (Parse : Parser_common.PARSER) (Type : Parser_common.TYPE) : let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.DeclareComponentTP (Type.type_params env) in let params = diff --git a/compiler/flow_parser/parser/dune b/compiler/flow_parser/parser/dune index 6b74a4e67d9..b1745a34803 100644 --- a/compiler/flow_parser/parser/dune +++ b/compiler/flow_parser/parser/dune @@ -8,7 +8,6 @@ Expression_parser File_key Flow_ast - Flow_ast_mapper Flow_ast_utils Flow_lexer Js_id diff --git a/compiler/flow_parser/parser/expression_parser.ml b/compiler/flow_parser/parser/expression_parser.ml index dee263b815e..40540c0b93c 100644 --- a/compiler/flow_parser/parser/expression_parser.ml +++ b/compiler/flow_parser/parser/expression_parser.ml @@ -815,7 +815,7 @@ module Expression let left = member_cover ~allow_optional_chain ~in_optional_chain env start_loc left in let left_to_callee env = let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing (as_expression env left) (fun remover left -> remover#expression left) + remove_trailing (as_expression env left) in let optional = match last_token env with @@ -946,7 +946,7 @@ module Expression if Peek.token env = T_LPAREN || (should_parse_types env && Peek.token env = T_LESS_THAN) then let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing callee (fun remover callee -> remover#expression callee) + remove_trailing callee else callee in @@ -1275,7 +1275,6 @@ module Expression let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) in (id, tparams) @@ -1948,7 +1947,7 @@ module Expression with_loc (fun env -> let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) + type_params_remove_trailing env (Type.type_params env) in (* Disallow all fancy features for identifier => body *) if Peek.is_identifier env && tparams = None then diff --git a/compiler/flow_parser/parser/flow_ast_mapper.ml b/compiler/flow_parser/parser/flow_ast_mapper.ml deleted file mode 100644 index 79190c77b47..00000000000 --- a/compiler/flow_parser/parser/flow_ast_mapper.ml +++ /dev/null @@ -1,4033 +0,0 @@ -(* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - *) - -module Ast = Flow_ast - -let map_opt : 'node. ('node -> 'node) -> 'node option -> 'node option = - fun map opt -> - match opt with - | Some item -> - let item' = map item in - if item == item' then - opt - else - Some item' - | None -> opt - -let id_loc : 'node 'a. ('loc -> 'node -> 'node) -> 'loc -> 'node -> 'a -> ('node -> 'a) -> 'a = - fun map loc item same diff -> - let item' = map loc item in - if item == item' then - same - else - diff item' - -let id : 'node 'a. ('node -> 'node) -> 'node -> 'a -> ('node -> 'a) -> 'a = - fun map item same diff -> - let item' = map item in - if item == item' then - same - else - diff item' - -let map_loc : 'node. ('loc -> 'node -> 'node) -> 'loc * 'node -> 'loc * 'node = - fun map same -> - let (loc, item) = same in - id_loc map loc item same (fun diff -> (loc, diff)) - -let map_loc_opt : 'node. ('loc -> 'node -> 'node) -> ('loc * 'node) option -> ('loc * 'node) option - = - fun map same -> - map_opt - (fun same -> - let (loc, item) = same in - id_loc map loc item same (fun diff -> (loc, diff))) - same - -let map_list map lst = - let (rev_lst, changed) = - List.fold_left - (fun (lst', changed) item -> - let item' = map item in - (item' :: lst', changed || item' != item)) - ([], false) - lst - in - if changed then - List.rev rev_lst - else - lst - -let map_list_multiple map lst = - let (rev_lst, changed) = - List.fold_left - (fun (lst', changed) item -> - match map item with - | [] -> (lst', true) - | [item'] -> (item' :: lst', changed || item != item') - | items' -> (List.rev_append items' lst', true)) - ([], false) - lst - in - if changed then - List.rev rev_lst - else - lst - -type type_params_context = - | ClassTP - | FunctionTP - | DeclareFunctionTP - | DeclareClassTP - | DeclareComponentTP - | TypeAliasTP - | InterfaceTP - | OpaqueTypeTP - | ComponentDeclarationTP - | ComponentTypeTP - | FunctionTypeTP - | InferTP - | ObjectMappedTypeTP - | RecordTP - -class ['loc] mapper = - object (this) - method program (program : ('loc, 'loc) Ast.Program.t) = - let open Ast.Program in - let (loc, { statements; interpreter; comments; all_comments }) = program in - let statements' = this#toplevel_statement_list statements in - let comments' = this#syntax_opt comments in - let all_comments' = map_list this#comment all_comments in - if statements == statements' && comments == comments' && all_comments == all_comments' then - program - else - ( loc, - { - statements = statements'; - interpreter; - comments = comments'; - all_comments = all_comments'; - } - ) - - method statement (stmt : ('loc, 'loc) Ast.Statement.t) = - let open Ast.Statement in - match stmt with - | (loc, Block block) -> id_loc this#block loc block stmt (fun block -> (loc, Block block)) - | (loc, Break break) -> id_loc this#break loc break stmt (fun break -> (loc, Break break)) - | (loc, ClassDeclaration cls) -> - id_loc this#class_declaration loc cls stmt (fun cls -> (loc, ClassDeclaration cls)) - | (loc, ComponentDeclaration component) -> - id_loc this#component_declaration loc component stmt (fun component -> - (loc, ComponentDeclaration component) - ) - | (loc, Continue cont) -> id_loc this#continue loc cont stmt (fun cont -> (loc, Continue cont)) - | (loc, Debugger dbg) -> id_loc this#debugger loc dbg stmt (fun dbg -> (loc, Debugger dbg)) - | (loc, DeclareClass stuff) -> - id_loc this#declare_class loc stuff stmt (fun stuff -> (loc, DeclareClass stuff)) - | (loc, DeclareComponent stuff) -> - id_loc this#declare_component loc stuff stmt (fun stuff -> (loc, DeclareComponent stuff)) - | (loc, DeclareEnum enum) -> - id_loc this#declare_enum loc enum stmt (fun enum -> (loc, DeclareEnum enum)) - | (loc, DeclareExportDeclaration decl) -> - id_loc this#declare_export_declaration loc decl stmt (fun decl -> - (loc, DeclareExportDeclaration decl) - ) - | (loc, DeclareFunction stuff) -> - id_loc this#declare_function loc stuff stmt (fun stuff -> (loc, DeclareFunction stuff)) - | (loc, DeclareInterface stuff) -> - id_loc this#declare_interface loc stuff stmt (fun stuff -> (loc, DeclareInterface stuff)) - | (loc, DeclareModule m) -> - id_loc this#declare_module loc m stmt (fun m -> (loc, DeclareModule m)) - | (loc, DeclareModuleExports annot) -> - id_loc this#declare_module_exports loc annot stmt (fun annot -> - (loc, DeclareModuleExports annot) - ) - | (loc, DeclareNamespace n) -> - id_loc this#declare_namespace loc n stmt (fun n -> (loc, DeclareNamespace n)) - | (loc, DeclareOpaqueType otype) -> - id_loc this#opaque_type loc otype stmt (fun otype -> (loc, DeclareOpaqueType otype)) - | (loc, DeclareTypeAlias stuff) -> - id_loc this#declare_type_alias loc stuff stmt (fun stuff -> (loc, DeclareTypeAlias stuff)) - | (loc, DeclareVariable stuff) -> - id_loc this#declare_variable loc stuff stmt (fun stuff -> (loc, DeclareVariable stuff)) - | (loc, DoWhile stuff) -> - id_loc this#do_while loc stuff stmt (fun stuff -> (loc, DoWhile stuff)) - | (loc, Empty empty) -> id_loc this#empty loc empty stmt (fun empty -> (loc, Empty empty)) - | (loc, EnumDeclaration enum) -> - id_loc this#enum_declaration loc enum stmt (fun enum -> (loc, EnumDeclaration enum)) - | (loc, ExportDefaultDeclaration decl) -> - id_loc this#export_default_declaration loc decl stmt (fun decl -> - (loc, ExportDefaultDeclaration decl) - ) - | (loc, ExportNamedDeclaration decl) -> - id_loc this#export_named_declaration loc decl stmt (fun decl -> - (loc, ExportNamedDeclaration decl) - ) - | (loc, ExportAssignment assign) -> - id_loc this#export_assignment loc assign stmt (fun assign -> (loc, ExportAssignment assign)) - | (loc, NamespaceExportDeclaration decl) -> - id_loc this#namespace_export_declaration loc decl stmt (fun decl -> - (loc, NamespaceExportDeclaration decl) - ) - | (loc, Expression expr) -> - id_loc this#expression_statement loc expr stmt (fun expr -> (loc, Expression expr)) - | (loc, For for_stmt) -> - id_loc this#for_statement loc for_stmt stmt (fun for_stmt -> (loc, For for_stmt)) - | (loc, ForIn stuff) -> - id_loc this#for_in_statement loc stuff stmt (fun stuff -> (loc, ForIn stuff)) - | (loc, ForOf stuff) -> - id_loc this#for_of_statement loc stuff stmt (fun stuff -> (loc, ForOf stuff)) - | (loc, FunctionDeclaration func) -> - id_loc this#function_declaration loc func stmt (fun func -> (loc, FunctionDeclaration func)) - | (loc, If if_stmt) -> - id_loc this#if_statement loc if_stmt stmt (fun if_stmt -> (loc, If if_stmt)) - | (loc, ImportDeclaration decl) -> - id_loc this#import_declaration loc decl stmt (fun decl -> (loc, ImportDeclaration decl)) - | (loc, ImportEqualsDeclaration decl) -> - id_loc this#import_equals_declaration loc decl stmt (fun decl -> - (loc, ImportEqualsDeclaration decl) - ) - | (loc, InterfaceDeclaration stuff) -> - id_loc this#interface_declaration loc stuff stmt (fun stuff -> - (loc, InterfaceDeclaration stuff) - ) - | (loc, Labeled label) -> - id_loc this#labeled_statement loc label stmt (fun label -> (loc, Labeled label)) - | (loc, Match x) -> id_loc this#match_statement loc x stmt (fun x -> (loc, Match x)) - | (loc, OpaqueType otype) -> - id_loc this#opaque_type loc otype stmt (fun otype -> (loc, OpaqueType otype)) - | (loc, RecordDeclaration record) -> - id_loc this#record_declaration loc record stmt (fun record -> - (loc, RecordDeclaration record) - ) - | (loc, Return ret) -> id_loc this#return loc ret stmt (fun ret -> (loc, Return ret)) - | (loc, Switch switch) -> - id_loc this#switch loc switch stmt (fun switch -> (loc, Switch switch)) - | (loc, Throw throw) -> id_loc this#throw loc throw stmt (fun throw -> (loc, Throw throw)) - | (loc, Try try_stmt) -> - id_loc this#try_catch loc try_stmt stmt (fun try_stmt -> (loc, Try try_stmt)) - | (loc, VariableDeclaration decl) -> - id_loc this#variable_declaration loc decl stmt (fun decl -> (loc, VariableDeclaration decl)) - | (loc, While stuff) -> id_loc this#while_ loc stuff stmt (fun stuff -> (loc, While stuff)) - | (loc, With stuff) -> id_loc this#with_ loc stuff stmt (fun stuff -> (loc, With stuff)) - | (loc, TypeAlias stuff) -> - id_loc this#type_alias loc stuff stmt (fun stuff -> (loc, TypeAlias stuff)) - - method comment (c : 'loc Ast.Comment.t) = c - - method syntax_opt - : 'internal. ('loc, 'internal) Ast.Syntax.t option -> ('loc, 'internal) Ast.Syntax.t option - = - map_opt this#syntax - - method syntax : 'internal. ('loc, 'internal) Ast.Syntax.t -> ('loc, 'internal) Ast.Syntax.t = - fun attached -> - let open Ast.Syntax in - let { leading; trailing; internal } = attached in - let leading' = map_list this#comment leading in - let trailing' = map_list this#comment trailing in - if leading == leading' && trailing == trailing' then - attached - else - { leading = leading'; trailing = trailing'; internal } - - method expression (expr : ('loc, 'loc) Ast.Expression.t) = - let open Ast.Expression in - match expr with - | (loc, Array x) -> id_loc this#array loc x expr (fun x -> (loc, Array x)) - | (loc, ArrowFunction x) -> - id_loc this#arrow_function loc x expr (fun x -> (loc, ArrowFunction x)) - | (loc, AsConstExpression x) -> - id_loc this#as_const_expression loc x expr (fun x -> (loc, AsConstExpression x)) - | (loc, AsExpression x) -> - id_loc this#as_expression loc x expr (fun x -> (loc, AsExpression x)) - | (loc, Assignment x) -> id_loc this#assignment loc x expr (fun x -> (loc, Assignment x)) - | (loc, Binary x) -> id_loc this#binary loc x expr (fun x -> (loc, Binary x)) - | (loc, Call x) -> id_loc this#call loc x expr (fun x -> (loc, Call x)) - | (loc, Class x) -> id_loc this#class_expression loc x expr (fun x -> (loc, Class x)) - | (loc, Conditional x) -> id_loc this#conditional loc x expr (fun x -> (loc, Conditional x)) - | (loc, Function x) -> id_loc this#function_expression loc x expr (fun x -> (loc, Function x)) - | (loc, Identifier x) -> id this#identifier x expr (fun x -> (loc, Identifier x)) - | (loc, Import x) -> id this#import x expr (fun x -> (loc, Import x)) - | (loc, JSXElement x) -> id_loc this#jsx_element loc x expr (fun x -> (loc, JSXElement x)) - | (loc, JSXFragment x) -> id_loc this#jsx_fragment loc x expr (fun x -> (loc, JSXFragment x)) - | (loc, StringLiteral x) -> id this#string_literal x expr (fun x -> (loc, StringLiteral x)) - | (loc, BooleanLiteral x) -> id this#boolean_literal x expr (fun x -> (loc, BooleanLiteral x)) - | (loc, NullLiteral x) -> id this#null_literal x expr (fun x -> (loc, NullLiteral x)) - | (loc, NumberLiteral x) -> id this#number_literal x expr (fun x -> (loc, NumberLiteral x)) - | (loc, BigIntLiteral x) -> id this#bigint_literal x expr (fun x -> (loc, BigIntLiteral x)) - | (loc, RegExpLiteral x) -> id this#regexp_literal x expr (fun x -> (loc, RegExpLiteral x)) - | (loc, ModuleRefLiteral x) -> - id this#module_ref_literal x expr (fun x -> (loc, ModuleRefLiteral x)) - | (loc, Logical x) -> id_loc this#logical loc x expr (fun x -> (loc, Logical x)) - | (loc, Match x) -> id_loc this#match_expression loc x expr (fun x -> (loc, Match x)) - | (loc, Member x) -> id_loc this#member loc x expr (fun x -> (loc, Member x)) - | (loc, MetaProperty x) -> - id_loc this#meta_property loc x expr (fun x -> (loc, MetaProperty x)) - | (loc, New x) -> id_loc this#new_ loc x expr (fun x -> (loc, New x)) - | (loc, Object x) -> id_loc this#object_ loc x expr (fun x -> (loc, Object x)) - | (loc, OptionalCall x) -> id (this#optional_call loc) x expr (fun x -> (loc, OptionalCall x)) - | (loc, OptionalMember x) -> - id_loc this#optional_member loc x expr (fun x -> (loc, OptionalMember x)) - | (loc, Record x) -> id_loc this#record loc x expr (fun x -> (loc, Record x)) - | (loc, Sequence x) -> id_loc this#sequence loc x expr (fun x -> (loc, Sequence x)) - | (loc, Super x) -> id_loc this#super_expression loc x expr (fun x -> (loc, Super x)) - | (loc, TaggedTemplate x) -> - id_loc this#tagged_template loc x expr (fun x -> (loc, TaggedTemplate x)) - | (loc, TemplateLiteral x) -> - id_loc this#template_literal loc x expr (fun x -> (loc, TemplateLiteral x)) - | (loc, This x) -> id_loc this#this_expression loc x expr (fun x -> (loc, This x)) - | (loc, TypeCast x) -> id_loc this#type_cast loc x expr (fun x -> (loc, TypeCast x)) - | (loc, TSSatisfies x) -> id_loc this#ts_satisfies loc x expr (fun x -> (loc, TSSatisfies x)) - | (loc, Unary x) -> id_loc this#unary_expression loc x expr (fun x -> (loc, Unary x)) - | (loc, Update x) -> id_loc this#update_expression loc x expr (fun x -> (loc, Update x)) - | (loc, Yield x) -> id_loc this#yield loc x expr (fun x -> (loc, Yield x)) - - method array _loc (expr : ('loc, 'loc) Ast.Expression.Array.t) = - let open Ast.Expression in - let { Array.elements; trailing_comma; comments } = expr in - let elements' = map_list this#array_element elements in - let comments' = this#syntax_opt comments in - if elements == elements' && comments == comments' then - expr - else - { Array.elements = elements'; trailing_comma; comments = comments' } - - method array_element element = - let open Ast.Expression.Array in - match element with - | Expression expr -> id this#expression expr element (fun expr -> Expression expr) - | Spread spread -> id this#spread_element spread element (fun spread -> Spread spread) - | Hole _ -> element - - method arrow_function loc (expr : ('loc, 'loc) Ast.Function.t) = this#function_ loc expr - - method as_const_expression _loc (expr : ('loc, 'loc) Ast.Expression.AsConstExpression.t) = - let open Ast.Expression.AsConstExpression in - let { expression; comments } = expr in - let expression' = this#expression expression in - let comments' = this#syntax_opt comments in - if expression' == expression && comments' == comments then - expr - else - { expression = expression'; comments = comments' } - - method as_expression _loc (expr : ('loc, 'loc) Ast.Expression.AsExpression.t) = - let open Ast.Expression.AsExpression in - let { expression; annot; comments } = expr in - let expression' = this#expression expression in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if expression' == expression && annot' == annot && comments' == comments then - expr - else - { expression = expression'; annot = annot'; comments = comments' } - - method assignment _loc (expr : ('loc, 'loc) Ast.Expression.Assignment.t) = - let open Ast.Expression.Assignment in - let { operator = _; left; right; comments } = expr in - let left' = this#assignment_pattern left in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if left == left' && right == right' && comments == comments' then - expr - else - { expr with left = left'; right = right'; comments = comments' } - - method binary _loc (expr : ('loc, 'loc) Ast.Expression.Binary.t) = - let open Ast.Expression.Binary in - let { operator = _; left; right; comments } = expr in - let left' = this#expression left in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if left == left' && right == right' && comments == comments' then - expr - else - { expr with left = left'; right = right'; comments = comments' } - - method block _loc (stmt : ('loc, 'loc) Ast.Statement.Block.t) = - let open Ast.Statement.Block in - let { body; comments } = stmt in - let body' = this#statement_list body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - stmt - else - { body = body'; comments = comments' } - - method break _loc (break : 'loc Ast.Statement.Break.t) = - let open Ast.Statement.Break in - let { label; comments } = break in - let label' = map_opt this#label_identifier label in - let comments' = this#syntax_opt comments in - if label == label' && comments == comments' then - break - else - { label = label'; comments = comments' } - - method call _loc (expr : ('loc, 'loc) Ast.Expression.Call.t) = - let open Ast.Expression.Call in - let { callee; targs; arguments; comments } = expr in - let callee' = this#expression callee in - let targs' = map_opt this#call_type_args targs in - let arguments' = this#arg_list arguments in - let comments' = this#syntax_opt comments in - if callee == callee' && targs == targs' && arguments == arguments' && comments == comments' - then - expr - else - { callee = callee'; targs = targs'; arguments = arguments'; comments = comments' } - - method arg_list (arg_list : ('loc, 'loc) Ast.Expression.ArgList.t) = - let open Ast.Expression.ArgList in - let (loc, { arguments; comments }) = arg_list in - let arguments' = map_list this#expression_or_spread arguments in - let comments' = this#syntax_opt comments in - if arguments == arguments' && comments == comments' then - arg_list - else - (loc, { arguments = arguments'; comments = comments' }) - - method optional_call loc (expr : ('loc, 'loc) Ast.Expression.OptionalCall.t) = - let open Ast.Expression.OptionalCall in - let { call; optional = _; filtered_out = _ } = expr in - let call' = this#call loc call in - if call == call' then - expr - else - { expr with call = call' } - - method call_type_args (targs : ('loc, 'loc) Ast.Expression.CallTypeArgs.t) = - let open Ast.Expression.CallTypeArgs in - let (loc, { arguments; comments }) = targs in - let arguments' = map_list this#call_type_arg arguments in - let comments' = this#syntax_opt comments in - if arguments == arguments' && comments == comments' then - targs - else - (loc, { arguments = arguments'; comments = comments' }) - - method call_type_arg t = - let open Ast.Expression.CallTypeArg in - match t with - | Explicit x -> - let x' = this#type_ x in - if x' == x then - t - else - Explicit x' - | Implicit (loc, { Implicit.comments }) -> - let comments' = this#syntax_opt comments in - if comments == comments' then - t - else - Implicit (loc, { Implicit.comments = comments' }) - - method catch_body (body : 'loc * ('loc, 'loc) Ast.Statement.Block.t) = map_loc this#block body - - method catch_clause _loc (clause : ('loc, 'loc) Ast.Statement.Try.CatchClause.t') = - let open Ast.Statement.Try.CatchClause in - let { param; body; comments } = clause in - let param' = map_opt this#catch_clause_pattern param in - let body' = this#catch_body body in - let comments' = this#syntax_opt comments in - if param == param' && body == body' && comments == comments' then - clause - else - { param = param'; body = body'; comments = comments' } - - method class_declaration loc (cls : ('loc, 'loc) Ast.Class.t) = this#class_ loc cls - - method class_expression loc (cls : ('loc, 'loc) Ast.Class.t) = this#class_ loc cls - - method class_ _loc (cls : ('loc, 'loc) Ast.Class.t) = - let open Ast.Class in - let { id; body; tparams; extends; implements; class_decorators; abstract; comments } = cls in - let id' = map_opt this#class_identifier id in - let tparams' = map_opt (this#type_params ~kind:ClassTP) tparams in - let body' = this#class_body body in - let extends' = map_opt (map_loc this#class_extends) extends in - let implements' = map_opt this#class_implements implements in - let class_decorators' = map_list this#class_decorator class_decorators in - let comments' = this#syntax_opt comments in - if - id == id' - && body == body' - && extends == extends' - && implements == implements' - && class_decorators == class_decorators' - && comments == comments' - && tparams == tparams' - then - cls - else - { - id = id'; - body = body'; - extends = extends'; - implements = implements'; - class_decorators = class_decorators'; - abstract; - tparams = tparams'; - comments = comments'; - } - - method class_extends _loc (extends : ('loc, 'loc) Ast.Class.Extends.t') = - let open Ast.Class.Extends in - let { expr; targs; comments } = extends in - let expr' = this#expression expr in - let targs' = map_opt this#type_args targs in - let comments' = this#syntax_opt comments in - if expr == expr' && targs == targs' && comments == comments' then - extends - else - { expr = expr'; targs = targs'; comments = comments' } - - method class_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = - this#pattern_identifier ~kind:Ast.Variable.Let ident - - method class_body (cls_body : ('loc, 'loc) Ast.Class.Body.t) = - let open Ast.Class.Body in - let (loc, { body; comments }) = cls_body in - let body' = map_list this#class_element body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - cls_body - else - (loc, { body = body'; comments = comments' }) - - method class_decorator (dec : ('loc, 'loc) Ast.Class.Decorator.t) = - let open Ast.Class.Decorator in - let (loc, { expression; comments }) = dec in - let expression' = this#expression expression in - let comments' = this#syntax_opt comments in - if expression == expression' && comments == comments' then - dec - else - (loc, { expression = expression'; comments = comments' }) - - method class_element (elem : ('loc, 'loc) Ast.Class.Body.element) = - let open Ast.Class.Body in - match elem with - | Method (loc, meth) -> id_loc this#class_method loc meth elem (fun meth -> Method (loc, meth)) - | Property (loc, prop) -> - id_loc this#class_property loc prop elem (fun prop -> Property (loc, prop)) - | PrivateField (loc, field) -> - id_loc this#class_private_field loc field elem (fun field -> PrivateField (loc, field)) - | StaticBlock (loc, block) -> - id_loc this#class_static_block loc block elem (fun block -> StaticBlock (loc, block)) - | DeclareMethod (loc, decl_meth) -> - id_loc this#class_declare_method loc decl_meth elem (fun decl_meth -> - DeclareMethod (loc, decl_meth) - ) - | AbstractMethod (loc, abs_meth) -> - id_loc this#class_abstract_method loc abs_meth elem (fun abs_meth -> - AbstractMethod (loc, abs_meth) - ) - | AbstractProperty (loc, abs_prop) -> - id_loc this#class_abstract_property loc abs_prop elem (fun abs_prop -> - AbstractProperty (loc, abs_prop) - ) - | IndexSignature indexer -> - id this#class_indexer indexer elem (fun indexer -> IndexSignature indexer) - - method class_indexer (indexer : ('loc, 'loc) Ast.Type.Object.Indexer.t) = - this#object_indexer_property_type indexer - - method class_implements (implements : ('loc, 'loc) Ast.Class.Implements.t) = - let open Ast.Class.Implements in - let (loc, { interfaces; comments }) = implements in - let interfaces' = map_list this#class_implements_interface interfaces in - let comments' = this#syntax_opt comments in - if interfaces == interfaces' && comments == comments' then - implements - else - (loc, { interfaces = interfaces'; comments = comments' }) - - method class_implements_interface (interface : ('loc, 'loc) Ast.Class.Implements.Interface.t) = - let open Ast.Class.Implements.Interface in - let (loc, { id; targs }) = interface in - let id' = this#generic_identifier_type id in - let targs' = map_opt this#type_args targs in - if id == id' && targs == targs' then - interface - else - (loc, { id = id'; targs = targs' }) - - method class_method _loc (meth : ('loc, 'loc) Ast.Class.Method.t') = - let open Ast.Class.Method in - let { - kind = _; - key; - value; - static = _; - override = _; - ts_accessibility = _; - decorators; - comments; - } = - meth - in - let key' = this#object_key key in - let value' = map_loc this#function_expression_or_method value in - let decorators' = map_list this#class_decorator decorators in - let comments' = this#syntax_opt comments in - if key == key' && value == value' && decorators == decorators' && comments == comments' then - meth - else - { meth with key = key'; value = value'; decorators = decorators'; comments = comments' } - - method class_declare_method _loc (decl_meth : ('loc, 'loc) Ast.Class.DeclareMethod.t') = - let open Ast.Class.DeclareMethod in - let { kind = _; key; annot; static = _; override = _; optional = _; comments } = decl_meth in - let key' = this#object_key key in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if key == key' && annot == annot' && comments == comments' then - decl_meth - else - { decl_meth with key = key'; annot = annot'; comments = comments' } - - method class_abstract_method _loc (abs_meth : ('loc, 'loc) Ast.Class.AbstractMethod.t') = - let open Ast.Class.AbstractMethod in - let { key; annot = (annot_loc, func); override = _; ts_accessibility = _; comments } = - abs_meth - in - let key' = this#object_key key in - let func' = this#function_type func in - let comments' = this#syntax_opt comments in - if key == key' && func == func' && comments == comments' then - abs_meth - else - { abs_meth with key = key'; annot = (annot_loc, func'); comments = comments' } - - method class_abstract_property _loc (abs_prop : ('loc, 'loc) Ast.Class.AbstractProperty.t') = - let open Ast.Class.AbstractProperty in - let { key; annot; override = _; ts_accessibility = _; variance; comments } = abs_prop in - let key' = this#object_key key in - let annot' = this#type_annotation_hint annot in - let variance' = this#variance_opt variance in - let comments' = this#syntax_opt comments in - if key == key' && annot == annot' && variance == variance' && comments == comments' then - abs_prop - else - { abs_prop with key = key'; annot = annot'; variance = variance'; comments = comments' } - - method class_property _loc (prop : ('loc, 'loc) Ast.Class.Property.t') = - let open Ast.Class.Property in - let { - key; - value; - annot; - static = _; - override = _; - optional = _; - variance; - ts_accessibility = _; - decorators; - comments; - } = - prop - in - let key' = this#object_key key in - let value' = this#class_property_value value in - let annot' = this#type_annotation_hint annot in - let variance' = this#variance_opt variance in - let decorators' = map_list this#class_decorator decorators in - let comments' = this#syntax_opt comments in - if - key == key' - && value == value' - && annot' == annot - && variance' == variance - && decorators' == decorators - && comments' == comments - then - prop - else - { - prop with - key = key'; - value = value'; - annot = annot'; - variance = variance'; - decorators = decorators'; - comments = comments'; - } - - method class_property_value (value : ('loc, 'loc) Ast.Class.Property.value) = - let open Ast.Class.Property in - match value with - | Declared -> value - | Uninitialized -> value - | Initialized x -> - let x' = this#expression x in - if x == x' then - value - else - Initialized x' - - method class_private_field _loc (prop : ('loc, 'loc) Ast.Class.PrivateField.t') = - let open Ast.Class.PrivateField in - let { - key; - value; - annot; - static = _; - override = _; - optional = _; - variance; - ts_accessibility = _; - decorators; - comments; - } = - prop - in - let key' = this#private_name key in - let value' = this#class_property_value value in - let annot' = this#type_annotation_hint annot in - let variance' = this#variance_opt variance in - let decorators' = map_list this#class_decorator decorators in - let comments' = this#syntax_opt comments in - if - key == key' - && value == value' - && annot' == annot - && variance' == variance - && decorators' == decorators - && comments' == comments - then - prop - else - { - prop with - key = key'; - value = value'; - annot = annot'; - variance = variance'; - decorators = decorators'; - comments = comments'; - } - - method class_static_block _loc (block : ('loc, 'loc) Ast.Class.StaticBlock.t') = - let open Ast.Class.StaticBlock in - let { body; comments } = block in - let body' = this#statement_list body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - block - else - { body = body'; comments = comments' } - - method default_opt (default : ('loc, 'loc) Ast.Expression.t option) = - map_opt this#expression default - - method component_declaration _loc (component : ('loc, 'loc) Ast.Statement.ComponentDeclaration.t) - = - let open Ast.Statement.ComponentDeclaration in - let { id = ident; tparams; params; body; renders; async; comments; sig_loc } = component in - let ident' = this#component_identifier ident in - let tparams' = map_opt (this#type_params ~kind:ComponentDeclarationTP) tparams in - let params' = this#component_params params in - let body' = map_opt this#component_body body in - let renders' = this#component_renders_annotation renders in - let comments' = this#syntax_opt comments in - if - ident == ident' - && tparams == tparams' - && params == params' - && body == body' - && renders == renders' - && comments == comments' - then - component - else - { - id = ident'; - tparams = tparams'; - params = params'; - body = body'; - renders = renders'; - async; - comments = comments'; - sig_loc; - } - - method component_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = - this#pattern_identifier ~kind:Ast.Variable.Var ident - - method component_params (params : ('loc, 'loc) Ast.Statement.ComponentDeclaration.Params.t) = - let open Ast.Statement.ComponentDeclaration in - let (loc, { Params.params = params_list; rest; comments }) = params in - let params_list' = map_list this#component_param params_list in - let rest' = map_opt this#component_rest_param rest in - let comments' = this#syntax_opt comments in - if params_list == params_list' && rest == rest' && comments == comments' then - params - else - (loc, { Params.params = params_list'; rest = rest'; comments = comments' }) - - method component_param (param : ('loc, 'loc) Ast.Statement.ComponentDeclaration.Param.t) = - let open Ast.Statement.ComponentDeclaration.Param in - let (loc, { name; local; default; shorthand }) = param in - let name' = this#component_param_name name in - let local' = this#component_param_pattern local in - let default' = this#default_opt default in - if name == name' && local == local' && default == default' then - param - else - (loc, { name = name'; local = local'; default = default'; shorthand }) - - method component_param_name - (param_name : ('loc, 'loc) Ast.Statement.ComponentDeclaration.Param.param_name) = - let open Ast.Statement.ComponentDeclaration.Param in - match param_name with - | Identifier ident -> id this#identifier ident param_name (fun x -> Identifier x) - | StringLiteral (loc, str) -> - id this#string_literal str param_name (fun x -> StringLiteral (loc, x)) - - method component_param_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = - this#binding_pattern ~kind:Ast.Variable.Let expr - - method component_rest_param (expr : ('loc, 'loc) Ast.Statement.ComponentDeclaration.RestParam.t) - = - let open Ast.Statement.ComponentDeclaration.RestParam in - let (loc, { argument; comments }) = expr in - let argument' = this#component_param_pattern argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - (loc, { argument = argument'; comments = comments' }) - - method component_body (body : 'loc * ('loc, 'loc) Ast.Statement.Block.t) = - let (loc, block) = body in - id_loc this#block loc block body (fun block -> (loc, block)) - - method conditional _loc (expr : ('loc, 'loc) Ast.Expression.Conditional.t) = - let open Ast.Expression.Conditional in - let { test; consequent; alternate; comments } = expr in - let test' = this#predicate_expression test in - let consequent' = this#expression consequent in - let alternate' = this#expression alternate in - let comments' = this#syntax_opt comments in - if - test == test' - && consequent == consequent' - && alternate == alternate' - && comments == comments' - then - expr - else - { test = test'; consequent = consequent'; alternate = alternate'; comments = comments' } - - method continue _loc (cont : 'loc Ast.Statement.Continue.t) = - let open Ast.Statement.Continue in - let { label; comments } = cont in - let label' = map_opt this#label_identifier label in - let comments' = this#syntax_opt comments in - if label == label' && comments == comments' then - cont - else - { label = label'; comments = comments' } - - method debugger _loc (dbg : 'loc Ast.Statement.Debugger.t) = - let open Ast.Statement.Debugger in - let { comments } = dbg in - let comments' = this#syntax_opt comments in - if comments == comments' then - dbg - else - { comments = comments' } - - method declare_class _loc (decl : ('loc, 'loc) Ast.Statement.DeclareClass.t) = - let open Ast.Statement.DeclareClass in - let { id = ident; tparams; body; extends; mixins; implements; abstract; comments } = decl in - let id' = this#class_identifier ident in - let tparams' = map_opt (this#type_params ~kind:DeclareClassTP) tparams in - let body' = - let (bloc, b) = body in - id this#object_type b body (fun b' -> (bloc, b')) - in - let extends' = - map_opt - (fun (ext_loc, ext) -> - let ext' = this#declare_class_extends ext in - if ext' == ext then - (ext_loc, ext) - else - (ext_loc, ext')) - extends - in - let mixins' = - map_list - (fun mix -> - let (mloc, m) = mix in - id this#generic_type m mix (fun m' -> (mloc, m'))) - mixins - in - let implements' = map_opt this#class_implements implements in - let comments' = this#syntax_opt comments in - if - id' == ident - && tparams' == tparams - && body' == body - && extends' == extends - && mixins' == mixins - && implements' == implements - && comments' == comments - then - decl - else - { - id = id'; - tparams = tparams'; - body = body'; - extends = extends'; - mixins = mixins'; - implements = implements'; - abstract; - comments = comments'; - } - - method declare_class_extends (ext : ('loc, 'loc) Ast.Statement.DeclareClass.extends) = - let open Ast.Statement.DeclareClass in - match ext with - | ExtendsIdent e -> id this#generic_type e ext (fun e' -> ExtendsIdent e') - | ExtendsCall { callee = (callee_loc, callee); arg = (arg_loc, arg) } -> - let callee' = id this#generic_type callee callee (fun c -> c) in - let arg' = this#declare_class_extends arg in - if callee' == callee && arg' == arg then - ext - else - ExtendsCall { callee = (callee_loc, callee'); arg = (arg_loc, arg') } - - method declare_component _loc (decl : ('loc, 'loc) Ast.Statement.DeclareComponent.t) = - let open Ast.Statement.DeclareComponent in - let { id = ident; tparams; params; renders; comments } = decl in - let ident' = this#component_identifier ident in - let tparams' = map_opt (this#type_params ~kind:DeclareComponentTP) tparams in - let params' = this#component_params params in - let renders' = this#component_renders_annotation renders in - let comments' = this#syntax_opt comments in - if - ident == ident' - && tparams == tparams' - && params == params' - && renders == renders' - && comments == comments' - then - decl - else - { - id = ident'; - tparams = tparams'; - params = params'; - renders = renders'; - comments = comments'; - } - - method component_type _loc (t : ('loc, 'loc) Ast.Type.Component.t) = - let open Ast.Type.Component in - let { tparams; params; renders; comments } = t in - let tparams' = map_opt (this#type_params ~kind:ComponentTypeTP) tparams in - let params' = this#component_type_params params in - let renders' = this#component_renders_annotation renders in - let comments' = this#syntax_opt comments in - if tparams == tparams' && params == params' && renders == renders' && comments == comments' - then - t - else - { tparams = tparams'; params = params'; renders = renders'; comments = comments' } - - method component_type_params (params : ('loc, 'loc) Ast.Type.Component.Params.t) = - let open Ast.Type.Component in - let (loc, { Params.params = params_list; rest; comments }) = params in - let params_list' = map_list this#component_type_param params_list in - let rest' = map_opt this#component_type_rest_param rest in - let comments' = this#syntax_opt comments in - if params_list == params_list' && rest == rest' && comments == comments' then - params - else - (loc, { Params.params = params_list'; rest = rest'; comments = comments' }) - - method component_type_param (param : ('loc, 'loc) Ast.Type.Component.Param.t) = - let open Ast.Type.Component.Param in - let (loc, { name; annot; optional }) = param in - let name' = this#component_param_name name in - let annot' = this#type_annotation annot in - if name == name' && annot == annot' then - param - else - (loc, { name = name'; annot = annot'; optional }) - - method component_type_rest_param (expr : ('loc, 'loc) Ast.Type.Component.RestParam.t) = - let open Ast.Type.Component.RestParam in - let (loc, { argument; annot; optional; comments }) = expr in - let argument' = map_opt this#identifier argument in - let annot' = this#type_ annot in - let comments' = this#syntax_opt comments in - if argument == argument' && annot == annot' && comments == comments' then - expr - else - (loc, { argument = argument'; annot = annot'; comments = comments'; optional }) - - method declare_enum loc (enum : ('loc, 'loc) Ast.Statement.EnumDeclaration.t) = - this#enum_declaration loc enum - - method declare_export_declaration - _loc (decl : ('loc, 'loc) Ast.Statement.DeclareExportDeclaration.t) = - let open Ast.Statement.DeclareExportDeclaration in - let { default; source; specifiers; declaration; comments } = decl in - let source' = map_loc_opt this#export_source source in - let specifiers' = map_opt this#export_named_specifier specifiers in - let declaration' = map_opt this#declare_export_declaration_decl declaration in - let comments' = this#syntax_opt comments in - if - source == source' - && specifiers == specifiers' - && declaration == declaration' - && comments == comments' - then - decl - else - { - default; - source = source'; - specifiers = specifiers'; - declaration = declaration'; - comments = comments'; - } - - method declare_export_declaration_decl - (decl : ('loc, 'loc) Ast.Statement.DeclareExportDeclaration.declaration) = - let open Ast.Statement.DeclareExportDeclaration in - match decl with - | Variable (loc, dv) -> - let dv' = this#declare_variable loc dv in - if dv' == dv then - decl - else - Variable (loc, dv') - | Function (loc, df) -> - let df' = this#declare_function loc df in - if df' == df then - decl - else - Function (loc, df') - | Class (loc, dc) -> - let dc' = this#declare_class loc dc in - if dc' == dc then - decl - else - Class (loc, dc') - | Component (loc, dc) -> - let dc' = this#declare_component loc dc in - if dc' == dc then - decl - else - Component (loc, dc') - | DefaultType t -> - let t' = this#type_ t in - if t' == t then - decl - else - DefaultType t' - | NamedType (loc, ta) -> - let ta' = this#type_alias loc ta in - if ta' == ta then - decl - else - NamedType (loc, ta') - | NamedOpaqueType (loc, ot) -> - let ot' = this#opaque_type loc ot in - if ot' == ot then - decl - else - NamedOpaqueType (loc, ot') - | Interface (loc, i) -> - let i' = this#interface loc i in - if i' == i then - decl - else - Interface (loc, i') - | Enum (loc, enum) -> - let enum' = this#enum_declaration loc enum in - if enum' == enum then - decl - else - Enum (loc, enum') - | Namespace (loc, ns) -> - let ns' = this#declare_namespace loc ns in - if ns' == ns then - decl - else - Namespace (loc, ns') - - method declare_function _loc (decl : ('loc, 'loc) Ast.Statement.DeclareFunction.t) = - let open Ast.Statement.DeclareFunction in - let { id = ident; annot; predicate; comments; implicit_declare } = decl in - let id' = map_opt this#function_identifier ident in - let annot' = this#type_annotation annot in - let predicate' = map_opt this#predicate predicate in - let comments' = this#syntax_opt comments in - if id' == ident && annot' == annot && predicate' == predicate && comments' == comments then - decl - else - { id = id'; annot = annot'; predicate = predicate'; comments = comments'; implicit_declare } - - method declare_interface loc (decl : ('loc, 'loc) Ast.Statement.Interface.t) = - this#interface loc decl - - method declare_module _loc (m : ('loc, 'loc) Ast.Statement.DeclareModule.t) = - let open Ast.Statement.DeclareModule in - let { id; body; comments } = m in - let body' = map_loc this#block body in - let comments' = this#syntax_opt comments in - if body' == body && comments == comments' then - m - else - { id; body = body'; comments = comments' } - - method declare_module_exports _loc (exports : ('loc, 'loc) Ast.Statement.DeclareModuleExports.t) - = - let open Ast.Statement.DeclareModuleExports in - let { annot; comments } = exports in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if annot == annot' && comments == comments' then - exports - else - { annot = annot'; comments = comments' } - - method declare_namespace _loc (m : ('loc, 'loc) Ast.Statement.DeclareNamespace.t) = - let open Ast.Statement.DeclareNamespace in - let { id; body; comments; implicit_declare; keyword } = m in - let id' = - match id with - | Global g_id -> - let g_id' = this#identifier g_id in - if g_id == g_id' then - id - else - Global g_id' - | Local p_id -> - let p_id' = this#pattern_identifier ~kind:Ast.Variable.Const p_id in - if p_id == p_id' then - id - else - Local p_id' - in - let body' = map_loc this#block body in - let comments' = this#syntax_opt comments in - if id' == id && body' == body && comments == comments' then - m - else - { id = id'; body = body'; comments = comments'; implicit_declare; keyword } - - method declare_type_alias loc (decl : ('loc, 'loc) Ast.Statement.TypeAlias.t) = - this#type_alias loc decl - - method declare_variable _loc (decl : ('loc, 'loc) Ast.Statement.DeclareVariable.t) = - let open Ast.Statement.DeclareVariable in - let { declarations; kind; comments } = decl in - let declarations' = map_list (this#declare_variable_declarator ~kind) declarations in - let comments' = this#syntax_opt comments in - if declarations == declarations' && comments == comments' then - decl - else - { declarations = declarations'; kind; comments = comments' } - - method declare_variable_declarator - ~kind (decl : ('loc, 'loc) Ast.Statement.VariableDeclaration.Declarator.t) = - let open Ast.Statement.VariableDeclaration.Declarator in - let (loc, { id; init }) = decl in - let id' = - match id with - | (ploc, Ast.Pattern.Identifier { Ast.Pattern.Identifier.name; annot; optional }) -> - let name' = this#pattern_identifier ~kind name in - let annot' = this#type_annotation_hint annot in - if name == name' && annot == annot' then - id - else - ( ploc, - Ast.Pattern.Identifier - { Ast.Pattern.Identifier.name = name'; annot = annot'; optional } - ) - | _ -> id - in - let init' = map_opt this#expression init in - if id == id' && init == init' then - decl - else - (loc, { id = id'; init = init' }) - - method do_while _loc (stuff : ('loc, 'loc) Ast.Statement.DoWhile.t) = - let open Ast.Statement.DoWhile in - let { body; test; comments } = stuff in - let body' = this#statement body in - let test' = this#predicate_expression test in - let comments' = this#syntax_opt comments in - if body == body' && test == test' && comments == comments' then - stuff - else - { body = body'; test = test'; comments = comments' } - - method empty _loc empty = - let open Ast.Statement.Empty in - let { comments } = empty in - let comments' = this#syntax_opt comments in - if comments == comments' then - empty - else - { comments = comments' } - - method enum_declaration _loc (enum : ('loc, 'loc) Ast.Statement.EnumDeclaration.t) = - let open Ast.Statement.EnumDeclaration in - let { id = ident; body; const_; comments } = enum in - let id' = this#pattern_identifier ~kind:Ast.Variable.Const ident in - let body' = this#enum_body body in - let comments' = this#syntax_opt comments in - if ident == id' && body == body' && comments == comments' then - enum - else - { id = id'; body = body'; const_; comments = comments' } - - method enum_body (body : 'loc Ast.Statement.EnumDeclaration.body) = - let open Ast.Statement.EnumDeclaration in - let (loc, body_t) = body in - let { Body.members; explicit_type = _; has_unknown_members = _; comments } = body_t in - let members' = map_list this#enum_member members in - let comments' = this#syntax_opt comments in - if members == members' && comments == comments' then - body - else - (loc, { body_t with Body.members = members'; comments = comments' }) - - method enum_member (member : 'loc Ast.Statement.EnumDeclaration.member) = - let open Ast.Statement.EnumDeclaration in - match member with - | BooleanMember m -> id this#enum_boolean_member m member (fun m -> BooleanMember m) - | NumberMember m -> id this#enum_number_member m member (fun m -> NumberMember m) - | StringMember m -> id this#enum_string_member m member (fun m -> StringMember m) - | BigIntMember m -> id this#enum_bigint_member m member (fun m -> BigIntMember m) - | DefaultedMember m -> id this#enum_defaulted_member m member (fun m -> DefaultedMember m) - - method enum_defaulted_member (member : 'loc Ast.Statement.EnumDeclaration.DefaultedMember.t) = - let open Ast.Statement.EnumDeclaration.DefaultedMember in - let (loc, { id = ident }) = member in - let id' = this#enum_member_name ident in - if ident == id' then - member - else - (loc, { id = id' }) - - method enum_boolean_member - (member : - ('loc Ast.BooleanLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t - ) = - let open Ast.Statement.EnumDeclaration.InitializedMember in - let (loc, { id = ident; init }) = member in - let id' = this#enum_member_name ident in - let (init_loc, init_val) = init in - let init_val' = this#boolean_literal init_val in - if ident == id' && init_val == init_val' then - member - else - (loc, { id = id'; init = (init_loc, init_val') }) - - method enum_number_member - (member : ('loc Ast.NumberLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t) - = - let open Ast.Statement.EnumDeclaration.InitializedMember in - let (loc, { id = ident; init }) = member in - let id' = this#enum_member_name ident in - let (init_loc, init_val) = init in - let init_val' = this#number_literal init_val in - if ident == id' && init_val == init_val' then - member - else - (loc, { id = id'; init = (init_loc, init_val') }) - - method enum_string_member - (member : ('loc Ast.StringLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t) - = - let open Ast.Statement.EnumDeclaration.InitializedMember in - let (loc, { id = ident; init }) = member in - let id' = this#enum_member_name ident in - let (init_loc, init_val) = init in - let init_val' = this#string_literal init_val in - if ident == id' && init_val == init_val' then - member - else - (loc, { id = id'; init = (init_loc, init_val') }) - - method enum_bigint_member - (member : ('loc Ast.BigIntLiteral.t, 'loc) Ast.Statement.EnumDeclaration.InitializedMember.t) - = - let open Ast.Statement.EnumDeclaration.InitializedMember in - let (loc, { id = ident; init }) = member in - let id' = this#enum_member_name ident in - let (init_loc, init_val) = init in - let init_val' = this#bigint_literal init_val in - if ident == id' && init_val == init_val' then - member - else - (loc, { id = id'; init = (init_loc, init_val') }) - - method enum_member_name (id : 'loc Ast.Statement.EnumDeclaration.member_name) = - let open Ast.Statement.EnumDeclaration in - match id with - | Identifier ident -> - let ident' = this#enum_member_identifier ident in - if ident == ident' then - id - else - Identifier ident' - | StringLiteral (loc, lit) -> - let lit' = this#string_literal lit in - if lit == lit' then - id - else - StringLiteral (loc, lit') - - method enum_member_identifier (id : ('loc, 'loc) Ast.Identifier.t) = this#identifier id - - method export_default_declaration - _loc (decl : ('loc, 'loc) Ast.Statement.ExportDefaultDeclaration.t) = - let open Ast.Statement.ExportDefaultDeclaration in - let { default; declaration; comments } = decl in - let declaration' = this#export_default_declaration_decl declaration in - let comments' = this#syntax_opt comments in - if declaration' == declaration && comments' == comments then - decl - else - { default; declaration = declaration'; comments = comments' } - - method export_default_declaration_decl - (decl : ('loc, 'loc) Ast.Statement.ExportDefaultDeclaration.declaration) = - let open Ast.Statement.ExportDefaultDeclaration in - match decl with - | Declaration stmt -> id this#statement stmt decl (fun stmt -> Declaration stmt) - | Expression expr -> id this#expression expr decl (fun expr -> Expression expr) - - method export_named_declaration _loc (decl : ('loc, 'loc) Ast.Statement.ExportNamedDeclaration.t) - = - let open Ast.Statement.ExportNamedDeclaration in - let { export_kind; source; specifiers; declaration; comments } = decl in - let source' = map_loc_opt this#export_source source in - let specifiers' = map_opt this#export_named_specifier specifiers in - let declaration' = map_opt this#statement declaration in - let comments' = this#syntax_opt comments in - if - source == source' - && specifiers == specifiers' - && declaration == declaration' - && comments == comments' - then - decl - else - { - export_kind; - source = source'; - specifiers = specifiers'; - declaration = declaration'; - comments = comments'; - } - - method export_named_declaration_specifier - (spec : ('loc, 'loc) Ast.Statement.ExportNamedDeclaration.ExportSpecifier.t) = - let open Ast.Statement.ExportNamedDeclaration.ExportSpecifier in - let (loc, { local; exported; export_kind; from_remote; imported_name_def_loc }) = spec in - let local' = this#identifier local in - let exported' = map_opt this#identifier exported in - if local == local' && exported == exported' then - spec - else - ( loc, - { local = local'; exported = exported'; export_kind; from_remote; imported_name_def_loc } - ) - - method export_batch_specifier - (spec : ('loc, 'loc) Ast.Statement.ExportNamedDeclaration.ExportBatchSpecifier.t) = - let (loc, id_opt) = spec in - let id_opt' = map_opt this#identifier id_opt in - if id_opt == id_opt' then - spec - else - (loc, id_opt') - - method export_named_specifier - (spec : ('loc, 'loc) Ast.Statement.ExportNamedDeclaration.specifier) = - let open Ast.Statement.ExportNamedDeclaration in - match spec with - | ExportSpecifiers spec_list -> - let spec_list' = map_list this#export_named_declaration_specifier spec_list in - if spec_list == spec_list' then - spec - else - ExportSpecifiers spec_list' - | ExportBatchSpecifier batch -> - let batch' = this#export_batch_specifier batch in - if batch == batch' then - spec - else - ExportBatchSpecifier batch' - - method export_source _loc (source : 'loc Ast.StringLiteral.t) = - let open Ast.StringLiteral in - let { value; raw; comments } = source in - let comments' = this#syntax_opt comments in - if comments == comments' then - source - else - { value; raw; comments = comments' } - - method export_assignment _loc (assign : ('loc, 'loc) Ast.Statement.ExportAssignment.t) = - let open Ast.Statement.ExportAssignment in - let { rhs; comments } = assign in - let rhs' = - match rhs with - | Expression expr -> - let expr' = this#expression expr in - if expr == expr' then - rhs - else - Expression expr' - | DeclareFunction (loc, decl) -> - let decl' = this#declare_function loc decl in - if decl == decl' then - rhs - else - DeclareFunction (loc, decl') - in - let comments' = this#syntax_opt comments in - if rhs == rhs' && comments == comments' then - assign - else - { rhs = rhs'; comments = comments' } - - method namespace_export_declaration - _loc (decl : ('loc, 'loc) Ast.Statement.NamespaceExportDeclaration.t) = - let open Ast.Statement.NamespaceExportDeclaration in - let { id; comments } = decl in - let id' = this#identifier id in - let comments' = this#syntax_opt comments in - if id == id' && comments == comments' then - decl - else - { id = id'; comments = comments' } - - method expression_statement _loc (stmt : ('loc, 'loc) Ast.Statement.Expression.t) = - let open Ast.Statement.Expression in - let { expression = expr; directive; comments } = stmt in - let expr' = this#expression expr in - let comments' = this#syntax_opt comments in - if expr == expr' && comments == comments' then - stmt - else - { expression = expr'; directive; comments = comments' } - - method expression_or_spread expr_or_spread = - let open Ast.Expression in - match expr_or_spread with - | Expression expr -> id this#expression expr expr_or_spread (fun expr -> Expression expr) - | Spread spread -> id this#spread_element spread expr_or_spread (fun spread -> Spread spread) - - method for_in_statement _loc (stmt : ('loc, 'loc) Ast.Statement.ForIn.t) = - let open Ast.Statement.ForIn in - let { left; right; body; each; comments } = stmt in - let left' = this#for_in_statement_lhs left in - let right' = this#expression right in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if left == left' && right == right' && body == body' && comments == comments' then - stmt - else - { left = left'; right = right'; body = body'; each; comments = comments' } - - method for_in_statement_lhs (left : ('loc, 'loc) Ast.Statement.ForIn.left) = - let open Ast.Statement.ForIn in - match left with - | LeftDeclaration decl -> - id this#for_in_left_declaration decl left (fun decl -> LeftDeclaration decl) - | LeftPattern patt -> - id this#for_in_assignment_pattern patt left (fun patt -> LeftPattern patt) - - method for_in_left_declaration left = - let (loc, decl) = left in - id_loc this#variable_declaration loc decl left (fun decl -> (loc, decl)) - - method for_of_statement _loc (stuff : ('loc, 'loc) Ast.Statement.ForOf.t) = - let open Ast.Statement.ForOf in - let { left; right; body; await; comments } = stuff in - let left' = this#for_of_statement_lhs left in - let right' = this#expression right in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if left == left' && right == right' && body == body' && comments == comments' then - stuff - else - { left = left'; right = right'; body = body'; await; comments = comments' } - - method for_of_statement_lhs (left : ('loc, 'loc) Ast.Statement.ForOf.left) = - let open Ast.Statement.ForOf in - match left with - | LeftDeclaration decl -> - id this#for_of_left_declaration decl left (fun decl -> LeftDeclaration decl) - | LeftPattern patt -> - id this#for_of_assignment_pattern patt left (fun patt -> LeftPattern patt) - - method for_of_left_declaration left = - let (loc, decl) = left in - id_loc this#variable_declaration loc decl left (fun decl -> (loc, decl)) - - method for_statement _loc (stmt : ('loc, 'loc) Ast.Statement.For.t) = - let open Ast.Statement.For in - let { init; test; update; body; comments } = stmt in - let init' = map_opt this#for_statement_init init in - let test' = map_opt this#predicate_expression test in - let update' = map_opt this#expression update in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if - init == init' - && test == test' - && update == update' - && body == body' - && comments == comments' - then - stmt - else - { init = init'; test = test'; update = update'; body = body'; comments = comments' } - - method for_statement_init (init : ('loc, 'loc) Ast.Statement.For.init) = - let open Ast.Statement.For in - match init with - | InitDeclaration decl -> - id this#for_init_declaration decl init (fun decl -> InitDeclaration decl) - | InitExpression expr -> id this#expression expr init (fun expr -> InitExpression expr) - - method for_init_declaration init = - let (loc, decl) = init in - id_loc this#variable_declaration loc decl init (fun decl -> (loc, decl)) - - method function_param_type (fpt : ('loc, 'loc) Ast.Type.Function.Param.t) = - let open Ast.Type.Function.Param in - let (loc, param) = fpt in - match param with - | Anonymous annot -> - let annot' = this#type_ annot in - if annot' == annot then - fpt - else - (loc, Anonymous annot') - | Labeled { name; annot; optional } -> - let name' = this#function_param_type_identifier name in - let annot' = this#type_ annot in - if name' == name && annot' == annot then - fpt - else - (loc, Labeled { name = name'; annot = annot'; optional }) - | Destructuring pattern -> - let pattern' = this#function_param_type_pattern pattern in - if pattern' == pattern then - fpt - else - (loc, Destructuring pattern') - - method function_param_type_identifier (id : ('loc, 'loc) Ast.Identifier.t) = this#identifier id - - method function_param_type_pattern (patt : ('loc, 'loc) Ast.Pattern.t) = this#pattern patt - - method function_rest_param_type (frpt : ('loc, 'loc) Ast.Type.Function.RestParam.t) = - let open Ast.Type.Function.RestParam in - let (loc, { argument; comments }) = frpt in - let argument' = this#function_param_type argument in - let comments' = this#syntax_opt comments in - if argument' == argument && comments' == comments then - frpt - else - (loc, { argument = argument'; comments = comments' }) - - method function_this_param_type (this_param : ('loc, 'loc) Ast.Type.Function.ThisParam.t) = - let open Ast.Type.Function.ThisParam in - let (loc, { annot; comments }) = this_param in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if annot' == annot && comments' == comments then - this_param - else - (loc, { annot = annot'; comments = comments' }) - - method function_type_return_annotation - (return : ('loc, 'loc) Ast.Type.Function.return_annotation) = - let open Ast.Type.Function in - match return with - | Available t -> id this#type_ t return (fun rt -> Available rt) - | TypeGuard g -> id this#type_guard g return (fun tg -> TypeGuard tg) - | Missing _ -> return - - method function_type (ft : ('loc, 'loc) Ast.Type.Function.t) = - let open Ast.Type.Function in - let { - params = (params_loc, { Params.this_; params = ps; rest = rpo; comments = params_comments }); - return; - tparams; - comments = func_comments; - effect_; - } = - ft - in - let tparams' = map_opt (this#type_params ~kind:FunctionTypeTP) tparams in - let this_' = map_opt this#function_this_param_type this_ in - let ps' = map_list this#function_param_type ps in - let rpo' = map_opt this#function_rest_param_type rpo in - let return' = this#function_type_return_annotation return in - let func_comments' = this#syntax_opt func_comments in - let params_comments' = this#syntax_opt params_comments in - if - ps' == ps - && rpo' == rpo - && return' == return - && tparams' == tparams - && func_comments' == func_comments - && params_comments' == params_comments - && this_' == this_ - then - ft - else - { - params = - ( params_loc, - { Params.this_ = this_'; params = ps'; rest = rpo'; comments = params_comments' } - ); - return = return'; - tparams = tparams'; - comments = func_comments'; - effect_; - } - - method label_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = this#identifier ident - - method object_property_value_type (opvt : ('loc, 'loc) Ast.Type.Object.Property.value) = - let open Ast.Type.Object.Property in - match opvt with - | Init (Some t) -> id this#type_ t opvt (fun t -> Init (Some t)) - | Init None -> opvt - | Get t -> id this#object_type_property_getter t opvt (fun t -> Get t) - | Set t -> id this#object_type_property_setter t opvt (fun t -> Set t) - - method object_type_property_getter getter = - let (loc, ft) = getter in - id this#function_type ft getter (fun ft -> (loc, ft)) - - method object_type_property_setter setter = - let (loc, ft) = setter in - id this#function_type ft setter (fun ft -> (loc, ft)) - - method object_property_type (opt : ('loc, 'loc) Ast.Type.Object.Property.t) = - let open Ast.Type.Object.Property in - let ( loc, - { - key; - value; - optional; - static; - proto; - _method; - abstract; - override; - variance; - ts_accessibility; - init; - comments; - } - ) = - opt - in - let key' = this#object_key key in - let value' = this#object_property_value_type value in - let variance' = this#variance_opt variance in - let init' = map_opt this#expression init in - let comments' = this#syntax_opt comments in - if - key' == key - && value' == value - && variance' == variance - && init' == init - && comments' == comments - then - opt - else - ( loc, - { - key = key'; - value = value'; - optional; - static; - proto; - _method; - abstract; - override; - variance = variance'; - ts_accessibility; - init = init'; - comments = comments'; - } - ) - - method object_spread_property_type (opt : ('loc, 'loc) Ast.Type.Object.SpreadProperty.t) = - let open Ast.Type.Object.SpreadProperty in - let (loc, { argument; comments }) = opt in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument' == argument && comments == comments' then - opt - else - (loc, { argument = argument'; comments = comments' }) - - method object_indexer_property_type (opt : ('loc, 'loc) Ast.Type.Object.Indexer.t) = - let open Ast.Type.Object.Indexer in - let (loc, { id; key; value; static; variance; optional; comments }) = opt in - let key' = this#type_ key in - let value' = this#type_ value in - let variance' = this#variance_opt variance in - let comments' = this#syntax_opt comments in - if key' == key && value' == value && variance' == variance && comments' == comments then - opt - else - ( loc, - { - id; - key = key'; - value = value'; - static; - variance = variance'; - optional; - comments = comments'; - } - ) - - method object_internal_slot_property_type (slot : ('loc, 'loc) Ast.Type.Object.InternalSlot.t) = - let open Ast.Type.Object.InternalSlot in - let (loc, { id; value; optional; static; _method; comments }) = slot in - let id' = this#identifier id in - let value' = this#type_ value in - let comments' = this#syntax_opt comments in - if id == id' && value == value' && comments == comments' then - slot - else - (loc, { id = id'; value = value'; optional; static; _method; comments = comments' }) - - method object_call_property_type (call : ('loc, 'loc) Ast.Type.Object.CallProperty.t) = - let open Ast.Type.Object.CallProperty in - let (loc, { value = (value_loc, value); static; comments }) = call in - let value' = this#function_type value in - let comments' = this#syntax_opt comments in - if value == value' && comments == comments' then - call - else - (loc, { value = (value_loc, value'); static; comments = comments' }) - - method object_mapped_type_property (mt : ('loc, 'loc) Ast.Type.Object.MappedType.t) = - let open Ast.Type.Object.MappedType in - let ( loc, - { - key_tparam; - prop_type; - source_type; - name_type; - variance; - variance_op; - comments; - optional; - } - ) = - mt - in - let key_tparam' = this#type_param ~kind:ObjectMappedTypeTP key_tparam in - let prop_type' = this#type_ prop_type in - let source_type' = this#type_ source_type in - let name_type' = map_opt this#type_ name_type in - let variance' = this#variance_opt variance in - let comments' = this#syntax_opt comments in - if - key_tparam' == key_tparam - && prop_type' == prop_type - && source_type' == source_type - && name_type' == name_type - && variance' == variance - && comments' == comments - then - mt - else - ( loc, - { - key_tparam = key_tparam'; - prop_type = prop_type'; - source_type = source_type'; - name_type = name_type'; - variance = variance'; - variance_op; - comments = comments'; - optional; - } - ) - - method object_type (ot : ('loc, 'loc) Ast.Type.Object.t) = - let open Ast.Type.Object in - let { properties; exact; inexact; comments } = ot in - let properties' = map_list this#object_type_property properties in - let comments' = this#syntax_opt comments in - if properties' == properties && comments == comments' then - ot - else - { properties = properties'; exact; inexact; comments = comments' } - - method object_type_property (p : ('loc, 'loc) Ast.Type.Object.property) = - let open Ast.Type.Object in - match p with - | Property p' -> id this#object_property_type p' p (fun p' -> Property p') - | SpreadProperty p' -> id this#object_spread_property_type p' p (fun p' -> SpreadProperty p') - | Indexer p' -> id this#object_indexer_property_type p' p (fun p' -> Indexer p') - | InternalSlot p' -> - id this#object_internal_slot_property_type p' p (fun p' -> InternalSlot p') - | CallProperty p' -> id this#object_call_property_type p' p (fun p' -> CallProperty p') - | MappedType p' -> id this#object_mapped_type_property p' p (fun p' -> MappedType p') - | PrivateField p' -> id this#object_private_field_type p' p (fun p' -> PrivateField p') - - method object_private_field_type (pf : ('loc, 'loc) Ast.Type.Object.PrivateField.t) = - let open Ast.Type.Object.PrivateField in - let (loc, { key; comments }) = pf in - let key' = this#private_name key in - let comments' = this#syntax_opt comments in - if key' == key && comments' == comments then - pf - else - (loc, { key = key'; comments = comments' }) - - method interface_type _loc (i : ('loc, 'loc) Ast.Type.Interface.t) = - let open Ast.Type.Interface in - let { extends; body; comments } = i in - let extends' = - map_list - (fun ext -> - let (eloc, e) = ext in - id this#generic_type e ext (fun e' -> (eloc, e'))) - extends - in - let body' = - let (bloc, b) = body in - id this#object_type b body (fun b' -> (bloc, b')) - in - let comments' = this#syntax_opt comments in - if extends' == extends && body' == body && comments == comments' then - i - else - { extends = extends'; body = body'; comments = comments' } - - method generic_identifier_type (git : ('loc, 'loc) Ast.Type.Generic.Identifier.t) = - let open Ast.Type.Generic.Identifier in - match git with - | Unqualified i -> id this#type_identifier_reference i git (fun i -> Unqualified i) - | Qualified i -> id this#generic_qualified_identifier_type i git (fun i -> Qualified i) - | ImportTypeAnnot (annot, import') -> - let import'' = this#generic_identifier_import_type import' in - if import' == import'' then - git - else - ImportTypeAnnot (annot, import'') - - method generic_identifier_import_type (import' : 'loc Ast.Type.Generic.Identifier.import_type') - = - let open Ast.Type.Generic.Identifier in - let { argument; comments } = import' in - let argument' = - let (loc, lit) = argument in - id this#string_literal lit argument (fun lit -> (loc, lit)) - in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - import' - else - { argument = argument'; comments = comments' } - - method generic_qualified_identifier_type qual = - let open Ast.Type.Generic.Identifier in - let (loc, { qualification; id }) = qual in - let qualification' = this#generic_identifier_type qualification in - let id' = this#member_type_identifier id in - if qualification' == qualification && id' == id then - qual - else - (loc, { qualification = qualification'; id = id' }) - - method member_type_identifier id = this#identifier id - - method variance (variance : 'loc Ast.Variance.t) = - let (loc, { Ast.Variance.kind; comments }) = variance in - let comments' = this#syntax_opt comments in - if comments == comments' then - variance - else - (loc, { Ast.Variance.kind; comments = comments' }) - - method variance_opt (opt : 'loc Ast.Variance.t option) = map_opt this#variance opt - - method tparam_const_modifier (c : 'loc Ast.Type.TypeParam.ConstModifier.t) = - let (loc, comments) = c in - let comments' = this#syntax_opt comments in - if comments == comments' then - c - else - (loc, comments') - - method type_args (targs : ('loc, 'loc) Ast.Type.TypeArgs.t) = - let open Ast.Type.TypeArgs in - let (loc, { arguments; comments }) = targs in - let arguments' = map_list this#type_ arguments in - let comments' = this#syntax_opt comments in - if arguments == arguments' && comments == comments' then - targs - else - (loc, { arguments = arguments'; comments = comments' }) - - method type_params ~kind (tparams : ('loc, 'loc) Ast.Type.TypeParams.t) = - let open Ast.Type.TypeParams in - let (loc, { params = tps; comments }) = tparams in - let tps' = map_list (this#type_param ~kind) tps in - let comments' = this#syntax_opt comments in - if tps' == tps && comments' == comments then - tparams - else - (loc, { params = tps'; comments = comments' }) - - method type_param ~kind:_ (tparam : ('loc, 'loc) Ast.Type.TypeParam.t) = - let open Ast.Type.TypeParam in - let (loc, { name; bound; bound_kind; variance; default; const }) = tparam in - let bound' = this#type_annotation_hint bound in - let variance' = this#variance_opt variance in - let default' = map_opt this#type_ default in - let const' = map_opt this#tparam_const_modifier const in - let name' = this#binding_type_identifier name in - if - name' == name - && bound' == bound - && variance' == variance - && default' == default - && const' == const - then - tparam - else - ( loc, - { - name = name'; - bound = bound'; - bound_kind; - variance = variance'; - default = default'; - const = const'; - } - ) - - method generic_type (gt : ('loc, 'loc) Ast.Type.Generic.t) = - let open Ast.Type.Generic in - let { id; targs; comments } = gt in - let id' = this#generic_identifier_type id in - let targs' = map_opt this#type_args targs in - let comments' = this#syntax_opt comments in - if id' == id && targs' == targs && comments' == comments then - gt - else - { id = id'; targs = targs'; comments = comments' } - - method indexed_access_type _loc (ia : ('loc, 'loc) Ast.Type.IndexedAccess.t) = - let open Ast.Type.IndexedAccess in - let { _object; index; comments } = ia in - let _object' = this#type_ _object in - let index' = this#type_ index in - let comments' = this#syntax_opt comments in - if _object' == _object && index' == index && comments' == comments then - ia - else - { _object = _object'; index = index'; comments = comments' } - - method optional_indexed_access_type loc (ia : ('loc, 'loc) Ast.Type.OptionalIndexedAccess.t) = - let open Ast.Type.OptionalIndexedAccess in - let { indexed_access; optional } = ia in - let indexed_access' = this#indexed_access_type loc indexed_access in - if indexed_access' == indexed_access then - ia - else - { indexed_access = indexed_access'; optional } - - method string_literal (lit : 'loc Ast.StringLiteral.t) = - let open Ast.StringLiteral in - let { value; raw; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { value; raw; comments = comments' } - - method number_literal (lit : 'loc Ast.NumberLiteral.t) = - let open Ast.NumberLiteral in - let { value; raw; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { value; raw; comments = comments' } - - method bigint_literal (lit : 'loc Ast.BigIntLiteral.t) = - let open Ast.BigIntLiteral in - let { value; raw; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { value; raw; comments = comments' } - - method boolean_literal (lit : 'loc Ast.BooleanLiteral.t) = - let open Ast.BooleanLiteral in - let { value; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { value; comments = comments' } - - method null_literal comments = this#syntax_opt comments - - method regexp_literal (lit : 'loc Ast.RegExpLiteral.t) = - let open Ast.RegExpLiteral in - let { pattern; flags; raw; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { pattern; flags; raw; comments = comments' } - - method module_ref_literal (lit : ('loc, 'loc) Ast.ModuleRefLiteral.t) = - let open Ast.ModuleRefLiteral in - let { value; require_loc; def_loc_opt; prefix_len; raw; comments } = lit in - let comments' = this#syntax_opt comments in - if comments == comments' then - lit - else - { value; require_loc; def_loc_opt; prefix_len; raw; comments = comments' } - - method nullable_type (t : ('loc, 'loc) Ast.Type.Nullable.t) = - let open Ast.Type.Nullable in - let { argument; comments } = t in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - t - else - { argument = argument'; comments = comments' } - - method conditional_type (t : ('loc, 'loc) Ast.Type.Conditional.t) = - let open Ast.Type.Conditional in - let { check_type; extends_type; true_type; false_type; comments } = t in - let check_type' = this#type_ check_type in - let extends_type' = this#type_ extends_type in - let true_type' = this#type_ true_type in - let false_type' = this#type_ false_type in - let comments' = this#syntax_opt comments in - if - check_type == check_type' - && extends_type == extends_type' - && true_type == true_type' - && false_type == false_type' - && comments == comments' - then - t - else - { - check_type = check_type'; - extends_type = extends_type'; - true_type = true_type'; - false_type = false_type'; - comments = comments'; - } - - method infer_type (t : ('loc, 'loc) Ast.Type.Infer.t) = - let open Ast.Type.Infer in - let { tparam; comments } = t in - let tparam' = this#type_param ~kind:InferTP tparam in - let comments' = this#syntax_opt comments in - if tparam == tparam' && comments == comments' then - t - else - { tparam = tparam'; comments = comments' } - - method typeof_type (t : ('loc, 'loc) Ast.Type.Typeof.t) = - let open Ast.Type.Typeof in - let { argument; targs; comments } = t in - let argument' = this#typeof_expression argument in - let targs' = map_opt this#type_args targs in - let comments' = this#syntax_opt comments in - if argument == argument' && targs == targs' && comments == comments' then - t - else - { argument = argument'; targs = targs'; comments = comments' } - - method typeof_expression (git : ('loc, 'loc) Ast.Type.Typeof.Target.t) = - let open Ast.Type.Typeof.Target in - match git with - | Unqualified i -> id this#typeof_identifier i git (fun i -> Unqualified i) - | Qualified i -> id this#typeof_qualified_identifier i git (fun i -> Qualified i) - | Import (annot, import') -> - let import'' = this#generic_identifier_import_type import' in - if import' == import'' then - git - else - Import (annot, import'') - - method typeof_identifier id = this#identifier id - - method typeof_member_identifier id = this#identifier id - - method typeof_qualified_identifier qual = - let open Ast.Type.Typeof.Target in - let (loc, { qualification; id }) = qual in - let qualification' = this#typeof_expression qualification in - let id' = this#typeof_member_identifier id in - if qualification' == qualification && id' == id then - qual - else - (loc, { qualification = qualification'; id = id' }) - - method keyof_type (t : ('loc, 'loc) Ast.Type.Keyof.t) = - let open Ast.Type.Keyof in - let { argument; comments } = t in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - t - else - { argument = argument'; comments = comments' } - - method render_type (t : ('loc, 'loc) Ast.Type.Renders.t) = - let open Ast.Type.Renders in - let { operator_loc; argument; variant; comments } = t in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - t - else - { operator_loc; argument = argument'; comments = comments'; variant } - - method readonly_type (t : ('loc, 'loc) Ast.Type.ReadOnly.t) = - let open Ast.Type.ReadOnly in - let { argument; comments } = t in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - t - else - { argument = argument'; comments = comments' } - - method tuple_type (t : ('loc, 'loc) Ast.Type.Tuple.t) = - let open Ast.Type.Tuple in - let { elements; inexact; comments } = t in - let elements' = map_list this#tuple_element elements in - let comments' = this#syntax_opt comments in - if elements == elements' && comments == comments' then - t - else - { elements = elements'; inexact; comments = comments' } - - method tuple_element (el : ('loc, 'loc) Ast.Type.Tuple.element) = - let open Ast.Type.Tuple in - match el with - | (loc, UnlabeledElement { UnlabeledElement.annot; optional }) -> - id this#type_ annot el (fun annot -> - (loc, UnlabeledElement { UnlabeledElement.annot; optional }) - ) - | (loc, LabeledElement e) -> - id this#tuple_labeled_element e el (fun e -> (loc, LabeledElement e)) - | (loc, SpreadElement e) -> id this#tuple_spread_element e el (fun e -> (loc, SpreadElement e)) - - method tuple_labeled_element (t : ('loc, 'loc) Ast.Type.Tuple.LabeledElement.t) = - let open Ast.Type.Tuple.LabeledElement in - (* Tuple element labels are not bindings so don't map over `name`. *) - let { annot; name; variance; optional } = t in - let annot' = this#type_ annot in - let variance' = this#variance_opt variance in - if annot' == annot && variance' == variance then - t - else - { annot = annot'; name; variance = variance'; optional } - - method tuple_spread_element (t : ('loc, 'loc) Ast.Type.Tuple.SpreadElement.t) = - let open Ast.Type.Tuple.SpreadElement in - let { annot; name } = t in - let annot' = this#type_ annot in - if annot' == annot then - t - else - { annot = annot'; name } - - method array_type (t : ('loc, 'loc) Ast.Type.Array.t) = - let open Ast.Type.Array in - let { argument; comments } = t in - let argument' = this#type_ argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - t - else - { argument = argument'; comments = comments' } - - method template_literal_type (t : ('loc, 'loc) Ast.Type.TemplateLiteral.t) = - let open Ast.Type.TemplateLiteral in - let { quasis; types; comments } = t in - let types' = map_list this#type_ types in - let comments' = this#syntax_opt comments in - if types' == types && comments' == comments then - t - else - { quasis; types = types'; comments = comments' } - - method union_type _loc (t : ('loc, 'loc) Ast.Type.Union.t) = - let open Ast.Type.Union in - let { types = (t0, t1, ts); comments } = t in - let t0' = this#type_ t0 in - let t1' = this#type_ t1 in - let ts' = map_list this#type_ ts in - let comments' = this#syntax_opt comments in - if t0' == t0 && t1' == t1 && ts' == ts && comments' == comments then - t - else - { types = (t0', t1', ts'); comments = comments' } - - method intersection_type _loc (t : ('loc, 'loc) Ast.Type.Intersection.t) = - let open Ast.Type.Intersection in - let { types = (t0, t1, ts); comments } = t in - let t0' = this#type_ t0 in - let t1' = this#type_ t1 in - let ts' = map_list this#type_ ts in - let comments' = this#syntax_opt comments in - if t0' == t0 && t1' == t1 && ts' == ts && comments' == comments then - t - else - { types = (t0', t1', ts'); comments = comments' } - - method type_ (t : ('loc, 'loc) Ast.Type.t) = - let open Ast.Type in - match t with - | (loc, Any comments) -> id this#syntax_opt comments t (fun comments -> (loc, Any comments)) - | (loc, Mixed comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Mixed comments)) - | (loc, Empty comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Empty comments)) - | (loc, Void comments) -> id this#syntax_opt comments t (fun comments -> (loc, Void comments)) - | (loc, Null comments) -> id this#syntax_opt comments t (fun comments -> (loc, Null comments)) - | (loc, Symbol comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Symbol comments)) - | (loc, Number comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Number comments)) - | (loc, BigInt comments) -> - id this#syntax_opt comments t (fun comments -> (loc, BigInt comments)) - | (loc, String comments) -> - id this#syntax_opt comments t (fun comments -> (loc, String comments)) - | (loc, Boolean { raw; comments }) -> - id this#syntax_opt comments t (fun comments -> (loc, Boolean { raw; comments })) - | (loc, Exists comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Exists comments)) - | (loc, Unknown comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Unknown comments)) - | (loc, Never comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Never comments)) - | (loc, Undefined comments) -> - id this#syntax_opt comments t (fun comments -> (loc, Undefined comments)) - | (loc, UniqueSymbol comments) -> - id this#syntax_opt comments t (fun comments -> (loc, UniqueSymbol comments)) - | (loc, Nullable t') -> id this#nullable_type t' t (fun t' -> (loc, Nullable t')) - | (loc, Array t') -> id this#array_type t' t (fun t' -> (loc, Array t')) - | (loc, Conditional t') -> id this#conditional_type t' t (fun t' -> (loc, Conditional t')) - | (loc, Infer t') -> id this#infer_type t' t (fun t' -> (loc, Infer t')) - | (loc, Typeof t') -> id this#typeof_type t' t (fun t' -> (loc, Typeof t')) - | (loc, Keyof t') -> id this#keyof_type t' t (fun t' -> (loc, Keyof t')) - | (loc, Renders t') -> id this#render_type t' t (fun t' -> (loc, Renders t')) - | (loc, ReadOnly t') -> id this#readonly_type t' t (fun t' -> (loc, ReadOnly t')) - | (loc, Function ft) -> id this#function_type ft t (fun ft -> (loc, Function ft)) - | (loc, ConstructorType { ConstructorType.abstract_; func }) -> - id this#function_type func t (fun func -> - (loc, ConstructorType { ConstructorType.abstract_; func }) - ) - | (loc, Component ct) -> id_loc this#component_type loc ct t (fun ct -> (loc, Component ct)) - | (loc, Object ot) -> id this#object_type ot t (fun ot -> (loc, Object ot)) - | (loc, Interface i) -> id_loc this#interface_type loc i t (fun i -> (loc, Interface i)) - | (loc, Generic gt) -> id this#generic_type gt t (fun gt -> (loc, Generic gt)) - | (loc, IndexedAccess ia) -> - id_loc this#indexed_access_type loc ia t (fun ia -> (loc, IndexedAccess ia)) - | (loc, OptionalIndexedAccess ia) -> - id_loc this#optional_indexed_access_type loc ia t (fun ia -> (loc, OptionalIndexedAccess ia)) - | (loc, StringLiteral lit) -> - id this#string_literal lit t (fun lit -> (loc, StringLiteral lit)) - | (loc, NumberLiteral lit) -> - id this#number_literal lit t (fun lit -> (loc, NumberLiteral lit)) - | (loc, BigIntLiteral lit) -> - id this#bigint_literal lit t (fun lit -> (loc, BigIntLiteral lit)) - | (loc, BooleanLiteral lit) -> - id this#boolean_literal lit t (fun lit -> (loc, BooleanLiteral lit)) - | (loc, TemplateLiteral t') -> - id this#template_literal_type t' t (fun t' -> (loc, TemplateLiteral t')) - | (loc, Union t') -> id_loc this#union_type loc t' t (fun t' -> (loc, Union t')) - | (loc, Intersection t') -> - id_loc this#intersection_type loc t' t (fun t' -> (loc, Intersection t')) - | (loc, Tuple t') -> id this#tuple_type t' t (fun t' -> (loc, Tuple t')) - - method type_annotation (annot : ('loc, 'loc) Ast.Type.annotation) = - let (loc, a) = annot in - id this#type_ a annot (fun a -> (loc, a)) - - method type_annotation_hint (return : ('M, 'T) Ast.Type.annotation_or_hint) = - let open Ast.Type in - match return with - | Available annot -> id this#type_annotation annot return (fun a -> Available a) - | Missing _loc -> return - - method component_renders_annotation (renders : ('M, 'T) Ast.Type.component_renders_annotation) = - let open Ast.Type in - match renders with - | AvailableRenders (loc, render_type) -> - let render_type' = this#render_type render_type in - if render_type' == render_type then - renders - else - AvailableRenders (loc, render_type') - | MissingRenders _loc -> renders - - method function_declaration loc (stmt : ('loc, 'loc) Ast.Function.t) = this#function_ loc stmt - - method function_expression loc (stmt : ('loc, 'loc) Ast.Function.t) = - this#function_expression_or_method loc stmt - - (** previously, we conflated [function_expression] and [class_method]. callers should be - updated to override those individually. - - DEPRECATED: use either function_expression or class_method *) - method function_expression_or_method loc (stmt : ('loc, 'loc) Ast.Function.t) = - this#function_ loc stmt - - (* Internal helper for function declarations, function expressions and arrow functions *) - method function_ _loc (expr : ('loc, 'loc) Ast.Function.t) = - let open Ast.Function in - let { - id = ident; - params; - body; - async; - generator; - effect_; - predicate; - return; - tparams; - sig_loc; - comments; - } = - expr - in - let ident' = map_opt this#function_identifier ident in - let tparams' = map_opt (this#type_params ~kind:FunctionTP) tparams in - let params' = this#function_params params in - let return' = this#function_return_annotation return in - let body' = this#function_body_any body in - let predicate' = map_opt this#predicate predicate in - let comments' = this#syntax_opt comments in - if - ident == ident' - && params == params' - && body == body' - && predicate == predicate' - && return == return' - && tparams == tparams' - && comments == comments' - then - expr - else - { - id = ident'; - params = params'; - return = return'; - body = body'; - async; - generator; - effect_; - predicate = predicate'; - tparams = tparams'; - sig_loc; - comments = comments'; - } - - method function_params (params : ('loc, 'loc) Ast.Function.Params.t) = - let open Ast.Function in - let (loc, { Params.params = params_list; rest; comments; this_ }) = params in - let params_list' = map_list this#function_param params_list in - let rest' = map_opt this#function_rest_param rest in - let this_' = map_opt this#function_this_param this_ in - let comments' = this#syntax_opt comments in - if params_list == params_list' && rest == rest' && comments == comments' && this_ == this_' - then - params - else - (loc, { Params.params = params_list'; rest = rest'; comments = comments'; this_ = this_' }) - - method function_this_param (this_param : ('loc, 'loc) Ast.Function.ThisParam.t) = - let open Ast.Function.ThisParam in - let (loc, { annot; comments }) = this_param in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if annot' == annot && comments' == comments then - this_param - else - (loc, { annot = annot'; comments = comments' }) - - method function_param (param : ('loc, 'loc) Ast.Function.Param.t) = - let open Ast.Function.Param in - let (loc, param') = param in - match param' with - | RegularParam { argument; default } -> - let argument' = this#function_param_pattern argument in - let default' = this#default_opt default in - if argument == argument' && default == default' then - param - else - (loc, RegularParam { argument = argument'; default = default' }) - | ParamProperty prop' -> - let prop'' = this#class_property loc prop' in - if prop' == prop'' then - param - else - (loc, ParamProperty prop'') - - method function_return_annotation (return : ('loc, 'loc) Ast.Function.ReturnAnnot.t) = - let open Ast.Function.ReturnAnnot in - match return with - | Missing _loc -> return - | Available t -> id this#type_annotation t return (fun rt -> Available rt) - | TypeGuard g -> id this#type_guard_annotation g return (fun tg -> TypeGuard tg) - - method function_body_any (body : ('loc, 'loc) Ast.Function.body) = - match body with - | Ast.Function.BodyBlock block -> - id this#function_body block body (fun block -> Ast.Function.BodyBlock block) - | Ast.Function.BodyExpression expr -> - id this#body_expression expr body (fun expr -> Ast.Function.BodyExpression expr) - - method function_body (body : 'loc * ('loc, 'loc) Ast.Statement.Block.t) = - let (loc, block) = body in - id_loc this#block loc block body (fun block -> (loc, block)) - - method body_expression (expr : ('loc, 'loc) Ast.Expression.t) = this#expression expr - - method function_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = - this#pattern_identifier ~kind:Ast.Variable.Var ident - - method identifier (id : ('loc, 'loc) Ast.Identifier.t) = - let open Ast.Identifier in - let (loc, { name; comments }) = id in - let comments' = this#syntax_opt comments in - if comments == comments' then - id - else - (loc, { name; comments = comments' }) - - method type_identifier (id : ('loc, 'loc) Ast.Identifier.t) = this#identifier id - - method type_identifier_reference (id : ('loc, 'loc) Ast.Identifier.t) = this#type_identifier id - - method binding_type_identifier (id : ('loc, 'loc) Ast.Identifier.t) = this#type_identifier id - - method interface _loc (interface : ('loc, 'loc) Ast.Statement.Interface.t) = - let open Ast.Statement.Interface in - let { id = ident; tparams; extends; body; comments } = interface in - let id' = this#binding_type_identifier ident in - let tparams' = map_opt (this#type_params ~kind:InterfaceTP) tparams in - let extends' = - map_list - (fun ext -> - let (eloc, e) = ext in - id this#generic_type e ext (fun e' -> (eloc, e'))) - extends - in - let body' = - let (bloc, b) = body in - id this#object_type b body (fun b' -> (bloc, b')) - in - let comments' = this#syntax_opt comments in - if - id' == ident - && tparams' == tparams - && extends' == extends - && body' == body - && comments' == comments - then - interface - else - { id = id'; tparams = tparams'; extends = extends'; body = body'; comments = comments' } - - method interface_declaration loc (decl : ('loc, 'loc) Ast.Statement.Interface.t) = - this#interface loc decl - - method private_name (id : 'loc Ast.PrivateName.t) = - let open Ast.PrivateName in - let (loc, { name; comments }) = id in - let comments' = this#syntax_opt comments in - if comments == comments' then - id - else - (loc, { name; comments = comments' }) - - method computed_key (key : ('loc, 'loc) Ast.ComputedKey.t) = - let open Ast.ComputedKey in - let (loc, { expression; comments }) = key in - let expression' = this#expression expression in - let comments' = this#syntax_opt comments in - if expression == expression' && comments == comments' then - key - else - (loc, { expression = expression'; comments = comments' }) - - method import (expr : ('loc, 'loc) Ast.Expression.Import.t) = - let open Ast.Expression.Import in - let { argument; options; comments } = expr in - let argument' = this#expression argument in - let options' = map_opt this#expression options in - let comments' = this#syntax_opt comments in - if argument == argument' && options == options' && comments == comments' then - expr - else - { argument = argument'; options = options'; comments = comments' } - - method if_consequent_statement ~has_else (stmt : ('loc, 'loc) Ast.Statement.t) = - ignore has_else; - this#statement stmt - - method if_alternate_statement _loc (altern : ('loc, 'loc) Ast.Statement.If.Alternate.t') = - let open Ast.Statement.If.Alternate in - let { body; comments } = altern in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - altern - else - { body = body'; comments = comments' } - - method if_statement _loc (stmt : ('loc, 'loc) Ast.Statement.If.t) = - let open Ast.Statement.If in - let { test; consequent; alternate; comments } = stmt in - let test' = this#predicate_expression test in - let consequent' = this#if_consequent_statement ~has_else:(alternate <> None) consequent in - let alternate' = map_opt (map_loc this#if_alternate_statement) alternate in - let comments' = this#syntax_opt comments in - if - test == test' - && consequent == consequent' - && alternate == alternate' - && comments == comments' - then - stmt - else - { test = test'; consequent = consequent'; alternate = alternate'; comments = comments' } - - method import_declaration _loc (decl : ('loc, 'loc) Ast.Statement.ImportDeclaration.t) = - let open Ast.Statement.ImportDeclaration in - let { import_kind; source; specifiers; default; attributes; comments } = decl in - let source' = map_loc this#import_source source in - let specifiers' = map_opt (this#import_specifier ~import_kind) specifiers in - let default' = - map_opt - (fun ({ identifier; remote_default_name_def_loc } as id) -> - let identifier' = this#import_default_specifier ~import_kind identifier in - if identifier' == identifier then - id - else - { identifier = identifier'; remote_default_name_def_loc }) - default - in - let attributes' = map_opt (map_loc this#import_attributes) attributes in - let comments' = this#syntax_opt comments in - if - source == source' - && specifiers == specifiers' - && default == default' - && attributes == attributes' - && comments == comments' - then - decl - else - { - import_kind; - source = source'; - specifiers = specifiers'; - default = default'; - attributes = attributes'; - comments = comments'; - } - - method import_source _loc (source : 'loc Ast.StringLiteral.t) = - let open Ast.StringLiteral in - let { value; raw; comments } = source in - let comments' = this#syntax_opt comments in - if comments == comments' then - source - else - { value; raw; comments = comments' } - - method import_equals_declaration - _loc (decl : ('loc, 'loc) Ast.Statement.ImportEqualsDeclaration.t) = - let open Ast.Statement.ImportEqualsDeclaration in - let { id = ident; module_reference; import_kind; is_export; comments } = decl in - let ident' = - let open Ast.Statement.ImportDeclaration in - match import_kind with - | ImportType - | ImportTypeof -> - this#binding_type_identifier ident - | ImportValue -> this#pattern_identifier ~kind:Ast.Variable.Let ident - in - let module_reference' = this#import_equals_module_reference module_reference in - let comments' = this#syntax_opt comments in - if ident == ident' && module_reference == module_reference' && comments == comments' then - decl - else - { - id = ident'; - module_reference = module_reference'; - import_kind; - is_export; - comments = comments'; - } - - method import_equals_module_reference - (ref : ('loc, 'loc) Ast.Statement.ImportEqualsDeclaration.module_reference) = - let open Ast.Statement.ImportEqualsDeclaration in - match ref with - | ExternalModuleReference (annot, lit) -> - let lit' = this#string_literal lit in - if lit == lit' then - ref - else - ExternalModuleReference (annot, lit') - | Identifier ident -> id this#generic_identifier_type ident ref (fun ident -> Identifier ident) - - method import_attributes - _loc (attrs : ('loc, 'loc) Ast.Statement.ImportDeclaration.import_attribute list) = - map_list this#import_attribute attrs - - method import_attribute (attr : ('loc, 'loc) Ast.Statement.ImportDeclaration.import_attribute) = - let open Ast.Statement.ImportDeclaration in - let { loc; key; value } = attr in - let key' = this#import_attribute_key key in - let value' = - let (vloc, vlit) = value in - id this#string_literal vlit value (fun vlit' -> (vloc, vlit')) - in - if key == key' && value == value' then - attr - else - { loc; key = key'; value = value' } - - method import_attribute_key - (key : ('loc, 'loc) Ast.Statement.ImportDeclaration.import_attribute_key) = - let open Ast.Statement.ImportDeclaration in - match key with - | Identifier ident -> id this#identifier ident key (fun ident' -> Identifier ident') - | StringLiteral (loc, lit) -> - id this#string_literal lit key (fun lit' -> StringLiteral (loc, lit')) - - method import_specifier - ~import_kind (specifier : ('loc, 'loc) Ast.Statement.ImportDeclaration.specifier) = - let open Ast.Statement.ImportDeclaration in - match specifier with - | ImportNamedSpecifiers named_specifiers -> - let named_specifiers' = - map_list (this#import_named_specifier ~import_kind) named_specifiers - in - if named_specifiers == named_specifiers' then - specifier - else - ImportNamedSpecifiers named_specifiers' - | ImportNamespaceSpecifier (loc, ident) -> - id_loc (this#import_namespace_specifier ~import_kind) loc ident specifier (fun ident -> - ImportNamespaceSpecifier (loc, ident) - ) - - method remote_identifier id = this#identifier id - - method import_named_specifier - ~(import_kind : Ast.Statement.ImportDeclaration.import_kind) - (specifier : ('loc, 'loc) Ast.Statement.ImportDeclaration.named_specifier) = - let open Ast.Statement.ImportDeclaration in - let { kind; kind_loc; local; remote; remote_name_def_loc } = specifier in - let (is_type_remote, is_type_local) = - match (import_kind, kind) with - | (ImportType, _) - | (_, Some ImportType) -> - (true, true) - | (ImportTypeof, _) - | (_, Some ImportTypeof) -> - (false, true) - | _ -> (false, false) - in - let remote' = - match local with - | None -> - if is_type_remote then - this#binding_type_identifier remote - else - this#pattern_identifier ~kind:Ast.Variable.Let remote - | Some _ -> this#remote_identifier remote - in - let local' = - match local with - | None -> None - | Some ident -> - let local_visitor = - if is_type_local then - this#binding_type_identifier - else - this#pattern_identifier ~kind:Ast.Variable.Let - in - id local_visitor ident local (fun ident -> Some ident) - in - if local == local' && remote == remote' then - specifier - else - { kind; kind_loc; local = local'; remote = remote'; remote_name_def_loc } - - method import_default_specifier ~import_kind (id : ('loc, 'loc) Ast.Identifier.t) = - let open Ast.Statement.ImportDeclaration in - let local_visitor = - match import_kind with - | ImportType - | ImportTypeof -> - this#binding_type_identifier - | _ -> this#pattern_identifier ~kind:Ast.Variable.Let - in - local_visitor id - - method import_namespace_specifier ~import_kind _loc (id : ('loc, 'loc) Ast.Identifier.t) = - let open Ast.Statement.ImportDeclaration in - let local_visitor = - match import_kind with - | ImportType - | ImportTypeof -> - this#binding_type_identifier - | _ -> this#pattern_identifier ~kind:Ast.Variable.Let - in - local_visitor id - - method jsx_element _loc (expr : ('loc, 'loc) Ast.JSX.element) = - let open Ast.JSX in - let { opening_element; closing_element; children; comments } = expr in - let opening_element' = this#jsx_opening_element opening_element in - let closing_element' = map_opt this#jsx_closing_element closing_element in - let children' = this#jsx_children children in - let comments' = this#syntax_opt comments in - if - opening_element == opening_element' - && closing_element == closing_element' - && children == children' - && comments == comments' - then - expr - else - { - opening_element = opening_element'; - closing_element = closing_element'; - children = children'; - comments = comments'; - } - - method jsx_fragment _loc (expr : ('loc, 'loc) Ast.JSX.fragment) = - let open Ast.JSX in - let { frag_children; frag_comments; _ } = expr in - let children' = this#jsx_children frag_children in - let frag_comments' = this#syntax_opt frag_comments in - if frag_children == children' && frag_comments == frag_comments' then - expr - else - { expr with frag_children = children'; frag_comments = frag_comments' } - - method jsx_opening_element (elem : ('loc, 'loc) Ast.JSX.Opening.t) = - let open Ast.JSX.Opening in - let (loc, { name; targs; self_closing; attributes }) = elem in - let name' = this#jsx_element_name name in - let targs' = map_opt this#call_type_args targs in - let attributes' = map_list this#jsx_opening_attribute attributes in - if name == name' && targs == targs' && attributes == attributes' then - elem - else - (loc, { name = name'; targs = targs'; self_closing; attributes = attributes' }) - - method jsx_closing_element (elem : ('loc, 'loc) Ast.JSX.Closing.t) = - let open Ast.JSX.Closing in - let (loc, { name }) = elem in - let name' = this#jsx_element_name name in - if name == name' then - elem - else - (loc, { name = name' }) - - method jsx_opening_attribute (jsx_attr : ('loc, 'loc) Ast.JSX.Opening.attribute) = - let open Ast.JSX.Opening in - match jsx_attr with - | Attribute attr -> id this#jsx_attribute attr jsx_attr (fun attr -> Attribute attr) - | SpreadAttribute (loc, attr) -> - id_loc this#jsx_spread_attribute loc attr jsx_attr (fun attr -> SpreadAttribute (loc, attr)) - - method jsx_spread_attribute _loc (attr : ('loc, 'loc) Ast.JSX.SpreadAttribute.t') = - let open Ast.JSX.SpreadAttribute in - let { argument; comments } = attr in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - attr - else - { argument = argument'; comments = comments' } - - method jsx_attribute (attr : ('loc, 'loc) Ast.JSX.Attribute.t) = - let open Ast.JSX.Attribute in - let (loc, { name; value }) = attr in - let name' = this#jsx_attribute_name name in - let value' = map_opt this#jsx_attribute_value value in - if name == name' && value == value' then - attr - else - (loc, { name = name'; value = value' }) - - method jsx_attribute_name (name : ('loc, 'loc) Ast.JSX.Attribute.name) = - let open Ast.JSX.Attribute in - match name with - | Identifier ident -> - id this#jsx_attribute_name_identifier ident name (fun ident -> Identifier ident) - | NamespacedName ns -> - id this#jsx_attribute_name_namespaced ns name (fun ns -> NamespacedName ns) - - method jsx_attribute_name_identifier ident = this#jsx_identifier ident - - method jsx_attribute_name_namespaced ns = this#jsx_namespaced_name ns - - method jsx_attribute_value (value : ('loc, 'loc) Ast.JSX.Attribute.value) = - let open Ast.JSX.Attribute in - match value with - | StringLiteral (loc, lit) -> - id_loc this#jsx_attribute_value_literal loc lit value (fun lit -> StringLiteral (loc, lit)) - | ExpressionContainer (loc, expr) -> - id_loc this#jsx_attribute_value_expression loc expr value (fun expr -> - ExpressionContainer (loc, expr) - ) - - method jsx_attribute_value_expression loc (jsx_expr : ('loc, 'loc) Ast.JSX.ExpressionContainer.t) - = - this#jsx_expression loc jsx_expr - - method jsx_attribute_value_literal _loc (lit : 'loc Ast.StringLiteral.t) = - this#string_literal lit - - method jsx_children ((loc, children) as orig : 'loc * ('loc, 'loc) Ast.JSX.child list) = - let children' = map_list this#jsx_child children in - if children == children' then - orig - else - (loc, children') - - method jsx_child (child : ('loc, 'loc) Ast.JSX.child) = - let open Ast.JSX in - match child with - | (loc, Element elem) -> - id_loc this#jsx_element loc elem child (fun elem -> (loc, Element elem)) - | (loc, Fragment frag) -> - id_loc this#jsx_fragment loc frag child (fun frag -> (loc, Fragment frag)) - | (loc, ExpressionContainer expr) -> - id_loc this#jsx_expression loc expr child (fun expr -> (loc, ExpressionContainer expr)) - | (loc, SpreadChild spread) -> - id this#jsx_spread_child spread child (fun spread -> (loc, SpreadChild spread)) - | (_loc, Text _) -> child - - method jsx_expression _loc (jsx_expr : ('loc, 'loc) Ast.JSX.ExpressionContainer.t) = - let open Ast.JSX.ExpressionContainer in - let { expression; comments } = jsx_expr in - let comments' = this#syntax_opt comments in - match expression with - | Expression expr -> - let expr' = this#expression expr in - if expr == expr' && comments == comments' then - jsx_expr - else - { expression = Expression expr'; comments = comments' } - | EmptyExpression -> - if comments == comments' then - jsx_expr - else - { expression = EmptyExpression; comments = comments' } - - method jsx_spread_child (jsx_spread_child : ('loc, 'loc) Ast.JSX.SpreadChild.t) = - let open Ast.JSX.SpreadChild in - let { expression; comments } = jsx_spread_child in - let expression' = this#expression expression in - let comments' = this#syntax_opt comments in - if expression == expression' && comments == comments' then - jsx_spread_child - else - { expression = expression'; comments = comments' } - - method jsx_element_name (name : ('loc, 'loc) Ast.JSX.name) = - let open Ast.JSX in - match name with - | Identifier ident -> - id this#jsx_element_name_identifier ident name (fun ident -> Identifier ident) - | NamespacedName ns -> - id this#jsx_element_name_namespaced ns name (fun ns -> NamespacedName ns) - | MemberExpression expr -> - id this#jsx_element_name_member_expression expr name (fun expr -> MemberExpression expr) - - method jsx_element_name_identifier ident = this#jsx_identifier ident - - method jsx_element_name_namespaced ns = this#jsx_namespaced_name ns - - method jsx_element_name_member_expression expr = this#jsx_member_expression expr - - method jsx_namespaced_name (namespaced_name : ('loc, 'loc) Ast.JSX.NamespacedName.t) = - let open Ast.JSX in - NamespacedName.( - let (loc, { namespace; name }) = namespaced_name in - let namespace' = this#jsx_identifier namespace in - let name' = this#jsx_identifier name in - if namespace == namespace' && name == name' then - namespaced_name - else - (loc, { namespace = namespace'; name = name' }) - ) - - method jsx_member_expression (member_exp : ('loc, 'loc) Ast.JSX.MemberExpression.t) = - let open Ast.JSX in - let (loc, { MemberExpression._object; MemberExpression.property }) = member_exp in - let _object' = this#jsx_member_expression_object _object in - let property' = this#jsx_identifier property in - if _object == _object' && property == property' then - member_exp - else - (loc, MemberExpression.{ _object = _object'; property = property' }) - - method jsx_member_expression_object (_object : ('loc, 'loc) Ast.JSX.MemberExpression._object) = - let open Ast.JSX.MemberExpression in - match _object with - | Identifier ident -> - id this#jsx_member_expression_identifier ident _object (fun ident -> Identifier ident) - | MemberExpression nested_exp -> - id this#jsx_member_expression nested_exp _object (fun exp -> MemberExpression exp) - - method jsx_member_expression_identifier ident = this#jsx_element_name_identifier ident - - method jsx_identifier (id : ('loc, 'loc) Ast.JSX.Identifier.t) = - let open Ast.JSX.Identifier in - let (loc, { name; comments }) = id in - let comments' = this#syntax_opt comments in - if comments == comments' then - id - else - (loc, { name; comments = comments' }) - - method labeled_statement _loc (stmt : ('loc, 'loc) Ast.Statement.Labeled.t) = - let open Ast.Statement.Labeled in - let { label; body; comments } = stmt in - let label' = this#label_identifier label in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if label == label' && body == body' && comments == comments' then - stmt - else - { label = label'; body = body'; comments = comments' } - - method logical _loc (expr : ('loc, 'loc) Ast.Expression.Logical.t) = - let open Ast.Expression.Logical in - let { operator = _; left; right; comments } = expr in - let left' = this#expression left in - let right' = this#expression right in - let comments' = this#syntax_opt comments in - if left == left' && right == right' && comments == comments' then - expr - else - { expr with left = left'; right = right'; comments = comments' } - - method match_ - : 'B. - 'loc -> - on_case_body:('B -> 'B) -> - ('loc, 'loc, 'B) Ast.Match.t -> - ('loc, 'loc, 'B) Ast.Match.t = - fun _loc ~on_case_body x -> - let open Ast.Match in - let { arg; cases; match_keyword_loc; comments } = x in - let arg' = this#expression arg in - let cases' = map_list (this#match_case ~on_case_body) cases in - let comments' = this#syntax_opt comments in - if arg == arg' && cases == cases' && comments == comments' then - x - else - { arg = arg'; cases = cases'; match_keyword_loc; comments = comments' } - - method match_case - : 'B. - on_case_body:('B -> 'B) -> - ('loc, 'loc, 'B) Ast.Match.Case.t -> - ('loc, 'loc, 'B) Ast.Match.Case.t = - fun ~on_case_body case -> - let open Ast.Match.Case in - let (loc, { pattern; body; guard; comments; invalid_syntax; case_match_root_loc }) = case in - let pattern' = this#match_pattern pattern in - let body' = on_case_body body in - let guard' = map_opt this#expression guard in - let comments' = this#syntax_opt comments in - if pattern == pattern' && body == body' && guard == guard' && comments == comments' then - case - else - ( loc, - { - pattern = pattern'; - body = body'; - guard = guard'; - comments = comments'; - invalid_syntax; - case_match_root_loc; - } - ) - - method match_expression loc (x : ('loc, 'loc) Ast.Expression.match_expression) = - this#match_ loc ~on_case_body:this#expression x - - method match_statement loc (x : ('loc, 'loc) Ast.Statement.match_statement) = - this#match_ loc ~on_case_body:this#statement x - - method match_pattern (pattern : ('loc, 'loc) Ast.MatchPattern.t) = - let open Ast.MatchPattern in - match pattern with - | (loc, WildcardPattern x) -> - id this#match_wildcard_pattern x pattern (fun x -> (loc, WildcardPattern x)) - | (loc, StringPattern x) -> id this#string_literal x pattern (fun x -> (loc, StringPattern x)) - | (loc, BooleanPattern x) -> - id this#boolean_literal x pattern (fun x -> (loc, BooleanPattern x)) - | (loc, NullPattern x) -> id this#syntax_opt x pattern (fun x -> (loc, NullPattern x)) - | (loc, NumberPattern x) -> id this#number_literal x pattern (fun x -> (loc, NumberPattern x)) - | (loc, BigIntPattern x) -> id this#bigint_literal x pattern (fun x -> (loc, BigIntPattern x)) - | (loc, UnaryPattern x) -> - id this#match_unary_pattern x pattern (fun x -> (loc, UnaryPattern x)) - | (loc, IdentifierPattern x) -> - id this#identifier x pattern (fun x -> (loc, IdentifierPattern x)) - | (loc, MemberPattern x) -> - id this#match_member_pattern x pattern (fun x -> (loc, MemberPattern x)) - | (loc, BindingPattern x) -> - id_loc this#match_binding_pattern loc x pattern (fun x -> (loc, BindingPattern x)) - | (loc, ObjectPattern x) -> - id_loc this#match_object_pattern loc x pattern (fun x -> (loc, ObjectPattern x)) - | (loc, ArrayPattern x) -> - id this#match_array_pattern x pattern (fun x -> (loc, ArrayPattern x)) - | (loc, InstancePattern x) -> - id this#match_instance_pattern x pattern (fun x -> (loc, InstancePattern x)) - | (loc, OrPattern x) -> id this#match_or_pattern x pattern (fun x -> (loc, OrPattern x)) - | (loc, AsPattern x) -> id this#match_as_pattern x pattern (fun x -> (loc, AsPattern x)) - - method match_unary_pattern (unary_pattern : 'loc Ast.MatchPattern.UnaryPattern.t) = - let open Ast.MatchPattern.UnaryPattern in - let { operator; argument; comments } = unary_pattern in - let (arg_loc, arg) = argument in - let argument' = - id_loc this#match_unary_pattern_argument arg_loc arg argument (fun arg -> (arg_loc, arg)) - in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - unary_pattern - else - { operator; argument = argument'; comments = comments' } - - method match_unary_pattern_argument _loc (argument : 'loc Ast.MatchPattern.UnaryPattern.argument) - = - let open Ast.MatchPattern.UnaryPattern in - match argument with - | NumberLiteral lit -> id this#number_literal lit argument (fun lit -> NumberLiteral lit) - | BigIntLiteral lit -> id this#bigint_literal lit argument (fun lit -> BigIntLiteral lit) - - method match_member_pattern (member_pattern : ('loc, 'loc) Ast.MatchPattern.MemberPattern.t) = - let open Ast.MatchPattern.MemberPattern in - let (loc, { base; property; comments }) = member_pattern in - let base' = this#match_member_pattern_base base in - let property' = this#match_member_pattern_property property in - let comments' = this#syntax_opt comments in - if base == base' && property == property' && comments == comments' then - member_pattern - else - (loc, { base = base'; property = property'; comments = comments' }) - - method match_member_pattern_base (base : ('loc, 'loc) Ast.MatchPattern.MemberPattern.base) = - let open Ast.MatchPattern.MemberPattern in - match base with - | BaseIdentifier x -> id this#identifier x base (fun x -> BaseIdentifier x) - | BaseMember x -> id this#match_member_pattern x base (fun x -> BaseMember x) - - method match_member_pattern_property - (prop : ('loc, 'loc) Ast.MatchPattern.MemberPattern.property) = - let open Ast.MatchPattern.MemberPattern in - match prop with - | PropertyString (loc, lit) -> - id this#string_literal lit prop (fun lit -> PropertyString (loc, lit)) - | PropertyNumber (loc, lit) -> - id this#number_literal lit prop (fun lit -> PropertyNumber (loc, lit)) - | PropertyBigInt (loc, lit) -> - id this#bigint_literal lit prop (fun lit -> PropertyBigInt (loc, lit)) - | PropertyIdentifier ident -> - id this#identifier ident prop (fun ident -> PropertyIdentifier ident) - - method match_binding_pattern - _loc (binding_pattern : ('loc, 'loc) Ast.MatchPattern.BindingPattern.t) = - let open Ast.MatchPattern.BindingPattern in - let { id; kind; comments } = binding_pattern in - let id' = this#pattern_identifier ~kind id in - let comments' = this#syntax_opt comments in - if id == id' && comments == comments' then - binding_pattern - else - { id = id'; kind; comments = comments' } - - method match_object_pattern _loc (object_pattern : ('loc, 'loc) Ast.MatchPattern.ObjectPattern.t) - = - let open Ast.MatchPattern.ObjectPattern in - let { properties; rest; comments } = object_pattern in - let properties' = map_list this#match_object_pattern_property properties in - let rest' = map_loc_opt this#match_rest_pattern rest in - let comments' = this#syntax_opt comments in - if properties == properties' && rest == rest' && comments == comments' then - object_pattern - else - { properties = properties'; rest = rest'; comments = comments' } - - method match_object_pattern_property - (prop : ('loc, 'loc) Ast.MatchPattern.ObjectPattern.Property.t) = - let open Ast.MatchPattern.ObjectPattern.Property in - match prop with - | (loc, Valid { key; pattern; shorthand; comments }) -> - let key' = this#match_object_pattern_property_key key in - let pattern' = this#match_pattern pattern in - let comments' = this#syntax_opt comments in - if key == key' && pattern == pattern' && comments == comments' then - prop - else - (loc, Valid { key = key'; pattern = pattern'; shorthand; comments = comments' }) - | (loc, InvalidShorthand id) -> - let id' = this#identifier id in - if id == id' then - prop - else - (loc, InvalidShorthand id') - - method match_object_pattern_property_key - (key : ('loc, 'loc) Ast.MatchPattern.ObjectPattern.Property.key) = - let open Ast.MatchPattern.ObjectPattern.Property in - match key with - | StringLiteral (loc, lit) -> - id this#string_literal lit key (fun lit -> StringLiteral (loc, lit)) - | NumberLiteral (loc, lit) -> - id this#number_literal lit key (fun lit -> NumberLiteral (loc, lit)) - | BigIntLiteral (loc, lit) -> - id this#bigint_literal lit key (fun lit -> BigIntLiteral (loc, lit)) - | Identifier ident -> id this#identifier ident key (fun ident -> Identifier ident) - - method match_array_pattern (array_pattern : ('loc, 'loc) Ast.MatchPattern.ArrayPattern.t) = - let open Ast.MatchPattern.ArrayPattern in - let { elements; rest; comments } = array_pattern in - let elements' = map_list this#match_pattern_array_element elements in - let rest' = map_loc_opt this#match_rest_pattern rest in - let comments' = this#syntax_opt comments in - if elements == elements' && rest == rest' && comments == comments' then - array_pattern - else - { elements = elements'; rest = rest'; comments = comments' } - - method match_pattern_array_element - (element : ('loc, 'loc) Ast.MatchPattern.ArrayPattern.Element.t) = - let open Ast.MatchPattern.ArrayPattern.Element in - let { pattern; index } = element in - let pattern' = this#match_pattern pattern in - if pattern == pattern' then - element - else - { pattern = pattern'; index } - - method match_instance_pattern - (instance_pattern : ('loc, 'loc) Ast.MatchPattern.InstancePattern.t) - : ('loc, 'loc) Ast.MatchPattern.InstancePattern.t = - let open Ast.MatchPattern.InstancePattern in - let { constructor; properties; comments } = instance_pattern in - let constructor' = this#match_instance_pattern_constructor constructor in - let properties' = map_loc this#match_object_pattern properties in - let comments' = this#syntax_opt comments in - if constructor == constructor' && properties == properties' && comments == comments' then - instance_pattern - else - { constructor = constructor'; properties = properties'; comments = comments' } - - method match_instance_pattern_constructor - (constructor : ('loc, 'loc) Ast.MatchPattern.InstancePattern.constructor) = - let open Ast.MatchPattern.InstancePattern in - match constructor with - | IdentifierConstructor ident -> - id this#identifier ident constructor (fun x -> IdentifierConstructor x) - | MemberConstructor member -> - id this#match_member_pattern member constructor (fun x -> MemberConstructor x) - - method match_rest_pattern _loc (rest : ('loc, 'loc) Ast.MatchPattern.RestPattern.t') = - let open Ast.MatchPattern.RestPattern in - let { argument; comments } = rest in - let argument' = map_loc_opt this#match_binding_pattern argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - rest - else - { argument = argument'; comments = comments' } - - method match_or_pattern (or_pattern : ('loc, 'loc) Ast.MatchPattern.OrPattern.t) = - let open Ast.MatchPattern.OrPattern in - let { patterns; comments } = or_pattern in - let patterns' = map_list this#match_pattern patterns in - let comments' = this#syntax_opt comments in - if patterns == patterns' && comments == comments' then - or_pattern - else - { patterns = patterns'; comments = comments' } - - method match_as_pattern (as_pattern : ('loc, 'loc) Ast.MatchPattern.AsPattern.t) = - let open Ast.MatchPattern.AsPattern in - let { pattern; target; comments } = as_pattern in - let pattern' = this#match_pattern pattern in - let target' = this#match_as_pattern_target target in - let comments' = this#syntax_opt comments in - if pattern == pattern' && target == target' && comments == comments' then - as_pattern - else - { pattern = pattern'; target = target'; comments = comments' } - - method match_as_pattern_target (target : ('loc, 'loc) Ast.MatchPattern.AsPattern.target) = - let open Ast.MatchPattern.AsPattern in - match target with - | Binding (loc, binding) -> - id_loc this#match_binding_pattern loc binding target (fun x -> Binding (loc, x)) - | Identifier ident -> - id (this#pattern_identifier ~kind:Ast.Variable.Const) ident target (fun x -> Identifier x) - - method match_wildcard_pattern (wildcard_pattern : 'loc Ast.MatchPattern.WildcardPattern.t) = - let open Ast.MatchPattern.WildcardPattern in - let { comments; invalid_syntax_default_keyword } = wildcard_pattern in - let comments' = this#syntax_opt comments in - if comments == comments' then - wildcard_pattern - else - { comments = comments'; invalid_syntax_default_keyword } - - method member _loc (expr : ('loc, 'loc) Ast.Expression.Member.t) = - let open Ast.Expression.Member in - let { _object; property; comments } = expr in - let _object' = this#expression _object in - let property' = this#member_property property in - let comments' = this#syntax_opt comments in - if _object == _object' && property == property' && comments == comments' then - expr - else - { _object = _object'; property = property'; comments = comments' } - - method optional_member loc (expr : ('loc, 'loc) Ast.Expression.OptionalMember.t) = - let open Ast.Expression.OptionalMember in - let { member; optional = _; filtered_out = _ } = expr in - let member' = this#member loc member in - if member == member' then - expr - else - { expr with member = member' } - - method member_property (expr : ('loc, 'loc) Ast.Expression.Member.property) = - let open Ast.Expression.Member in - match expr with - | PropertyIdentifier ident -> - id this#member_property_identifier ident expr (fun ident -> PropertyIdentifier ident) - | PropertyPrivateName ident -> - id this#member_private_name ident expr (fun ident -> PropertyPrivateName ident) - | PropertyExpression e -> - id this#member_property_expression e expr (fun e -> PropertyExpression e) - - method member_property_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = - this#identifier ident - - method member_private_name (name : 'loc Ast.PrivateName.t) = this#private_name name - - method member_property_expression (expr : ('loc, 'loc) Ast.Expression.t) = this#expression expr - - method record _loc (expr : ('loc, 'loc) Ast.Expression.Record.t) = - let open Ast.Expression.Record in - let { constructor; targs; properties = (props_loc, props); comments } = expr in - let constructor' = this#expression constructor in - let targs' = map_opt this#call_type_args targs in - let props' = this#object_ props_loc props in - let comments' = this#syntax_opt comments in - if constructor == constructor' && targs == targs' && props == props' && comments == comments' - then - expr - else - { - constructor = constructor'; - targs = targs'; - properties = (props_loc, props'); - comments = comments'; - } - - method meta_property _loc (expr : 'loc Ast.Expression.MetaProperty.t) = - let open Ast.Expression.MetaProperty in - let { meta; property; comments } = expr in - let meta' = this#identifier meta in - let property' = this#identifier property in - let comments' = this#syntax_opt comments in - if meta == meta' && property == property' && comments == comments' then - expr - else - { meta = meta'; property = property'; comments = comments' } - - method new_ _loc (expr : ('loc, 'loc) Ast.Expression.New.t) = - let open Ast.Expression.New in - let { callee; targs; arguments; comments } = expr in - let callee' = this#expression callee in - let targs' = map_opt this#call_type_args targs in - let arguments' = map_opt this#arg_list arguments in - let comments' = this#syntax_opt comments in - if callee == callee' && targs == targs' && arguments == arguments' && comments == comments' - then - expr - else - { callee = callee'; targs = targs'; arguments = arguments'; comments = comments' } - - method object_ _loc (expr : ('loc, 'loc) Ast.Expression.Object.t) = - let open Ast.Expression.Object in - let { properties; comments } = expr in - let properties' = - map_list - (fun prop -> - match prop with - | Property p -> - let p' = this#object_property p in - if p == p' then - prop - else - Property p' - | SpreadProperty s -> - let s' = this#spread_property s in - if s == s' then - prop - else - SpreadProperty s') - properties - in - let comments' = this#syntax_opt comments in - if properties == properties' && comments == comments' then - expr - else - { properties = properties'; comments = comments' } - - method object_property (prop : ('loc, 'loc) Ast.Expression.Object.Property.t) = - let open Ast.Expression.Object.Property in - match prop with - | (loc, Init { key; value; shorthand }) -> - let key' = this#object_key key in - let value' = this#expression value in - let shorthand' = - (* Try to figure out if shorthand should still be true--if - key and value change differently, it should become false *) - shorthand - && - match (key', value') with - | ( Identifier (_, { Ast.Identifier.name = key_name; _ }), - (_, Ast.Expression.Identifier (_, { Ast.Identifier.name = value_name; _ })) - ) -> - String.equal key_name value_name - | _ -> key == key' && value == value' - in - if key == key' && value == value' && shorthand == shorthand' then - prop - else - (loc, Init { key = key'; value = value'; shorthand = shorthand' }) - | (loc, Method { key; value = fn }) -> - let key' = this#object_key key in - let fn' = map_loc this#function_expression_or_method fn in - if key == key' && fn == fn' then - prop - else - (loc, Method { key = key'; value = fn' }) - | (loc, Get { key; value = fn; comments }) -> - let key' = this#object_key key in - let fn' = map_loc this#function_expression_or_method fn in - let comments' = this#syntax_opt comments in - if key == key' && fn == fn' && comments == comments' then - prop - else - (loc, Get { key = key'; value = fn'; comments = comments' }) - | (loc, Set { key; value = fn; comments }) -> - let key' = this#object_key key in - let fn' = map_loc this#function_expression_or_method fn in - let comments' = this#syntax_opt comments in - if key == key' && fn == fn' && comments == comments' then - prop - else - (loc, Set { key = key'; value = fn'; comments = comments' }) - - method object_key (key : ('loc, 'loc) Ast.Expression.Object.Property.key) = - let open Ast.Expression.Object.Property in - match key with - | StringLiteral (loc, lit) -> - id this#object_key_string_literal lit key (fun lit -> StringLiteral (loc, lit)) - | NumberLiteral (loc, lit) -> - id this#object_key_number_literal lit key (fun lit -> NumberLiteral (loc, lit)) - | BigIntLiteral (loc, lit) -> - id this#object_key_bigint_literal lit key (fun lit -> BigIntLiteral (loc, lit)) - | Identifier ident -> id this#object_key_identifier ident key (fun ident -> Identifier ident) - | PrivateName ident -> id this#private_name ident key (fun ident -> PrivateName ident) - | Computed computed -> id this#object_key_computed computed key (fun expr -> Computed expr) - - method object_key_string_literal (literal : 'loc Ast.StringLiteral.t) = - this#string_literal literal - - method object_key_number_literal (literal : 'loc Ast.NumberLiteral.t) = - this#number_literal literal - - method object_key_bigint_literal (literal : 'loc Ast.BigIntLiteral.t) = - this#bigint_literal literal - - method object_key_identifier (ident : ('loc, 'loc) Ast.Identifier.t) = this#identifier ident - - method object_key_computed (key : ('loc, 'loc) Ast.ComputedKey.t) = this#computed_key key - - method opaque_type _loc (otype : ('loc, 'loc) Ast.Statement.OpaqueType.t) = - let open Ast.Statement.OpaqueType in - let { id; tparams; impl_type; lower_bound; upper_bound; legacy_upper_bound; comments } = - otype - in - let id' = this#binding_type_identifier id in - let tparams' = map_opt (this#type_params ~kind:OpaqueTypeTP) tparams in - let impl_type' = map_opt this#type_ impl_type in - let legacy_upper_bound' = map_opt this#type_ legacy_upper_bound in - let lower_bound' = map_opt this#type_ lower_bound in - let upper_bound' = map_opt this#type_ upper_bound in - let comments' = this#syntax_opt comments in - if - id == id' - && impl_type == impl_type' - && tparams == tparams' - && lower_bound == lower_bound' - && upper_bound == upper_bound' - && legacy_upper_bound == legacy_upper_bound' - && comments == comments' - then - otype - else - { - id = id'; - tparams = tparams'; - impl_type = impl_type'; - lower_bound = lower_bound'; - upper_bound = upper_bound'; - legacy_upper_bound = legacy_upper_bound'; - comments = comments'; - } - - method function_param_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = - this#binding_pattern ~kind:Ast.Variable.Let expr - - method variable_declarator_pattern ~kind (expr : ('loc, 'loc) Ast.Pattern.t) = - this#binding_pattern ~kind expr - - method catch_clause_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = - this#binding_pattern ~kind:Ast.Variable.Let expr - - method for_in_assignment_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = - this#assignment_pattern expr - - method for_of_assignment_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = - this#assignment_pattern expr - - method binding_pattern ?(kind = Ast.Variable.Var) (expr : ('loc, 'loc) Ast.Pattern.t) = - this#pattern ~kind expr - - method assignment_pattern (expr : ('loc, 'loc) Ast.Pattern.t) = this#pattern expr - - (* NOTE: Patterns are highly overloaded. A pattern can be a binding pattern, - which has a kind (Var/Let/Const, with Var being the default for all pre-ES5 - bindings), or an assignment pattern, which has no kind. Subterms that are - patterns inherit the kind (or lack thereof). *) - method pattern ?kind (expr : ('loc, 'loc) Ast.Pattern.t) = - let open Ast.Pattern in - let (loc, patt) = expr in - let patt' = - match patt with - | Object { Object.properties; annot; optional; comments } -> - let properties' = map_list (this#pattern_object_p ?kind) properties in - let annot' = this#type_annotation_hint annot in - let comments' = this#syntax_opt comments in - if properties' == properties && annot' == annot && comments' == comments then - patt - else - Object - { Object.properties = properties'; annot = annot'; optional; comments = comments' } - | Array { Array.elements; annot; optional; comments } -> - let elements' = map_list (this#pattern_array_e ?kind) elements in - let annot' = this#type_annotation_hint annot in - let comments' = this#syntax_opt comments in - if comments == comments' && elements' == elements && annot' == annot then - patt - else - Array { Array.elements = elements'; annot = annot'; optional; comments = comments' } - | Identifier { Identifier.name; annot; optional } -> - let name' = this#pattern_identifier ?kind name in - let annot' = this#type_annotation_hint annot in - if name == name' && annot == annot' then - patt - else - Identifier { Identifier.name = name'; annot = annot'; optional } - | Expression e -> id this#pattern_expression e patt (fun e -> Expression e) - in - if patt == patt' then - expr - else - (loc, patt') - - method pattern_identifier ?kind (ident : ('loc, 'loc) Ast.Identifier.t) = - ignore kind; - this#identifier ident - - method pattern_string_literal ?kind _loc (expr : 'loc Ast.StringLiteral.t) = - ignore kind; - this#string_literal expr - - method pattern_number_literal ?kind _loc (expr : 'loc Ast.NumberLiteral.t) = - ignore kind; - this#number_literal expr - - method pattern_bigint_literal ?kind _loc (expr : 'loc Ast.BigIntLiteral.t) = - ignore kind; - this#bigint_literal expr - - method pattern_object_p ?kind (p : ('loc, 'loc) Ast.Pattern.Object.property) = - let open Ast.Pattern.Object in - match p with - | Property prop -> id (this#pattern_object_property ?kind) prop p (fun prop -> Property prop) - | RestElement prop -> - id (this#pattern_object_rest_property ?kind) prop p (fun prop -> RestElement prop) - - method pattern_object_property ?kind (prop : ('loc, 'loc) Ast.Pattern.Object.Property.t) = - let open Ast.Pattern.Object.Property in - let (loc, { key; pattern; default; shorthand }) = prop in - let key' = this#pattern_object_property_key ?kind key in - let pattern' = this#pattern_object_property_pattern ?kind pattern in - let default' = this#default_opt default in - let shorthand' = - (* Try to figure out if shorthand should still be true--if - key and value change differently, it should become false *) - shorthand - && - match (key', pattern') with - | ( Identifier (_, { Ast.Identifier.name = key_name; _ }), - ( _, - Ast.Pattern.Identifier - { Ast.Pattern.Identifier.name = (_, { Ast.Identifier.name = value_name; _ }); _ } - ) - ) -> - String.equal key_name value_name - | _ -> key == key' && pattern == pattern' - in - if key' == key && pattern' == pattern && default' == default && shorthand == shorthand' then - prop - else - (loc, { key = key'; pattern = pattern'; default = default'; shorthand = shorthand' }) - - method pattern_object_property_key ?kind (key : ('loc, 'loc) Ast.Pattern.Object.Property.key) = - let open Ast.Pattern.Object.Property in - match key with - | StringLiteral lit -> - id (this#pattern_object_property_string_literal_key ?kind) lit key (fun lit' -> - StringLiteral lit' - ) - | NumberLiteral lit -> - id (this#pattern_object_property_number_literal_key ?kind) lit key (fun lit' -> - NumberLiteral lit' - ) - | BigIntLiteral lit -> - id (this#pattern_object_property_bigint_literal_key ?kind) lit key (fun lit' -> - BigIntLiteral lit' - ) - | Identifier identifier -> - id (this#pattern_object_property_identifier_key ?kind) identifier key (fun id' -> - Identifier id' - ) - | Computed expr -> - id (this#pattern_object_property_computed_key ?kind) expr key (fun expr' -> Computed expr') - - method pattern_object_property_string_literal_key - ?kind (literal : 'loc * 'loc Ast.StringLiteral.t) = - let (loc, key) = literal in - id_loc (this#pattern_string_literal ?kind) loc key literal (fun key' -> (loc, key')) - - method pattern_object_property_number_literal_key - ?kind (literal : 'loc * 'loc Ast.NumberLiteral.t) = - let (loc, key) = literal in - id_loc (this#pattern_number_literal ?kind) loc key literal (fun key' -> (loc, key')) - - method pattern_object_property_bigint_literal_key - ?kind (literal : 'loc * 'loc Ast.BigIntLiteral.t) = - let (loc, key) = literal in - id_loc (this#pattern_bigint_literal ?kind) loc key literal (fun key' -> (loc, key')) - - method pattern_object_property_identifier_key ?kind (key : ('loc, 'loc) Ast.Identifier.t) = - this#pattern_identifier ?kind key - - method pattern_object_property_computed_key ?kind (key : ('loc, 'loc) Ast.ComputedKey.t) = - ignore kind; - this#computed_key key - - method pattern_object_rest_property ?kind (prop : ('loc, 'loc) Ast.Pattern.RestElement.t) = - let open Ast.Pattern.RestElement in - let (loc, { argument; comments }) = prop in - let argument' = this#pattern_object_rest_property_pattern ?kind argument in - let comments' = this#syntax_opt comments in - if argument' == argument && comments == comments' then - prop - else - (loc, { argument = argument'; comments = comments' }) - - method pattern_object_property_pattern ?kind (expr : ('loc, 'loc) Ast.Pattern.t) = - this#pattern ?kind expr - - method pattern_object_rest_property_pattern ?kind (expr : ('loc, 'loc) Ast.Pattern.t) = - this#pattern ?kind expr - - method pattern_array_e ?kind (e : ('loc, 'loc) Ast.Pattern.Array.element) = - let open Ast.Pattern.Array in - match e with - | Hole _ -> e - | Element elem -> id (this#pattern_array_element ?kind) elem e (fun elem -> Element elem) - | RestElement elem -> - id (this#pattern_array_rest_element ?kind) elem e (fun elem -> RestElement elem) - - method pattern_array_element ?kind (elem : ('loc, 'loc) Ast.Pattern.Array.Element.t) = - let open Ast.Pattern.Array.Element in - let (loc, { argument; default }) = elem in - let argument' = this#pattern_array_element_pattern ?kind argument in - let default' = this#default_opt default in - if argument == argument' && default == default' then - elem - else - (loc, { argument = argument'; default = default' }) - - method pattern_array_element_pattern ?kind (patt : ('loc, 'loc) Ast.Pattern.t) = - this#pattern ?kind patt - - method pattern_array_rest_element ?kind (elem : ('loc, 'loc) Ast.Pattern.RestElement.t) = - let open Ast.Pattern.RestElement in - let (loc, { argument; comments }) = elem in - let argument' = this#pattern_array_rest_element_pattern ?kind argument in - let comments' = this#syntax_opt comments in - if argument' == argument && comments == comments' then - elem - else - (loc, { argument = argument'; comments = comments' }) - - method pattern_array_rest_element_pattern ?kind (expr : ('loc, 'loc) Ast.Pattern.t) = - this#pattern ?kind expr - - method pattern_expression (expr : ('loc, 'loc) Ast.Expression.t) = this#expression expr - - method predicate (pred : ('loc, 'loc) Ast.Type.Predicate.t) = - let open Ast.Type.Predicate in - let (loc, { kind; comments }) = pred in - let kind' = - match kind with - | Inferred -> kind - | Declared expr -> id this#expression expr kind (fun expr' -> Declared expr') - in - let comments' = this#syntax_opt comments in - if kind == kind' && comments == comments' then - pred - else - (loc, { kind = kind'; comments = comments' }) - - method predicate_expression (expr : ('loc, 'loc) Ast.Expression.t) = this#expression expr - - method type_guard_annotation - (type_guard_annotation : ('loc, 'loc) Ast.Type.type_guard_annotation) = - let (loc, type_guard) = type_guard_annotation in - let type_guard' = this#type_guard type_guard in - if type_guard' == type_guard then - type_guard_annotation - else - (loc, type_guard') - - method type_guard (guard : ('loc, 'loc) Ast.Type.TypeGuard.t) = - let open Ast.Type.TypeGuard in - let (loc, { kind; guard = (x, t); comments }) = guard in - let x' = this#identifier x in - let t' = map_opt this#type_ t in - let comments' = this#syntax_opt comments in - if x' == x && t' == t && comments' == comments then - guard - else - (loc, { kind; guard = (x', t'); comments = comments' }) - - method function_rest_param (expr : ('loc, 'loc) Ast.Function.RestParam.t) = - let open Ast.Function.RestParam in - let (loc, { argument; comments }) = expr in - let argument' = this#function_param_pattern argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - (loc, { argument = argument'; comments = comments' }) - - method record_declaration _loc (record : ('loc, 'loc) Ast.Statement.RecordDeclaration.t) = - let open Ast.Statement.RecordDeclaration in - let { id; tparams; implements; body; comments; invalid_syntax } = record in - let id' = this#pattern_identifier ~kind:Ast.Variable.Const id in - let tparams' = map_opt (this#type_params ~kind:RecordTP) tparams in - let implements' = map_opt this#class_implements implements in - let body' = this#record_body body in - let comments' = this#syntax_opt comments in - if - id == id' - && tparams == tparams' - && implements == implements' - && body == body' - && comments == comments' - then - record - else - { - id = id'; - tparams = tparams'; - implements = implements'; - body = body'; - comments = comments'; - invalid_syntax; - } - - method record_body (record_body : ('loc, 'loc) Ast.Statement.RecordDeclaration.Body.t) = - let open Ast.Statement.RecordDeclaration.Body in - let (loc, { body; comments }) = record_body in - let body' = map_list this#record_element body in - let comments' = this#syntax_opt comments in - if body == body' && comments == comments' then - record_body - else - (loc, { body = body'; comments = comments' }) - - method record_element (element : ('loc, 'loc) Ast.Statement.RecordDeclaration.Body.element) = - let open Ast.Statement.RecordDeclaration.Body in - match element with - | Method (loc, meth) -> - id_loc this#class_method loc meth element (fun meth -> Method (loc, meth)) - | Property (loc, prop) -> - id_loc this#record_property loc prop element (fun prop -> Property (loc, prop)) - | StaticProperty (loc, prop) -> - id_loc this#record_static_property loc prop element (fun prop -> StaticProperty (loc, prop)) - - method record_property _loc (prop : ('loc, 'loc) Ast.Statement.RecordDeclaration.Property.t') = - let open Ast.Statement.RecordDeclaration.Property in - let { key; annot; default_value; comments; invalid_syntax } = prop in - let key' = this#object_key key in - let annot' = this#type_annotation annot in - let default_value' = map_opt this#expression default_value in - let comments' = this#syntax_opt comments in - if key' == key && annot' == annot && default_value' == default_value && comments' == comments - then - prop - else - { - key = key'; - annot = annot'; - default_value = default_value'; - comments = comments'; - invalid_syntax; - } - - method record_static_property - _loc (prop : ('loc, 'loc) Ast.Statement.RecordDeclaration.StaticProperty.t') = - let open Ast.Statement.RecordDeclaration.StaticProperty in - let { key; annot; value; comments; invalid_syntax } = prop in - let key' = this#object_key key in - let annot' = this#type_annotation annot in - let value' = this#expression value in - let comments' = this#syntax_opt comments in - if key' == key && annot' == annot && value' == value && comments' == comments then - prop - else - { key = key'; annot = annot'; value = value'; comments = comments'; invalid_syntax } - - method return _loc (stmt : ('loc, 'loc) Ast.Statement.Return.t) = - let open Ast.Statement.Return in - let { argument; comments; return_out } = stmt in - let argument' = map_opt this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - stmt - else - { argument = argument'; comments = comments'; return_out } - - method sequence _loc (expr : ('loc, 'loc) Ast.Expression.Sequence.t) = - let open Ast.Expression.Sequence in - let { expressions; comments } = expr in - let expressions' = map_list this#expression expressions in - let comments' = this#syntax_opt comments in - if expressions == expressions' && comments == comments' then - expr - else - { expressions = expressions'; comments = comments' } - - method toplevel_statement_list (stmts : ('loc, 'loc) Ast.Statement.t list) = - this#statement_list stmts - - method statement_list (stmts : ('loc, 'loc) Ast.Statement.t list) = - map_list_multiple this#statement_fork_point stmts - - method statement_fork_point (stmt : ('loc, 'loc) Ast.Statement.t) = [this#statement stmt] - - method spread_element (expr : ('loc, 'loc) Ast.Expression.SpreadElement.t) = - let open Ast.Expression.SpreadElement in - let (loc, { argument; comments }) = expr in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - (loc, { argument = argument'; comments = comments' }) - - method spread_property (expr : ('loc, 'loc) Ast.Expression.Object.SpreadProperty.t) = - let open Ast.Expression.Object.SpreadProperty in - let (loc, { argument; comments }) = expr in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - (loc, { argument = argument'; comments = comments' }) - - method super_expression _loc (expr : 'loc Ast.Expression.Super.t) = - let open Ast.Expression.Super in - let { comments } = expr in - let comments' = this#syntax_opt comments in - if comments == comments' then - expr - else - { comments = comments' } - - method switch _loc (switch : ('loc, 'loc) Ast.Statement.Switch.t) = - let open Ast.Statement.Switch in - let { discriminant; cases; comments; exhaustive_out } = switch in - let discriminant' = this#expression discriminant in - let cases' = map_list this#switch_case cases in - let comments' = this#syntax_opt comments in - if discriminant == discriminant' && cases == cases' && comments == comments' then - switch - else - { discriminant = discriminant'; cases = cases'; comments = comments'; exhaustive_out } - - method switch_case (case : ('loc, 'loc) Ast.Statement.Switch.Case.t) = - let open Ast.Statement.Switch.Case in - let (loc, { test; case_test_loc; consequent; comments }) = case in - let test' = map_opt this#expression test in - let consequent' = this#statement_list consequent in - let comments' = this#syntax_opt comments in - if test == test' && consequent == consequent' && comments == comments' then - case - else - (loc, { test = test'; case_test_loc; consequent = consequent'; comments = comments' }) - - method tagged_template _loc (expr : ('loc, 'loc) Ast.Expression.TaggedTemplate.t) = - let open Ast.Expression.TaggedTemplate in - let { tag; targs; quasi; comments } = expr in - let tag' = this#expression tag in - let targs' = map_opt this#call_type_args targs in - let quasi' = map_loc this#template_literal quasi in - let comments' = this#syntax_opt comments in - if tag == tag' && targs == targs' && quasi == quasi' && comments == comments' then - expr - else - { tag = tag'; targs = targs'; quasi = quasi'; comments = comments' } - - method template_literal _loc (expr : ('loc, 'loc) Ast.Expression.TemplateLiteral.t) = - let open Ast.Expression.TemplateLiteral in - let { quasis; expressions; comments } = expr in - let quasis' = map_list this#template_literal_element quasis in - let expressions' = map_list this#expression expressions in - let comments' = this#syntax_opt comments in - if quasis == quasis' && expressions == expressions' && comments == comments' then - expr - else - { quasis = quasis'; expressions = expressions'; comments = comments' } - - (* TODO *) - method template_literal_element (elem : 'loc Ast.Expression.TemplateLiteral.Element.t) = elem - - method this_expression _loc (expr : 'loc Ast.Expression.This.t) = - let open Ast.Expression.This in - let { comments } = expr in - let comments' = this#syntax_opt comments in - if comments == comments' then - expr - else - { comments = comments' } - - method throw _loc (stmt : ('loc, 'loc) Ast.Statement.Throw.t) = - let open Ast.Statement.Throw in - let { argument; comments } = stmt in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - stmt - else - { argument = argument'; comments = comments' } - - method try_catch _loc (stmt : ('loc, 'loc) Ast.Statement.Try.t) = - let open Ast.Statement.Try in - let { block; handler; finalizer; comments } = stmt in - let block' = map_loc this#block block in - let handler' = - match handler with - | Some (loc, clause) -> - id_loc this#catch_clause loc clause handler (fun clause -> Some (loc, clause)) - | None -> handler - in - let finalizer' = - match finalizer with - | Some (finalizer_loc, block) -> - id_loc this#block finalizer_loc block finalizer (fun block -> Some (finalizer_loc, block)) - | None -> finalizer - in - let comments' = this#syntax_opt comments in - if block == block' && handler == handler' && finalizer == finalizer' && comments == comments' - then - stmt - else - { block = block'; handler = handler'; finalizer = finalizer'; comments = comments' } - - method type_cast _loc (expr : ('loc, 'loc) Ast.Expression.TypeCast.t) = - let open Ast.Expression.TypeCast in - let { expression; annot; comments } = expr in - let expression' = this#expression expression in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if expression' == expression && annot' == annot && comments' == comments then - expr - else - { expression = expression'; annot = annot'; comments = comments' } - - method ts_satisfies _loc (expr : ('loc, 'loc) Ast.Expression.TSSatisfies.t) = - let open Ast.Expression.TSSatisfies in - let { expression; annot; comments } = expr in - let expression' = this#expression expression in - let annot' = this#type_annotation annot in - let comments' = this#syntax_opt comments in - if expression' == expression && annot' == annot && comments' == comments then - expr - else - { expression = expression'; annot = annot'; comments = comments' } - - method unary_expression _loc (expr : ('loc, 'loc) Flow_ast.Expression.Unary.t) = - let open Flow_ast.Expression.Unary in - let { argument; operator = _; comments } = expr in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - { expr with argument = argument'; comments = comments' } - - method update_expression _loc (expr : ('loc, 'loc) Ast.Expression.Update.t) = - let open Ast.Expression.Update in - let { argument; operator = _; prefix = _; comments } = expr in - let argument' = this#expression argument in - let comments' = this#syntax_opt comments in - if argument == argument' && comments == comments' then - expr - else - { expr with argument = argument'; comments = comments' } - - method variable_declaration _loc (decl : ('loc, 'loc) Ast.Statement.VariableDeclaration.t) = - let open Ast.Statement.VariableDeclaration in - let { declarations; kind; comments } = decl in - let decls' = map_list (this#variable_declarator ~kind) declarations in - let comments' = this#syntax_opt comments in - if declarations == decls' && comments == comments' then - decl - else - { declarations = decls'; kind; comments = comments' } - - method variable_declarator - ~kind (decl : ('loc, 'loc) Ast.Statement.VariableDeclaration.Declarator.t) = - let open Ast.Statement.VariableDeclaration.Declarator in - let (loc, { id; init }) = decl in - let id' = this#variable_declarator_pattern ~kind id in - let init' = map_opt this#expression init in - if id == id' && init == init' then - decl - else - (loc, { id = id'; init = init' }) - - method while_ _loc (stuff : ('loc, 'loc) Ast.Statement.While.t) = - let open Ast.Statement.While in - let { test; body; comments } = stuff in - let test' = this#predicate_expression test in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if test == test' && body == body' && comments == comments' then - stuff - else - { test = test'; body = body'; comments = comments' } - - method with_ _loc (stuff : ('loc, 'loc) Ast.Statement.With.t) = - let open Ast.Statement.With in - let { _object; body; comments } = stuff in - let _object' = this#expression _object in - let body' = this#statement body in - let comments' = this#syntax_opt comments in - if _object == _object' && body == body' && comments == comments' then - stuff - else - { _object = _object'; body = body'; comments = comments' } - - method type_alias _loc (stuff : ('loc, 'loc) Ast.Statement.TypeAlias.t) = - let open Ast.Statement.TypeAlias in - let { id; tparams; right; comments } = stuff in - let id' = this#binding_type_identifier id in - let tparams' = map_opt (this#type_params ~kind:TypeAliasTP) tparams in - let right' = this#type_ right in - let comments' = this#syntax_opt comments in - if id == id' && right == right' && tparams == tparams' && comments == comments' then - stuff - else - { id = id'; tparams = tparams'; right = right'; comments = comments' } - - method yield _loc (expr : ('loc, 'loc) Ast.Expression.Yield.t) = - let open Ast.Expression.Yield in - let { argument; delegate; comments; result_out } = expr in - let argument' = map_opt this#expression argument in - let comments' = this#syntax_opt comments in - if comments == comments' && argument == argument' then - expr - else - { argument = argument'; delegate; comments = comments'; result_out } - end - -let fold_program (mappers : 'a mapper list) ast = - List.fold_left (fun ast (m : 'a mapper) -> m#program ast) ast mappers diff --git a/compiler/flow_parser/parser/object_parser.ml b/compiler/flow_parser/parser/object_parser.ml index cde4d5f29dd..5d66fb5c944 100644 --- a/compiler/flow_parser/parser/object_parser.ml +++ b/compiler/flow_parser/parser/object_parser.ml @@ -30,7 +30,7 @@ module Object else trailing_and_remover_after_last_loc env in - remove_trailing expression (fun remover expression -> remover#expression expression) + remove_trailing expression in let decorator env = let leading = Peek.comments env in @@ -202,7 +202,6 @@ module Object let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) in let params = @@ -581,7 +580,7 @@ module Object expr else let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing expr (fun remover expr -> remover#expression expr) + remove_trailing expr in let targs = Type.type_args env in { Class.Extends.expr; targs; comments = Flow_ast_utils.mk_comments_opt ~leading () } @@ -595,7 +594,7 @@ module Object let (loc, extends) = class_extends ~leading env in let { remove_trailing; _ } = trailing_and_remover env in Some - (loc, remove_trailing extends (fun remover extends -> remover#class_extends loc extends)) + (loc, remove_trailing extends) else None in @@ -784,7 +783,6 @@ module Object let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) in let params = @@ -955,7 +953,7 @@ module Object match Peek.token env with | T_EOF | T_RCURLY -> - { trailing = []; remove_trailing = (fun x _ -> x) } + { trailing = []; remove_trailing = (fun x -> x) } | _ when Peek.is_line_terminator env -> Comment_attachment.trailing_and_remover_after_last_line env | _ -> Comment_attachment.trailing_and_remover_after_last_loc env @@ -968,18 +966,18 @@ module Object ( key, annot, Class.Property.Initialized - (remover.remove_trailing expr (fun remover expr -> remover#expression expr)) + (remover.remove_trailing expr) ) (* prop: annot *) | (Ast.Type.Available annot, _) -> ( key, Ast.Type.Available - (remover.remove_trailing annot (fun remover annot -> remover#type_annotation annot)), + (remover.remove_trailing annot), value ) (* prop *) | _ -> - (remover.remove_trailing key (fun remover key -> remover#object_key key), annot, value) + (remover.remove_trailing key, annot, value) in (key, annot, value, []) in @@ -1158,7 +1156,6 @@ module Object let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) in let params = @@ -1269,7 +1266,6 @@ module Object let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTP (Type.type_params env) in let params = @@ -1890,7 +1886,7 @@ module Object | _ when Peek.is_identifier env -> let id = Parse.identifier tmp_env in let { remove_trailing; _ } = trailing_and_remover env in - let id = remove_trailing id (fun remover id -> remover#identifier id) in + let id = remove_trailing id in Some id | _ -> (* error, but don't consume a token like Parse.identifier does. this helps @@ -1905,9 +1901,7 @@ module Object | Some tparams -> let { remove_trailing; _ } = trailing_and_remover env in Some - (remove_trailing tparams (fun remover tparams -> - remover#type_params ~kind:Flow_ast_mapper.ClassTP tparams - ) + (remove_trailing tparams ) in let (extends, implements) = class_heritage env in @@ -2140,9 +2134,7 @@ module Object | Some tparams -> let { remove_trailing; _ } = trailing_and_remover env in Some - (remove_trailing tparams (fun remover tparams -> - remover#type_params ~kind:Flow_ast_mapper.RecordTP tparams - ) + (remove_trailing tparams ) in let invalid_syntax = diff --git a/compiler/flow_parser/parser/statement_parser.ml b/compiler/flow_parser/parser/statement_parser.ml index 058f4818825..ed58962f276 100644 --- a/compiler/flow_parser/parser/statement_parser.ml +++ b/compiler/flow_parser/parser/statement_parser.ml @@ -64,7 +64,7 @@ module Statement match Peek.token env with | T_EOF | T_RCURLY -> - Implicit { trailing = Eat.trailing_comments env; remove_trailing = (fun x _ -> x) } + Implicit { trailing = Eat.trailing_comments env; remove_trailing = (fun x -> x) } | T_SEMICOLON -> Eat.token env; (match Peek.token env with @@ -90,26 +90,15 @@ module Statement match Peek.token env with | T_EOF | T_RCURLY -> - { trailing = Eat.trailing_comments env; remove_trailing = (fun x _ -> x) } + { trailing = Eat.trailing_comments env; remove_trailing = (fun x -> x) } | _ when Peek.is_line_terminator env -> Comment_attachment.trailing_and_remover_after_last_line env | _ -> Comment_attachment.trailing_and_remover_after_last_loc env - let variable_declaration_end ~kind env declarations = + let variable_declaration_end env declarations = match semicolon env with | Explicit comments -> (comments, declarations) - | Implicit { remove_trailing; _ } -> - (* Remove trailing comments from the last declarator *) - let declarations = - match List.rev declarations with - | [] -> [] - | decl :: decls -> - let decl' = - remove_trailing decl (fun remover decl -> remover#variable_declarator ~kind decl) - in - List.rev (decl' :: decls) - in - ([], declarations) + | Implicit _ -> ([], declarations) let rec empty env = let loc = Peek.loc env in @@ -144,7 +133,7 @@ module Statement | (Implicit { trailing; _ }, None) -> (trailing, label) | (Implicit { remove_trailing; _ }, Some label) -> - ([], Some (remove_trailing label (fun remover label -> remover#identifier label))) + ([], Some (remove_trailing label)) in (label, trailing)) env @@ -177,7 +166,7 @@ module Statement | (Implicit { trailing; _ }, None) -> (trailing, label) | (Implicit { remove_trailing; _ }, Some label) -> - ([], Some (remove_trailing label (fun remover label -> remover#identifier label))) + ([], Some (remove_trailing label)) in (label, trailing)) env @@ -512,7 +501,7 @@ module Statement | (Implicit { trailing = comments; _ }, None) -> (trailing @ comments, argument) | (Implicit { remove_trailing; _ }, Some arg) -> - (trailing, Some (remove_trailing arg (fun remover arg -> remover#expression arg))) + (trailing, Some (remove_trailing arg)) in Statement.Return { @@ -675,7 +664,7 @@ module Statement match semicolon env with | Explicit trailing -> (trailing, argument) | Implicit { remove_trailing; _ } -> - ([], remove_trailing argument (fun remover arg -> remover#expression arg)) + ([], remove_trailing argument) in let open Statement in Throw { Throw.argument; comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () } @@ -718,7 +707,7 @@ module Statement let body = if Peek.token env <> T_FINALLY then let { remove_trailing; _ } = statement_end_trailing_comments env in - remove_trailing body (fun remover (loc, body) -> (loc, remover#block loc body)) + remove_trailing body else body in @@ -738,7 +727,7 @@ module Statement Expect.token env T_FINALLY; let (loc, body) = Parse.block_body env in let { remove_trailing; _ } = statement_end_trailing_comments env in - let body = remove_trailing body (fun remover body -> remover#block loc body) in + let body = remove_trailing body in Some (loc, body) | _ -> None in @@ -759,7 +748,7 @@ module Statement with_loc (fun env -> let kind = Variable.Var in let (declarations, leading, errs) = Declaration.var env in - let (trailing, declarations) = variable_declaration_end ~kind env declarations in + let (trailing, declarations) = variable_declaration_end env declarations in errs |> List.iter (error_at env); Statement.VariableDeclaration { @@ -773,7 +762,7 @@ module Statement with_loc (fun env -> let kind = Variable.Const in let (declarations, leading, errs) = Declaration.const env in - let (trailing, declarations) = variable_declaration_end ~kind env declarations in + let (trailing, declarations) = variable_declaration_end env declarations in errs |> List.iter (error_at env); Statement.VariableDeclaration { @@ -787,7 +776,7 @@ module Statement with_loc (fun env -> let kind = Variable.Let in let (declarations, leading, errs) = Declaration.let_ env in - let (trailing, declarations) = variable_declaration_end ~kind env declarations in + let (trailing, declarations) = variable_declaration_end env declarations in errs |> List.iter (error_at env); Statement.VariableDeclaration { @@ -841,7 +830,7 @@ module Statement and block env = let (loc, block) = Parse.block_body env in let { remove_trailing; _ } = statement_end_trailing_comments env in - let block = remove_trailing block (fun remover block -> remover#block loc block) in + let block = remove_trailing block in (loc, Statement.Block block) and maybe_labeled = @@ -869,7 +858,7 @@ module Statement match semicolon ~expected:"the end of an expression statement (`;`)" env with | Explicit comments -> (comments, expression) | Implicit { remove_trailing; _ } -> - ([], remove_trailing expression (fun remover expr -> remover#expression expr)) + ([], remove_trailing expression) in let open Statement in Expression @@ -892,7 +881,7 @@ module Statement match semicolon ~expected:"the end of an expression statement (`;`)" env with | Explicit comments -> (comments, expression) | Implicit { remove_trailing; _ } -> - ([], remove_trailing expression (fun remover expr -> remover#expression expr)) + ([], remove_trailing expression) in let directive = if allow_directive env then @@ -937,7 +926,7 @@ module Statement match semicolon env with | Explicit comments -> (comments, right) | Implicit { remove_trailing; _ } -> - ([], remove_trailing right (fun remover right -> remover#type_ right)) + ([], remove_trailing right) in { @@ -1052,7 +1041,7 @@ module Statement lower_bound, upper_bound, legacy_upper_bound, - Some (remove_trailing impl (fun remover impl -> remover#type_ impl)) + Some (remove_trailing impl) ) (* opaque type Foo: U *) | (Implicit { remove_trailing; _ }, _, _, Some upper_bound, _, None) -> @@ -1060,7 +1049,7 @@ module Statement id, tparams, lower_bound, - Some (remove_trailing upper_bound (fun remover t -> remover#type_ t)), + Some (remove_trailing upper_bound), legacy_upper_bound, None ) @@ -1070,7 +1059,7 @@ module Statement tparams, lower_bound, upper_bound, - Some (remove_trailing legacy_upper_bound (fun remover t -> remover#type_ t)), + Some (remove_trailing legacy_upper_bound), None ) (* opaque type Foo super Super *) @@ -1078,7 +1067,7 @@ module Statement ( [], id, tparams, - Some (remove_trailing lower_bound (fun remover t -> remover#type_ t)), + Some (remove_trailing lower_bound), upper_bound, legacy_upper_bound, None @@ -1088,9 +1077,7 @@ module Statement ( [], id, Some - (remove_trailing tparams (fun remover tparams -> - remover#type_params ~kind:Flow_ast_mapper.OpaqueTypeTP tparams - ) + (remove_trailing tparams ), None, None, @@ -1100,7 +1087,7 @@ module Statement (* declare opaque type Foo *) | (Implicit { remove_trailing; _ }, None, None, None, None, None) -> ( [], - remove_trailing id (fun remover id -> remover#identifier id), + remove_trailing id, None, None, None, @@ -1151,11 +1138,11 @@ module Statement if Peek.token env = T_EXTENDS then tparams else - type_params_remove_trailing env ~kind:Flow_ast_mapper.InterfaceTP tparams + type_params_remove_trailing env tparams in let (extends, body) = Type.interface_helper env in let { remove_trailing; _ } = statement_end_trailing_comments env in - let body = remove_trailing body (fun remover (loc, body) -> (loc, remover#object_type body)) in + let body = remove_trailing body in { Statement.Interface.id; @@ -1220,7 +1207,7 @@ module Statement let tparams = let tparams = Type.type_params env in match Peek.token env with - | T_LCURLY -> type_params_remove_trailing env ~kind:Flow_ast_mapper.DeclareClassTP tparams + | T_LCURLY -> type_params_remove_trailing env tparams | _ -> tparams in let extends = @@ -1251,7 +1238,7 @@ module Statement let body = Type._object ~is_class:true env in let { remove_trailing; _ } = statement_end_trailing_comments env in let body = - remove_trailing body (fun remover (loc, body) -> (loc, remover#object_type body)) + remove_trailing body in let comments = Flow_ast_utils.mk_comments_opt ~leading () in Statement.DeclareClass.{ id; tparams; body; extends; mixins; implements; abstract; comments } @@ -1276,7 +1263,7 @@ module Statement (Parse.identifier ~restricted_error:Parse_error.StrictFunctionName env) in let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.DeclareComponentTP (Type.type_params env) + type_params_remove_trailing env (Type.type_params env) in let params = Declaration.component_params env in let (params, renders) = @@ -1294,7 +1281,7 @@ module Statement | Explicit comments -> (comments, renders) | Implicit { remove_trailing; _ } -> ( [], - remove_trailing renders (fun remover annot -> remover#component_renders_annotation annot) + remove_trailing renders ) in { @@ -1403,7 +1390,6 @@ module Statement let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.DeclareFunctionTP (Type.type_params env) in let params = Type.function_param_list env in @@ -1429,9 +1415,9 @@ module Statement match (semicolon env, predicate) with | (Explicit comments, _) -> (comments, annot, predicate) | (Implicit { remove_trailing; _ }, None) -> - ([], remove_trailing annot (fun remover annot -> remover#type_ annot), None) + ([], remove_trailing annot, None) | (Implicit { remove_trailing; _ }, Some pred) -> - ([], annot, Some (remove_trailing pred (fun remover pred -> remover#predicate pred))) + ([], annot, Some (remove_trailing pred)) in let annot = (fst annot, annot) in @@ -1468,7 +1454,7 @@ module Statement | Ast.Variable.Const -> Expect.token env T_CONST); let (declarations, errs) = Declaration.variable_declaration_list env in List.iter (error_at env) errs; - let (trailing, declarations) = variable_declaration_end ~kind env declarations in + let (trailing, declarations) = variable_declaration_end env declarations in { Statement.DeclareVariable.declarations; kind; @@ -1652,7 +1638,7 @@ module Statement match semicolon env with | Explicit trailing -> (annot, trailing) | Implicit { remove_trailing; _ } -> - (remove_trailing annot (fun remover annot -> remover#type_annotation annot), []) + (remove_trailing annot, []) in let comments = Flow_ast_utils.mk_comments_opt ~leading ~trailing () in Statement.DeclareModuleExports { Statement.DeclareModuleExports.annot; comments } @@ -1711,7 +1697,7 @@ module Statement match semicolon env with | Explicit trailing -> ((source_loc, source), trailing) | Implicit { remove_trailing; _ } -> - ( (source_loc, remove_trailing source (fun remover source -> remover#string_literal source)), + ( (source_loc, remove_trailing source), [] ) @@ -2059,7 +2045,7 @@ module Statement match semicolon env with | Explicit trailing -> (expr, trailing) | Implicit { remove_trailing; _ } -> - (remove_trailing expr (fun remover expr -> remover#expression expr), []) + (remove_trailing expr, []) in (Expression expr, trailing) in @@ -2496,7 +2482,7 @@ module Statement match semicolon env with | Explicit trailing -> (type_, trailing) | Implicit { remove_trailing; _ } -> - (remove_trailing type_ (fun remover type_ -> remover#type_ type_), []) + (remove_trailing type_, []) in (Some (DefaultType type_), trailing) in @@ -3060,9 +3046,7 @@ module Statement | Explicit trailing -> (trailing, source) | Implicit { remove_trailing; _ } -> ( [], - remove_trailing source (fun remover (loc, source) -> - (loc, remover#string_literal source) - ) + remove_trailing source ) in let with_specifiers import_kind env leading = diff --git a/compiler/flow_parser/parser/type_parser.ml b/compiler/flow_parser/parser/type_parser.ml index 6389256ade3..1f8ea5ca08c 100644 --- a/compiler/flow_parser/parser/type_parser.ml +++ b/compiler/flow_parser/parser/type_parser.ml @@ -475,7 +475,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct let leading = Peek.comments env in Expect.identifier env "component"; let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.ComponentTypeTP (type_params env) + type_params_remove_trailing env (type_params env) in let params = component_param_list env in let (params, renders) = @@ -1298,7 +1298,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct and _function env = let start_loc = Peek.loc env in let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTP (type_params env) + type_params_remove_trailing env (type_params env) in let params = function_param_list env in function_with_params ~effect_:Function.Arbitrary env start_loc tparams params @@ -1317,7 +1317,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct let start_loc = Peek.loc env in Eat.token env; let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTP (type_params env) + type_params_remove_trailing env (type_params env) in let params = function_param_list env in function_with_params ~effect_:Function.Hook env start_loc tparams params @@ -1327,7 +1327,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct if abstract_ then Eat.token env; Eat.token env; let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTP (type_params env) + type_params_remove_trailing env (type_params env) in let params = function_param_list env in (match params with @@ -1420,7 +1420,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct env start_loc static ~abstract ~override key ~optional ~ts_accessibility ~leading = let key = object_key_remove_trailing env key in let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTypeTP (type_params env) + type_params_remove_trailing env (type_params env) in let value = methodish env start_loc tparams in let value = (fst value, Type.Function (snd value)) in @@ -1451,7 +1451,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct (fun env -> let start_loc = Peek.loc env in let tparams = - type_params_remove_trailing env ~kind:Flow_ast_mapper.FunctionTypeTP (type_params env) + type_params_remove_trailing env (type_params env) in let value = methodish env start_loc tparams in Type.Object.CallProperty. @@ -1693,7 +1693,6 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct let tparams = type_params_remove_trailing env - ~kind:Flow_ast_mapper.FunctionTypeTP (type_params env) in let value = @@ -2700,7 +2699,7 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct id else let { remove_trailing; _ } = trailing_and_remover env in - remove_trailing id (fun remover id -> remover#generic_identifier_type id) + remove_trailing id in let targs = type_args env in { Type.Generic.id; targs; comments = None }