1
1
(*
2
- * Copyright (c) Facebook , Inc. and its affiliates.
2
+ * Copyright (c) Meta Platforms , Inc. and affiliates.
3
3
*
4
4
* This source code is licensed under the MIT license found in the
5
5
* 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 =
25
25
else
26
26
List. rev (hd' :: tl)
27
27
28
+ (* Mapper that removes all trailing comments that appear after a given position in an AST node *)
28
29
class ['loc ] trailing_comments_remover ~after_pos =
29
30
object (this )
30
31
inherit ['loc] Flow_ast_mapper. mapper
@@ -33,11 +34,7 @@ class ['loc] trailing_comments_remover ~after_pos =
33
34
let open Syntax in
34
35
let { trailing; _ } = comments in
35
36
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
41
38
in
42
39
if List. length trailing = List. length trailing' then
43
40
comments
@@ -93,13 +90,13 @@ class ['loc] trailing_comments_remover ~after_pos =
93
90
let open Ast.Expression.ArgList in
94
91
let (loc, { arguments; comments }) = arg_list in
95
92
id this#syntax_opt comments arg_list (fun comments' ->
96
- (loc, { arguments; comments = comments' }))
93
+ (loc, { arguments; comments = comments' })
94
+ )
97
95
98
96
method! call_type_args targs =
99
97
let open Ast.Expression.CallTypeArgs in
100
98
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' }))
103
100
104
101
method! class_ _loc cls =
105
102
let open Ast.Class in
@@ -115,7 +112,8 @@ class ['loc] trailing_comments_remover ~after_pos =
115
112
let open Ast.Class.Body in
116
113
let (loc, { body = _body; comments }) = body in
117
114
id this#syntax_opt comments body (fun comments' ->
118
- (loc, { body = _body; comments = comments' }))
115
+ (loc, { body = _body; comments = comments' })
116
+ )
119
117
120
118
method! class_extends _loc extends =
121
119
let open Ast.Class.Extends in
@@ -129,7 +127,8 @@ class ['loc] trailing_comments_remover ~after_pos =
129
127
let open Ast.Class.Implements in
130
128
let (loc, { interfaces; comments }) = implements in
131
129
id (id_list_last this#class_implements_interface) interfaces implements (fun interfaces' ->
132
- (loc, { interfaces = interfaces'; comments }))
130
+ (loc, { interfaces = interfaces'; comments })
131
+ )
133
132
134
133
method! class_implements_interface interface =
135
134
let open Ast.Class.Implements.Interface in
@@ -138,7 +137,8 @@ class ['loc] trailing_comments_remover ~after_pos =
138
137
id this#identifier id_ interface (fun id' -> (loc, { id = id'; targs }))
139
138
else
140
139
id (map_opt this#type_args) targs interface (fun targs' ->
141
- (loc, { id = id_; targs = targs' }))
140
+ (loc, { id = id_; targs = targs' })
141
+ )
142
142
143
143
method! computed_key key =
144
144
let open Ast.ComputedKey in
@@ -169,7 +169,8 @@ class ['loc] trailing_comments_remover ~after_pos =
169
169
let open Ast.Function.Params in
170
170
let { comments; _ } = params in
171
171
id this#syntax_opt comments (loc, params) (fun comments' ->
172
- (loc, { params with comments = comments' }))
172
+ (loc, { params with comments = comments' })
173
+ )
173
174
174
175
method! function_type _loc func =
175
176
let open Ast.Type.Function in
@@ -245,18 +246,21 @@ class ['loc] trailing_comments_remover ~after_pos =
245
246
let { callee; targs; arguments; comments } = expr in
246
247
let comments' = this#syntax_opt comments in
247
248
match (targs, arguments) with
249
+ (* new Callee<T>() *)
248
250
| (_ , Some _ ) ->
249
251
let arguments' = map_opt this#call_arguments arguments in
250
252
if arguments == arguments' && comments == comments' then
251
253
expr
252
254
else
253
255
{ expr with arguments = arguments'; comments = comments' }
256
+ (* new Callee<T> *)
254
257
| (Some _ , _ ) ->
255
258
let targs' = map_opt this#call_type_args targs in
256
259
if targs == targs' && comments == comments' then
257
260
expr
258
261
else
259
262
{ expr with targs = targs'; comments = comments' }
263
+ (* new Callee *)
260
264
| (None, None) ->
261
265
let callee' = this#expression callee in
262
266
if callee == callee' && comments == comments' then
@@ -338,7 +342,8 @@ class ['loc] trailing_comments_remover ~after_pos =
338
342
match init with
339
343
| None ->
340
344
id (this#variable_declarator_pattern ~kind ) ident decl (fun ident' ->
341
- (loc, { id = ident'; init }))
345
+ (loc, { id = ident'; init })
346
+ )
342
347
| Some init ->
343
348
id this#expression init decl (fun init' -> (loc, { id = ident; init = Some init' }))
344
349
end
@@ -348,6 +353,8 @@ type trailing_and_remover_result = {
348
353
remove_trailing : 'a . 'a -> (Loc .t trailing_comments_remover -> 'a -> 'a ) -> 'a ;
349
354
}
350
355
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. *)
351
358
let trailing_and_remover_after_last_loc : Parser_env.env -> trailing_and_remover_result =
352
359
fun env ->
353
360
let open Loc in
@@ -369,6 +376,8 @@ let trailing_and_remover_after_last_loc : Parser_env.env -> trailing_and_remover
369
376
| Some remover -> f remover node);
370
377
}
371
378
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. *)
372
381
let trailing_and_remover_after_last_line : Parser_env.env -> trailing_and_remover_result =
373
382
fun env ->
374
383
let open Loc in
@@ -448,7 +457,8 @@ let generic_type_remove_trailing env ty =
448
457
let generic_type_list_remove_trailing env extends =
449
458
let { remove_trailing; _ } = trailing_and_remover env in
450
459
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
+ )
452
462
453
463
let class_implements_remove_trailing env implements =
454
464
let { remove_trailing; _ } = trailing_and_remover env in
@@ -537,8 +547,12 @@ let statement_add_comments
537
547
VariableDeclaration { s with VariableDeclaration. comments = merge_comments comments }
538
548
| While ({ While. comments; _ } as s ) ->
539
549
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
+ )
541
552
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. *)
542
556
class ['loc ] comment_bounds_collector ~loc =
543
557
object (this )
544
558
inherit ['loc] Flow_ast_mapper. mapper
@@ -584,11 +598,14 @@ class ['loc] comment_bounds_collector ~loc =
584
598
block
585
599
end
586
600
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. *)
587
603
let comment_bounds loc node f =
588
604
let collector = new comment_bounds_collector ~loc in
589
605
ignore (f collector node);
590
606
collector#comment_bounds
591
607
608
+ (* Expand node's loc to include its attached comments *)
592
609
let expand_loc_with_comment_bounds loc (first_leading , last_trailing ) =
593
610
let open Loc in
594
611
let start =
@@ -603,6 +620,7 @@ let expand_loc_with_comment_bounds loc (first_leading, last_trailing) =
603
620
in
604
621
btwn start _end
605
622
623
+ (* Remove the trailing comment bound if it is a line comment *)
606
624
let comment_bounds_without_trailing_line_comment (leading , trailing ) =
607
625
match trailing with
608
626
| Some (_ , { Ast.Comment. kind = Ast.Comment. Line ; _ } ) -> (leading, None )
@@ -611,6 +629,7 @@ let comment_bounds_without_trailing_line_comment (leading, trailing) =
611
629
let collect_without_trailing_line_comment collector =
612
630
comment_bounds_without_trailing_line_comment collector#comment_bounds
613
631
632
+ (* Return the first leading and last trailing comment of a statement *)
614
633
let statement_comment_bounds ((loc , _ ) as stmt : (Loc. t , Loc. t ) Statement. t ) :
615
634
Loc. t Comment. t option * Loc. t Comment. t option =
616
635
let collector = new comment_bounds_collector ~loc in
0 commit comments