Skip to content

Commit

Permalink
Fix retrieving specs for macros with when, closes #1191
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Oct 6, 2020
1 parent 4e647e6 commit 9bfcdd8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/ex_doc/retriever.ex
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ defmodule ExDoc.Retriever do
{:"::", info, [{name, info2, rest_args}, return]}
end

defp remove_first_macro_arg({:when, meta, [lhs, rhs]}) do
{:when, meta, [remove_first_macro_arg(lhs), rhs]}
end

defp find_module_line(%{abst_code: abst_code, name: name}) do
Enum.find_value(abst_code, fn
{:attribute, anno, :module, ^name} -> anno_line(anno)
Expand Down
15 changes: 10 additions & 5 deletions test/ex_doc/retriever_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,17 @@ defmodule ExDoc.RetrieverTest do

test "returns the specs for non-private macros" do
[module_node] = docs_from_files(["TypesAndSpecs"])
[_, _, macro] = module_node.docs
[_, _, macro1, macro2] = module_node.docs

assert macro.id == "macro_spec/1"
assert macro.doc == nil
assert macro.type == :macro
assert Macro.to_string(macro.specs) == "[macro_spec(any()) :: {:ok, any()}]"
assert macro1.id == "macro_spec/1"
assert macro1.doc == nil
assert macro1.type == :macro
assert Macro.to_string(macro1.specs) == "[macro_spec(any()) :: {:ok, any()}]"

assert macro2.id == "macro_with_spec/1"
assert macro2.doc == nil
assert macro2.type == :macro
assert Macro.to_string(macro2.specs) == "[macro_with_spec(v) :: {:ok, v} when v: any()]"
end

test "returns the spec info for each non-private module type" do
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/types_and_specs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ defmodule TypesAndSpecs do
@spec macro_spec(any) :: {:ok, any}
defmacro macro_spec(v), do: {:ok, v}

@spec macro_with_spec(v) :: {:ok, v} when v: any()
defmacro macro_with_spec(v), do: {:ok, v}

@spec priv_macro_spec(any) :: {:no, any}
defmacrop priv_macro_spec(v), do: {:no, v}

Expand Down

0 comments on commit 9bfcdd8

Please sign in to comment.