Skip to content

Commit

Permalink
Fix false negative for ModuleDoc
Browse files Browse the repository at this point in the history
Refs #1168
  • Loading branch information
rrrene committed Dec 15, 2024
1 parent 91502ce commit bb54b0f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/credo/code/module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ defmodule Credo.Code.Module do
end

@doc "Reads an attribute from a module's `ast`"
def attribute({:defmodule, _, _arguments} = ast, attr_name) do
arguments =
case Credo.Code.Block.do_block_for!(ast) do
{:__block__, [], arguments} -> arguments
value -> List.wrap(value)
end

attr_value =
Enum.find_value(arguments, fn
{:@, _meta, [{^attr_name, _, [value]} | _tail]} -> {:ok, value}
_ -> nil
end)

case attr_value do
{:ok, value} -> value
error -> {:error, error}
end
end

def attribute(ast, attr_name) do
case Credo.Code.postwalk(ast, &find_attribute(&1, &2, attr_name), {:error, nil}) do
{:ok, value} ->
Expand Down
41 changes: 41 additions & 0 deletions test/credo/code/module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,47 @@ defmodule Credo.Code.ModuleTest do
assert false == Module.attribute(ast, :moduledoc)
end

test "should return the given module attribute for the top module in the given AST" do
{:ok, ast} =
"""
defmodule CredoExample do
@attr_list [:atom1, :atom2]
@attr_string "This is a String"
@attr_number 42
@moduledoc false
defmodule Foo do
@attr_list [:atom3, :atom4]
@attr_string "This is another String"
@attr_number -1
@moduledoc "test"
end
end
"""
|> Code.string_to_quoted()

assert [:atom1, :atom2] == Module.attribute(ast, :attr_list)
assert "This is a String" == Module.attribute(ast, :attr_string)
assert 42 == Module.attribute(ast, :attr_number)
assert false == Module.attribute(ast, :moduledoc)
end

test "should return the given module attribute, if found" do
ast =
{:defmodule, [line: 2, column: 3],
[
{:__aliases__, [line: 2, column: 13], [:SubModule]},
[do: {:__block__, [], []}]
]}

assert {:error, nil} == Module.attribute(ast, :attr_list)
assert {:error, nil} == Module.attribute(ast, :attr_string)
assert {:error, nil} == Module.attribute(ast, :attr_number)
assert {:error, nil} == Module.attribute(ast, :moduledoc)
end

#
# def_count
#
Expand Down

0 comments on commit bb54b0f

Please sign in to comment.