Skip to content

Commit

Permalink
Merge pull request #1156 from paulanthonywilson/fix-params-count-with…
Browse files Browse the repository at this point in the history
…-guard

Fixes counting params when a function has a guard
  • Loading branch information
rrrene authored Oct 20, 2024
2 parents 6127e70 + 3dfd2e8 commit b1ad8c4
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 b1ad8c4

Please sign in to comment.