Skip to content

Commit

Permalink
Fix accidentally showing opaque shape
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Aug 2, 2021
1 parent b943950 commit 7a4d5dc
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/ex_doc/language/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ defmodule ExDoc.Language.Elixir do

@impl true
def type_data(entry, module_data) do
{{kind, name, arity}, _anno, _signature, _doc, _metadata} = entry
{{_kind, name, arity}, _anno, _signature, _doc, _metadata} = entry

%{type: type, spec: spec, line: line} = type_from_module_data(module_data, name, arity)
quoted = spec |> Code.Typespec.type_to_quoted() |> process_type_ast(kind)
quoted = spec |> Code.Typespec.type_to_quoted() |> process_type_ast(type)
signature = [get_typespec_signature(quoted, arity)]

%{
Expand Down
17 changes: 15 additions & 2 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ defmodule ExDoc.Language.Erlang do
%{
type: type,
line: line,
spec: {:attribute, 0, :type, spec},
spec: {:attribute, 0, type, spec},
signature: signature
}
end
Expand All @@ -121,6 +121,19 @@ defmodule ExDoc.Language.Erlang do
nil
end

def autolink_spec({:attribute, _, :opaque, ast}, _opts) do
{name, _, args} = ast

args =
for arg <- args do
{:var, _, name} = arg
Atom.to_string(name)
end
|> Enum.intersperse(", ")

IO.iodata_to_binary([Atom.to_string(name), "(", args, ")"])
end

def autolink_spec(ast, opts) do
config = struct!(Autolink, opts)

Expand All @@ -129,7 +142,7 @@ defmodule ExDoc.Language.Erlang do
{:attribute, _, kind, {{name, _arity}, ast}} when kind in [:spec, :callback] ->
{name, Enum.map(ast, &Code.Typespec.spec_to_quoted(name, &1))}

{:attribute, _, kind, ast} when kind in [:type, :opaque] ->
{:attribute, _, :type, ast} ->
{name, _, _} = ast
{name, Code.Typespec.type_to_quoted(ast)}
end
Expand Down
8 changes: 6 additions & 2 deletions test/ex_doc/language/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,12 @@ defmodule ExDoc.Language.ErlangTest do
end

test "opaque", c do
assert autolink_spec("-opaque foo() :: opaque_t().", [current_module: :foo], c) ==
~s|foo() :: <a href="#t:opaque_t/0">opaque_t</a>().|
assert autolink_spec("-opaque foo() :: opaque_t().", [current_module: :foo], c) == ~s|foo()|
end

test "opaque with variables", c do
assert autolink_spec("-opaque foo(X, Y) :: {X, Y}.", [current_module: :foo], c) ==
~s|foo(X, Y)|
end

test "tuple", c do
Expand Down
37 changes: 34 additions & 3 deletions test/ex_doc/retriever/elixir_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,43 @@ defmodule ExDoc.Retriever.ElixirTest do
assert callback1.type == :function
assert callback1.annotations == []

assert callback1.doc ==
ExDoc.Markdown.to_ast("Callback implementation for `c:Mod.callback1/0`.")
assert callback1.doc |> DocAST.to_string() ==
~s|<p>Callback implementation for <code class="inline">c:Mod.callback1/0</code>.</p>|

assert optional_callback1.id == "optional_callback1/0"
assert optional_callback1.type == :function
assert DocAST.to_string(optional_callback1.doc) == "<p>optional_callback1/0 docs.</p>"
assert optional_callback1.doc |> DocAST.to_string() == ~s|<p>optional_callback1/0 docs.</p>|
end

test "types", c do
elixirc(c, ~S"""
defmodule Mod do
@typedoc "type1/0 docs."
@type type1() :: atom()
@typedoc "opaque1/0 docs."
@opaque opaque1() :: atom()
end
""")

[mod] = Retriever.docs_from_modules([Mod], %ExDoc.Config{})
[opaque1, type1] = mod.typespecs

assert type1.id == "t:type1/0"
assert type1.signature == "type1()"
assert type1.type == :type
assert type1.annotations == []
assert type1.doc_line == 2
assert DocAST.to_string(type1.doc) == "<p>type1/0 docs.</p>"
assert Macro.to_string(type1.spec) == "type1() :: atom()"

assert opaque1.id == "t:opaque1/0"
assert opaque1.signature == "opaque1()"
assert opaque1.type == :opaque
assert opaque1.annotations == ["opaque"]
assert opaque1.doc_line == 5
assert opaque1.doc |> DocAST.to_string() == ~s|<p>opaque1/0 docs.</p>|
assert opaque1.spec |> Macro.to_string() == "opaque1()"
end

test "protocols", c do
Expand Down
8 changes: 4 additions & 4 deletions test/ex_doc/retriever/erlang_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ defmodule ExDoc.Retriever.ErlangTest do
assert opaque1.id == "t:opaque1/0"
assert opaque1.type == :opaque
assert opaque1.signature == "opaque1/0"
assert DocAST.to_string(opaque1.doc) == "opaque1/0 docs."
assert Erlang.autolink_spec(opaque1.spec, []) == "opaque1() :: atom()."
assert opaque1.doc |> DocAST.to_string() == "opaque1/0 docs."
assert opaque1.spec |> Erlang.autolink_spec([]) == "opaque1()"

assert type1.id == "t:type1/0"
assert type1.type == :type
assert type1.signature == "type1/0"
assert DocAST.to_string(type1.doc) == "type1/0 docs."
assert Erlang.autolink_spec(type1.spec, []) == "type1() :: atom()."
assert type1.doc |> DocAST.to_string() == "type1/0 docs."
assert type1.spec |> Erlang.autolink_spec([]) == "type1() :: atom()."
end

test "module with no chunk", c do
Expand Down

0 comments on commit 7a4d5dc

Please sign in to comment.