From 8be3f207e60b7f72c7a72d2583dd1cac85b5c52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Mon, 5 Dec 2016 22:48:35 +0100 Subject: [PATCH] Add Runner.prepare_config and Output.print_skipped_checks Refs #244 --- lib/credo/check/runner.ex | 32 ++-- lib/credo/cli/command/explain.ex | 1 + lib/credo/cli/command/list.ex | 1 + lib/credo/cli/command/suggest.ex | 1 + lib/credo/cli/output.ex | 24 +++ lib/credo/cli/output/categories.ex | 19 +-- lib/credo/cli/output/explain.ex | 137 ++++++++++-------- lib/credo/cli/output/issue_helper.ex | 43 +++--- lib/credo/cli/output/issues_by_scope.ex | 54 +++---- .../cli/output/issues_grouped_by_category.ex | 25 ++-- lib/credo/cli/output/issues_short_list.ex | 12 +- lib/credo/cli/output/summary.ex | 41 +++--- lib/credo/cli/output/ui.ex | 2 +- 13 files changed, 218 insertions(+), 174 deletions(-) diff --git a/lib/credo/check/runner.ex b/lib/credo/check/runner.ex index abe2dbd9f..4cbec2be0 100644 --- a/lib/credo/check/runner.ex +++ b/lib/credo/check/runner.ex @@ -4,12 +4,6 @@ defmodule Credo.Check.Runner do alias Credo.Service.SourceFileIssues def run(source_files, config) when is_list(source_files) do - config = - config - |> set_lint_attributes(source_files) - |> exclude_low_priority_checks(config.min_priority - 9) - |> exclude_checks_based_on_elixir_version - {_time_run_on_all, source_files_after_run_on_all} = :timer.tc fn -> run_checks_that_run_on_all(source_files, config) @@ -36,6 +30,16 @@ defmodule Credo.Check.Runner do %SourceFile{source_file | issues: source_file.issues ++ issues} end + @doc """ + Prepares the Config struct based on a given list of `source_files`. + """ + def prepare_config(source_files, config) do + config + |> set_lint_attributes(source_files) + |> exclude_low_priority_checks(config.min_priority - 9) + |> exclude_checks_based_on_elixir_version + end + defp set_lint_attributes(config, source_files) do lint_attribute_map = source_files @@ -69,18 +73,18 @@ defmodule Credo.Check.Runner do defp exclude_checks_based_on_elixir_version(config) do version = System.version() - skipped_checks = Enum.reject(config.checks, fn - ({check}) -> Version.match?(version, check.elixir_version) - ({check, _}) -> Version.match?(version, check.elixir_version) - end) - checks = Enum.filter(config.checks, fn - ({check}) -> Version.match?(version, check.elixir_version) - ({check, _}) -> Version.match?(version, check.elixir_version) - end) + skipped_checks = + Enum.reject(config.checks, &matches_requirement?(&1, version)) + checks = + Enum.filter(config.checks, &matches_requirement?(&1, version)) %Config{config | checks: checks, skipped_checks: skipped_checks} end + defp matches_requirement?({check, _}, version) do + Version.match?(version, check.elixir_version) + end + defp run_checks_that_run_on_all(source_files, config) do checks = config |> Config.checks |> Enum.filter(&run_on_all_check?/1) diff --git a/lib/credo/cli/command/explain.ex b/lib/credo/cli/command/explain.ex index 27a809966..ac2bbc06a 100644 --- a/lib/credo/cli/command/explain.ex +++ b/lib/credo/cli/command/explain.ex @@ -16,6 +16,7 @@ defmodule Credo.CLI.Command.Explain do def run([], _), do: print_help() def run([file | _], config) do {_, source_files} = load_and_validate_source_files(config) + config = Runner.prepare_config(source_files, config) {_, {source_files, config}} = run_checks(source_files, config) file diff --git a/lib/credo/cli/command/list.ex b/lib/credo/cli/command/list.ex index d69c63063..a377cfe03 100644 --- a/lib/credo/cli/command/list.ex +++ b/lib/credo/cli/command/list.ex @@ -14,6 +14,7 @@ defmodule Credo.CLI.Command.List do def run(_args, %Config{help: true}), do: print_help() def run(_args, config) do {time_load, source_files} = load_and_validate_source_files(config) + config = Runner.prepare_config(source_files, config) {time_run, {source_files, config}} = run_checks(source_files, config) print_results_and_summary(source_files, config, time_load, time_run) diff --git a/lib/credo/cli/command/suggest.ex b/lib/credo/cli/command/suggest.ex index 1c3f1a9b7..e3dd55182 100644 --- a/lib/credo/cli/command/suggest.ex +++ b/lib/credo/cli/command/suggest.ex @@ -14,6 +14,7 @@ defmodule Credo.CLI.Command.Suggest do def run(_args, %Config{help: true}), do: print_help() def run(_args, config) do {time_load, source_files} = load_and_validate_source_files(config) + config = Runner.prepare_config(source_files, config) out = output_mod(config) out.print_before_info(source_files, config) diff --git a/lib/credo/cli/output.ex b/lib/credo/cli/output.ex index a71097e30..3fdb46ed2 100644 --- a/lib/credo/cli/output.ex +++ b/lib/credo/cli/output.ex @@ -1,5 +1,6 @@ defmodule Credo.CLI.Output do alias Credo.CLI.Output.UI + alias Credo.Config @category_tag_map %{"refactor" => "F"} @@ -98,4 +99,27 @@ defmodule Credo.CLI.Output do end) |> UI.puts end + + def print_skipped_checks(%Config{skipped_checks: []}), do: nil + def print_skipped_checks(%Config{skipped_checks: skipped_checks}) do + msg = + [ + :reset, :bright, :orange, "info: ", :reset, :faint, "the following checks were skipped because they're not compatible with\n", + :reset, :faint, "your version of Elixir (#{System.version()}). Upgrade to the newest version of Elixir to\n", + :reset, :faint, "get the most out of Credo!\n", + ] + UI.puts + UI.puts(msg, :faint) + Enum.each(skipped_checks, &print_skipped_check_name/1) + UI.puts + end + + defp print_skipped_check_name({check, _check_info}) do + check_name = + check + |> to_string + |> String.replace(~r/^Elixir\./, "") + + UI.puts(" - #{check_name}", :faint) + end end diff --git a/lib/credo/cli/output/categories.ex b/lib/credo/cli/output/categories.ex index 5de2e825f..d5667de6a 100644 --- a/lib/credo/cli/output/categories.ex +++ b/lib/credo/cli/output/categories.ex @@ -44,21 +44,22 @@ defmodule Credo.CLI.Output.Categories do ] def print do - Enum.each(@order, &print_category/1) + @order + |> Enum.each(&print_category/1) end defp print_category(category) do term_width = Output.term_columns color = @category_colors[category] title = @category_titles[category] - output = [ - :bright, String.to_atom("#{color}_background"), color, " ", - Output.foreground_color(color), :normal, - String.ljust(" #{title}", term_width - 1), - ] UI.puts - UI.puts(output) + [ + :bright, "#{color}_background" |> String.to_atom, color, " ", + Output.foreground_color(color), :normal, + " #{title}" |> String.ljust(term_width - 1), + ] + |> UI.puts color |> UI.edge @@ -70,8 +71,8 @@ defmodule Credo.CLI.Output.Categories do end defp print_line(line, color) do - output = [UI.edge(color), " ", :reset, line] - UI.puts(output) + [UI.edge(color), " ", :reset, line] + |> UI.puts end end diff --git a/lib/credo/cli/output/explain.ex b/lib/credo/cli/output/explain.ex index 2fdf093b0..f5e1d20b0 100644 --- a/lib/credo/cli/output/explain.ex +++ b/lib/credo/cli/output/explain.ex @@ -9,13 +9,15 @@ defmodule Credo.CLI.Output.Explain do @indent 8 @doc "Called before the analysis is run." - def print_before_info(source_files, _config) do + def print_before_info(source_files, config) do UI.puts case Enum.count(source_files) do 0 -> UI.puts "No files found!" 1 -> UI.puts "Checking 1 source file ..." count -> UI.puts "Checking #{count} source files ..." end + + Output.print_skipped_checks(config) end @doc "Called after the analysis has run." @@ -43,31 +45,34 @@ defmodule Credo.CLI.Output.Explain do end defp print_issues(issues, _filename, source_file, _config, term_width, _line_no, _column) do - first_issue = List.first(issues) + first_issue = issues |> List.first scope_name = Scope.mod_name(first_issue.scope) color = Output.check_color(first_issue) - output = [ - :bright, String.to_atom("#{color}_background"), color, " ", + + UI.puts + + [ + :bright, "#{color}_background" |> String.to_atom, color, " ", Output.foreground_color(color), :normal, - String.ljust(" #{scope_name}", term_width - 1), + " #{scope_name}" |> String.ljust(term_width - 1), ] + |> UI.puts - UI.puts - UI.puts(output) UI.puts_edge(color) - Enum.each(issues, &print_issue(&1, source_file, term_width)) + issues + |> Enum.each(&print_issue(&1, source_file, term_width)) end defp filter_issues(issues, line_no, nil) do - line_no = String.to_integer(line_no) - Enum.filter(issues, &filter_issue(&1, line_no, nil)) + line_no = line_no |> String.to_integer + issues |> Enum.filter(&filter_issue(&1, line_no, nil)) end defp filter_issues(issues, line_no, column) do - line_no = String.to_integer(line_no) - column = String.to_integer(column) + line_no = line_no |> String.to_integer + column = column |> String.to_integer - Enum.filter(issues, &filter_issue(&1, line_no, column)) + issues |> Enum.filter(&filter_issue(&1, line_no, column)) end defp filter_issue(%Issue{line_no: a, column: b}, a, b), do: true @@ -75,14 +80,16 @@ defmodule Credo.CLI.Output.Explain do defp filter_issue(_, _, _), do: false defp print_issue(%Issue{check: check, message: message, filename: filename, priority: priority} = issue, source_file, term_width) do - pos = pos_string(issue.line_no, issue.column) + pos = + pos_string(issue.line_no, issue.column) + outer_color = Output.check_color(issue) inner_color = Output.check_color(issue) message_color = inner_color filename_color = :default_color tag_style = if outer_color == inner_color, do: :faint, else: :bright - category_output = [ + [ UI.edge(outer_color), inner_color, tag_style, @@ -90,45 +97,45 @@ defmodule Credo.CLI.Output.Explain do Output.check_tag(check.category), :reset, " Category: #{check.category} " ] - UI.puts(category_output) + |> UI.puts - priority_output = [ + [ UI.edge(outer_color), inner_color, tag_style, " ", - Output.priority_arrow(priority), + priority |> Output.priority_arrow, :reset, " Priority: #{Output.priority_name(priority)} " ] - UI.puts(priority_output) + |> UI.puts UI.puts_edge(outer_color) - message_output = [ + [ UI.edge(outer_color), inner_color, tag_style, " ", :normal, message_color, " ", message, ] - UI.puts(message_output) + |> UI.puts - scope_output = [ + [ UI.edge(outer_color, @indent), - filename_color, :faint, to_string(filename), + filename_color, :faint, filename |> to_string, :default_color, :faint, pos, :faint, " (#{issue.scope})" ] - UI.puts(scope_output) + |> UI.puts if issue.line_no do UI.puts_edge([outer_color, :faint]) - question_output = [ + [ UI.edge([outer_color, :faint]), :reset, :color239, String.duplicate(" ", @indent - 5), "__ CODE IN QUESTION" ] - UI.puts(question_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) @@ -146,12 +153,12 @@ defmodule Credo.CLI.Output.Explain do atom -> atom |> to_string |> String.length end - column_output = [ + [ UI.edge([outer_color, :faint], @indent), inner_color, String.duplicate(" ", x), :faint, String.duplicate("^", w) ] - UI.puts(column_output) + |> UI.puts end print_source_line(source_file, issue.line_no + 1, term_width, code_color, outer_color) print_source_line(source_file, issue.line_no + 2, term_width, code_color, outer_color) @@ -159,17 +166,15 @@ defmodule Credo.CLI.Output.Explain do UI.puts_edge([outer_color, :faint], @indent) - why_it_matters_output = [ + [ UI.edge([outer_color, :faint]), :reset, :color239, String.duplicate(" ", @indent - 5), "__ WHY IT MATTERS" ] - UI.puts(why_it_matters_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - explanation = issue.check.explanation || "TODO: Insert explanation" - - explanation + (issue.check.explanation || "TODO: Insert explanation") |> String.strip |> String.split("\n") |> Enum.flat_map(&format_explanation(&1, outer_color)) @@ -178,31 +183,34 @@ defmodule Credo.CLI.Output.Explain do UI.puts_edge([outer_color, :faint]) - print_params_explanation(issue.check, outer_color) + issue.check + |> print_params_explanation(outer_color) UI.puts_edge([outer_color, :faint]) end - defp print_source_line(%SourceFile{lines: lines}, line_no, _, _, _) when line_no < 1 or line_no > length(lines) do + defp print_source_line(_, line_no, _, _, _) when line_no < 1 do nil end - defp print_source_line(%SourceFile{lines: lines}, line_no, term_width, color, outer_color) do - {_, line} = Enum.at(lines, line_no - 1) + defp print_source_line(source_file, line_no, term_width, color, outer_color) do + {_, line} = Enum.at(source_file.lines, line_no - 1) - line_no_str = String.rjust("#{line_no} ", @indent - 2) + line_no_str = + "#{line_no} " + |> String.rjust(@indent - 2) - line_no_output = [ + [ UI.edge([outer_color, :faint]), :reset, :faint, line_no_str, :reset, color, UI.truncate(line, term_width - @indent) ] - UI.puts(line_no_output) + |> UI.puts end def format_explanation(line, outer_color) do [ UI.edge([outer_color, :faint], @indent), - :reset, format_explanation_text(line), + :reset, line |> format_explanation_text, "\n" ] end @@ -227,70 +235,71 @@ defmodule Credo.CLI.Output.Explain do keywords = check.explanation_for_params check_name = check |> to_string |> String.replace(~r/^Elixir\./, "") - config_output = [ + [ UI.edge([outer_color, :faint]), :reset, :color239, String.duplicate(" ", @indent-5), "__ CONFIGURATION OPTIONS", ] - UI.puts(config_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) if keywords |> List.wrap |> Enum.any? do - keywords_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), "To configure this check, use this tuple" ] - UI.puts(keywords_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - params_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), " {", :cyan, check_name, :reset, ", ", :cyan, :faint, "", :reset ,"}" ] - UI.puts(params_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - additional_params_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), "with ", :cyan, :faint, "", :reset ," being ", :cyan, "false", :reset, " or any combination of these keywords:" ] - UI.puts(additional_params_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - Enum.each(keywords, fn({param, text}) -> - output = [ - UI.edge([outer_color, :faint]), :reset, - String.duplicate(" ", @indent-2), - :cyan, String.ljust(" #{param}:", 20), - :reset, text - ] - UI.puts(output) - end) + keywords + |> Enum.each(fn({param, text}) -> + [ + UI.edge([outer_color, :faint]), :reset, + String.duplicate(" ", @indent-2), + :cyan, " #{param}:" |> String.ljust(20), + :reset, text + ] + |> UI.puts + end) else - disable_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), "You can disable this check by using this tuple" ] - UI.puts(disable_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - edge_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), " {", :cyan, check_name, :reset, ", ", :cyan, "false", :reset ,"}" ] - UI.puts(edge_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) - config_output = [ + [ UI.edge([outer_color, :faint]), :reset, String.duplicate(" ", @indent-2), "There are no other configuration options." ] - UI.puts(config_output) + |> UI.puts UI.puts_edge([outer_color, :faint]) end diff --git a/lib/credo/cli/output/issue_helper.ex b/lib/credo/cli/output/issue_helper.ex index a6248c5a1..39623647a 100644 --- a/lib/credo/cli/output/issue_helper.ex +++ b/lib/credo/cli/output/issue_helper.ex @@ -11,10 +11,10 @@ defmodule Credo.CLI.Output.IssueHelper do %Config{format: "flycheck"} = _config, _term_width) do tag = Output.check_tag(issue, false) - output = [ - to_string(filename), Filename.pos_suffix(issue.line_no, issue.column), ": ", tag, ": ", message, + [ + filename |> to_string, Filename.pos_suffix(issue.line_no, issue.column), ": ", tag, ": ", message, ] - UI.puts(output) + |> UI.puts end def print_issue(%Issue{check: check, message: message, filename: filename, priority: priority} = issue, _source_file, %Config{format: "oneline"} = _config, _term_width) do @@ -22,14 +22,14 @@ defmodule Credo.CLI.Output.IssueHelper do message_color = inner_color filename_color = :default_color - output = [ + [ inner_color, - Output.check_tag(check.category), " ", Output.priority_arrow(priority), " ", - :reset, filename_color, :faint, to_string(filename), + Output.check_tag(check.category), " ", priority |> Output.priority_arrow, " ", + :reset, filename_color, :faint, filename |> to_string, :default_color, :faint, Filename.pos_suffix(issue.line_no, issue.column), :reset, message_color, " ", message, ] - UI.puts(output) + |> UI.puts end def print_issue(%Issue{check: check, message: message, filename: filename, priority: priority} = issue, source_file, %Config{format: _} = config, term_width) do outer_color = Output.check_color(issue) @@ -42,13 +42,13 @@ defmodule Credo.CLI.Output.IssueHelper do |> UI.wrap_at(term_width - @indent) |> print_issue_message(check, outer_color, message_color, tag_style, priority) - output = [ + [ UI.edge(outer_color, @indent), - filename_color, :faint, to_string(filename), + filename_color, :faint, filename |> to_string, :default_color, :faint, Filename.pos_suffix(issue.line_no, issue.column), :faint, " (#{issue.scope})" ] - UI.puts(output) + |> UI.puts if config.verbose do print_issue_line(issue, source_file, inner_color, outer_color, term_width) @@ -58,27 +58,28 @@ defmodule Credo.CLI.Output.IssueHelper do end defp print_issue_message([first_line | other_lines], check, outer_color, message_color, tag_style, priority) do - output = [ + [ UI.edge(outer_color), outer_color, tag_style, - Output.check_tag(check.category), " ", Output.priority_arrow(priority), + Output.check_tag(check.category), " ", priority |> Output.priority_arrow, :normal, message_color, " ", first_line, ] - UI.puts(output) + |> UI.puts - Enum.each(other_lines, &print_issue_message(&1, outer_color, message_color)) + other_lines + |> Enum.each(&print_issue_message(&1, outer_color, message_color)) end defp print_issue_message("", _outer_color, _message_color) do end defp print_issue_message(message, outer_color, message_color) do - output = [ + [ UI.edge(outer_color), outer_color, String.duplicate(" ", @indent - 3), :normal, message_color, " ", message, ] - UI.puts(output) + |> UI.puts end defp print_issue_line(%Issue{line_no: nil}, _source_file, _inner_color, _outer_color, _term_width) do @@ -86,18 +87,18 @@ defmodule Credo.CLI.Output.IssueHelper do end defp print_issue_line(%Issue{} = issue, source_file, inner_color, outer_color, term_width) do {_, raw_line} = Enum.at(source_file.lines, issue.line_no - 1) - line = String.strip(raw_line) + line = raw_line |> String.strip [outer_color, :faint] |> UI.edge |> UI.puts - output = [ + [ UI.edge([outer_color, :faint]), :cyan, :faint, String.duplicate(" ", @indent-2), UI.truncate(line, term_width - @indent) ] - UI.puts(output) + |> UI.puts print_issue_trigger_marker(issue, raw_line, inner_color, outer_color) end @@ -114,11 +115,11 @@ defmodule Credo.CLI.Output.IssueHelper do atom -> atom |> to_string |> String.length end - output = [ + [ UI.edge([outer_color, :faint], @indent), inner_color, String.duplicate(" ", x), :faint, String.duplicate("^", w) ] - UI.puts(output) + |> UI.puts end end diff --git a/lib/credo/cli/output/issues_by_scope.ex b/lib/credo/cli/output/issues_by_scope.ex index bffa36d45..8b14dfa4f 100644 --- a/lib/credo/cli/output/issues_by_scope.ex +++ b/lib/credo/cli/output/issues_by_scope.ex @@ -11,13 +11,15 @@ defmodule Credo.CLI.Output.IssuesByScope do @indent 8 @doc "Called before the analysis is run." - def print_before_info(source_files, _config) do + def print_before_info(source_files, config) do UI.puts "" case Enum.count(source_files) do 0 -> UI.puts "No files found!" 1 -> UI.puts "Checking 1 source file ..." count -> UI.puts "Checking #{count} source files ..." end + + Output.print_skipped_checks(config) end @doc "Called after the analysis has run." @@ -29,7 +31,8 @@ defmodule Credo.CLI.Output.IssuesByScope do |> Enum.sort_by(&(&1.filename)) |> Enum.each(&print_issues(&1, config, term_width)) - Summary.print(source_files, config, time_load, time_run) + source_files + |> Summary.print(config, time_load, time_run) end @lint {Credo.Check.Refactor.PipeChainStart, false} @@ -40,21 +43,21 @@ defmodule Credo.CLI.Output.IssuesByScope do |> print_issues(filename, source_file, config, term_width) end defp print_issues(issues, _filename, source_file, _config, term_width) do - if Enum.any?(issues) do - first_issue = List.first(issues) + if issues |> Enum.any? do + first_issue = issues |> List.first scope_name = Scope.mod_name(first_issue.scope) color = Output.check_color(first_issue) - output = [ - :bright, String.to_atom("#{color}_background"), color, " ", - Output.foreground_color(color), :normal, - String.ljust(" #{scope_name}", term_width - 1), - ] UI.puts - UI.puts(output) - color - |> UI.edge + [ + :bright, "#{color}_background" |> String.to_atom, color, " ", + Output.foreground_color(color), :normal, + " #{scope_name}" |> String.ljust(term_width - 1), + ] + |> UI.puts + + UI.edge(color) |> UI.puts issues @@ -70,33 +73,34 @@ defmodule Credo.CLI.Output.IssuesByScope do filename_color = :default_color tag_style = if outer_color == inner_color, do: :faint, else: :bright - output = [ + [ UI.edge(outer_color), inner_color, tag_style, - Output.check_tag(check.category), " ", Output.priority_arrow(priority), + Output.check_tag(check.category), " ", priority |> Output.priority_arrow, :normal, message_color, " ", message, ] - UI.puts(output) + |> UI.puts - edge = [ + [ UI.edge(outer_color, @indent), - filename_color, :faint, to_string(filename), + filename_color, :faint, filename |> to_string, :default_color, :faint, Filename.pos_suffix(issue.line_no, issue.column), :faint, " (#{issue.scope})" ] - UI.puts(edge) + |> UI.puts if issue.line_no do {_, line} = Enum.at(source_file.lines, issue.line_no - 1) - line_edge = [ + + UI.puts_edge([outer_color, :faint]) + + [ UI.edge([outer_color, :faint]), :cyan, :faint, String.duplicate(" ", @indent - 2), UI.truncate(line, term_width - @indent) ] - - UI.puts_edge([outer_color, :faint]) - UI.puts(line_edge) + |> UI.puts if issue.column do offset = String.length(line) - String.length(String.strip(line)) @@ -106,13 +110,13 @@ defmodule Credo.CLI.Output.IssuesByScope do nil -> 1 atom -> atom |> to_string |> String.length end - column_edge = [ + + [ UI.edge([outer_color, :faint], @indent), inner_color, String.duplicate(" ", x), :faint, String.duplicate("^", w) ] - - UI.puts(column_edge) + |> UI.puts end end diff --git a/lib/credo/cli/output/issues_grouped_by_category.ex b/lib/credo/cli/output/issues_grouped_by_category.ex index b61d8738b..bc6d1127d 100644 --- a/lib/credo/cli/output/issues_grouped_by_category.ex +++ b/lib/credo/cli/output/issues_grouped_by_category.ex @@ -32,12 +32,14 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do def print_before_info(_source_files, %Config{format: "flycheck"}) do :ok end - def print_before_info(source_files, _config) do + def print_before_info(source_files, config) do case Enum.count(source_files) do 0 -> UI.puts "No files found!" 1 -> UI.puts "Checking 1 source file ..." count -> UI.puts "Checking #{count} source files#{checking_suffix(count)} ..." end + + Output.print_skipped_checks(config) end defp checking_suffix(count) do @@ -52,7 +54,7 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do def print_after_info(source_files, config, time_load, time_run) do term_width = Output.term_columns - issues = Enum.flat_map(source_files, &(&1.issues)) + issues = source_files |> Enum.flat_map(&(&1.issues)) shown_issues = issues |> Filter.important(config) @@ -66,7 +68,7 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do issue_map = categories |> Enum.map(fn(category) -> - {category, Enum.filter(shown_issues, &(&1.category == category))} + {category, shown_issues |> Enum.filter(&(&1.category == category))} end) |> Enum.into(%{}) @@ -81,7 +83,8 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do print_issues(category, issue_map[category], source_file_map, config, term_width) end) - Summary.print(source_files, config, time_load, time_run) + source_files + |> Summary.print(config, time_load, time_run) end defp print_issues(_category, nil, _source_file_map, _config, _term_width) do @@ -99,12 +102,12 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do UI.puts - title_output = [ - :bright, String.to_atom("#{color}_background"), color, " ", + [ + :bright, "#{color}_background" |> String.to_atom, color, " ", Output.foreground_color(color), :normal, - String.ljust(" #{title}", term_width - 1), + " #{title}" |> String.ljust(term_width - 1), ] - UI.puts(title_output) + |> UI.puts color |> UI.edge @@ -115,8 +118,8 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do if Enum.count(issues) > per_category(config) do not_shown = Enum.count(issues) - per_category(config) - not_shown_output = [UI.edge(color), :faint, " ... (#{not_shown} more, use `-a` to show them)"] - UI.puts(not_shown_output) + [UI.edge(color), :faint, " ... (#{not_shown} more, use `-a` to show them)"] + |> UI.puts end end @@ -124,7 +127,7 @@ defmodule Credo.CLI.Output.IssuesGroupedByCategory do issues |> Enum.sort_by(fn(issue) -> {issue.priority, issue.severity} end) |> Enum.reverse - |> Enum.take(per_category(config)) + |> Enum.take(config |> per_category) |> Enum.each(&print_issue(&1, source_file_map, config, term_width)) end diff --git a/lib/credo/cli/output/issues_short_list.ex b/lib/credo/cli/output/issues_short_list.ex index 7ed49d363..820c8a7d5 100644 --- a/lib/credo/cli/output/issues_short_list.ex +++ b/lib/credo/cli/output/issues_short_list.ex @@ -9,11 +9,13 @@ defmodule Credo.CLI.Output.IssuesShortList do @indent 8 @doc "Called before the analysis is run." - def print_before_info(source_files, _config) do + def print_before_info(source_files, config) do case Enum.count(source_files) do 0 -> UI.puts "No files found!" _ -> :ok end + + Output.print_skipped_checks(config) end @doc "Called after the analysis has run." @@ -44,15 +46,15 @@ defmodule Credo.CLI.Output.IssuesShortList do message_color = inner_color filename_color = :default_color - output = [ + [ inner_color, check_tag_style(outer_color, inner_color), - Output.check_tag(check.category), " ", Output.priority_arrow(priority), " ", - :reset, filename_color, :faint, to_string(filename), + Output.check_tag(check.category), " ", priority |> Output.priority_arrow, " ", + :reset, filename_color, :faint, filename |> to_string, :default_color, :faint, Filename.pos_suffix(issue.line_no, issue.column), :reset, message_color, " ", message, ] - UI.puts(output) + |> UI.puts end defp check_tag_style(a, a), do: :faint diff --git a/lib/credo/cli/output/summary.ex b/lib/credo/cli/output/summary.ex index 830f9eff4..ce9dcbe86 100644 --- a/lib/credo/cli/output/summary.ex +++ b/lib/credo/cli/output/summary.ex @@ -34,7 +34,6 @@ defmodule Credo.CLI.Output.Summary do UI.puts UI.puts [:faint, @cry_for_help] UI.puts - print_skipped_checks(config.skipped_checks) UI.puts [:faint, format_time_spent(time_load, time_run)] UI.puts summary_parts(source_files, shown_issues) @@ -42,28 +41,17 @@ defmodule Credo.CLI.Output.Summary do # print_badge(source_files, issues) UI.puts - print_priority_hint(shown_issues, config) - end - - defp print_skipped_checks(checks) do - msg = """ -The following checks were skipped because they're not compatible with your -version of Elixir (#{System.version()}). Upgrade to the newest version of Elixir to -get the most out of Credo! - """ - UI.puts(msg, :faint) - Enum.each(checks, fn({check, _check_info}) -> UI.puts(" - #{check}", :faint) end) - UI.puts + shown_issues |> print_priority_hint(config) end def print_priority_hint([], %Config{min_priority: min_priority}) when min_priority >= 0 do - hint = "Use `--strict` to show all issues, `--help` for options." - UI.puts(hint, :faint) + "Use `--strict` to show all issues, `--help` for options." + |> UI.puts(:faint) end def print_priority_hint([], _config), do: nil def print_priority_hint(_, %Config{min_priority: min_priority}) when min_priority >= 0 do - hint = "Showing priority issues: ↑ ↗ → (use `--strict` to show all issues, `--help` for options)." - UI.puts(hint, :faint) + "Showing priority issues: ↑ ↗ → (use `--strict` to show all issues, `--help` for options)." + |> UI.puts(:faint) end def print_priority_hint(_, _config), do: nil @@ -71,7 +59,9 @@ get the most out of Credo! def print_badge(source_files, issues) do scopes = scope_count(source_files) - parts = Enum.map(@category_wording, fn({category, _, _}) -> category_count(issues, category) end) + parts = + @category_wording + |> Enum.map(fn({category, _, _}) -> category_count(issues, category) end) parts = [scopes] ++ parts sum = Enum.sum(parts) @@ -88,18 +78,19 @@ get the most out of Credo! if index == 0 do :green else - {category, _, _} = Enum.at(@category_wording, index - 1) + {category, _, _} = @category_wording |> Enum.at(index - 1) Output.check_color(category) end [color, String.duplicate("=", round(quota * width))] end) - UI.puts([bar]) + [bar] + |> UI.puts end defp format_time_spent(time_load, time_run) do - time_run = div(time_run, 10_000) - time_load = div(time_load, 10_000) + time_run = time_run |> div(10_000) + time_load = time_load |> div(10_000) formatted_total = format_in_seconds(time_run + time_load) total_in_seconds = @@ -133,10 +124,12 @@ get the most out of Credo! end defp summary_parts(source_files, issues) do - parts = Enum.flat_map(@category_wording, &summary_part(&1, issues)) + parts = + @category_wording + |> Enum.flat_map(&summary_part(&1, issues)) parts = - List.update_at(parts, Enum.count(parts) - 1, fn(last_part) -> + parts |> List.update_at(Enum.count(parts) - 1, fn(last_part) -> String.replace(last_part, ", ", "") end) diff --git a/lib/credo/cli/output/ui.ex b/lib/credo/cli/output/ui.ex index f0e8d89e2..772e4dc57 100644 --- a/lib/credo/cli/output/ui.ex +++ b/lib/credo/cli/output/ui.ex @@ -4,7 +4,7 @@ defmodule Credo.CLI.Output.UI do @ellipsis "…" def edge(color, indent \\ 2) when is_integer(indent) do - [:reset, color, String.ljust(@edge, indent)] + [:reset, color, @edge |> String.ljust(indent)] end defdelegate puts, to: Bunt