Skip to content

Commit

Permalink
Fix handling typespecs with the same name as the type
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Aug 29, 2021
1 parent 6bec83b commit 08f9234
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,10 @@ defmodule ExDoc.Language.Erlang do
{other, acc}
end)

acc |> Enum.reverse() |> Enum.drop(1)
acc
|> Enum.reverse()
# drop the name of the typespec
|> Enum.drop(1)
end
|> Enum.concat()

Expand All @@ -489,13 +492,18 @@ defmodule ExDoc.Language.Erlang do
# Drop and re-add type name (it, the first element in acc, is dropped there too)
#
# 1. foo() :: bar()
# 2. ) :: bar()
# 3. ) :: <a>bar</a>()
# 2. () :: bar()
# 3. () :: <a>bar</a>()
# 4. foo() :: <a>bar</a>()
name = pp(name)
formatted = String.trim_leading(formatted, name <> "(")
formatted = trim_name(formatted, name)
formatted = replace(formatted, acc, config)
name <> "(" <> formatted
name <> formatted
end

defp trim_name(string, name) do
name_size = byte_size(name)
binary_part(string, name_size, byte_size(string) - name_size)
end

defp replace(formatted, [], _config) do
Expand Down
5 changes: 5 additions & 0 deletions test/ex_doc/language/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ defmodule ExDoc.Language.ErlangTest do
~s|t() -> <a href="#t:t/0">t</a>().|
end

test "same spec and type name", c do
assert autolink_spec(~S"-spec t(t()) -> t().", c) ==
~s|t(<a href="#t:t/0">t</a>()) -> <a href="#t:t/0">t</a>().|
end

test "non-standard name", c do
assert autolink_spec(~S"-spec 'Foo'() -> ok.", c) ==
~s|'Foo'() -> ok.|
Expand Down

0 comments on commit 08f9234

Please sign in to comment.