Skip to content

Commit 7e2b057

Browse files
committedNov 10, 2022
upgrade flow parser
1 parent 9eac232 commit 7e2b057

35 files changed

+18864
-139749
lines changed
 

‎jscomp/js_parser/comment_attachment.ml

+35-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(*
2-
* Copyright (c) Facebook, Inc. and its affiliates.
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
33
*
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
@@ -25,6 +25,7 @@ let id_list_last (map : 'a -> 'a) (lst : 'a list) : 'a list =
2525
else
2626
List.rev (hd' :: tl)
2727

28+
(* Mapper that removes all trailing comments that appear after a given position in an AST node *)
2829
class ['loc] trailing_comments_remover ~after_pos =
2930
object (this)
3031
inherit ['loc] Flow_ast_mapper.mapper
@@ -33,11 +34,7 @@ class ['loc] trailing_comments_remover ~after_pos =
3334
let open Syntax in
3435
let { trailing; _ } = comments in
3536
let trailing' =
36-
List.filter
37-
(fun (loc, _) ->
38-
let open Loc in
39-
pos_cmp loc.start after_pos < 0)
40-
trailing
37+
List.filter (fun (loc, _) -> Loc.(pos_cmp loc.start after_pos < 0)) trailing
4138
in
4239
if List.length trailing = List.length trailing' then
4340
comments
@@ -93,13 +90,13 @@ class ['loc] trailing_comments_remover ~after_pos =
9390
let open Ast.Expression.ArgList in
9491
let (loc, { arguments; comments }) = arg_list in
9592
id this#syntax_opt comments arg_list (fun comments' ->
96-
(loc, { arguments; comments = comments' }))
93+
(loc, { arguments; comments = comments' })
94+
)
9795

9896
method! call_type_args targs =
9997
let open Ast.Expression.CallTypeArgs in
10098
let (loc, { arguments; comments }) = targs in
101-
id this#syntax_opt comments targs (fun comments' ->
102-
(loc, { arguments; comments = comments' }))
99+
id this#syntax_opt comments targs (fun comments' -> (loc, { arguments; comments = comments' }))
103100

104101
method! class_ _loc cls =
105102
let open Ast.Class in
@@ -115,7 +112,8 @@ class ['loc] trailing_comments_remover ~after_pos =
115112
let open Ast.Class.Body in
116113
let (loc, { body = _body; comments }) = body in
117114
id this#syntax_opt comments body (fun comments' ->
118-
(loc, { body = _body; comments = comments' }))
115+
(loc, { body = _body; comments = comments' })
116+
)
119117

120118
method! class_extends _loc extends =
121119
let open Ast.Class.Extends in
@@ -129,7 +127,8 @@ class ['loc] trailing_comments_remover ~after_pos =
129127
let open Ast.Class.Implements in
130128
let (loc, { interfaces; comments }) = implements in
131129
id (id_list_last this#class_implements_interface) interfaces implements (fun interfaces' ->
132-
(loc, { interfaces = interfaces'; comments }))
130+
(loc, { interfaces = interfaces'; comments })
131+
)
133132

134133
method! class_implements_interface interface =
135134
let open Ast.Class.Implements.Interface in
@@ -138,7 +137,8 @@ class ['loc] trailing_comments_remover ~after_pos =
138137
id this#identifier id_ interface (fun id' -> (loc, { id = id'; targs }))
139138
else
140139
id (map_opt this#type_args) targs interface (fun targs' ->
141-
(loc, { id = id_; targs = targs' }))
140+
(loc, { id = id_; targs = targs' })
141+
)
142142

143143
method! computed_key key =
144144
let open Ast.ComputedKey in
@@ -169,7 +169,8 @@ class ['loc] trailing_comments_remover ~after_pos =
169169
let open Ast.Function.Params in
170170
let { comments; _ } = params in
171171
id this#syntax_opt comments (loc, params) (fun comments' ->
172-
(loc, { params with comments = comments' }))
172+
(loc, { params with comments = comments' })
173+
)
173174

174175
method! function_type _loc func =
175176
let open Ast.Type.Function in
@@ -245,18 +246,21 @@ class ['loc] trailing_comments_remover ~after_pos =
245246
let { callee; targs; arguments; comments } = expr in
246247
let comments' = this#syntax_opt comments in
247248
match (targs, arguments) with
249+
(* new Callee<T>() *)
248250
| (_, Some _) ->
249251
let arguments' = map_opt this#call_arguments arguments in
250252
if arguments == arguments' && comments == comments' then
251253
expr
252254
else
253255
{ expr with arguments = arguments'; comments = comments' }
256+
(* new Callee<T> *)
254257
| (Some _, _) ->
255258
let targs' = map_opt this#call_type_args targs in
256259
if targs == targs' && comments == comments' then
257260
expr
258261
else
259262
{ expr with targs = targs'; comments = comments' }
263+
(* new Callee *)
260264
| (None, None) ->
261265
let callee' = this#expression callee in
262266
if callee == callee' && comments == comments' then
@@ -338,7 +342,8 @@ class ['loc] trailing_comments_remover ~after_pos =
338342
match init with
339343
| None ->
340344
id (this#variable_declarator_pattern ~kind) ident decl (fun ident' ->
341-
(loc, { id = ident'; init }))
345+
(loc, { id = ident'; init })
346+
)
342347
| Some init ->
343348
id this#expression init decl (fun init' -> (loc, { id = ident; init = Some init' }))
344349
end
@@ -348,6 +353,8 @@ type trailing_and_remover_result = {
348353
remove_trailing: 'a. 'a -> (Loc.t trailing_comments_remover -> 'a -> 'a) -> 'a;
349354
}
350355

356+
(* Returns a remover function which removes comments beginning after the previous token.
357+
No trailing comments are returned, since all comments since the last loc should be removed. *)
351358
let trailing_and_remover_after_last_loc : Parser_env.env -> trailing_and_remover_result =
352359
fun env ->
353360
let open Loc in
@@ -369,6 +376,8 @@ let trailing_and_remover_after_last_loc : Parser_env.env -> trailing_and_remover
369376
| Some remover -> f remover node);
370377
}
371378

379+
(* Consumes and returns comments on the same line as the previous token. Also returns a remover
380+
function which can be used to remove comments beginning after the previous token's line. *)
372381
let trailing_and_remover_after_last_line : Parser_env.env -> trailing_and_remover_result =
373382
fun env ->
374383
let open Loc in
@@ -448,7 +457,8 @@ let generic_type_remove_trailing env ty =
448457
let generic_type_list_remove_trailing env extends =
449458
let { remove_trailing; _ } = trailing_and_remover env in
450459
remove_trailing extends (fun remover extends ->
451-
id_list_last (map_loc remover#generic_type) extends)
460+
id_list_last (map_loc remover#generic_type) extends
461+
)
452462

453463
let class_implements_remove_trailing env implements =
454464
let { remove_trailing; _ } = trailing_and_remover env in
@@ -537,8 +547,12 @@ let statement_add_comments
537547
VariableDeclaration { s with VariableDeclaration.comments = merge_comments comments }
538548
| While ({ While.comments; _ } as s) ->
539549
While { s with While.comments = merge_comments comments }
540-
| With ({ With.comments; _ } as s) -> With { s with With.comments = merge_comments comments } )
550+
| With ({ With.comments; _ } as s) -> With { s with With.comments = merge_comments comments }
551+
)
541552

553+
(* Collects the first leading and last trailing comment on an AST node or its children.
554+
The first leading comment is the first attached comment that begins before the given node's loc,
555+
and the last trailing comment is the last attached comment that begins after the given node's loc. *)
542556
class ['loc] comment_bounds_collector ~loc =
543557
object (this)
544558
inherit ['loc] Flow_ast_mapper.mapper
@@ -584,11 +598,14 @@ class ['loc] comment_bounds_collector ~loc =
584598
block
585599
end
586600

601+
(* Given an AST node and a function to collect all its comments, return the first leading
602+
and last trailing comment on the node. *)
587603
let comment_bounds loc node f =
588604
let collector = new comment_bounds_collector ~loc in
589605
ignore (f collector node);
590606
collector#comment_bounds
591607

608+
(* Expand node's loc to include its attached comments *)
592609
let expand_loc_with_comment_bounds loc (first_leading, last_trailing) =
593610
let open Loc in
594611
let start =
@@ -603,6 +620,7 @@ let expand_loc_with_comment_bounds loc (first_leading, last_trailing) =
603620
in
604621
btwn start _end
605622

623+
(* Remove the trailing comment bound if it is a line comment *)
606624
let comment_bounds_without_trailing_line_comment (leading, trailing) =
607625
match trailing with
608626
| Some (_, { Ast.Comment.kind = Ast.Comment.Line; _ }) -> (leading, None)
@@ -611,6 +629,7 @@ let comment_bounds_without_trailing_line_comment (leading, trailing) =
611629
let collect_without_trailing_line_comment collector =
612630
comment_bounds_without_trailing_line_comment collector#comment_bounds
613631

632+
(* Return the first leading and last trailing comment of a statement *)
614633
let statement_comment_bounds ((loc, _) as stmt : (Loc.t, Loc.t) Statement.t) :
615634
Loc.t Comment.t option * Loc.t Comment.t option =
616635
let collector = new comment_bounds_collector ~loc in

0 commit comments

Comments
 (0)