From 3dfd2e87b71551f333d9f67cf0b3c58a8e8a44cd Mon Sep 17 00:00:00 2001 From: Paul Wilson Date: Wed, 9 Oct 2024 13:03:15 +0100 Subject: [PATCH] Fixes counting params when a function has a guard --- lib/credo/code/parameters.ex | 3 +++ test/credo/code/parameters_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) 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],