Skip to content

Commit

Permalink
remove the spellcheck bolierplate, add a hint for missing type rec
Browse files Browse the repository at this point in the history
…in napkin
  • Loading branch information
bobzhang committed Jul 8, 2020
1 parent be70ce6 commit 63b6ad6
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions jscomp/super_errors/super_typetexp.ml
Expand Up @@ -9,51 +9,33 @@ open Ctype *)
open Format
open Printtyp

(* taken from https://github.com/BuckleScript/ocaml/blob/d4144647d1bf9bc7dc3aadc24c25a7efa3a67915/typing/typetexp.ml#L869 *)
let spellcheck ppf fold env lid =
let cutoff =
match String.length (Longident.last lid) with
| 1 | 2 -> 0
| 3 | 4 -> 1
| 5 | 6 -> 2
| _ -> 3
in
let compare target head acc =
let (best_choice, best_dist) = acc in
match Misc.edit_distance target head cutoff with
| None -> (best_choice, best_dist)
| Some dist ->
let choice =
if dist < best_dist then [head]
else if dist = best_dist then head :: best_choice
else best_choice in
(choice, min dist best_dist)
in
let init = ([], max_int) in
let handle (choice, _dist) =
match List.rev choice with
| [] -> ()
| last :: rev_rest ->
(* the modified part *)
fprintf ppf "@[<v 2>@,@,@{<info>Hint: Did you mean %s%s%s?@}@]"
(String.concat ", " (List.rev rev_rest))
(if rev_rest = [] then "" else " or ")
last
in
let did_you_mean ppf choices : bool =
(* flush now to get the error report early, in the (unheard of) case
where the linear search would take a bit of time; in the worst
case, the user has seen the error, she can interrupt the process
before the spell-checking terminates. *)
fprintf ppf "@?";
fprintf ppf "@?";
match choices () with
| [] -> false
| last :: rev_rest ->
fprintf ppf "@[<v 2>@,@,@{<info>Hint: Did you mean %s%s%s?@}@]"
(String.concat ", " (List.rev rev_rest))
(if rev_rest = [] then "" else " or ")
last;
true


let spellcheck ppf fold env lid =
let choices path name : string list =
let env : string list = fold (fun x _ _ xs -> x ::xs ) path env [] in
Misc.spellcheck env name in
match lid with
| Longident.Lapply _ -> ()
| Longident.Lapply _ -> false
| Longident.Lident s ->
handle (fold (compare s) None env init)
did_you_mean ppf (fun _ -> choices None s)
| Longident.Ldot (r, s) ->
handle (fold (compare s) (Some r) env init)
did_you_mean ppf (fun _ -> choices (Some r) s)

let spellcheck ppf fold =
spellcheck ppf (fun f -> fold (fun s _ _ x -> f s x))

let fold_descr fold get_name f = fold (fun descr acc -> f (get_name descr) acc)
let fold_constructors x = fold_descr Env.fold_constructors (fun d -> d.cstr_name) x
Expand All @@ -64,8 +46,10 @@ let fold_labels x = fold_descr Env.fold_labels (fun d -> d.lbl_name) x
let report_error env ppf = function
| Typetexp.Unbound_type_constructor lid ->
(* modified *)
fprintf ppf "This type constructor's parameter, `%a`, can't be found. Is it a typo?" longident lid;
spellcheck ppf Env.fold_types env lid;
fprintf ppf "@[<v>This type constructor, `%a`, can't be found.@ " longident lid;
let has_candidate = spellcheck ppf Env.fold_types env lid in
if !Js_config.napkin && not has_candidate then
fprintf ppf "For recursive type declaration, you may forget rec in `type rec` ?@]"
| Unbound_value lid ->
(* modified *)
begin
Expand All @@ -76,7 +60,7 @@ let report_error env ppf = function
Printtyp.longident outer;
| other_ident -> fprintf ppf "The value %a can't be found" Printtyp.longident other_ident
end;
spellcheck ppf Env.fold_values env lid;
spellcheck ppf Env.fold_values env lid |> ignore
| Unbound_module lid ->
(* modified *)
begin match lid with
Expand All @@ -103,7 +87,7 @@ let report_error env ppf = function
longident lid
end
end;
spellcheck ppf Env.fold_modules env lid
spellcheck ppf Env.fold_modules env lid |> ignore
| Unbound_constructor lid ->
(* modified *)
fprintf ppf "@[<v>\
Expand Down

0 comments on commit 63b6ad6

Please sign in to comment.