@@ -42,18 +42,6 @@ open Parsetree
4242 in
4343 process false [] attrs
4444
45- let collectIfExpressions expr =
46- let rec collect acc expr = match expr.pexp_desc with
47- | Pexp_ifthenelse (ifExpr , thenExpr , Some elseExpr ) ->
48- collect ((ifExpr, thenExpr)::acc) elseExpr
49- | Pexp_ifthenelse (ifExpr , thenExpr , (None as elseExpr )) ->
50- let ifs = List. rev ((ifExpr, thenExpr)::acc) in
51- (ifs, elseExpr)
52- | _ ->
53- (List. rev acc, Some expr)
54- in
55- collect [] expr
56-
5745 let collectListExpressions expr =
5846 let rec collect acc expr = match expr.pexp_desc with
5947 | Pexp_construct ({txt = Longident. Lident "[]" } , _ ) ->
@@ -165,7 +153,7 @@ open Parsetree
165153 let filterParsingAttrs attrs =
166154 List. filter (fun attr ->
167155 match attr with
168- | ({Location. txt = ("ns.ternary" | "ns.braces" | "bs" | "ns.namedArgLoc" )} , _ ) -> false
156+ | ({Location. txt = ("ns.ternary" | "ns.braces" | "bs" | "ns.iflet" | "ns. namedArgLoc" )} , _ ) -> false
169157 | _ -> true
170158 ) attrs
171159
@@ -269,7 +257,7 @@ open Parsetree
269257
270258 let hasAttributes attrs =
271259 List. exists (fun attr -> match attr with
272- | ({Location. txt = "bs" | "ns.ternary" | "ns.braces" } , _ ) -> false
260+ | ({Location. txt = "bs" | "ns.ternary" | "ns.braces" | "ns.iflet" } , _ ) -> false
273261 | _ -> true
274262 ) attrs
275263
@@ -280,6 +268,52 @@ open Parsetree
280268 ) -> true
281269 | _ -> false
282270
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
283+
284+ type ifConditionKind =
285+ | If of Parsetree .expression
286+ | IfLet of Parsetree .pattern * Parsetree .expression * Parsetree .expression option
287+
288+ let collectIfExpressions expr =
289+ let rec collect acc expr = match expr.pexp_desc with
290+ | Pexp_ifthenelse (ifExpr , thenExpr , Some elseExpr ) ->
291+ collect ((If (ifExpr), thenExpr)::acc) elseExpr
292+ | Pexp_ifthenelse (ifExpr , thenExpr , (None as elseExpr )) ->
293+ let ifs = List. rev ((If (ifExpr), thenExpr)::acc) in
294+ (ifs, elseExpr)
295+ | Pexp_match (condition, [{
296+ pc_lhs = pattern;
297+ pc_guard = guard;
298+ pc_rhs = thenExpr;
299+ }; {
300+ pc_rhs = {pexp_desc = Pexp_construct ({txt = Longident. Lident " ()" }, _)}
301+ }]) when isIfLetExpr expr ->
302+ let ifs = List. rev ((IfLet (pattern, condition, guard), thenExpr)::acc) in
303+ (ifs, None )
304+ | Pexp_match (condition, [{
305+ pc_lhs = pattern;
306+ pc_guard = guard;
307+ pc_rhs = thenExpr;
308+ }; {
309+ pc_rhs = elseExpr;
310+ }]) when isIfLetExpr expr ->
311+ collect ((IfLet (pattern, condition, guard), thenExpr)::acc) elseExpr
312+ | _ ->
313+ (List. rev acc, Some expr)
314+ in
315+ collect [] expr
316+
283317 let rec hasTernaryAttribute attrs =
284318 match attrs with
285319 | [] -> false
@@ -318,6 +352,12 @@ open Parsetree
318352 | _ -> true
319353 ) attrs
320354
355+ let filterIfLetAttributes attrs =
356+ List. filter (fun attr -> match attr with
357+ | ({Location. txt= " ns.iflet" },_) -> false
358+ | _ -> true
359+ ) attrs
360+
321361 let isJsxExpression expr =
322362 let rec loop attrs =
323363 match attrs with
@@ -371,13 +411,13 @@ open Parsetree
371411
372412 let filterPrinteableAttributes attrs =
373413 List. filter (fun attr -> match attr with
374- | ({Location. txt ="bs" | "ns.ternary" } , _ ) -> false
414+ | ({Location. txt ="bs" | "ns.ternary" | "ns.iflet" } , _ ) -> false
375415 | _ -> true
376416 ) attrs
377417
378418 let partitionPrinteableAttributes attrs =
379419 List. partition (fun attr -> match attr with
380- | ({Location. txt ="bs" | "ns.ternary" } , _ ) -> false
420+ | ({Location. txt ="bs" | "ns.ternary" | "ns.iflet" } , _ ) -> false
381421 | _ -> true
382422 ) attrs
383423
@@ -513,4 +553,4 @@ open Parsetree
513553 {ppat_desc = Ppat_var {txt= " __x" }},
514554 {pexp_desc = Pexp_apply _}
515555 ) -> true
516- | _ -> false
556+ | _ -> false
0 commit comments