Permalink
Browse files

Merge pull request #606 from roehst/bugfix-function-calls

Fix bug on counting function calls in ABC
  • Loading branch information...
rrrene committed Dec 28, 2018
2 parents 2c4ef83 + a7795d9 commit cb1d8cf02b9c301a0af23fd4dab3fbb545e52979
Showing with 34 additions and 9 deletions.
  1. +14 −9 lib/credo/check/refactor/abc_size.ex
  2. +20 −0 test/credo/check/refactor/abc_size_test.exs
@@ -25,6 +25,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, :|>, :%{}]

use Credo.Check

@@ -185,6 +186,19 @@ defmodule Credo.Check.Refactor.ABCSize do
end
end

defp traverse_abc(
{fun_name, _meta, arguments} = ast,
[a: a, b: b, c: c, var_names: var_names],
excluded_functions
)
when is_atom(fun_name) and fun_name not in @non_calls and is_list(arguments) do
if Enum.member?(excluded_functions, to_string(fun_name)) do
{nil, [a: a, b: b, c: c, var_names: var_names]}
else
{ast, [a: a, b: b + 1, c: c, var_names: var_names]}
end
end

defp traverse_abc(
{fun_or_var_name, _meta, nil} = ast,
[a: a, b: b, c: c, var_names: var_names],
@@ -211,15 +225,6 @@ defmodule Credo.Check.Refactor.ABCSize do
end
end

defp traverse_abc({fun_name, _meta, arguments} = ast, abc, excluded_functions)
when is_atom(fun_name) and is_list(arguments) do
if Enum.member?(excluded_functions, to_string(fun_name)) do
{nil, abc}
else
{ast, abc}
end
end

defp traverse_abc(ast, abc, _excluded_functions) do
{ast, abc}
end
@@ -15,6 +15,26 @@ defmodule Credo.Check.Refactor.ABCSizeTest do
|> Float.round(2)
end

test "it should return the correct ABC size for nullary function calls" do
source = """
def foo() do
baz()
end
"""

assert rounded_abc_size(source) == 1.0
end

test "it should return the correct ABC size for regular function calls" do
source = """
def foo() do
baz 1, 2
end
"""

assert rounded_abc_size(source) == 1.0
end

test "it should return the correct ABC size for value assignment" do
source = """
def first_fun do

0 comments on commit cb1d8cf

Please sign in to comment.