Skip to content

Commit

Permalink
Make language-specific private data explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Jul 28, 2021
1 parent 1671d05 commit 5887fb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
7 changes: 5 additions & 2 deletions lib/ex_doc/language.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ defmodule ExDoc.Language do
* `:callback_types` - a list of types that are considered callbacks
* `:nesting_info` - A `{nested_title, nested_context}` tuple or `nil`.
* `:nesting_info` - a `{nested_title, nested_context}` tuple or `nil`.
For example, `"A.B.C"` becomes `{"C", "A.B"}`.
* `:private` - a map with language-specific data
"""
@type module_data() :: %{
module: module(),
Expand All @@ -37,7 +39,8 @@ defmodule ExDoc.Language do
skip: boolean(),
line: non_neg_integer(),
callback_types: [atom()],
nesting_info: {String.t(), String.t()} | nil
nesting_info: {String.t(), String.t()} | nil,
private: map()
}

@doc """
Expand Down
24 changes: 13 additions & 11 deletions lib/ex_doc/language/elixir.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ defmodule ExDoc.Language.Elixir do
title: title,
type: type,
skip: skip,
line: line,
callback_types: [:callback, :macrocallback],
nesting_info: nesting_info(title, config.nest_modules_by_prefix),
line: line,
abst_code: abst_code,
specs: Erlang.get_specs(module),
callbacks: Erlang.get_callbacks(module),
impls: get_impls(module)
private: %{
abst_code: abst_code,
specs: Erlang.get_specs(module),
callbacks: Erlang.get_callbacks(module),
impls: get_impls(module)
}
}
end

Expand All @@ -49,7 +51,7 @@ defmodule ExDoc.Language.Elixir do

%{
doc_fallback: fn ->
impl = Map.fetch(module_data.impls, actual_def)
impl = Map.fetch(module_data.private.impls, actual_def)

callback_doc_ast(name, arity, impl) ||
delegate_doc_ast(metadata[:delegate_to])
Expand All @@ -66,7 +68,7 @@ defmodule ExDoc.Language.Elixir do
actual_def = actual_def(name, arity, kind)

specs =
case Map.fetch(module_data.callbacks, actual_def) do
case Map.fetch(module_data.private.callbacks, actual_def) do
{:ok, specs} ->
specs

Expand Down Expand Up @@ -111,7 +113,7 @@ defmodule ExDoc.Language.Elixir do

@doc false
def type_from_module_data(module_data, name, arity) do
Enum.find_value(module_data.abst_code, fn
Enum.find_value(module_data.private.abst_code, fn
{:attribute, anno, type, {^name, _, args} = spec} ->
if type in [:opaque, :type] and length(args) == arity do
%{
Expand Down Expand Up @@ -218,7 +220,7 @@ defmodule ExDoc.Language.Elixir do

defp specs(kind, name, actual_def, module_data) do
specs =
module_data.specs
module_data.private.specs
|> Map.get(actual_def, [])
|> Enum.map(&Code.Typespec.spec_to_quoted(name, &1))

Expand Down Expand Up @@ -273,8 +275,8 @@ defmodule ExDoc.Language.Elixir do
nil
end

defp find_function_line(%{abst_code: abst_code}, {name, arity}) do
Enum.find_value(abst_code, fn
defp find_function_line(module_data, {name, arity}) do
Enum.find_value(module_data.private.abst_code, fn
{:function, anno, ^name, ^arity, _} -> anno_line(anno)
_ -> nil
end)
Expand Down
14 changes: 8 additions & 6 deletions lib/ex_doc/language/erlang.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ defmodule ExDoc.Language.Erlang do
title: id,
type: module_type(module),
skip: false,
line: line,
callback_types: [:callback],
nesting_info: nil,
line: line,
abst_code: abst_code,
specs: get_specs(module),
callbacks: get_callbacks(module)
private: %{
abst_code: abst_code,
specs: get_specs(module),
callbacks: get_callbacks(module)
}
}
end

Expand All @@ -33,7 +35,7 @@ defmodule ExDoc.Language.Erlang do
{{_kind, name, arity}, _anno, _signature, _doc_content, _metadata} = entry

specs =
case Map.fetch(module_data.specs, {name, arity}) do
case Map.fetch(module_data.private.specs, {name, arity}) do
{:ok, specs} ->
[{:attribute, 0, :spec, {{name, arity}, specs}}]

Expand All @@ -54,7 +56,7 @@ defmodule ExDoc.Language.Erlang do
{{_kind, name, arity}, anno, signature, _doc, _metadata} = entry

specs =
case Map.fetch(module_data.callbacks, {name, arity}) do
case Map.fetch(module_data.private.callbacks, {name, arity}) do
{:ok, specs} ->
[{:attribute, 0, :callback, {{name, arity}, specs}}]

Expand Down

0 comments on commit 5887fb1

Please sign in to comment.