Skip to content

Commit 13af842

Browse files
maennchenJosé Valim
authored and
José Valim
committed
IEx: Sort Types in t helper (#9131)
1 parent a211223 commit 13af842

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

lib/iex/lib/iex/introspection.ex

+30-19
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ defmodule IEx.Introspection do
644644
types_not_found(inspect(module))
645645

646646
{:ok, types} ->
647-
Enum.each(types, &(&1 |> format_type() |> IO.puts()))
647+
types
648+
|> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end)
649+
|> Enum.each(&(&1 |> format_type() |> IO.puts()))
648650
end
649651

650652
dont_display_result()
@@ -656,15 +658,19 @@ defmodule IEx.Introspection do
656658
no_beam(module)
657659

658660
{:ok, types} ->
659-
printed =
660-
for {kind, {^type, _, args}} = typespec <- types,
661-
kind != :typep do
662-
type_doc(module, type, length(args), typespec)
663-
|> print_typespec()
664-
end
665-
666-
if printed == [] do
667-
types_not_found_or_private("#{inspect(module)}.#{type}")
661+
types
662+
|> Enum.filter(&match?({kind, {^type, _, _}} when kind in [:type, :opaque], &1))
663+
|> Enum.sort_by(fn {_, {name, _, args}} -> {name, length(args)} end)
664+
|> case do
665+
[] ->
666+
types_not_found_or_private("#{inspect(module)}.#{type}")
667+
668+
types ->
669+
Enum.map(types, fn {_, {_, _, args}} = typespec ->
670+
module
671+
|> type_doc(type, length(args), typespec)
672+
|> print_typespec()
673+
end)
668674
end
669675
end
670676

@@ -677,16 +683,21 @@ defmodule IEx.Introspection do
677683
no_beam(module)
678684

679685
{:ok, types} ->
680-
printed =
681-
for {kind, {^type, _, args}} = typespec <- types,
682-
kind != :typep,
683-
length(args) == arity do
684-
type_doc(module, type, arity, typespec)
685-
|> print_typespec()
686-
end
686+
types
687+
|> Enum.find(
688+
&match?(
689+
{kind, {^type, _, args}} when kind in [:type, :opaque] and length(args) == arity,
690+
&1
691+
)
692+
)
693+
|> case do
694+
nil ->
695+
types_not_found_or_private("#{inspect(module)}.#{type}")
687696

688-
if printed == [] do
689-
types_not_found_or_private("#{inspect(module)}.#{type}")
697+
typespec ->
698+
module
699+
|> type_doc(type, arity, typespec)
700+
|> print_typespec()
690701
end
691702
end
692703

lib/iex/test/iex/helpers_test.exs

+14
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,27 @@ defmodule IEx.HelpersTest do
824824
end) >= 2
825825
end
826826

827+
test "prints private types" do
828+
assert capture_io(fn -> t(Date.Range) end) =~ "@typep iso_days"
829+
end
830+
827831
test "prints type information" do
828832
assert "@type t() ::" <> _ = capture_io(fn -> t(Enum.t()) end)
829833
assert capture_io(fn -> t(Enum.t()) end) == capture_io(fn -> t(Enum.t() / 0) end)
830834
assert "@type child_spec() ::" <> _ = capture_io(fn -> t(:supervisor.child_spec()) end)
831835
assert capture_io(fn -> t(URI.t()) end) == capture_io(fn -> t(URI.t() / 0) end)
832836
end
833837

838+
test "sorts types alphabetically" do
839+
unsorted =
840+
capture_io(fn -> t(Enum) end)
841+
|> String.split("\n")
842+
|> Enum.reject(&(&1 == ""))
843+
|> Enum.map(&String.replace(&1, ~r/@(type|opaque) /, ""))
844+
845+
assert unsorted == Enum.sort(unsorted)
846+
end
847+
834848
test "prints type documentation" do
835849
content = """
836850
defmodule TypeSample do

0 commit comments

Comments
 (0)