IEx: Sort Types in t helper (#9131)

This commit is contained in:
Jonatan Männchen
2019-06-11 23:50:56 +02:00
committed by José Valim
parent a211223810
commit 13af842c66
2 changed files with 43 additions and 18 deletions
+29 -18
View File
@@ -644,7 +644,9 @@ defmodule IEx.Introspection do
types_not_found(inspect(module))
{:ok, types} ->
Enum.each(types, &(&1 |> format_type() |> IO.puts()))
types
|> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end)
|> Enum.each(&(&1 |> format_type() |> IO.puts()))
end
dont_display_result()
@@ -656,15 +658,19 @@ defmodule IEx.Introspection do
no_beam(module)
{:ok, types} ->
printed =
for {kind, {^type, _, args}} = typespec <- types,
kind != :typep do
type_doc(module, type, length(args), typespec)
|> print_typespec()
end
types
|> Enum.filter(&match?({kind, {^type, _, _}} when kind in [:type, :opaque], &1))
|> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end)
|> case do
[] ->
types_not_found_or_private("#{inspect(module)}.#{type}")
if printed == [] do
types_not_found_or_private("#{inspect(module)}.#{type}")
types ->
Enum.map(types, fn {_, {_, _, args}} = typespec ->
module
|> type_doc(type, length(args), typespec)
|> print_typespec()
end)
end
end
@@ -677,16 +683,21 @@ defmodule IEx.Introspection do
no_beam(module)
{:ok, types} ->
printed =
for {kind, {^type, _, args}} = typespec <- types,
kind != :typep,
length(args) == arity do
type_doc(module, type, arity, typespec)
|> print_typespec()
end
types
|> Enum.find(
&match?(
{kind, {^type, _, args}} when kind in [:type, :opaque] and length(args) == arity,
&1
)
)
|> case do
nil ->
types_not_found_or_private("#{inspect(module)}.#{type}")
if printed == [] do
types_not_found_or_private("#{inspect(module)}.#{type}")
typespec ->
module
|> type_doc(type, arity, typespec)
|> print_typespec()
end
end
+14
View File
@@ -824,6 +824,10 @@ defmodule IEx.HelpersTest do
end) >= 2
end
test "prints private types" do
assert capture_io(fn -> t(Date.Range) end) =~ "@typep iso_days"
end
test "prints type information" do
assert "@type t() ::" <> _ = capture_io(fn -> t(Enum.t()) end)
assert capture_io(fn -> t(Enum.t()) end) == capture_io(fn -> t(Enum.t() / 0) end)
@@ -831,6 +835,16 @@ defmodule IEx.HelpersTest do
assert capture_io(fn -> t(URI.t()) end) == capture_io(fn -> t(URI.t() / 0) end)
end
test "sorts types alphabetically" do
unsorted =
capture_io(fn -> t(Enum) end)
|> String.split("\n")
|> Enum.reject(&(&1 == ""))
|> Enum.map(&String.replace(&1, ~r/@(type|opaque) /, ""))
assert unsorted == Enum.sort(unsorted)
end
test "prints type documentation" do
content = """
defmodule TypeSample do