Skip to content

Commit b1ad8c4

Browse files
authored
Merge pull request #1156 from paulanthonywilson/fix-params-count-with-guard
Fixes counting params when a function has a guard
2 parents 6127e70 + 3dfd2e8 commit b1ad8c4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/credo/code/parameters.ex

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ defmodule Credo.Code.Parameters do
1515
{_atom, _meta, nil} ->
1616
0
1717

18+
{:when, _when_meta, [{_fun, _meta, args} | _guards]} ->
19+
length(args || [])
20+
1821
{_atom, _meta, list} ->
1922
Enum.count(list)
2023

test/credo/code/parameters_test.exs

+26
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,32 @@ defmodule Credo.Code.ParametersTest do
146146
assert 2 == Parameters.count(ast)
147147
end
148148

149+
test "returns the correct paramter counts for a function with a guard condition" do
150+
{:ok, ast} =
151+
"""
152+
def foobar(a, b, c, d) when is_integer(a), do: :ok
153+
"""
154+
|> Code.string_to_quoted()
155+
156+
assert 4 == Parameters.count(ast)
157+
158+
{:ok, ast} =
159+
"""
160+
def foobar(a, b, c, d, e) when is_integer(a) and is_map(b), do: :ok
161+
"""
162+
|> Code.string_to_quoted()
163+
164+
assert 5 == Parameters.count(ast)
165+
166+
{:ok, ast} =
167+
"""
168+
def foobar when is_integer(@a), do: :ok
169+
"""
170+
|> Code.string_to_quoted()
171+
172+
assert 0 == Parameters.count(ast)
173+
end
174+
149175
test "returns the correct parameter counts for ASTs" do
150176
ast =
151177
{:def, [line: 2],

0 commit comments

Comments
 (0)