Skip to content

Commit

Permalink
Add Runner.prepare_config and Output.print_skipped_checks
Browse files Browse the repository at this point in the history
Refs #244
  • Loading branch information
rrrene authored and svoynow committed Dec 7, 2016
1 parent b146bf6 commit 8be3f20
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 174 deletions.
32 changes: 18 additions & 14 deletions lib/credo/check/runner.ex
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lib/credo/cli/command/explain.ex
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/credo/cli/command/list.ex
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/credo/cli/command/suggest.ex
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions 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"}

Expand Down Expand Up @@ -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
19 changes: 10 additions & 9 deletions lib/credo/cli/output/categories.ex
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 8be3f20

Please sign in to comment.