|
23 | 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) |
24 | 24 | open Ast_helper |
25 | 25 |
|
| 26 | +(* [%todo]: warns, and throws at runtime with the location and message. [loc] |
| 27 | + is the location of [%todo], [expr_loc] that of the whole expression. *) |
| 28 | +let todo ~loc ~expr_loc (message : string option) = |
| 29 | + Location.prerr_warning expr_loc (Bs_todo message); |
| 30 | + (* Like [__LOC__]: the file's base name, so the generated code does not |
| 31 | + depend on where it was built, and columns rather than offsets. *) |
| 32 | + let file, line, start_col = Location.get_pos_info loc.Location.loc_start in |
| 33 | + let end_col = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + start_col in |
| 34 | + let pretext = |
| 35 | + Printf.sprintf "%s:%d:%d-%d" (Filename.basename file) line start_col end_col |
| 36 | + in |
| 37 | + Exp.apply ~loc |
| 38 | + (* The compilation unit, not [JsError]: a user module of that name would |
| 39 | + shadow the [Stdlib] alias. *) |
| 40 | + (Exp.ident ~loc |
| 41 | + {txt = Ldot (Lident "Stdlib_JsError", "throwWithMessage"); loc}) |
| 42 | + [ |
| 43 | + ( Nolabel, |
| 44 | + Exp.constant ~loc |
| 45 | + (Ast_helper.Const.string |
| 46 | + (pretext |
| 47 | + ^ |
| 48 | + match message with |
| 49 | + | None -> " - Todo" |
| 50 | + | Some msg -> " - Todo: " ^ msg)) ); |
| 51 | + ] |
| 52 | + |
26 | 53 | let handle_extension e (_self : Ast_mapper.mapper) |
27 | 54 | (({txt; loc}, payload) : Parsetree.extension) = |
28 | 55 | match txt with |
29 | 56 | | "todo" -> |
30 | | - let todo_message = |
31 | | - match Ast_payload.semantic_string_of_payload payload with |
32 | | - | Some s -> Some s |
33 | | - | None -> None |
34 | | - in |
35 | | - Location.prerr_warning e.Parsetree.pexp_loc (Bs_todo todo_message); |
36 | | - (* Like [__LOC__]: the file's base name, so the generated code does not |
37 | | - depend on where it was built, and columns rather than offsets. *) |
38 | | - let file, line, start_col = Location.get_pos_info loc.loc_start in |
39 | | - let end_col = loc.loc_end.pos_cnum - loc.loc_start.pos_cnum + start_col in |
40 | | - let pretext = |
41 | | - Printf.sprintf "%s:%d:%d-%d" (Filename.basename file) line start_col |
42 | | - end_col |
43 | | - in |
44 | | - |
45 | | - Exp.apply ~loc |
46 | | - (* The compilation unit, not [JsError]: a user module of that name |
47 | | - would shadow the [Stdlib] alias. *) |
48 | | - (Exp.ident ~loc |
49 | | - {txt = Ldot (Lident "Stdlib_JsError", "throwWithMessage"); loc}) |
50 | | - [ |
51 | | - ( Nolabel, |
52 | | - Exp.constant ~loc |
53 | | - (Ast_helper.Const.string |
54 | | - (pretext |
55 | | - ^ |
56 | | - match todo_message with |
57 | | - | None -> " - Todo" |
58 | | - | Some msg -> " - Todo: " ^ msg)) ); |
59 | | - ] |
| 57 | + (* A payload the parser did not turn into [Pexp_todo], or one from a v0 |
| 58 | + PPX. A payload other than a string gives no message. *) |
| 59 | + todo ~loc ~expr_loc:e.Parsetree.pexp_loc |
| 60 | + (Ast_payload.semantic_string_of_payload payload) |
60 | 61 | | "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload |
61 | 62 | | "raw" -> Ast_exp_handle_external.handle_raw loc payload |
62 | 63 | | _ -> e |
|
0 commit comments