@@ -145,6 +145,75 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
145145 ] ) ->
146146 default_expr_mapper self
147147 {e with pexp_desc = Pexp_ifthenelse (b, t_exp, Some f_exp)}
148+ (* Transform:
149+ - `@let.unwrap let Ok(inner_pat) = expr`
150+ - `@let.unwrap let Some(inner_pat) = expr`
151+ ...into switches *)
152+ | Pexp_let
153+ ( Nonrecursive ,
154+ [
155+ {
156+ pvb_pat =
157+ {
158+ ppat_desc =
159+ Ppat_construct
160+ ( {txt = Lident ((" Ok" | " Some" ) as variant_name)},
161+ Some _inner_pat );
162+ } as pvb_pat;
163+ pvb_expr;
164+ pvb_attributes;
165+ };
166+ ],
167+ body )
168+ when Ast_attributes. has_unwrap_attr pvb_attributes -> (
169+ let variant =
170+ match variant_name with
171+ | "Ok" -> `Result
172+ | _ -> `Option
173+ in
174+ match pvb_expr.pexp_desc with
175+ | Pexp_pack _ -> default_expr_mapper self e
176+ | _ ->
177+ let ok_case =
178+ {
179+ Parsetree. pc_bar = None ;
180+ pc_lhs = pvb_pat;
181+ pc_guard = None ;
182+ pc_rhs = body;
183+ }
184+ in
185+ let loc = Location. none in
186+ let error_case =
187+ match variant with
188+ | `Result ->
189+ {
190+ Parsetree. pc_bar = None ;
191+ pc_lhs =
192+ Ast_helper.Pat. construct ~loc
193+ {txt = Lident " Error" ; loc}
194+ (Some (Ast_helper.Pat. var ~loc {txt = " e" ; loc}));
195+ pc_guard = None ;
196+ pc_rhs =
197+ Ast_helper.Exp. construct ~loc
198+ {txt = Lident " Error" ; loc}
199+ (Some (Ast_helper.Exp. ident ~loc {txt = Lident " e" ; loc}));
200+ }
201+ | `Option ->
202+ {
203+ Parsetree. pc_bar = None ;
204+ pc_lhs =
205+ Ast_helper.Pat. construct ~loc {txt = Lident " None" ; loc} None ;
206+ pc_guard = None ;
207+ pc_rhs =
208+ Ast_helper.Exp. construct ~loc {txt = Lident " None" ; loc} None ;
209+ }
210+ in
211+ default_expr_mapper self
212+ {
213+ e with
214+ pexp_desc = Pexp_match (pvb_expr, [ok_case; error_case]);
215+ pexp_attributes = e.pexp_attributes @ pvb_attributes;
216+ })
148217 | Pexp_let
149218 ( Nonrecursive ,
150219 [
0 commit comments