Skip to content

Commit

Permalink
Lowercase Type Variables and Fix Underscore (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjh authored and asummers committed Sep 16, 2019
1 parent f5b2a4d commit 2683c6e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
21 changes: 15 additions & 6 deletions lib/erlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -507,18 +507,18 @@ defmodule Erlex do
end

defp do_pretty_print({:when_names, when_names, {:list, :paren, items}}) do
Enum.map_join(items, ", ", &trim_when_names(do_pretty_print(&1), when_names))
Enum.map_join(items, ", ", &format_when_names(do_pretty_print(&1), when_names))
end

defp do_pretty_print({:when_names, when_names, item}) do
trim_when_names(do_pretty_print(item), when_names)
format_when_names(do_pretty_print(item), when_names)
end

defp trim_when_names(item, when_names) do
defp format_when_names(item, when_names) do
trimmed = String.trim_leading(item, ":")

if trimmed in when_names do
trimmed
downcase_first(trimmed)
else
item
end
Expand All @@ -527,19 +527,28 @@ defmodule Erlex do
defp collect_and_print_whens(whens) do
{pretty_names, when_names} =
Enum.reduce(whens, {[], []}, fn {_, when_name, type}, {prettys, whens} ->
pretty_name = do_pretty_print({:named_type_with_appended_colon, when_name, type})
pretty_name =
{:named_type_with_appended_colon, when_name, type}
|> do_pretty_print()
|> downcase_first()

{[pretty_name | prettys], [when_name | whens]}
end)

when_names =
when_names
|> Enum.map(fn {_, v} -> to_string(v) end)
|> Enum.map(fn {_, v} -> v |> atomize() |> String.trim_leading(":") end)

printed_whens = pretty_names |> Enum.reverse() |> Enum.join(", ")

{printed_whens, when_names}
end

defp downcase_first(string) do
{first, rest} = String.split_at(string, 1)
String.downcase(first) <> rest
end

defp atomize("Elixir." <> module_name) do
String.trim(module_name, "'")
end
Expand Down
24 changes: 23 additions & 1 deletion test/pretty_print_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ defmodule Erlex.Test.PretyPrintTest do

pretty_printed = Erlex.pretty_print(input)

expected_output = "(Atom, Encoding) :: binary() when Atom: atom(), Encoding: :latin1 | :utf8"
expected_output = "(atom, encoding) :: binary() when atom: atom(), encoding: :latin1 | :utf8"

assert pretty_printed == expected_output
end
Expand Down Expand Up @@ -423,6 +423,28 @@ defmodule Erlex.Test.PretyPrintTest do
assert pretty_printed == expected_output
end

test "underscore in when type variable prints appropriately" do
input = ~S"""
(This_one, any()) -> 'ok' when This_one :: key()
"""

pretty_printed = Erlex.pretty_print_contract(input)

expected_output = "(this_one, any()) :: :ok when this_one: key()"
assert pretty_printed == expected_output
end

test "mixed case in when type variable prints appropriately" do
input = ~S"""
(ThisOne, any()) -> 'ok' when ThisOne :: key()
"""

pretty_printed = Erlex.pretty_print_contract(input)

expected_output = "(thisOne, any()) :: :ok when thisOne: key()"
assert pretty_printed == expected_output
end

test "whens with single letter type variables prints appropriately" do
input = ~S"""
() -> a when a :: atom()
Expand Down

0 comments on commit 2683c6e

Please sign in to comment.