Skip to content

Commit

Permalink
Do not count tuples and underscored matches in ABCSize
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Nov 29, 2024
1 parent 2143bd9 commit 2e15cdd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/credo/check/refactor/abc_size.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule Credo.Check.Refactor.ABCSize do
@def_ops [:def, :defp, :defmacro]
@branch_ops [:.]
@condition_ops [:if, :unless, :for, :try, :case, :cond, :and, :or, :&&, :||]
@non_calls [:==, :fn, :__aliases__, :__block__, :if, :or, :|>, :%{}, :^]
@non_calls [:==, :fn, :__aliases__, :__block__, :if, :or, :|>, :%{}, :{}, :^]

@doc false
@impl true
Expand Down Expand Up @@ -223,7 +223,9 @@ defmodule Credo.Check.Refactor.ABCSize do
[a: a, b: b, c: c, var_names: var_names],
_excluded_functions
) do
is_variable = Enum.member?(var_names, fun_or_var_name)
is_variable =
Enum.member?(var_names, fun_or_var_name) ||
String.starts_with?(to_string(fun_or_var_name), "_")

if is_variable do
{ast, [a: a, b: b, c: c, var_names: var_names]}
Expand Down
20 changes: 20 additions & 0 deletions test/credo/check/refactor/abc_size_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,24 @@ defmodule Credo.Check.Refactor.ABCSizeTest do

assert rounded_abc_size(source, ["where", "join", "select", "distinct"]) == 1
end

test "it should return the same ABC size for equivalently complex code" do
source1 = """
def call(ast) do
if match?({:fn, _meta, _args}, ast) do
# ...
end
end
"""

source2 = """
def call(ast) do
if {:fn, _meta, _args} = ast do
# ...
end
end
"""

assert rounded_abc_size(source1) == rounded_abc_size(source2)
end
end

0 comments on commit 2e15cdd

Please sign in to comment.