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