-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathres_parsetree_viewer.ml
More file actions
896 lines (822 loc) · 25 KB
/
res_parsetree_viewer.ml
File metadata and controls
896 lines (822 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
open Parsetree
let arrow_type ?(max_arity = max_int) ct =
let has_as_attr attrs =
Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as")
in
let rec process attrs_before acc typ max_arity =
match typ with
| _ when max_arity < 0 -> (attrs_before, List.rev acc, typ)
| {ptyp_desc = Ptyp_arrow {arity = Some _; arg = {attrs = []}}}
when acc <> [] ->
(attrs_before, List.rev acc, typ)
| {ptyp_desc = Ptyp_arrow {arg = {lbl = Nolabel; attrs = []} as arg; ret}}
->
process attrs_before (arg :: acc) ret (max_arity - 1)
| {ptyp_desc = Ptyp_arrow {arg = {lbl = Nolabel}}; ptyp_attributes = _attrs}
as return_type ->
let args = List.rev acc in
(attrs_before, args, return_type)
| {
ptyp_desc = Ptyp_arrow {arg = {lbl = Labelled _ | Optional _} as arg; ret};
ptyp_attributes = _attrs;
} ->
(* Res_core.parse_es6_arrow_type has a workaround that removed an extra arity for the function if the
argument is a Ptyp_any with @as attribute i.e. ~x: @as(`{prop: value}`) _.
When this case is encountered we add that missing arity so the arrow is printed properly.
*)
let arity =
match arg.typ with
| {ptyp_desc = Ptyp_any; ptyp_attributes = attrs1}
when has_as_attr attrs1 ->
max_arity
| _ -> max_arity - 1
in
process attrs_before (arg :: acc) ret arity
| typ -> (attrs_before, List.rev acc, typ)
in
match ct with
| {ptyp_desc = Ptyp_arrow _; ptyp_attributes = attrs1} as typ ->
process attrs1 [] {typ with ptyp_attributes = []} max_arity
| typ -> process [] [] typ max_arity
let functor_type modtype =
let rec process acc modtype =
match modtype with
| {
pmty_desc = Pmty_functor (lbl, arg_type, return_type);
pmty_attributes = attrs;
} ->
let arg = (attrs, lbl, arg_type) in
process (arg :: acc) return_type
| mod_type -> (List.rev acc, mod_type)
in
process [] modtype
let has_await_attribute attrs =
List.exists
(function
| {Location.txt = "res.await"}, _ -> true
| _ -> false)
attrs
let expr_is_await e =
match e.pexp_desc with
| Pexp_await _ -> true
| _ -> false
let has_inline_record_definition_attribute attrs =
List.exists
(function
| {Location.txt = "res.inlineRecordDefinition"}, _ -> true
| _ -> false)
attrs
let has_res_pat_variant_spread_attribute attrs =
List.exists
(function
| {Location.txt = "res.patVariantSpread"}, _ -> true
| _ -> false)
attrs
let has_dict_pattern_attribute attrs =
attrs
|> List.find_opt (fun (({txt}, _) : Parsetree.attribute) ->
txt = "res.dictPattern")
|> Option.is_some
let has_dict_spread_attribute attrs =
attrs
|> List.find_opt (fun (({txt}, _) : Parsetree.attribute) ->
txt = "res.dictSpread")
|> Option.is_some
type dict_expr_part =
| DictExprRows of Parsetree.expression
| DictExprSpread of Parsetree.expression
let collect_array_expressions expr =
match expr.pexp_desc with
| Pexp_array exprs -> (exprs, None)
| _ -> ([], Some expr)
let collect_list_expressions expr =
let rec collect acc expr =
match expr.pexp_desc with
| Pexp_construct ({txt = Longident.Lident "[]"}, _) -> (List.rev acc, None)
| Pexp_construct
( {txt = Longident.Lident "::"},
Some {pexp_desc = Pexp_tuple (hd :: [tail])} ) ->
collect (hd :: acc) tail
| _ -> (List.rev acc, Some expr)
in
collect [] expr
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
let rewrite_underscore_apply expr =
match expr.pexp_desc with
| Pexp_fun
{
arg_label = Nolabel;
default = None;
lhs = {ppat_desc = Ppat_var {txt = "__x"}};
rhs = {pexp_desc = Pexp_apply {funct = call_expr; args}} as e;
} ->
let new_args =
List.map
(fun arg ->
match arg with
| ( lbl,
({pexp_desc = Pexp_ident ({txt = Longident.Lident "__x"} as lid)}
as arg_expr) ) ->
( lbl,
{
arg_expr with
pexp_desc = Pexp_ident {lid with txt = Longident.Lident "_"};
} )
| arg -> arg)
args
in
{
e with
pexp_desc =
Pexp_apply
{
funct = call_expr;
args = new_args;
partial = false;
transformed_jsx = false;
};
}
| _ -> expr
(* For pipe RHS: (__x) => f(__x, a, b) -----> f(a, b)
Note: Ppat_var "__x" and Pexp_ident "__x" represent `_` placeholders in user code.
Omits the first __x argument only if it's the sole occurrence.
If multiple __x exist (e.g., f(__x, __x, b)), keeps all to preserve semantics. *)
let rewrite_underscore_apply_in_pipe expr =
let is_underscore_arg = function
| _, {pexp_desc = Pexp_ident {txt = Longident.Lident "__x"}} -> true
| _ -> false
in
let convert_underscore_to_placeholder arg =
match arg with
| ( lbl,
({pexp_desc = Pexp_ident ({txt = Longident.Lident "__x"} as lid)} as
arg_expr) ) ->
( lbl,
{
arg_expr with
pexp_desc = Pexp_ident {lid with txt = Longident.Lident "_"};
} )
| arg -> arg
in
match expr.pexp_desc with
| Pexp_fun
{
arg_label = Nolabel;
default = None;
lhs = {ppat_desc = Ppat_var {txt = "__x"}};
rhs = {pexp_desc = Pexp_apply {funct; args}} as e;
} -> (
match args with
| first_arg :: rest_args when is_underscore_arg first_arg ->
if List.exists is_underscore_arg rest_args then
(* Multiple __x - keep all to preserve semantics *)
rewrite_underscore_apply expr
else
(* Single __x in first position - safe to omit *)
{
e with
pexp_desc =
Pexp_apply
{
funct;
args = List.map convert_underscore_to_placeholder rest_args;
partial = false;
transformed_jsx = false;
};
}
| _ -> rewrite_underscore_apply expr)
| _ -> expr
type fun_param_kind =
| Parameter of {
attrs: Parsetree.attributes;
lbl: Asttypes.arg_label;
default_expr: Parsetree.expression option;
pat: Parsetree.pattern;
}
| NewTypes of {attrs: Parsetree.attributes; locs: string Asttypes.loc list}
let fun_expr expr_ =
let async = Ast_async.dig_async_payload_from_function expr_ in
let rec collect_params ~n_fun ~params expr =
match expr with
| {
pexp_desc =
Pexp_fun
{
arg_label = lbl;
default = default_expr;
lhs = pattern;
rhs = return_expr;
arity;
};
pexp_attributes = attrs;
}
when arity = None || n_fun = 0 ->
let parameter = Parameter {attrs; lbl; default_expr; pat = pattern} in
collect_params ~n_fun:(n_fun + 1) ~params:(parameter :: params)
return_expr
| _ -> (async, List.rev params, expr)
in
(* Turns (type t, type u, type z) into "type t u z" *)
let rec collect_new_types acc return_expr =
match return_expr with
| {pexp_desc = Pexp_newtype (string_loc, return_expr)} ->
collect_new_types (string_loc :: acc) return_expr
| return_expr -> (List.rev acc, return_expr)
in
match expr_ with
| {pexp_desc = Pexp_newtype (string_loc, rest)} ->
let string_locs, return_expr = collect_new_types [string_loc] rest in
let param = NewTypes {attrs = []; locs = string_locs} in
collect_params ~n_fun:0 ~params:[param] return_expr
| _ -> collect_params ~n_fun:0 ~params:[] {expr_ with pexp_attributes = []}
let process_braces_attr expr =
match expr.pexp_attributes with
| (({txt = "res.braces" | "ns.braces"}, _) as attr) :: attrs ->
(Some attr, {expr with pexp_attributes = attrs})
| _ -> (None, expr)
let filter_parsing_attrs attrs =
List.filter
(fun attr ->
match attr with
| ( {
Location.txt =
( "res.braces" | "ns.braces" | "res.iflet" | "res.ternary"
| "res.await" | "res.template" | "res.taggedTemplate"
| "res.patVariantSpread" | "res.dictPattern" | "res.dictSpread"
| "res.inlineRecordDefinition" );
},
_ ) ->
false
| _ -> true)
attrs
let is_block_expr expr =
match expr.pexp_desc with
| Pexp_letmodule _ | Pexp_letexception _ | Pexp_let _ | Pexp_open _
| Pexp_sequence _ ->
true
| _ -> false
let is_braced_expr expr =
match process_braces_attr expr with
| Some _, _ -> true
| _ -> false
let is_multiline_text txt =
let len = String.length txt in
let rec check i =
if i >= len then false
else
let c = String.unsafe_get txt i in
match c with
| '\010' | '\013' -> true
| '\\' -> if i + 2 = len then false else check (i + 2)
| _ -> check (i + 1)
in
check 0
let is_huggable_expression expr =
match expr.pexp_desc with
| Pexp_array _ | Pexp_tuple _
| Pexp_constant (Pconst_string (_, Some _))
| Pexp_construct ({txt = Longident.Lident ("::" | "[]")}, _)
| Pexp_extension ({txt = "obj"}, _)
| Pexp_record _ ->
true
| _ when is_block_expr expr -> true
| _ when is_braced_expr expr -> true
| Pexp_constant (Pconst_string (txt, None)) when is_multiline_text txt -> true
| _ -> false
let is_huggable_rhs expr =
match expr.pexp_desc with
| Pexp_array _ | Pexp_tuple _
| Pexp_extension ({txt = "obj"}, _)
| Pexp_record _ ->
true
| _ when is_braced_expr expr -> true
| _ -> false
let is_huggable_pattern pattern =
match pattern.ppat_desc with
| Ppat_array _ | Ppat_tuple _ | Ppat_record _ | Ppat_variant _
| Ppat_construct _ ->
true
| _ -> false
let operator_precedence operator =
match operator with
| ":=" -> 1
| "||" -> 2
| "&&" -> 3
| "|||" -> 4
| "^^^" -> 5
| "&&&" -> 6
| "==" | "===" | "<" | ">" | "!=" | "<>" | "!==" | "<=" | ">=" -> 7
| "<<" | ">>" | ">>>" -> 8
| "+" | "+." | "-" | "-." | "++" -> 9
| "*" | "*." | "/" | "/." | "%" -> 10
| "**" -> 11
| "#" | "##" | "->" -> 12
| _ -> 0
let is_unary_operator operator =
match operator with
| "~+" | "~+." | "~-" | "~-." | "~~~" | "not" -> true
| _ -> false
let is_unary_expression expr =
match expr.pexp_desc with
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident operator}};
args = [(Nolabel, _arg)];
}
when is_unary_operator operator ->
true
| _ -> false
let is_binary_operator operator =
match operator with
| ":=" | "||" | "&&" | "==" | "===" | "<" | ">" | "!=" | "!==" | "<=" | ">="
| "+" | "+." | "-" | "-." | "++" | "*" | "*." | "/" | "/." | "**" | "->"
| "<>" | "%" | "|||" | "^^^" | "&&&" | "<<" | ">>" | ">>>" ->
true
| _ -> false
let not_ghost_operator operator (loc : Location.t) =
is_binary_operator operator && not (loc.loc_ghost && operator = "++")
let is_binary_expression expr =
match expr.pexp_desc with
| Pexp_apply
{
funct =
{
pexp_desc =
Pexp_ident {txt = Longident.Lident operator; loc = operator_loc};
};
args = [(Nolabel, _operand1); (Nolabel, _operand2)];
}
when not_ghost_operator operator operator_loc ->
true
| _ -> false
let is_equality_operator operator =
match operator with
| "==" | "===" | "!=" | "!==" -> true
| _ -> false
let is_rhs_binary_operator operator =
match operator with
| "**" -> true
| _ -> false
let flattenable_operators parent_operator child_operator =
let prec_parent = operator_precedence parent_operator in
let prec_child = operator_precedence child_operator in
if prec_parent == prec_child then
not
(is_equality_operator parent_operator
&& is_equality_operator child_operator)
else false
let rec has_if_let_attribute attrs =
match attrs with
| [] -> false
| ({Location.txt = "res.iflet"}, _) :: _ -> true
| _ :: attrs -> has_if_let_attribute attrs
let is_if_let_expr expr =
match expr with
| {pexp_attributes = attrs; pexp_desc = Pexp_match _}
when has_if_let_attribute attrs ->
true
| _ -> false
let has_attributes attrs =
List.exists
(fun attr ->
match attr with
| ( {
Location.txt =
( "res.braces" | "ns.braces" | "res.iflet" | "res.ternary"
| "res.await" | "res.template" | "res.inlineRecordDefinition" );
},
_ ) ->
false
(* Remove the fragile pattern warning for iflet expressions *)
| ( {Location.txt = "warning"},
PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_string ("-4", None))}, _);
};
] ) ->
not (has_if_let_attribute attrs)
| _ -> true)
attrs
let is_array_access expr =
match expr.pexp_desc with
| Pexp_apply
{
funct =
{
pexp_desc =
Pexp_ident
{txt = Longident.Ldot (Longident.Lident "Array", "get")};
};
args = [(Nolabel, _parentExpr); (Nolabel, _memberExpr)];
} ->
true
| _ -> false
type if_condition_kind =
| If of Parsetree.expression
| IfLet of Parsetree.pattern * Parsetree.expression
let collect_if_expressions expr =
let rec collect acc expr =
let expr_loc = expr.pexp_loc in
match expr.pexp_desc with
| Pexp_ifthenelse (if_expr, then_expr, Some else_expr) ->
collect ((expr_loc, If if_expr, then_expr) :: acc) else_expr
| Pexp_ifthenelse (if_expr, then_expr, (None as else_expr)) ->
let ifs = List.rev ((expr_loc, If if_expr, then_expr) :: acc) in
(ifs, else_expr)
| Pexp_match
( condition,
[
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
{
pc_rhs =
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)};
};
] )
when is_if_let_expr expr ->
let ifs =
List.rev ((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
in
(ifs, None)
| Pexp_match
( condition,
[
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
{pc_rhs = else_expr};
] )
when is_if_let_expr expr ->
collect
((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
else_expr
| _ -> (List.rev acc, Some expr)
in
collect [] expr
let rec has_ternary_attribute attrs =
match attrs with
| [] -> false
| ({Location.txt = "res.ternary"}, _) :: _ -> true
| _ :: attrs -> has_ternary_attribute attrs
let is_ternary_expr expr =
match expr with
| {pexp_attributes = attrs; pexp_desc = Pexp_ifthenelse _}
when has_ternary_attribute attrs ->
true
| _ -> false
let collect_ternary_parts expr =
let rec collect acc expr =
match expr with
| {
pexp_attributes = attrs;
pexp_desc = Pexp_ifthenelse (condition, consequent, Some alternate);
}
when has_ternary_attribute attrs ->
collect ((condition, consequent) :: acc) alternate
| alternate -> (List.rev acc, alternate)
in
collect [] expr
let parameters_should_hug parameters =
match parameters with
| [Parameter {attrs = []; lbl = Nolabel; default_expr = None; pat}]
when is_huggable_pattern pat ->
true
| _ -> false
let filter_ternary_attributes attrs =
List.filter
(fun attr ->
match attr with
| {Location.txt = "res.ternary"}, _ -> false
| _ -> true)
attrs
let filter_fragile_match_attributes attrs =
List.filter
(fun attr ->
match attr with
| ( {Location.txt = "warning"},
PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_string ("-4", _))}, _);
};
] ) ->
false
| _ -> true)
attrs
let should_indent_binary_expr expr =
let same_precedence_sub_expression operator sub_expression =
match sub_expression with
| {
pexp_desc =
Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident sub_operator}};
args = [(Nolabel, _lhs); (Nolabel, _rhs)];
};
}
when is_binary_operator sub_operator ->
flattenable_operators operator sub_operator
| _ -> true
in
match expr with
| {
pexp_desc =
Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident operator}};
args = [(Nolabel, lhs); (Nolabel, _rhs)];
};
}
when is_binary_operator operator ->
is_equality_operator operator
|| (not (same_precedence_sub_expression operator lhs))
|| operator = ":="
| _ -> false
let should_inline_rhs_binary_expr rhs =
match rhs.pexp_desc with
| Parsetree.Pexp_constant _ | Pexp_let _ | Pexp_letmodule _
| Pexp_letexception _ | Pexp_sequence _ | Pexp_open _ | Pexp_ifthenelse _
| Pexp_for _ | Pexp_for_of _ | Pexp_for_await_of _ | Pexp_while _ | Pexp_try _
| Pexp_array _ | Pexp_record _ ->
true
| _ -> false
let is_printable_attribute attr =
match attr with
| ( {
Location.txt =
( "res.iflet" | "res.braces" | "ns.braces" | "JSX" | "res.await"
| "res.template" | "res.taggedTemplate" | "res.ternary"
| "res.inlineRecordDefinition" | "res.dictSpread" );
},
_ ) ->
false
| _ -> true
let has_printable_attributes attrs = List.exists is_printable_attribute attrs
let filter_printable_attributes attrs = List.filter is_printable_attribute attrs
let partition_printable_attributes attrs =
List.partition is_printable_attribute attrs
let partition_doc_comment_attributes attrs =
List.partition
(fun ((id, payload) : Parsetree.attribute) ->
match (id, payload) with
| ( {txt = "res.doc"},
PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_string (_, _))}, _);
};
] ) ->
true
| _ -> false)
attrs
let is_fun_newtype expr =
match expr.pexp_desc with
| Pexp_fun _ | Pexp_newtype _ -> true
| _ -> false
let requires_special_callback_printing_last_arg args =
let rec loop args =
match args with
| [] -> false
| [(_, expr)] when is_fun_newtype expr -> true
| (_, expr) :: _ when is_fun_newtype expr -> false
| _ :: rest -> loop rest
in
loop args
let requires_special_callback_printing_first_arg args =
let rec loop args =
match args with
| [] -> true
| (_, expr) :: _ when is_fun_newtype expr -> false
| _ :: rest -> loop rest
in
match args with
| [(_, expr)] when is_fun_newtype expr -> false
| (_, expr) :: rest when is_fun_newtype expr -> loop rest
| _ -> false
let mod_expr_apply mod_expr =
let rec loop acc mod_expr =
match mod_expr with
| {pmod_desc = Pmod_apply (next, arg)} -> loop (arg :: acc) next
| _ -> (acc, mod_expr)
in
loop [] mod_expr
let mod_expr_functor mod_expr =
let rec loop acc mod_expr =
match mod_expr with
| {
pmod_desc = Pmod_functor (lbl, mod_type, return_mod_expr);
pmod_attributes = attrs;
} ->
let param = (attrs, lbl, mod_type) in
loop (param :: acc) return_mod_expr
| return_mod_expr -> (List.rev acc, return_mod_expr)
in
loop [] mod_expr
let rec collect_patterns_from_list_construct acc pattern =
let open Parsetree in
match pattern.ppat_desc with
| Ppat_construct
({txt = Longident.Lident "::"}, Some {ppat_desc = Ppat_tuple [pat; rest]})
->
collect_patterns_from_list_construct (pat :: acc) rest
| _ -> (List.rev acc, pattern)
let has_template_literal_attr attrs =
List.exists
(fun attr ->
match attr with
| {Location.txt = "res.template"}, _ -> true
| _ -> false)
attrs
let has_tagged_template_literal_attr attrs =
List.exists
(fun attr ->
match attr with
| {Location.txt = "res.taggedTemplate"}, _ -> true
| _ -> false)
attrs
let is_template_literal expr =
match expr.pexp_desc with
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident "++"}};
args = [(Nolabel, _); (Nolabel, _)];
}
when has_template_literal_attr expr.pexp_attributes ->
true
| Pexp_constant (Pconst_string (_, Some "")) -> true
| Pexp_constant _ when has_template_literal_attr expr.pexp_attributes -> true
| _ -> false
let is_tagged_template_literal expr =
match expr with
| {pexp_desc = Pexp_apply _; pexp_attributes = attrs} ->
has_tagged_template_literal_attr attrs
| _ -> false
let has_spread_attr attrs =
List.exists
(fun attr ->
match attr with
| {Location.txt = "res.spread"}, _ -> true
| _ -> false)
attrs
let is_spread_belt_list_concat expr =
match expr.pexp_desc with
| Pexp_ident
{
txt =
Longident.Ldot
(Longident.Ldot (Longident.Lident "Belt", "List"), "concatMany");
} ->
has_spread_attr expr.pexp_attributes
| _ -> false
let is_spread_belt_array_concat expr =
match expr.pexp_desc with
| Pexp_ident
{
txt =
Longident.Ldot
(Longident.Ldot (Longident.Lident "Belt", "Array"), "concatMany");
} ->
has_spread_attr expr.pexp_attributes
| _ -> false
let is_tuple_array (expr : Parsetree.expression) =
let is_plain_tuple (expr : Parsetree.expression) =
match expr with
| {pexp_desc = Pexp_tuple _} -> true
| _ -> false
in
match expr with
| {pexp_desc = Pexp_array items} -> List.for_all is_plain_tuple items
| _ -> false
let collect_spread_dict_expr_parts expr =
let extract_literal_dict_rows (expr : Parsetree.expression) =
match expr with
| {
pexp_desc =
Pexp_apply
{
funct =
{
pexp_desc =
Pexp_ident
{txt = Longident.Ldot (Lident "Primitive_dict", "make")};
};
args = [(Nolabel, key_values)];
};
}
when is_tuple_array key_values ->
Some key_values
| _ -> None
in
let is_empty_tuple_array (expr : Parsetree.expression) =
match expr.pexp_desc with
| Pexp_array [] -> true
| _ -> false
in
match expr with
| {
pexp_desc =
Pexp_apply
{
funct =
{
pexp_desc =
Pexp_ident
{txt = Longident.Ldot (Lident "Primitive_dict", "spread")};
pexp_attributes;
};
args =
[(Nolabel, target_expr); (Nolabel, {pexp_desc = Pexp_array sources})];
};
}
when has_dict_spread_attribute pexp_attributes ->
let to_part expr =
match extract_literal_dict_rows expr with
| Some rows_expr ->
if is_empty_tuple_array rows_expr then None
else Some (DictExprRows rows_expr)
| None -> Some (DictExprSpread expr)
in
Some (List.filter_map to_part (target_expr :: sources))
| _ -> None
(* Blue | Red | Green -> [Blue; Red; Green] *)
let collect_or_pattern_chain pat =
let rec loop pattern chain =
match pattern.ppat_desc with
| Ppat_or (left, right) -> loop left (right :: chain)
| _ -> pattern :: chain
in
loop pat []
let is_single_pipe_expr expr =
(* handles:
* x
* ->Js.Dict.get("wm-property")
* ->Option.flatMap(Js.Json.decodeString)
* ->Option.flatMap(x =>
* switch x {
* | "like-of" => Some(#like)
* | "repost-of" => Some(#repost)
* | _ => None
* }
* )
*)
let is_pipe_expr expr =
match expr.pexp_desc with
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident "->"}};
args = [(Nolabel, _operand1); (Nolabel, _operand2)];
} ->
true
| _ -> false
in
match expr.pexp_desc with
| Pexp_apply
{
funct = {pexp_desc = Pexp_ident {txt = Longident.Lident "->"}};
args = [(Nolabel, operand1); (Nolabel, _operand2)];
}
when not (is_pipe_expr operand1) ->
true
| _ -> false
let is_underscore_apply_sugar expr =
match expr.pexp_desc with
| Pexp_fun
{
arg_label = Nolabel;
default = None;
lhs = {ppat_desc = Ppat_var {txt = "__x"}};
rhs = {pexp_desc = Pexp_apply _};
} ->
true
| _ -> false
let is_rewritten_underscore_apply_sugar expr =
match expr.pexp_desc with
| Pexp_ident {txt = Longident.Lident "_"} -> true
| _ -> false
let get_jsx_prop_loc = function
| Parsetree.JSXPropPunning (_, name) -> name.loc
| Parsetree.JSXPropValue (name, _, value) ->
{name.loc with loc_end = value.pexp_loc.loc_end}
| Parsetree.JSXPropSpreading (loc, _) -> loc
let container_element_closing_tag_loc
(tag : Parsetree.jsx_closing_container_tag) =
{
tag.jsx_closing_container_tag_name.loc with
loc_start = tag.jsx_closing_container_tag_start;
loc_end = tag.jsx_closing_container_tag_end;
}
(** returns the location of the /> token in a unary element *)
let unary_element_closing_token (expression_loc : Warnings.loc) =
{
expression_loc with
loc_start =
{
expression_loc.loc_end with
pos_cnum = expression_loc.loc_end.pos_cnum - 2;
pos_bol = expression_loc.loc_end.pos_bol - 2;
};
}