Skip to content

Report error in Reason syntax when the error happens in a Reason file #1870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jscomp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ BSB_SRCS= bsb_config\
oCamlRes\
bsb_templates\
bsb_init
SUPER_ERRORS_SRCS=super_misc super_warnings super_typecore super_typetexp super_location super_main
SUPER_ERRORS_SRCS=super_reason_oprint super_misc super_warnings super_typemod super_typecore super_typetexp super_location super_main

BSB_CMXS=$(addprefix bsb/, $(addsuffix .cmx, $(BSB_SRCS)))
BSB_CMOS=$(addprefix bsb/, $(addsuffix .cmo, $(BSB_SRCS)))
Expand Down
2 changes: 1 addition & 1 deletion jscomp/super_errors/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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.
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.

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.

Expand Down
26 changes: 12 additions & 14 deletions jscomp/super_errors/super_location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ open Ctype
open Format
open Printtyp

open Location

let file_lines filePath =
(* open_in_bin works on windows, as opposed to open_in, afaik? *)
let chan = open_in_bin filePath in
Expand All @@ -23,7 +21,7 @@ let file_lines filePath =
[||]
with
| End_of_file -> begin
close_in chan;
close_in chan;
List.rev (!lines) |> Array.of_list
end

Expand All @@ -33,25 +31,25 @@ let setup_colors () =
let print_filename ppf file =
Format.fprintf ppf "%s" (Location.show_filename file)

let print_loc ppf loc =
let print_loc ppf (loc: Location.t) =
setup_colors ();
let (file, _, _) = Location.get_pos_info loc.loc_start in
if file = "//toplevel//" then begin
if highlight_locations ppf [loc] then () else
if Location.highlight_locations ppf [loc] then () else
fprintf ppf "Characters %i-%i"
loc.loc_start.pos_cnum loc.loc_end.pos_cnum
end else begin
fprintf ppf "@{<filename>%a@}" print_filename file;
end
;;

let print ~is_warning intro ppf loc =
let print ~is_warning intro ppf (loc: Location.t) =
setup_colors ();
(* TODO: handle locations such as _none_ and "" *)
if loc.loc_start.pos_fname = "//toplevel//"
&& highlight_locations ppf [loc] then ()
&& Location.highlight_locations ppf [loc] then ()
else
if is_warning then
if is_warning then
fprintf ppf "@[@{<info>%s@}@]@," intro
else begin
fprintf ppf "@[@{<error>%s@}@]@," intro
Expand All @@ -64,7 +62,7 @@ let print ~is_warning intro ppf loc =
(* happens sometimes. Syntax error for example *)
fprintf ppf "Is there an error before this one? If so, it's likely a syntax error. The more relevant message should be just above!@ If it's not, please file an issue here:@ github.com/facebook/reason/issues@,"
else begin
try
try
let lines = file_lines file in
fprintf ppf "%a"
(Super_misc.print_file
Expand Down Expand Up @@ -106,15 +104,15 @@ let rec super_error_reporter ppf ({Location.loc; msg; sub; if_highlight} as err)

(* extracted from https://github.com/ocaml/ocaml/blob/4.02/parsing/location.ml#L280 *)
(* This is the warning report entry point. We'll replace the default printer with this one *)
let super_warning_printer loc ppf w =
let super_warning_printer (loc: Location.t) ppf w =
if Warnings.is_active w then begin
Super_misc.setup_colors ppf;
Misc.Color.setup !Clflags.color;
(* open a vertical box. Everything in our message is indented 2 spaces *)
Format.fprintf ppf "@[<v 2>@,%a@,%a@,@]"
(print ~is_warning:true ("Warning number " ^ (Super_warnings.number w |> string_of_int)))
loc
Super_warnings.print
Format.fprintf ppf "@[<v 2>@,%a@,%a@,@]"
(print ~is_warning:true ("Warning number " ^ (Super_warnings.number w |> string_of_int)))
loc
Super_warnings.print
w
end
;;
Expand Down
2 changes: 2 additions & 0 deletions jscomp/super_errors/super_main.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(* the entry point. This is used by js_main.ml *)
let setup () =
Super_misc.setup_reason_syntax_printing ();
Super_location.setup ();
Super_typetexp.setup ();
Super_typecore.setup ();
Super_typemod.setup ();
14 changes: 12 additions & 2 deletions jscomp/super_errors/super_misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let pad ?(ch=' ') content n =
let leading_space_count str =
let rec _leading_space_count str str_length current_index =
if current_index == str_length then current_index
else if (str.[current_index]) <> ' ' then current_index
else if (str.[current_index]) <> ' ' then current_index
else _leading_space_count str str_length (current_index + 1)
in
_leading_space_count str (String.length str) 0
Expand Down Expand Up @@ -63,7 +63,7 @@ let print_file ~is_warning ~range:((start_line, start_char), (end_line, end_char
explicitly tell Format to treat them as length 1 below *)
let separator = if columns_to_cut = 0 then "│" else "┆" in
(* coloring *)
let (highlighted_line_number, highlighted_content): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
let (highlighted_line_number, highlighted_content): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
if is_warning then ("@{<info>%s@}@{<dim> @<1>%s @}", "@{<info>")
else ("@{<error>%s@}@{<dim> @<1>%s @}", "@{<error>")
in
Expand Down Expand Up @@ -125,6 +125,16 @@ let print_file ~is_warning ~range:((start_line, start_char), (end_line, end_char
done;
fprintf ppf "@]" (* v *)

let setup_reason_syntax_printing () =
Oprint.out_value := Super_reason_oprint.print_out_value;
Oprint.out_type := Super_reason_oprint.print_out_type;
Oprint.out_class_type := Super_reason_oprint.print_out_class_type;
Oprint.out_module_type := Super_reason_oprint.print_out_module_type;
Oprint.out_sig_item := Super_reason_oprint.print_out_sig_item;
Oprint.out_signature := Super_reason_oprint.print_out_signature;
Oprint.out_type_extension := Super_reason_oprint.print_out_type_extension;
Oprint.out_phrase := Super_reason_oprint.print_out_phrase

let setup_colors ppf =
Format.pp_set_formatter_tag_functions ppf
({ (Format.pp_get_formatter_tag_functions ppf () ) with
Expand Down
4 changes: 3 additions & 1 deletion jscomp/super_errors/super_misc.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
val print_file: is_warning:bool -> range:(int * int) * (int * int) -> lines:string array -> Format.formatter -> unit -> unit
(** Range coordinates all 1-indexed, like for editors. Otherwise this code
would have way too many off-by-one errors *)
val print_file: is_warning:bool -> range:(int * int) * (int * int) -> lines:string array -> Format.formatter -> unit -> unit

val setup_colors: Format.formatter -> unit

val setup_reason_syntax_printing: unit -> unit
Loading