Skip to content

Commit 0a50228

Browse files
committed
PoC for adding a %todo extension that warns with proper locations
1 parent 77d234f commit 0a50228

File tree

7 files changed

+50
-0
lines changed

7 files changed

+50
-0
lines changed
Lines changed: 11 additions & 0 deletions
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 will crash if it's run. Be sure to finish it before running your program.
Lines changed: 12 additions & 0 deletions
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 will crash if it's run. Be sure to finish it before running your program.
Lines changed: 5 additions & 0 deletions
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"))
Lines changed: 5 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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\nThis code will crash if it's run. Be sure to finish it before running your program."
512519

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

jscomp/ext/warnings.mli

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ 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+
Location.prerr_warning e.Parsetree.pexp_loc
31+
(Bs_todo
32+
(match Ast_payload.is_single_string payload with
33+
| Some (s, _) -> Some s
34+
| None -> None));
35+
Exp.apply ~loc
36+
(Exp.ident ~loc {txt = Longident.parse "Obj.magic"; loc})
37+
[(Nolabel, Exp.construct ~loc {txt = Longident.Lident "()"; loc} None)]
2938
| "ffi" -> Ast_exp_handle_external.handle_ffi ~loc ~payload
3039
| "bs.raw" | "raw" ->
3140
Ast_exp_handle_external.handle_raw ~kind:Raw_exp loc payload

0 commit comments

Comments
 (0)