Skip to content

Commit 104fe9f

Browse files
authored
%todo extension (rescript-lang#6713)
* PoC for adding a %todo extension that warns with proper locations * raise instead of emulating Obj.magic * align * use Js.Exn.raiseError and include ReScript file name + location of todo code in error thrown at runtime * update fixtures * change text * fixtures * changelog
1 parent 4a38f30 commit 104fe9f

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
1313
# 11.1.0-rc.8 (Unreleased)
1414

15+
#### :rocket: New Feature
16+
17+
- Add `%todo` extension for leaving implementation for later. https://github.com/rescript-lang/rescript-compiler/pull/6713
18+
1519
#### :bug: Bug Fix
1620

1721
- Improve error when using '@deriving(accessors)' on a variant with record arguments. https://github.com/rescript-lang/rescript-compiler/pull/6712
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
Warning number 110
3+
/.../fixtures/todo_with_no_payload.res:1:38-42
4+
5+
1 │ let implementMeLater = (): string => %todo
6+
2 │
7+
3 │ let x = implementMeLater()
8+
9+
Todo found.
10+
11+
This code is not implemented yet and will crash at runtime. Make sure you implement this before running the code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
Warning number 110
3+
/.../fixtures/todo_with_payload.res:1:38-85
4+
5+
1 │ let implementMeLater = (): string => %todo("This should return a string 
6+
│ eventually.")
7+
2 │
8+
3 │ let x = implementMeLater()
9+
10+
Todo found: This should return a string eventually.
11+
12+
This code is not implemented yet and will crash at runtime. Make sure you implement this before running the code.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let implementMeLater = (): string => %todo
2+
3+
let x = implementMeLater()
4+
5+
Js.log(x->Js.String2.includes("x"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let implementMeLater = (): string => %todo("This should return a string eventually.")
2+
3+
let x = implementMeLater()
4+
5+
Js.log(x->Js.String2.includes("x"))

jscomp/ext/warnings.ml

+7
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ type t =
8686
| Bs_integer_literal_overflow (* 107 *)
8787
| Bs_uninterpreted_delimiters of string (* 108 *)
8888
| Bs_toplevel_expression_unit of (string * topLevelUnitHelp) option (* 109 *)
89+
| Bs_todo of string option (* 110 *)
8990

9091
(* If you remove a warning, leave a hole in the numbering. NEVER change
9192
the numbers of existing warnings.
@@ -151,6 +152,7 @@ let number = function
151152
| Bs_integer_literal_overflow -> 107
152153
| Bs_uninterpreted_delimiters _ -> 108
153154
| Bs_toplevel_expression_unit _ -> 109
155+
| Bs_todo _ -> 110
154156

155157
let last_warning_number = 110
156158

@@ -509,6 +511,11 @@ let message = function
509511
| Other -> "yourExpression") in
510512
Printf.sprintf "\n\n Possible solutions:\n - Assigning to a value that is then ignored: `let _ = %s`\n - Piping into the built-in ignore function to ignore the result: `%s->ignore`" helpText helpText
511513
| _ -> "")
514+
| Bs_todo maybe_text -> (
515+
match maybe_text with
516+
| None -> "Todo found."
517+
| Some todo -> "Todo found: " ^ todo
518+
) ^ "\n\n This code is not implemented yet and will crash at runtime. Make sure you implement this before running the code."
512519

513520
let sub_locs = function
514521
| Deprecated (_, def, use) ->

jscomp/ext/warnings.mli

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type t =
7979
| Bs_integer_literal_overflow (* 107 *)
8080
| Bs_uninterpreted_delimiters of string (* 108 *)
8181
| Bs_toplevel_expression_unit of (string * topLevelUnitHelp) option (* 109 *)
82+
| Bs_todo of string option (* 110 *)
8283

8384
val parse_options : bool -> string -> unit
8485

jscomp/frontend/ast_exp_extension.ml

+29
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,35 @@ open Ast_helper
2626
let handle_extension e (self : Bs_ast_mapper.mapper)
2727
(({txt; loc}, payload) : Parsetree.extension) =
2828
match txt with
29+
| "todo" ->
30+
let todo_message =
31+
match Ast_payload.is_single_string 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+
let pretext =
37+
loc.loc_start.pos_fname ^ ":"
38+
^ string_of_int loc.loc_start.pos_lnum
39+
^ ":"
40+
^ string_of_int loc.loc_start.pos_cnum
41+
^ "-"
42+
^ string_of_int loc.loc_end.pos_cnum
43+
in
44+
45+
Exp.apply ~loc
46+
(Exp.ident ~loc {txt = Longident.parse "Js.Exn.raiseError"; loc})
47+
[
48+
( Nolabel,
49+
Exp.constant ~loc
50+
(Pconst_string
51+
( (pretext
52+
^
53+
match todo_message with
54+
| None -> " - Todo"
55+
| Some msg -> " - Todo: " ^ msg),
56+
None )) );
57+
]
2958
| "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload
3059
| "bs.raw" | "raw" ->
3160
Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload

0 commit comments

Comments
 (0)