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0.4.10
0.4.10-dev
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.4.0-beta5
0.4.0-beta4
0.4.0-beta3
0.4.0-beta2
0.4.0-beta1
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-dev2
0.3.0-dev
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.1-dev
A static code analysis tool with a focus on code consistency and teaching.
Current section
Files
Jump to
Current section
Files
lib/credo/config_builder.ex
defmodule Credo.ConfigBuilder do
alias Credo.CLI.Filename
alias Credo.CLI.Options
alias Credo.ConfigFile
alias Credo.Execution
@pattern_split_regex ~r/,\s*/
def parse(exec) do
options = exec.cli_options
case get_config_file(exec, options) do
{:ok, config_file} ->
exec
|> add_config_file_to_exec(config_file)
|> add_strict_to_exec(config_file, options)
|> add_switches_to_exec(options.switches)
|> run_cli_switch_plugin_param_converters()
{:error, _} = error ->
error
end
end
defp get_config_file(exec, %Options{} = options) do
config_name = options.switches[:config_name]
config_filename = options.switches[:config_file]
dir = Filename.remove_line_no_and_column(options.path)
if is_binary(config_filename) do
filename = Path.expand(config_filename)
ConfigFile.read_from_file_path(exec, dir, filename, config_name)
else
ConfigFile.read_or_default(exec, dir, config_name)
end
end
defp add_config_file_to_exec(exec, %ConfigFile{} = config_file) do
%Execution{
exec
| files: config_file.files,
color: config_file.color,
checks: config_file.checks,
requires: config_file.requires,
plugins: config_file.plugins,
parse_timeout: config_file.parse_timeout
}
end
defp add_strict_to_exec(exec, %ConfigFile{} = config_file, options) do
%Execution{
exec
| strict: strict_via_args_or_config_file?(options.args, config_file)
}
end
defp strict_via_args_or_config_file?([], config_file) do
config_file.strict
end
defp strict_via_args_or_config_file?([potential_path | _], config_file) do
user_expecting_explain_command? = Filename.contains_line_no?(potential_path)
user_expecting_explain_command? || config_file.strict
end
defp add_switches_to_exec(%Execution{} = exec, switches) do
exec
|> add_switch_all(switches)
|> add_switch_color(switches)
|> add_switch_crash_on_error(switches)
|> add_switch_debug(switches)
|> add_switch_enable_disabled_checks(switches)
|> add_switch_files_excluded(switches)
|> add_switch_files_included(switches)
|> add_switch_checks_without_tag(switches)
|> add_switch_checks_with_tag(switches)
|> add_switch_format(switches)
|> add_switch_help(switches)
|> add_switch_ignore(switches)
|> add_switch_mute_exit_status(switches)
|> add_switch_only(switches)
|> add_switch_read_from_stdin(switches)
|> add_switch_strict(switches)
|> add_switch_min_priority(switches)
|> add_switch_verbose(switches)
|> add_switch_version(switches)
end
# add_switch_all
defp add_switch_all(exec, %{all: true}) do
%Execution{exec | all: true}
end
defp add_switch_all(exec, _), do: exec
# add_switch_files_included
defp add_switch_files_included(exec, %{files_included: [_head | _tail] = files_included}) do
%Execution{exec | files: %{exec.files | included: files_included}}
end
defp add_switch_files_included(exec, _), do: exec
# add_switch_files_excluded
defp add_switch_files_excluded(exec, %{files_excluded: [_head | _tail] = files_excluded}) do
%Execution{exec | files: %{exec.files | excluded: files_excluded}}
end
defp add_switch_files_excluded(exec, _), do: exec
# add_switch_checks_with_tag
defp add_switch_checks_with_tag(exec, %{
checks_with_tag: [_head | _tail] = checks_with_tag
}) do
%Execution{exec | only_checks_tags: checks_with_tag}
end
defp add_switch_checks_with_tag(exec, _), do: exec
# add_switch_checks_without_tag
defp add_switch_checks_without_tag(exec, %{
checks_without_tag: [_head | _tail] = checks_without_tag
}) do
%Execution{exec | ignore_checks_tags: checks_without_tag}
end
defp add_switch_checks_without_tag(exec, _), do: exec
# add_switch_color
defp add_switch_color(exec, %{color: color}) do
%Execution{exec | color: color}
end
defp add_switch_color(exec, _), do: exec
# add_switch_debug
defp add_switch_debug(exec, %{debug: debug}) do
%Execution{exec | debug: debug}
end
defp add_switch_debug(exec, _), do: exec
# add_switch_strict
defp add_switch_strict(exec, %{all_priorities: true}) do
add_switch_strict(exec, %{strict: true})
end
defp add_switch_strict(exec, %{strict: true}) do
new_config = %Execution{exec | strict: true}
Execution.set_strict(new_config)
end
defp add_switch_strict(exec, %{strict: false}) do
new_config = %Execution{exec | strict: false}
Execution.set_strict(new_config)
end
defp add_switch_strict(exec, _), do: Execution.set_strict(exec)
defp add_switch_help(exec, %{help: true}) do
%Execution{exec | help: true}
end
defp add_switch_help(exec, _), do: exec
# add_switch_verbose
defp add_switch_verbose(exec, %{verbose: true}) do
%Execution{exec | verbose: true}
end
defp add_switch_verbose(exec, _), do: exec
# add_switch_crash_on_error
defp add_switch_crash_on_error(exec, %{crash_on_error: true}) do
%Execution{exec | crash_on_error: true}
end
defp add_switch_crash_on_error(exec, _), do: exec
# add_switch_mute_exit_status
defp add_switch_mute_exit_status(exec, %{mute_exit_status: true}) do
%Execution{exec | mute_exit_status: true}
end
defp add_switch_mute_exit_status(exec, _), do: exec
# add_switch_read_from_stdin
defp add_switch_read_from_stdin(exec, %{read_from_stdin: true}) do
%Execution{exec | read_from_stdin: true}
end
defp add_switch_read_from_stdin(exec, _), do: exec
# add_switch_version
defp add_switch_version(exec, %{version: true}) do
%Execution{exec | version: true}
end
defp add_switch_version(exec, _), do: exec
# add_switch_format
defp add_switch_format(exec, %{format: format}) do
%Execution{exec | format: format}
end
defp add_switch_format(exec, _), do: exec
# add_switch_min_priority
defp add_switch_min_priority(exec, %{min_priority: min_priority}) do
%Execution{exec | min_priority: min_priority}
end
defp add_switch_min_priority(exec, _), do: exec
# add_switch_enable_disabled_checks
defp add_switch_enable_disabled_checks(exec, %{enable_disabled_checks: check_pattern}) do
%Execution{exec | enable_disabled_checks: String.split(check_pattern, @pattern_split_regex)}
end
defp add_switch_enable_disabled_checks(exec, _), do: exec
# add_switch_only
# exclude/ignore certain checks
defp add_switch_only(exec, %{only: only}) do
add_switch_only(exec, %{checks: only})
end
# this catches a `--checks/only` without an argument after it
defp add_switch_only(exec, %{checks: true}) do
exec
end
defp add_switch_only(exec, %{checks: check_pattern}) do
new_config = %Execution{
exec
| strict: true,
only_checks: String.split(check_pattern, @pattern_split_regex)
}
Execution.set_strict(new_config)
end
defp add_switch_only(exec, _), do: exec
# add_switch_ignore
# exclude/ignore certain checks
defp add_switch_ignore(exec, %{ignore: ignore}) do
add_switch_ignore(exec, %{ignore_checks: ignore})
end
# this catches a `--ignore-checks/ignore` without an argument after it
defp add_switch_ignore(exec, %{ignore_checks: true}) do
exec
end
defp add_switch_ignore(exec, %{ignore_checks: ignore_pattern}) do
%Execution{exec | ignore_checks: String.split(ignore_pattern, @pattern_split_regex)}
end
defp add_switch_ignore(exec, _), do: exec
defp run_cli_switch_plugin_param_converters(exec) do
Enum.reduce(
exec.cli_switch_plugin_param_converters,
exec,
&reduce_converters/2
)
end
defp reduce_converters({_switch_name, _plugin_mod, false}, exec) do
exec
end
defp reduce_converters({switch_name, plugin_mod, true}, exec) do
reduce_converters({switch_name, plugin_mod, switch_name}, exec)
end
defp reduce_converters({switch_name, plugin_mod, param_name}, exec) when is_atom(param_name) do
converter_fun = fn switch_value -> {param_name, switch_value} end
reduce_converters({switch_name, plugin_mod, converter_fun}, exec)
end
defp reduce_converters({switch_name, plugin_mod, converter_fun}, exec)
when is_function(converter_fun) do
case Execution.get_given_cli_switch(exec, switch_name) do
{:ok, switch_value} ->
validate_converter_fun_result(exec, plugin_mod, switch_name, converter_fun.(switch_value))
_ ->
exec
end
end
defp validate_converter_fun_result(exec, plugin_mod, _switch_name, {param_name, param_value}) do
Execution.put_plugin_param(exec, plugin_mod, param_name, param_value)
end
defp validate_converter_fun_result(_exec, plugin_mod, switch_name, value) do
raise "Expected CLI switch to plugin param converter function to return a two-element tuple of {param_name, param_value}, got #{inspect(value)} (plugin: #{inspect(plugin_mod)}, switch: #{inspect(switch_name)})"
end
end