-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: improve documentation of
compile.unused
- Loading branch information
Showing
3 changed files
with
81 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
defmodule MixUnused.Exports do | ||
@moduledoc false | ||
|
||
@type t() :: %{mfa() => metadata()} | [{mfa(), metadata()}] | ||
@type metadata() :: %{ | ||
file: String.t() | ||
} | ||
|
||
@types ~w[function macro]a | ||
|
||
@spec fetch(module()) :: [{mfa(), metadata()}] | ||
def fetch(module) do | ||
# Check exported functions without loading modules as this could cause | ||
# unexpected behaviours in case of `on_load` callbacks | ||
with path when is_list(path) <- :code.which(module), | ||
{:ok, {^module, data}} <- :beam_lib.chunks(path, [:attributes, :compile_info]), | ||
{:docs_v1, _anno, _lang, _format, _mod_doc, _meta, docs} <- | ||
Code.fetch_docs(to_string(path)) do | ||
callbacks = data[:attributes] |> Keyword.get(:behaviour, []) |> callbacks() | ||
source = Keyword.get(data[:compile_info], :source, "nofile") |> to_string() | ||
|
||
for {{type, name, arity}, anno, _sig, _doc, meta} when type in @types <- docs, | ||
not Map.get(meta, :export, false), | ||
{name, arity} not in callbacks do | ||
line = :erl_anno.line(anno) | ||
{{module, name, arity}, %{file: source, line: line}} | ||
end | ||
else | ||
_ -> [] | ||
end | ||
end | ||
|
||
defp callbacks(behaviours) do | ||
# We need to load behaviours as there is no other way to get list of | ||
# callbacks than to call `behaviour_info/1` | ||
Enum.flat_map(behaviours, & &1.behaviour_info(:callbacks)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters