diff --git a/lib/credo/code/parameters.ex b/lib/credo/code/parameters.ex index b29f0e60e..a9616df89 100644 --- a/lib/credo/code/parameters.ex +++ b/lib/credo/code/parameters.ex @@ -15,6 +15,9 @@ defmodule Credo.Code.Parameters do {_atom, _meta, nil} -> 0 + {:when, _when_meta, [{_fun, _meta, args} | _guards]} -> + length(args || []) + {_atom, _meta, list} -> Enum.count(list) diff --git a/test/credo/code/parameters_test.exs b/test/credo/code/parameters_test.exs index 313de7c3e..b3ad89110 100644 --- a/test/credo/code/parameters_test.exs +++ b/test/credo/code/parameters_test.exs @@ -146,6 +146,32 @@ defmodule Credo.Code.ParametersTest do assert 2 == Parameters.count(ast) end + test "returns the correct paramter counts for a function with a guard condition" do + {:ok, ast} = + """ + def foobar(a, b, c, d) when is_integer(a), do: :ok + """ + |> Code.string_to_quoted() + + assert 4 == Parameters.count(ast) + + {:ok, ast} = + """ + def foobar(a, b, c, d, e) when is_integer(a) and is_map(b), do: :ok + """ + |> Code.string_to_quoted() + + assert 5 == Parameters.count(ast) + + {:ok, ast} = + """ + def foobar when is_integer(@a), do: :ok + """ + |> Code.string_to_quoted() + + assert 0 == Parameters.count(ast) + end + test "returns the correct parameter counts for ASTs" do ast = {:def, [line: 2],