Skip to content

Commit 4be6da2

Browse files
committed
Report error in Reason syntax when the error happens in a Reason file
1 parent b2a267d commit 4be6da2

10 files changed

+733
-15
lines changed

Diff for: jscomp/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ BSB_SRCS= bsb_config\
342342
oCamlRes\
343343
bsb_templates\
344344
bsb_init
345-
SUPER_ERRORS_SRCS=super_misc super_warnings super_typecore super_typetexp super_location super_main
345+
SUPER_ERRORS_SRCS=super_reason_oprint super_misc super_warnings super_typemod super_typecore super_typetexp super_location super_main
346346

347347
BSB_CMXS=$(addprefix bsb/, $(addsuffix .cmx, $(BSB_SRCS)))
348348
BSB_CMOS=$(addprefix bsb/, $(addsuffix .cmo, $(BSB_SRCS)))

Diff for: jscomp/super_errors/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Hello! This is the subdirectory for the new, newcomer-friendly OCaml/Reason warning & error report system. Most of the logic are lifted from the compiler (https://github.com/ocaml/ocaml/tree/4.02). The convention here is to have a `super_foo` for each corresponding compiler's file `foo`. So, for example, `warnings.ml` becomes `super_warnings.ml`. The exception is `super_main`, the entry point.
1+
Hello! This is the subdirectory for the new, newcomer-friendly OCaml/Reason warning & error report system. Most of the logic are lifted from the compiler (https://github.com/ocaml/ocaml/tree/4.02). The convention here is to have a `super_foo` for each corresponding compiler's file `foo`. So, for example, `warnings.ml` becomes `super_warnings.ml`. The exception is `super_main`, `super_reason_oprint` and `super_misc`. `super_main` is the entry point.
22

33
Feel free to submit new ones or tweak existing messages in these files! They also have more precise comments in them that tells you how they work.
44

Diff for: jscomp/super_errors/super_location.ml

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ open Ctype
99
open Format
1010
open Printtyp
1111

12-
open Location
13-
1412
let file_lines filePath =
1513
(* open_in_bin works on windows, as opposed to open_in, afaik? *)
1614
let chan = open_in_bin filePath in
@@ -33,23 +31,23 @@ let setup_colors () =
3331
let print_filename ppf file =
3432
Format.fprintf ppf "%s" (Location.show_filename file)
3533

36-
let print_loc ppf loc =
34+
let print_loc ppf (loc: Location.t) =
3735
setup_colors ();
3836
let (file, _, _) = Location.get_pos_info loc.loc_start in
3937
if file = "//toplevel//" then begin
40-
if highlight_locations ppf [loc] then () else
38+
if Location.highlight_locations ppf [loc] then () else
4139
fprintf ppf "Characters %i-%i"
4240
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
4341
end else begin
4442
fprintf ppf "@{<filename>%a@}" print_filename file;
4543
end
4644
;;
4745

48-
let print ~is_warning intro ppf loc =
46+
let print ~is_warning intro ppf (loc: Location.t) =
4947
setup_colors ();
5048
(* TODO: handle locations such as _none_ and "" *)
5149
if loc.loc_start.pos_fname = "//toplevel//"
52-
&& highlight_locations ppf [loc] then ()
50+
&& Location.highlight_locations ppf [loc] then ()
5351
else
5452
if is_warning then
5553
fprintf ppf "@[@{<info>%s@}@]@," intro
@@ -106,7 +104,7 @@ let rec super_error_reporter ppf ({Location.loc; msg; sub; if_highlight} as err)
106104

107105
(* extracted from https://github.com/ocaml/ocaml/blob/4.02/parsing/location.ml#L280 *)
108106
(* This is the warning report entry point. We'll replace the default printer with this one *)
109-
let super_warning_printer loc ppf w =
107+
let super_warning_printer (loc: Location.t) ppf w =
110108
if Warnings.is_active w then begin
111109
Super_misc.setup_colors ppf;
112110
Misc.Color.setup !Clflags.color;

Diff for: jscomp/super_errors/super_main.ml

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ let setup () =
33
Super_location.setup ();
44
Super_typetexp.setup ();
55
Super_typecore.setup ();
6+
Super_typemod.setup ();

Diff for: jscomp/super_errors/super_misc.ml

+19-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let pad ?(ch=' ') content n =
2121
let leading_space_count str =
2222
let rec _leading_space_count str str_length current_index =
2323
if current_index == str_length then current_index
24-
else if (str.[current_index]) <> ' ' then current_index
24+
else if (str.[current_index]) <> ' ' then current_index
2525
else _leading_space_count str str_length (current_index + 1)
2626
in
2727
_leading_space_count str (String.length str) 0
@@ -63,7 +63,7 @@ let print_file ~is_warning ~range:((start_line, start_char), (end_line, end_char
6363
explicitly tell Format to treat them as length 1 below *)
6464
let separator = if columns_to_cut = 0 then "" else "" in
6565
(* coloring *)
66-
let (highlighted_line_number, highlighted_content): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
66+
let (highlighted_line_number, highlighted_content): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
6767
if is_warning then ("@{<info>%s@}@{<dim> @<1>%s @}", "@{<info>")
6868
else ("@{<error>%s@}@{<dim> @<1>%s @}", "@{<error>")
6969
in
@@ -125,6 +125,23 @@ let print_file ~is_warning ~range:((start_line, start_char), (end_line, end_char
125125
done;
126126
fprintf ppf "@]" (* v *)
127127

128+
let is_reason_file file =
129+
let extension = Ext_filename.get_extension file in
130+
extension = ".re" || extension = ".rei"
131+
132+
let setup_reason_syntax_printing_if_file_is_reason loc =
133+
let (file, _, _) = Location.get_pos_info loc.Location.loc_start in
134+
if is_reason_file file then begin
135+
Oprint.out_value := Super_reason_oprint.print_out_value;
136+
Oprint.out_type := Super_reason_oprint.print_out_type;
137+
Oprint.out_class_type := Super_reason_oprint.print_out_class_type;
138+
Oprint.out_module_type := Super_reason_oprint.print_out_module_type;
139+
Oprint.out_sig_item := Super_reason_oprint.print_out_sig_item;
140+
Oprint.out_signature := Super_reason_oprint.print_out_signature;
141+
Oprint.out_type_extension := Super_reason_oprint.print_out_type_extension;
142+
Oprint.out_phrase := Super_reason_oprint.print_out_phrase
143+
end
144+
128145
let setup_colors ppf =
129146
Format.pp_set_formatter_tag_functions ppf
130147
({ (Format.pp_get_formatter_tag_functions ppf () ) with

Diff for: jscomp/super_errors/super_misc.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
val print_file: is_warning:bool -> range:(int * int) * (int * int) -> lines:string array -> Format.formatter -> unit -> unit
12
(** Range coordinates all 1-indexed, like for editors. Otherwise this code
23
would have way too many off-by-one errors *)
3-
val print_file: is_warning:bool -> range:(int * int) * (int * int) -> lines:string array -> Format.formatter -> unit -> unit
44

55
val setup_colors: Format.formatter -> unit
6+
7+
val setup_reason_syntax_printing_if_file_is_reason: Location.t -> unit

0 commit comments

Comments
 (0)