From 2e15cdd295a5e3d531d5f1485d85a6eacca4ac23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20F=C3=B6hring?= Date: Fri, 29 Nov 2024 21:08:43 +0100 Subject: [PATCH] Do not count tuples and underscored matches in ABCSize --- lib/credo/check/refactor/abc_size.ex | 6 ++++-- test/credo/check/refactor/abc_size_test.exs | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/credo/check/refactor/abc_size.ex b/lib/credo/check/refactor/abc_size.ex index 7c8a49fc2..670247b1a 100644 --- a/lib/credo/check/refactor/abc_size.ex +++ b/lib/credo/check/refactor/abc_size.ex @@ -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 @@ -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]} diff --git a/test/credo/check/refactor/abc_size_test.exs b/test/credo/check/refactor/abc_size_test.exs index 75112fbc3..ff228ff73 100644 --- a/test/credo/check/refactor/abc_size_test.exs +++ b/test/credo/check/refactor/abc_size_test.exs @@ -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