credo icon indicating copy to clipboard operation
credo copied to clipboard

AbcSize does not exclude method declarations.

Open noizu opened this issue 1 year ago • 1 comments

I am getting ABC errors for defmacro deriving calls, it appears that the logic does not exclude the method declarations themselves only calls within logic to other methods.

https://github.com/rrrene/credo/blob/master/lib/credo/check/refactor/abc_size.ex#L73-L99

A crude patch like the following addressing the issue but this needs to be thought out to avoid headaches with existing users. Possibly a new param is needed? There is already a similiar ex ception above line L73 for :using macros.

  for op <- @def_ops do
    defp traverse(
           {unquote(op), meta, arguments} = ast,
           issues,
           issue_meta,
           max_abc_size,
           excluded_functions
         )
         when is_list(arguments) do
      
      # Exclude declarations of methods included in excluded_function list.
      with [{f,_,_}|_] <- arguments,
           true <- is_atom(f),
           true <- Enum.member?(excluded_functions, Atom.to_string(f)) do
        {ast, issues}
      else
        _ ->
          abc_size =
            ast
            |> abc_size_for(excluded_functions)
            |> round
          
          if abc_size > max_abc_size do
            fun_name = Credo.Code.Module.def_name(ast)
            
            {ast,
              [
                issue_for(issue_meta, meta[:line], fun_name, max_abc_size, abc_size)
                | issues
              ]}
          else
            {ast, issues}
          end
      
      end
      
    end
  end

Additionally this might be faster with

    defp traverse(
           {op, meta, arguments} = ast,
           issues,
           issue_meta,
           max_abc_size,
           excluded_functions
         )
         when is_list(arguments) and op in @def_ops do

to reduce the amount of generated code while replacing with guard checks. (although we'd have to run benchee to verify).

noizu avatar Jan 19 '25 05:01 noizu

Hi, thx for creating an issue.

Could you please fill out the issue template you deleted and provide a code example that reproduces your issue?

rrrene avatar Mar 18 '25 04:03 rrrene