Skip to content

Commit

Permalink
Merge pull request #17 from asummers/add-when-support
Browse files Browse the repository at this point in the history
Add when support for typespecs and fix warning spacing.
  • Loading branch information
asummers authored Sep 18, 2018
2 parents 5517874 + 95312e6 commit 810fb1a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
5 changes: 1 addition & 4 deletions lib/erlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ defmodule Erlex do
joiner = "Contract head:\n"

pretty =
Enum.map_join([head | tail], "\n" <> joiner, fn contract ->
Enum.map_join([head | tail], "\n\n" <> joiner, fn contract ->
contract
|> to_charlist()
|> do_pretty_print_contract()
Expand Down Expand Up @@ -259,9 +259,6 @@ defmodule Erlex do
end

defp do_pretty_print({:contract, {:args, args}, {:return, return}, {:whens, whens}}) do
[{:named_type, {:atom, ['w', 'h', 'e', 'n' | rest_name]}, type_list} | rest_types] = whens
whens = [{:named_type, {:atom, rest_name}, type_list} | rest_types]

printed_whens =
Enum.map_join(whens, ", ", fn {_, name, type} ->
do_pretty_print({:named_type_with_appended_colon, name, type})
Expand Down
2 changes: 2 additions & 0 deletions src/lexer.xrl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ NUMBERED = _@[0-9]+::
REST = \.\.\.
RANGE = \.\.
ATOM = \'[^']+\'
WHEN = \swhen\s
Rules.
{WHITESPACE} : skip_token.
{NUMBERED} : skip_token.
{REST} : {token, {'...', TokenLine}}.
{WHEN} : {token, {'when', TokenLine}}.
fun\( : {token, {'fun(', TokenLine}}.
\* : {token, {'*', TokenLine}}.
\[ : {token, {'[', TokenLine}}.
Expand Down
3 changes: 2 additions & 1 deletion src/parser.yrl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int
'<<' '>>'
'<' '>'
'*'
'when'
'='.

Rootsymbol document.
Expand Down Expand Up @@ -98,7 +99,7 @@ assignment -> value '=' value : {assignment, '$1', '$3'}.

byte -> '#' '<' int '>' '(' int ',' int ',' atom ',' '[' atom ',' atom ']' ')' : unwrap('$3').

contract -> list '->' value value_items : {contract, {args, '$1'}, {return, '$3'}, {whens, '$4'}}.
contract -> list '->' value when value_items : {contract, {args, '$1'}, {return, '$3'}, {whens, '$5'}}.
contract -> list '->' value : {contract, {args, '$1'}, {return, '$3'}}.

integer -> int : {int, unwrap('$1')}.
Expand Down
15 changes: 13 additions & 2 deletions test/pretty_print_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ defmodule Erlex.Test.PretyPrintTest do
)

expected_output =
"Contract head:\n(any(), nil) :: nil\nContract head:\n(Ecto.Queryable.t(), String.t()) :: String.t()"
"Contract head:\n(any(), nil) :: nil\n\nContract head:\n(Ecto.Queryable.t(), String.t()) :: String.t()"

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

test "whens in contracts pretty prints appropriately" do
input = ~S"""
(one,any(),'Elixir.Keyword':t()) -> 'ok' when one :: key()
"""

pretty_printed = Erlex.pretty_print_contract(input)

expected_output = "(:one, any(), Keyword.t()) :: :ok when one: key()"
assert pretty_printed == expected_output
end

test "timeout transformation layer pretty prints appropriately" do
input = ~S"""
'infinity' | non_neg_integer()
Expand Down Expand Up @@ -536,7 +547,7 @@ defmodule Erlex.Test.PretyPrintTest do
pretty_printed = Erlex.pretty_print_contract(input)

expected_output =
"Contract head:\n(nil, Dnsimple.Events.HostCreateRequested.t()) ::\n {:ok, {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()}}\nContract head:\n(\n {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()},\n Dnsimple.Events.HostCreateSucceeded.t()\n) :: {:ok, {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()}}"
"Contract head:\n(nil, Dnsimple.Events.HostCreateRequested.t()) ::\n {:ok, {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()}}\n\nContract head:\n(\n {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()},\n Dnsimple.Events.HostCreateSucceeded.t()\n) :: {:ok, {Dnsimple.Models.Host.t(), Dnsimple.Models.Order.t()}}"

assert pretty_printed == expected_output
end
Expand Down

0 comments on commit 810fb1a

Please sign in to comment.