Skip to content

Commit

Permalink
Fix some Credo issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Jul 29, 2022
1 parent 0710977 commit 1c711c5
Show file tree
Hide file tree
Showing 34 changed files with 78 additions and 102 deletions.
4 changes: 2 additions & 2 deletions lib/credo/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ defmodule Credo.Application do
]

if Version.match?(System.version(), ">= 1.10.0-rc") do
def children() do
def children do
Enum.map(@worker_modules, &{&1, []})
end
else
def children() do
def children do
import Supervisor.Spec, warn: false
Enum.map(@worker_modules, &worker(&1, []))
end
Expand Down
12 changes: 5 additions & 7 deletions lib/credo/build_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ defmodule Credo.BuildInfo do
end

defp in_git_work_tree? do
try do
{_, exit_status} =
System.cmd("git", ["rev-parse", "--is-inside-work-tree"], stderr_to_stdout: true)
{_, exit_status} =
System.cmd("git", ["rev-parse", "--is-inside-work-tree"], stderr_to_stdout: true)

exit_status == 0
rescue
_ -> false
end
exit_status == 0
rescue
_ -> false
end

defp git_branch do
Expand Down
5 changes: 2 additions & 3 deletions lib/credo/check/consistency/exception_names/collector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ defmodule Credo.Check.Consistency.ExceptionNames.Collector do

use Credo.Check.Consistency.Collector

alias Credo.Code
alias Credo.Code.Module
alias Credo.Code.Name

def collect_matches(source_file, _params) do
exception_recorder = &record_exception/2

Code.prewalk(source_file, &traverse(exception_recorder, &1, &2), %{})
Credo.Code.prewalk(source_file, &traverse(exception_recorder, &1, &2), %{})
end

def find_locations_not_matching(expected, source_file) do
location_recorder = &record_not_matching(expected, &1, &2)

source_file
|> Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Credo.Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Enum.reverse()
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ defmodule Credo.Check.Consistency.MultiAliasImportRequireUse.Collector do

use Credo.Check.Consistency.Collector

alias Credo.Code

@directives [:alias, :import, :require, :use]

def collect_matches(source_file, _params) do
source_file
|> Code.prewalk(&traverse/2, [])
|> Credo.Code.prewalk(&traverse/2, [])
|> group_usages
|> count_occurrences
end

def find_locations_not_matching(expected, source_file) do
source_file
|> Code.prewalk(&traverse/2, [])
|> Credo.Code.prewalk(&traverse/2, [])
|> group_usages
|> drop_locations(expected)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ defmodule Credo.Check.Consistency.ParameterPatternMatching.Collector do

use Credo.Check.Consistency.Collector

alias Credo.Code

def collect_matches(source_file, _params) do
position_recorder = &record_position/4

Code.prewalk(source_file, &traverse(position_recorder, &1, &2), %{})
Credo.Code.prewalk(source_file, &traverse(position_recorder, &1, &2), %{})
end

def find_locations_not_matching(expected, source_file) do
location_recorder = &record_not_matching(expected, &1, &2, &3, &4)

source_file
|> Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Credo.Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Enum.reverse()
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators.Collector do

use Credo.Check.Consistency.Collector

alias Credo.Code

import Credo.Check.Consistency.SpaceAroundOperators.SpaceHelper,
only: [
operator?: 1,
Expand All @@ -16,13 +14,13 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators.Collector do

def collect_matches(source_file, _params) do
source_file
|> Code.to_tokens()
|> Credo.Code.to_tokens()
|> traverse_tokens(&record_spaces(&1, &2, &3, &4), %{})
end

def find_locations_not_matching(expected, source_file) do
source_file
|> Code.to_tokens()
|> Credo.Code.to_tokens()
|> traverse_tokens(&record_not_matching(expected, &1, &2, &3, &4), [])
|> Enum.reverse()
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ defmodule Credo.Check.Consistency.UnusedVariableNames.Collector do

use Credo.Check.Consistency.Collector

alias Credo.Code

def collect_matches(source_file, _params) do
unused_variable_recorder = &record_unused_variable/2

Code.prewalk(source_file, &traverse(unused_variable_recorder, &1, &2), %{})
Credo.Code.prewalk(source_file, &traverse(unused_variable_recorder, &1, &2), %{})
end

def find_locations_not_matching(expected, source_file) do
location_recorder = &record_not_matching(expected, &1, &2)

source_file
|> Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Credo.Code.prewalk(&traverse(location_recorder, &1, &2), [])
|> Enum.reverse()
end

Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/alias_as.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ defmodule Credo.Check.Readability.AliasAs do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
source_file
|> Code.prewalk(&traverse(&1, &2, IssueMeta.for(source_file, params)))
|> Credo.Code.prewalk(&traverse(&1, &2, IssueMeta.for(source_file, params)))
|> Enum.reverse()
end

Expand Down
29 changes: 16 additions & 13 deletions lib/credo/check/readability/alias_order.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ defmodule Credo.Check.Readability.AliasOrder do
"""
]

alias Credo.Code
alias Credo.Code.Name

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)

Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

defp traverse({:defmodule, _, _} = ast, issues, issue_meta) do
Expand Down Expand Up @@ -126,19 +125,23 @@ defmodule Credo.Check.Readability.AliasOrder do
sorted_downcased_mod_list = Enum.sort(downcased_mod_list)

if downcased_mod_list != sorted_downcased_mod_list do
trigger =
downcased_mod_list
|> Enum.with_index()
|> Enum.find_value(fn {downcased_mod_entry, index} ->
if downcased_mod_entry != Enum.at(sorted_downcased_mod_list, index) do
Enum.at(mod_list, index)
end
end)

issue_opts(line_no, [base, trigger], trigger)
issue_opts(line_no, base, mod_list, downcased_mod_list, sorted_downcased_mod_list)
end
end

defp issue_opts(line_no, base, mod_list, downcased_mod_list, sorted_downcased_mod_list) do
trigger =
downcased_mod_list
|> Enum.with_index()
|> Enum.find_value(fn {downcased_mod_entry, index} ->
if downcased_mod_entry != Enum.at(sorted_downcased_mod_list, index) do
Enum.at(mod_list, index)
end
end)

issue_opts(line_no, [base, trigger], trigger)
end

defp issue_opts(line_no, module, trigger) do
%{
line_no: line_no,
Expand All @@ -149,7 +152,7 @@ defmodule Credo.Check.Readability.AliasOrder do

defp extract_alias_groups({:defmodule, _, _} = ast) do
ast
|> Code.postwalk(&find_alias_groups/2)
|> Credo.Code.postwalk(&find_alias_groups/2)
|> Enum.reverse()
|> Enum.reduce([[]], fn definition, acc ->
case definition do
Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/readability/function_names.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ defmodule Credo.Check.Readability.FunctionNames do
|> issues_list()
end

defp empty_issues(), do: %{}
defp empty_issues, do: %{}

defp add_issue(issues, name, arity, issue), do: Map.put_new(issues, {name, arity}, issue)

Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/readability/max_line_length.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ defmodule Credo.Check.Readability.MaxLineLength do
]

alias Credo.Code.Heredocs
alias Credo.Code.Strings
alias Credo.Code.Sigils
alias Credo.Code.Strings

@def_ops [:def, :defp, :defmacro]
@url_regex ~r/[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)/
Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/multi_alias.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ defmodule Credo.Check.Readability.MultiAlias do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)

Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

# TODO: consider for experimental check front-loader (ast)
Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/single_function_to_block_pipe.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ defmodule Credo.Check.Readability.SingleFunctionToBlockPipe do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
Code.prewalk(source_file, &traverse(&1, &2, IssueMeta.for(source_file, params)))
Credo.Code.prewalk(source_file, &traverse(&1, &2, IssueMeta.for(source_file, params)))
end

defp traverse(ast, {false, issues}, _issue_meta) do
Expand Down
3 changes: 1 addition & 2 deletions lib/credo/check/readability/strict_module_layout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ defmodule Credo.Check.Readability.StrictModuleLayout do
ignore: []
]

alias Credo.Code
alias Credo.CLI.Output.UI

@doc false
Expand All @@ -77,7 +76,7 @@ defmodule Credo.Check.Readability.StrictModuleLayout do
params = normalize_params(params)

source_file
|> Code.ast()
|> Credo.Code.ast()
|> Credo.Code.Module.analyze()
|> all_errors(params, IssueMeta.for(source_file, params))
|> Enum.sort_by(&{&1.line_no, &1.column})
Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/readability/string_sigils.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Credo.Check.Readability.StringSigils do
alias Credo.SourceFile
alias Credo.Code.Heredocs
alias Credo.SourceFile

use Credo.Check,
base_priority: :low,
Expand Down
5 changes: 2 additions & 3 deletions lib/credo/check/readability/trailing_white_space.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ defmodule Credo.Check.Readability.TrailingWhiteSpace do
]
]

alias Credo.Code
alias Credo.Code.Strings
alias Credo.Code.Heredocs
alias Credo.Code.Strings

@doc false
@impl true
Expand All @@ -35,7 +34,7 @@ defmodule Credo.Check.Readability.TrailingWhiteSpace do
source_file
|> Strings.replace_with_spaces(".", ".")
|> Heredocs.replace_with_spaces(".", ".", ".", source_file.filename)
|> Code.to_lines()
|> Credo.Code.to_lines()
end

defp to_lines(source_file, false) do
Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/unnecessary_alias_expansion.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ defmodule Credo.Check.Readability.UnnecessaryAliasExpansion do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)

Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

# TODO: consider for experimental check front-loader (ast)
Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/with_custom_tagged_tuple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ defmodule Credo.Check.Readability.WithCustomTaggedTuple do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params \\ []) do
Expand All @@ -51,7 +49,7 @@ defmodule Credo.Check.Readability.WithCustomTaggedTuple do
end

defp errors(source_file) do
{_ast, errors} = Macro.prewalk(Code.ast(source_file), MapSet.new(), &traverse/2)
{_ast, errors} = Macro.prewalk(Credo.Code.ast(source_file), MapSet.new(), &traverse/2)
Enum.sort_by(errors, &{&1.line, &1.column})
end

Expand Down
4 changes: 1 addition & 3 deletions lib/credo/check/readability/with_single_clause.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@ defmodule Credo.Check.Readability.WithSingleClause do
"""
]

alias Credo.Code

@doc false
@impl true
def run(%SourceFile{} = source_file, params) do
issue_meta = IssueMeta.for(source_file, params)

Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
end

# TODO: consider for experimental check front-loader (ast)
Expand Down
2 changes: 1 addition & 1 deletion lib/credo/check/refactor/module_dependencies.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ defmodule Credo.Check.Refactor.ModuleDependencies do
]
]

alias Credo.Code.Name
alias Credo.Code.Module
alias Credo.Code.Name

@doc false
@impl true
Expand Down
Loading

0 comments on commit 1c711c5

Please sign in to comment.