Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 2131063

Browse files
committed
Suppress fragile match warning for iflet
1 parent 2e0b219 commit 2131063

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

src/napkin_core.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ let jsxAttr = (Location.mknoloc "JSX", Parsetree.PStr [])
8484
let uncurryAttr = (Location.mknoloc "bs", Parsetree.PStr [])
8585
let ternaryAttr = (Location.mknoloc "ns.ternary", Parsetree.PStr [])
8686
let ifLetAttr = (Location.mknoloc "ns.iflet", Parsetree.PStr [])
87+
let suppressFragileMatchWarningAttr = (Location.mknoloc "warning", Parsetree.PStr [Ast_helper.Str.eval (Ast_helper.Exp.constant (Pconst_string ("-4", None)))])
8788
let makeBracesAttr loc = (Location.mkloc "ns.braces" loc, Parsetree.PStr [])
8889

8990
type typDefOrExt =
@@ -3070,7 +3071,7 @@ and parseIfLetExpr startPos p =
30703071
Ast_helper.Exp.construct ~loc (Location.mkloc (Longident.Lident "()") loc) None
30713072
in
30723073
let loc = mkLoc startPos p.prevEndPos in
3073-
Ast_helper.Exp.match_ ~attrs:[ifLetAttr] ~loc conditionExpr [
3074+
Ast_helper.Exp.match_ ~attrs:[ifLetAttr; suppressFragileMatchWarningAttr] ~loc conditionExpr [
30743075
Ast_helper.Exp.case pattern thenExpr;
30753076
Ast_helper.Exp.case (Ast_helper.Pat.any ()) elseExpr;
30763077
]

src/napkin_parsetree_viewer.ml

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,30 @@ open Parsetree
255255
else
256256
false
257257

258+
let rec hasIfLetAttribute attrs =
259+
match attrs with
260+
| [] -> false
261+
| ({Location.txt="ns.iflet"},_)::_ -> true
262+
| _::attrs -> hasIfLetAttribute attrs
263+
264+
let isIfLetExpr expr = match expr with
265+
| {
266+
pexp_attributes = attrs;
267+
pexp_desc = Pexp_match _
268+
} when hasIfLetAttribute attrs -> true
269+
| _ -> false
270+
258271
let hasAttributes attrs =
259272
List.exists (fun attr -> match attr with
260273
| ({Location.txt = "bs" | "ns.ternary" | "ns.braces" | "ns.iflet"}, _) -> false
274+
(* Remove the fragile pattern warning for iflet expressions *)
275+
| ({Location.txt="warning"}, PStr [{
276+
pstr_desc = Pstr_eval ({
277+
pexp_desc = Pexp_constant (
278+
Pconst_string ("-4", None)
279+
)
280+
}, _)
281+
}]) -> not (hasIfLetAttribute attrs)
261282
| _ -> true
262283
) attrs
263284

@@ -268,18 +289,6 @@ open Parsetree
268289
) -> true
269290
| _ -> false
270291

271-
let rec hasIfLetAttribute attrs =
272-
match attrs with
273-
| [] -> false
274-
| ({Location.txt="ns.iflet"},_)::_ -> true
275-
| _::attrs -> hasIfLetAttribute attrs
276-
277-
let isIfLetExpr expr = match expr with
278-
| {
279-
pexp_attributes = attrs;
280-
pexp_desc = Pexp_match _
281-
} when hasIfLetAttribute attrs -> true
282-
| _ -> false
283292

284293
type ifConditionKind =
285294
| If of Parsetree.expression
@@ -352,6 +361,18 @@ open Parsetree
352361
| _ -> true
353362
) attrs
354363

364+
let filterFragileMatchAttributes attrs =
365+
List.filter (fun attr -> match attr with
366+
| ({Location.txt="warning"}, PStr [{
367+
pstr_desc = Pstr_eval ({
368+
pexp_desc = Pexp_constant (
369+
Pconst_string ("-4", _)
370+
)
371+
}, _)
372+
}]) -> false
373+
| _ -> true
374+
) attrs
375+
355376
let isJsxExpression expr =
356377
let rec loop attrs =
357378
match attrs with

src/napkin_parsetree_viewer.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ val parametersShouldHug:
7474
funParamKind list -> bool
7575

7676
val filterTernaryAttributes: Parsetree.attributes -> Parsetree.attributes
77+
val filterFragileMatchAttributes: Parsetree.attributes -> Parsetree.attributes
7778

7879
val isJsxExpression: Parsetree.expression -> bool
7980
val hasJsxAttribute: Parsetree.attributes -> bool

src/napkin_printer.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2411,8 +2411,9 @@ and printIfChain pexp_attributes ifs elseExpr cmtTbl =
24112411
printExpressionBlock ~braces:true expr cmtTbl;
24122412
]
24132413
in
2414+
let attrs = ParsetreeViewer.filterFragileMatchAttributes pexp_attributes in
24142415
Doc.concat [
2415-
printAttributes pexp_attributes cmtTbl;
2416+
printAttributes attrs cmtTbl;
24162417
ifDocs;
24172418
elseDoc;
24182419
]
@@ -3072,6 +3073,7 @@ and printExpression (e : Parsetree.expression) cmtTbl =
30723073
| Pexp_newtype _
30733074
| Pexp_setfield _
30743075
| Pexp_ifthenelse _ -> true
3076+
| Pexp_match _ when ParsetreeViewer.isIfLetExpr e -> true
30753077
| Pexp_construct _ when ParsetreeViewer.hasJsxAttribute e.pexp_attributes -> true
30763078
| _ -> false
30773079
in

tests/parsing/grammar/expressions/__snapshots__/parse.spec.js.snap

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,62 +504,64 @@ let ifElseIfThen =
504504
then f ()
505505
else if foo = bar2 then f1 () else if foo = bar3 then f2 () else f3 ()
506506
let x = (if true then 1 else 2) + (if false then 2 else 3)
507-
;;((match foo () with | Some x -> doSomethingWithX x | _ -> ())[@ns.iflet ])
507+
;;((match foo () with | Some x -> doSomethingWithX x | _ -> ())
508+
[@ns.iflet ][@warning \\"-4\\"])
508509
;;((match foo () with
509510
| Some x -> doSomethingWithX x
510-
| _ -> doSomethingElse ())[@ns.iflet ])
511+
| _ -> doSomethingElse ())[@ns.iflet ][@warning \\"-4\\"])
511512
;;((match foo () with
512513
| Some x -> doSomethingWithX x
513514
| _ ->
514515
(((match bar () with
515516
| Some y -> doSomethingWithY y
516517
| _ -> doSomethingElse ()))
517-
[@ns.iflet ]))[@ns.iflet ])
518+
[@ns.iflet ][@warning \\"-4\\"]))[@ns.iflet ][@warning \\"-4\\"])
518519
;;((match foo () with
519520
| Some x -> doSomethingWithX x
520521
| _ -> if n > 10 then doSomethingWithForN () else doSomethingElse ())
521-
[@ns.iflet ])
522+
[@ns.iflet ][@warning \\"-4\\"])
522523
;;if n > 10
523524
then doSomethingWithForN ()
524525
else
525526
(((match foo () with
526527
| Some x -> doSomethingWithX x
527528
| _ -> doSomethingElse ()))
528-
[@ns.iflet ])
529+
[@ns.iflet ][@warning \\"-4\\"])
529530
;;if n > 10
530531
then ((doSomethingWithForN ())[@aa ])
531532
else
532533
(((match ((foo ())[@dd ]) with
533534
| ((Some ((x)[@cc ]))[@bb ]) -> ((doSomethingWithY x)[@ee ])
534535
| _ -> ((doSomethingElse ())[@ff ])))
535-
[@ns.iflet ])
536+
[@ns.iflet ][@warning \\"-4\\"])
536537
;;((match foo () with
537538
| Some x -> doSomethingWithX x
538539
| _ ->
539540
(((match bar () with
540541
| Some y -> doSomethingWithY y
541542
| _ ->
542543
(((match baz () with | Some z -> doSomethingWithZ z | _ -> ()))
543-
[@ns.iflet ])))
544-
[@ns.iflet ]))[@ns.iflet ])
544+
[@ns.iflet ][@warning \\"-4\\"])))
545+
[@ns.iflet ][@warning \\"-4\\"]))[@ns.iflet ][@warning \\"-4\\"])
545546
;;((match foo () with
546547
| Some (Thing (With { many = Internal [|Components;q|] })) as p ->
547548
doSomethingWithE e
548-
| _ -> ())[@ns.iflet ])
549-
let a = ((match foo () with | Some x -> x | _ -> 123)[@ns.iflet ])
549+
| _ -> ())[@ns.iflet ][@warning \\"-4\\"])
550+
let a = ((match foo () with | Some x -> x | _ -> 123)
551+
[@ns.iflet ][@warning \\"-4\\"])
550552
let getZ nested =
551553
((match nested.origin with
552554
| Some point -> (((match point.z with | Some z -> z | _ -> 0))
553-
[@ns.iflet ])
555+
[@ns.iflet ][@warning \\"-4\\"])
554556
| _ -> 0)
555-
[@ns.iflet ])
557+
[@ns.iflet ][@warning \\"-4\\"])
556558
;;((match foo () with
557559
| Some (Thing (With { many = Internal [|Components;q|] })) as p ->
558560
((((match foo () with
559561
| Other (Thing (With { many = Internal [|Components;q|] })) as p
560562
-> doSomethingElse e
561563
| _ -> ()))
562-
[@ns.iflet ]);
564+
[@ns.iflet ][@warning \\"-4\\"]);
563565
doSomethingWithE e)
564566
| _ ->
565567
(((match foo () with
@@ -570,10 +572,13 @@ let getZ nested =
570572
| Some (Thing (With { many = Internal [|Components;q|] }))
571573
as p -> doSomethingWithE e
572574
| _ -> ()))
573-
[@ns.iflet ])))
574-
[@ns.iflet ]))[@ns.iflet ])
575+
[@ns.iflet ][@warning \\"-4\\"])))
576+
[@ns.iflet ][@warning \\"-4\\"]))[@ns.iflet ][@warning \\"-4\\"])
575577
let a =
576-
((if b then ((match foo () with | Some x -> 1 | _ -> 2)[@ns.iflet ]) else 3)
578+
((if b
579+
then ((match foo () with | Some x -> 1 | _ -> 2)
580+
[@ns.iflet ][@warning \\"-4\\"])
581+
else 3)
577582
[@ns.ternary ])"
578583
`;
579584

0 commit comments

Comments
 (0)