Skip to content

Commit 2e15cdd

Browse files
committed
Do not count tuples and underscored matches in ABCSize
1 parent 2143bd9 commit 2e15cdd

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/credo/check/refactor/abc_size.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule Credo.Check.Refactor.ABCSize do
2727
@def_ops [:def, :defp, :defmacro]
2828
@branch_ops [:.]
2929
@condition_ops [:if, :unless, :for, :try, :case, :cond, :and, :or, :&&, :||]
30-
@non_calls [:==, :fn, :__aliases__, :__block__, :if, :or, :|>, :%{}, :^]
30+
@non_calls [:==, :fn, :__aliases__, :__block__, :if, :or, :|>, :%{}, :{}, :^]
3131

3232
@doc false
3333
@impl true
@@ -223,7 +223,9 @@ defmodule Credo.Check.Refactor.ABCSize do
223223
[a: a, b: b, c: c, var_names: var_names],
224224
_excluded_functions
225225
) do
226-
is_variable = Enum.member?(var_names, fun_or_var_name)
226+
is_variable =
227+
Enum.member?(var_names, fun_or_var_name) ||
228+
String.starts_with?(to_string(fun_or_var_name), "_")
227229

228230
if is_variable do
229231
{ast, [a: a, b: b, c: c, var_names: var_names]}

test/credo/check/refactor/abc_size_test.exs

+20
Original file line numberDiff line numberDiff line change
@@ -367,4 +367,24 @@ defmodule Credo.Check.Refactor.ABCSizeTest do
367367

368368
assert rounded_abc_size(source, ["where", "join", "select", "distinct"]) == 1
369369
end
370+
371+
test "it should return the same ABC size for equivalently complex code" do
372+
source1 = """
373+
def call(ast) do
374+
if match?({:fn, _meta, _args}, ast) do
375+
# ...
376+
end
377+
end
378+
"""
379+
380+
source2 = """
381+
def call(ast) do
382+
if {:fn, _meta, _args} = ast do
383+
# ...
384+
end
385+
end
386+
"""
387+
388+
assert rounded_abc_size(source1) == rounded_abc_size(source2)
389+
end
370390
end

0 commit comments

Comments
 (0)