File tree Expand file tree Collapse file tree 4 files changed +117
-3
lines changed
tests/syntax_tests/data/ast-mapping Expand file tree Collapse file tree 4 files changed +117
-3
lines changed Original file line number Diff line number Diff line change @@ -310,6 +310,20 @@ module E = struct
310
310
| _ -> true )
311
311
attrs
312
312
313
+ let extract_finally_attribute attrs =
314
+ List. find_map
315
+ (function
316
+ | {Location. txt = "res.finally" } , Pt. PPat (_ , Some expr ) -> Some expr
317
+ | _ -> None )
318
+ attrs
319
+
320
+ let remove_finally_attribute attrs =
321
+ List. filter
322
+ (function
323
+ | {Location. txt = "res.finally" } , _ -> false
324
+ | _ -> true )
325
+ attrs
326
+
313
327
let map_jsx_children sub (e : expression ) : Pt.jsx_children =
314
328
let rec visit (e : expression ) : Pt.expression list =
315
329
match e.pexp_desc with
@@ -460,8 +474,9 @@ module E = struct
460
474
| Pexp_match (e , pel ) ->
461
475
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
462
476
| Pexp_try (e , pel ) ->
463
- try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
464
- (Some (ident {txt = Longident. Lident " _" ; loc}))
477
+ let finally_expr = extract_finally_attribute attrs in
478
+ let attrs = remove_finally_attribute attrs in
479
+ try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel) finally_expr
465
480
| Pexp_tuple el -> tuple ~loc ~attrs (List. map (sub.expr sub) el)
466
481
(* <></> *)
467
482
| Pexp_construct ({txt = Longident. Lident " []" | Longident. Lident " ::" }, _)
Original file line number Diff line number Diff line change @@ -419,7 +419,16 @@ module E = struct
419
419
args)
420
420
| Pexp_match (e , pel ) ->
421
421
match_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
422
- | Pexp_try (e , pel , _ ) ->
422
+ | Pexp_try (e , pel , finally_expr ) ->
423
+ let attrs =
424
+ match finally_expr with
425
+ | Some expr ->
426
+ let finally_attr =
427
+ sub.attribute sub (Location. mknoloc " res.finally" , Parsetree. PPat (Ast_helper.Pat. any () , Some expr))
428
+ in
429
+ finally_attr :: attrs
430
+ | None -> attrs
431
+ in
423
432
try_ ~loc ~attrs (sub.expr sub e) (sub.cases sub pel)
424
433
| Pexp_tuple el -> tuple ~loc ~attrs (List. map (sub.expr sub) el)
425
434
| Pexp_construct (lid , arg ) ->
Original file line number Diff line number Diff line change
1
+ // Test for try..catch (existing syntax)
2
+ let tryCatch = try {
3
+ Console .log ("Trying" )
4
+ } catch {
5
+ | _ => Console .log ("Caught" )
6
+ }
7
+
8
+ // Test for try..finally (new syntax)
9
+ let tryFinally = try {
10
+ Console .log ("Trying" )
11
+ } finally {
12
+ Console .log ("Finally" )
13
+ }
14
+
15
+ // Test for try..catch..finally (new syntax)
16
+ let tryCatchFinally = try {
17
+ Console .log ("Trying" )
18
+ } catch {
19
+ | _ => Console .log ("Caught" )
20
+ } finally {
21
+ Console .log ("Finally" )
22
+ }
23
+
24
+ // Test with complex expressions
25
+ let complexTry = try {
26
+ let x = 1 + 2
27
+ let y = x * 3
28
+ Some (y )
29
+ } catch {
30
+ | Not_found => None
31
+ | exn => Console .log ("Error: " ++ Js .String .make (exn ))
32
+ }
33
+
34
+ // Test nested try expressions
35
+ let nestedTry = try {
36
+ try {
37
+ dangerousOperation ()
38
+ } catch {
39
+ | InnerError => Console .log ("Inner caught" )
40
+ }
41
+ } catch {
42
+ | OuterError => Console .log ("Outer caught" )
43
+ } finally {
44
+ Console .log ("Outer finally" )
45
+ }
Original file line number Diff line number Diff line change
1
+ // Test for try..catch (existing syntax)
2
+ let tryCatch = try {
3
+ Console.log("Trying")
4
+ } catch {
5
+ | _ => Console.log("Caught")
6
+ }
7
+
8
+ // Test for try..finally (new syntax)
9
+ let tryFinally = try {
10
+ Console.log("Trying")
11
+ } finally {
12
+ Console.log("Finally")
13
+ }
14
+
15
+ // Test for try..catch..finally (new syntax)
16
+ let tryCatchFinally = try {
17
+ Console.log("Trying")
18
+ } catch {
19
+ | _ => Console.log("Caught")
20
+ } finally {
21
+ Console.log("Finally")
22
+ }
23
+
24
+ // Test with complex expressions
25
+ let complexTry = try {
26
+ let x = 1 + 2
27
+ let y = x * 3
28
+ Some(y)
29
+ } catch {
30
+ | Not_found => None
31
+ | exn => Console.log("Error: " ++ Js.String.make(exn))
32
+ }
33
+
34
+ // Test nested try expressions
35
+ let nestedTry = try {
36
+ try {
37
+ dangerousOperation()
38
+ } catch {
39
+ | InnerError => Console.log("Inner caught")
40
+ }
41
+ } catch {
42
+ | OuterError => Console.log("Outer caught")
43
+ } finally {
44
+ Console.log("Outer finally")
45
+ }
You can’t perform that action at this time.
0 commit comments