Skip to content

Commit

Permalink
Fixes counting params when a function has a guard
Browse files Browse the repository at this point in the history
  • Loading branch information
paulanthonywilson committed Oct 9, 2024
1 parent 599a7d8 commit 3dfd2e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/credo/code/parameters.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 26 additions & 0 deletions test/credo/code/parameters_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down

0 comments on commit 3dfd2e8

Please sign in to comment.