@@ -42,18 +42,6 @@ open Parsetree
42
42
in
43
43
process false [] attrs
44
44
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
-
57
45
let collectListExpressions expr =
58
46
let rec collect acc expr = match expr.pexp_desc with
59
47
| Pexp_construct ({txt = Longident. Lident "[]" } , _ ) ->
@@ -165,7 +153,7 @@ open Parsetree
165
153
let filterParsingAttrs attrs =
166
154
List. filter (fun attr ->
167
155
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
169
157
| _ -> true
170
158
) attrs
171
159
@@ -269,7 +257,7 @@ open Parsetree
269
257
270
258
let hasAttributes attrs =
271
259
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
273
261
| _ -> true
274
262
) attrs
275
263
@@ -280,6 +268,52 @@ open Parsetree
280
268
) -> true
281
269
| _ -> false
282
270
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
+
283
317
let rec hasTernaryAttribute attrs =
284
318
match attrs with
285
319
| [] -> false
@@ -318,6 +352,12 @@ open Parsetree
318
352
| _ -> true
319
353
) attrs
320
354
355
+ let filterIfLetAttributes attrs =
356
+ List. filter (fun attr -> match attr with
357
+ | ({Location. txt= " ns.iflet" },_) -> false
358
+ | _ -> true
359
+ ) attrs
360
+
321
361
let isJsxExpression expr =
322
362
let rec loop attrs =
323
363
match attrs with
@@ -371,13 +411,13 @@ open Parsetree
371
411
372
412
let filterPrinteableAttributes attrs =
373
413
List. filter (fun attr -> match attr with
374
- | ({Location. txt ="bs" | "ns.ternary" } , _ ) -> false
414
+ | ({Location. txt ="bs" | "ns.ternary" | "ns.iflet" } , _ ) -> false
375
415
| _ -> true
376
416
) attrs
377
417
378
418
let partitionPrinteableAttributes attrs =
379
419
List. partition (fun attr -> match attr with
380
- | ({Location. txt ="bs" | "ns.ternary" } , _ ) -> false
420
+ | ({Location. txt ="bs" | "ns.ternary" | "ns.iflet" } , _ ) -> false
381
421
| _ -> true
382
422
) attrs
383
423
@@ -513,4 +553,4 @@ open Parsetree
513
553
{ppat_desc = Ppat_var {txt= " __x" }},
514
554
{pexp_desc = Pexp_apply _}
515
555
) -> true
516
- | _ -> false
556
+ | _ -> false
0 commit comments