Skip to content

Commit

Permalink
remove --old-output-format
Browse files Browse the repository at this point in the history
Summary:
we have too many output formats. this one's not necessary. if someone
*really* wanted it, they could recreate it from the --json output.

Fixes #2844

Reviewed By: gabelevi

Differential Revision: D4184543

fbshipit-source-id: 68f80ef183c1049dccd8e4c48b072069be4cc717
  • Loading branch information
mroch authored and Facebook Github Bot committed Nov 21, 2016
1 parent c48dc1a commit a17e73f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 174 deletions.
6 changes: 2 additions & 4 deletions src/commands/commandUtils.ml
Expand Up @@ -85,15 +85,15 @@ let sleep seconds =
then timeout_go_boom ()
else Unix.sleep seconds

let collect_error_flags main color one_line show_all_errors old_output_format =
let collect_error_flags main color one_line show_all_errors =
let color = match color with
| Some "never" -> Tty.Color_Never
| Some "always" -> Tty.Color_Always
| Some "auto"
| None -> Tty.Color_Auto
| _ -> assert false (* the enum type enforces this *)
in
main { Options.color; one_line; show_all_errors; old_output_format; }
main { Options.color; one_line; show_all_errors; }

let error_flags prev = CommandSpec.ArgSpec.(
prev
Expand All @@ -104,8 +104,6 @@ let error_flags prev = CommandSpec.ArgSpec.(
~doc:"Escapes newlines so that each error prints on one line"
|> flag "--show-all-errors" no_arg
~doc:"Print all errors (the default is to truncate after 50 errors)"
|> flag "--old-output-format" no_arg
~doc:"Use old output format (absolute file names, line and column numbers)"
)

let json_flags prev = CommandSpec.ArgSpec.(
Expand Down
126 changes: 12 additions & 114 deletions src/common/errors.ml
Expand Up @@ -97,27 +97,6 @@ let message_of_string s =

let internal_error_prefix = "Internal error (see logs): "

(* for old error format. DEPRECATED *)
let prepend_kind_message messages kind =
match kind, messages with
| _, BlameM ({Loc.source = Some Loc.LibFile _; _} as loc, _) :: _ ->
let header = match kind with
| ParseError -> "Library parse error:"
| InferError -> "Library type error:"
(* TODO: we don't publicly distinguish warnings vs errors right now *)
| InferWarning -> "Library type error:"
| InternalError -> internal_error_prefix
(* TODO: is this possible? What happens when there are two `declare
module`s with the same name? *)
| DuplicateProviderError -> "Library duplicate provider error:"
in
BlameM (loc, header) :: messages
| InternalError, BlameM (loc, msg) :: messages ->
BlameM (loc, internal_error_prefix^msg) :: messages
| InternalError, CommentM msg :: messages ->
CommentM (internal_error_prefix^msg) :: messages
| _ -> messages

let prepend_op_reason messages = function
| Some message -> message :: (message_of_string "Error:") :: messages
| None -> messages
Expand All @@ -128,25 +107,6 @@ let append_trace_reasons message_list trace_reasons =
| _ ->
message_list @ (BlameM (Loc.none, "Trace:") :: trace_reasons)

(* note: this is used only in old output format, which is deprecated
except for error diffs. indenting would be hard (locs are printed
hard left in the old format), so we leave the tree flat. If there
are any use cases for humans reading the old format, will need to
revisit. *)
let messages_of_extra extra =
let rec messages_of_info_tree tree =
let infos, kids = match tree with
| InfoLeaf infos -> infos, []
| InfoNode (infos, kids) -> infos, kids
in
infos_to_messages infos @
List.concat (List.map messages_of_info_tree kids)
in
List.concat (List.map messages_of_info_tree extra)

let append_extra_info messages extra =
messages @ messages_of_extra extra

let rec strip_root_from_info_tree root tree =
let strip_root_from_infos root =
List.map (function (loc, strs) ->
Expand Down Expand Up @@ -191,71 +151,6 @@ let strip_root_from_errors root errors =
) ae;
Array.to_list ae

let format_reason_color
?(first=false)
?(one_line=false)
(message: message)
= Loc.(
let loc, s = to_pp message in
let l0, c0 = loc.start.line, loc.start.column + 1 in
let l1, c1 = loc._end.line, loc._end.column in
let err_clr = if first then Tty.Normal Tty.Red else Tty.Normal Tty.Green in
let file_clr = if first then Tty.Bold Tty.Blue else Tty.Bold Tty.Magenta in
let line_clr = Tty.Normal Tty.Yellow in
let col_clr = Tty.Normal Tty.Cyan in

let s = if one_line then Str.global_replace (Str.regexp "\n") "\\n" s else s in

let source = match loc.source with
| Some LibFile filename
| Some SourceFile filename
| Some JsonFile filename
| Some ResourceFile filename -> [file_clr, filename]
| None | Some Builtins -> []
in
let loc_format =
source @
(if l0 > 0 && c0 > 0 && l1 > 0 && c1 > 0 then [
(Tty.Normal Tty.Default, ":");
(line_clr, string_of_int l0);
(Tty.Normal Tty.Default, ":");
(col_clr, string_of_int c0);
(Tty.Normal Tty.Default, ",")
] @ (if l0 < l1 then [
(line_clr, string_of_int l1);
(Tty.Normal Tty.Default, ":")
] else []) @ [
(col_clr, string_of_int c1)
] else []) in
let s_format = [
(err_clr, s);
(Tty.Normal Tty.Default, if one_line then "\\n" else "\n");
] in
if loc_format = [] then s_format
else loc_format @ ((Tty.Normal Tty.Default, ": ")::s_format)
)

let format_info info =
let msg = info_to_message info in
let formatted = format_reason_color msg in
String.concat "" (List.map snd formatted)

let print_reason_color ?(out_channel=stdout) ~first ~one_line ~color (message: message) =
let to_print = format_reason_color ~first ~one_line message in
(if first then Printf.printf "\n");
Tty.cprint ~color_mode:color ~out_channel to_print

let print_error_color_old
?(out_channel=stdout) ~one_line ~color ~strip_root ~root
(e : error) =
let e = if strip_root then strip_root_from_error root e else e in
let { kind; messages; op; trace; extra } = e in
let messages = prepend_kind_message messages kind in
let messages = prepend_op_reason messages op in
let messages = append_extra_info messages extra in
let messages = append_trace_reasons messages trace in
print_reason_color ~out_channel ~first:true ~one_line ~color (List.hd messages);
List.iter (print_reason_color ~out_channel ~first:false ~one_line ~color) (List.tl messages)

let file_location_style text = (Tty.Underline Tty.Default, text)
let default_style text = (Tty.Normal Tty.Default, text)
Expand Down Expand Up @@ -548,7 +443,7 @@ let merge_comments_into_blames messages =
let remove_newlines (color, text) =
(color, Str.global_replace (Str.regexp "\n") "\\n" text)

let get_pretty_printed_error_new ~stdin_file:stdin_file ~strip_root ~one_line ~root
let get_pretty_printed_error ~stdin_file:stdin_file ~strip_root ~one_line ~root
(error : error) =
let { kind; messages; op; trace; extra } = error in
let messages = prepend_op_reason messages op in
Expand All @@ -569,9 +464,16 @@ let get_pretty_printed_error_new ~stdin_file:stdin_file ~strip_root ~one_line ~r
else to_print in
(to_print @ [default_style "\n"])

let print_error_color_new ?(out_channel=stdout) ~stdin_file:stdin_file ~strip_root ~one_line ~color ~root (error : error) =
let print_error_color
?(out_channel=stdout)
~stdin_file:stdin_file
~strip_root
~one_line
~color
~root
(error : error) =
let to_print =
get_pretty_printed_error_new ~stdin_file ~strip_root ~one_line ~root error in
get_pretty_printed_error ~stdin_file ~strip_root ~one_line ~root error in
Tty.cprint ~out_channel ~color_mode:color to_print

(* TODO: deprecate this in favor of Reason.json_of_loc *)
Expand Down Expand Up @@ -898,13 +800,9 @@ let print_error_summary ?(out_channel=stdout) ~flags ?(stdin_file=None) ~strip_r
let truncate = not (flags.Options.show_all_errors) in
let one_line = flags.Options.one_line in
let color = flags.Options.color in
let print_error_color = if flags.Options.old_output_format
then print_error_color_old ~strip_root ~root
else print_error_color_new ~stdin_file ~strip_root ~root
in
let print_error_if_not_truncated curr e =
if not(truncate) || curr < 50
then print_error_color ~one_line ~color ~out_channel e;
if not(truncate) || curr < 50 then print_error_color
~stdin_file ~strip_root ~root ~one_line ~color ~out_channel e;

curr + 1
in
Expand Down
13 changes: 0 additions & 13 deletions src/common/errors.mli
Expand Up @@ -80,16 +80,6 @@ val json_of_errors_with_context :
error list ->
Hh_json.json

val print_error_color_new:
?out_channel:out_channel ->
stdin_file:stdin_file ->
strip_root:bool ->
one_line:bool ->
color:Tty.color_mode ->
root: Path.t ->
error ->
unit

val print_error_json :
strip_root: bool ->
root:Path.t ->
Expand All @@ -116,6 +106,3 @@ val print_error_deprecated:
strip_root: bool ->
root: Path.t ->
out_channel -> error list -> unit

(* only used to get indentation info for trace formatting - TODO fumigate *)
val format_info: info -> string
2 changes: 0 additions & 2 deletions src/common/options.ml
Expand Up @@ -12,7 +12,6 @@ type error_flags = {
color: Tty.color_mode;
one_line: bool;
show_all_errors: bool;
old_output_format: bool;
}

type esproposal_feature_mode =
Expand Down Expand Up @@ -76,7 +75,6 @@ let default_error_flags = {
color = Tty.Color_Auto;
one_line = false;
show_all_errors = false;
old_output_format = false;
}

let all opts = opts.opt_all
Expand Down
8 changes: 1 addition & 7 deletions src/typing/flow_error.ml
Expand Up @@ -298,13 +298,7 @@ end = struct
downstream for obscure reasons, and then to messages *)
let max_trace_depth = Context.max_trace_depth cx in
if max_trace_depth = 0 then [] else
let strip_root = Context.should_strip_root cx in
let root = Context.root cx in
let prep_path r =
if not strip_root then r
else Reason.strip_root root r
in
Trace.reasons_of_trace ~prep_path ~level:max_trace_depth trace
Trace.reasons_of_trace ~level:max_trace_depth trace
|> List.map info_of_reason
in
(* NOTE: We include the operation's reason in the error message, unless it
Expand Down
42 changes: 13 additions & 29 deletions src/typing/trace.ml
Expand Up @@ -106,47 +106,31 @@ let index_trace =
tmap, imap


let spaces n = String.make n ' '

(* string length of printed position, as it would
appear in an error *)
let pos_len ~prep_path r =
let r = prep_path r in
let loc = loc_of_reason r in
let str = Errors.format_info (loc, []) in
String.length str


(* scan a trace tree, return maximum position length
of reasons at or above the given depth limit, and
min of that limit and actual max depth *)
let max_pos_len_and_depth ~prep_path limit trace =
let rec f (len, depth) (lower, upper, parent, _) =
let len = max len (pos_len ~prep_path (reason_of_t lower)) in
let len = max len (pos_len ~prep_path (reason_of_use_t upper)) in
if depth > limit then len, depth
let max_depth_of_trace limit trace =
let rec f depth (_, _, parent, _) =
if depth > limit then depth
else (
match parent with
| Parent [] -> len, depth
| Parent trace -> List.fold_left f (len, depth + 1) trace
| Parent [] -> depth
| Parent trace -> List.fold_left f (depth + 1) trace
)
in List.fold_left f (0, 0) trace
in List.fold_left f 0 trace


(* reformat a reason's description with
- the given left margin
- the given prefix and suffix: if either is nonempty,
"desc" becomes "prefix[desc]suffix"
*)
let pretty_r ~prep_path margin r prefix suffix =
let len = pos_len ~prep_path r in
let ind = if margin > len then spaces (margin - len) else "" in
let pretty_r r prefix suffix =
replace_reason (fun desc ->
let desc_str = string_of_desc desc in
let custom =
if prefix = "" && suffix = ""
then spf "%s%s" ind desc_str
else spf "%s%s%s" (ind ^ spf "%s[" prefix) desc_str (spf "]%s" suffix)
then desc_str
else spf "%s[%s]%s" prefix desc_str suffix
in
RCustom custom
) r
Expand All @@ -163,8 +147,8 @@ let pretty_r ~prep_path margin r prefix suffix =
if the step was derived from another path, we append a note
to that effect.
*)
let reasons_of_trace ~prep_path ?(level=0) trace =
let max_pos_len, max_depth = max_pos_len_and_depth ~prep_path level trace in
let reasons_of_trace ?(level=0) trace =
let max_depth = max_depth_of_trace level trace in
let level = min level max_depth in

let tmap, imap = index_trace level trace in
Expand All @@ -181,11 +165,11 @@ let reasons_of_trace ~prep_path ?(level=0) trace =
(* omit lower if it's a pipelined tvar *)
(if is_pipelined_tvar ~steps ~i lower
then []
else [pretty_r ~prep_path max_pos_len (reason_of_t_add_id lower)
else [pretty_r (reason_of_t_add_id lower)
(spf "%s " (string_of_ctor lower)) ""]
)
@
[pretty_r ~prep_path max_pos_len (reason_of_use_t_add_id upper)
[pretty_r (reason_of_use_t_add_id upper)
(spf "~> %s " (string_of_use_ctor upper))
(if parent = []
then ""
Expand Down
1 change: 0 additions & 1 deletion src/typing/trace.mli
Expand Up @@ -18,7 +18,6 @@ val rec_trace: max: int -> Type.t -> Type.use_t -> t -> t
val concat_trace: t list -> t

val reasons_of_trace:
prep_path: (Reason.reason -> Reason.reason) ->
?level:int ->
t ->
Reason.reason list
6 changes: 3 additions & 3 deletions tests/suppress_traces/suppress_traces.exp
Expand Up @@ -14,7 +14,7 @@ Trace:
15: var a: string = foo('hi'); // error number ~> string
^^^^^^^^^ ~> UseT(UnknownUse, OpenT) [assignment of var `a`] (from path 2)
15: var a: string = foo('hi'); // error number ~> string
^ ~> UseT(UnknownUse, OpenT) [var `a`]
^ ~> UseT(UnknownUse, OpenT) [var `a`]
* path 2:
15: var a: string = foo('hi'); // error number ~> string
^^^^^^^^^ NumT [number]
Expand Down Expand Up @@ -42,9 +42,9 @@ Trace:
^^^^^ ~> CallT [function call]
* path 5:
9: function bar(): number { return 5; }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FunT [function]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FunT [function]
9: function bar(): number { return 5; }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~> UseT(UnknownUse, OpenT) [function]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ~> UseT(UnknownUse, OpenT) [function]
12: return bar();
^^^ ~> ReposLowerT [identifier `bar`]
* path 6:
Expand Down
2 changes: 1 addition & 1 deletion tests/traces/traces.exp
Expand Up @@ -18,7 +18,7 @@ Trace:
^ ~> ReposLowerT [identifier `x`] (from [not shown])
* path 3:
4: f0(0);
^ NumT [number]
^ NumT [number]
3: function f0(x) { g0(x) }
^ ~> UseT(FunCallParam, OpenT) [parameter `x`] (from [not shown])
3: function f0(x) { g0(x) }
Expand Down

0 comments on commit a17e73f

Please sign in to comment.