Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partially fix abstract Erlang types ({@type ...}) #1787

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ defmodule ExDoc.Language.Erlang do
binary
end

defp walk_doc({:code, attrs, [{:a, a_attrs, [a_inner], _a_meta} | _] = a, meta}, _config) do
{arity, extra} =
case :lists.keydelete(:a, 1, a) do
[] ->
{0, ""}

[extra] ->
# a_meta is not broken down, so we have to parse stuff like (X, Y)
{extra |> String.split(",") |> Enum.count(), extra}
end
paulo-ferraz-oliveira marked this conversation as resolved.
Show resolved Hide resolved

type = type_from_fragment(:otp, a_attrs[:href])
paulo-ferraz-oliveira marked this conversation as resolved.
Show resolved Hide resolved
url = fragment(:ex_doc, :type, type, arity)
text = a_inner <> extra

{:a, [href: url], {:code, attrs, [text], meta}, %{}}
end

defp walk_doc({:code, attrs, [code], meta} = ast, config) do
{text, url} =
case parse_autolink(code) do
Expand Down Expand Up @@ -456,6 +474,8 @@ defmodule ExDoc.Language.Erlang do
"#t:#{name}/#{arity}"
end

defp type_from_fragment(:otp, "#type-" <> name), do: name

defp kind("c:" <> rest), do: {:callback, rest}
defp kind("t:" <> rest), do: {:type, rest}
defp kind(rest), do: {:function, rest}
Expand Down
21 changes: 21 additions & 0 deletions test/ex_doc/language/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,27 @@ defmodule ExDoc.Language.ErlangTest do
~s|<a href="https://www.erlang.org/doc/man/array.html#type-array"><code>array:array()</code></a>|
end

test "abstract types - description", c do
# if type exists:
# <code><a href="#type-myList">myList</a>(X)</code>
assert autolink_doc("{@type myList(X). A special kind of lists ...}", c) ==
~s|<a href="#t:myList/1"><code>myList(X)</code></a>|
end

test "abstract types - description+dot", c do
# if type exists:
# <code><a href="#type-myList">myList</a>(X)</code>
assert autolink_doc("{@type myList(X, Y).}", c) ==
~s|<a href="#t:myList/2"><code>myList(X, Y)</code></a>|
end

test "abstract types - no description", c do
# if type exists:
# <code><a href="#type-myList">myList</a>(X)</code>
assert autolink_doc("{@type myList()}", c) ==
~s|<a href="#t:myList/0"><code>myList()</code></a>|
end

test "bad module", c do
assert ExUnit.CaptureIO.capture_io(:stderr, fn ->
assert autolink_doc("{@link bad}", c) == ~s|<code>bad</code>|
Expand Down