Skip to content

Commit

Permalink
[Fix #50] Remove superfluous parentheses from macros (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brujo Benavides authored Jan 21, 2020
1 parent b48c488 commit a7894a6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
27 changes: 11 additions & 16 deletions src/rebar3_prettypr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ lay_no_comments(Node, Ctxt) ->
end,
maybe_parentheses(D3, Prec, Ctxt);
application ->
lay_type_application(erl_syntax:application_operator(Node),
erl_syntax:application_arguments(Node), Ctxt);
lay_application(erl_syntax:application_operator(Node),
erl_syntax:application_arguments(Node), Ctxt);
match_expr ->
{PrecL, Prec, PrecR} = inop_prec('='),
D1 = lay(erl_syntax:match_expr_pattern(Node), set_prec(Ctxt, PrecL)),
Expand Down Expand Up @@ -341,7 +341,7 @@ lay_no_comments(Node, Ctxt) ->
Type = dodge_macros(Type0),
As0 = dodge_macros(Elements),
As = erl_syntax:concrete(As0),
D1 = lay_type_application(TypeName, As, Ctxt1),
D1 = lay_application(TypeName, As, Ctxt1),
D2 = lay(erl_syntax:concrete(Type), Ctxt1),
beside(follow(lay(N, Ctxt1), beside(D1, lay_text_float(" :: ")),
Ctxt1#ctxt.break_indent),
Expand All @@ -353,7 +353,7 @@ lay_no_comments(Node, Ctxt) ->
beside(lay(N, Ctxt1),
beside(text("("), beside(lay(As, Ctxt1), lay_text_float(")"))));
_ when Args =:= none -> lay(N, Ctxt1);
_ -> D1 = lay_items(Args, Ctxt1, fun lay/2), lay_application(N, D1, Ctxt1)
_ -> lay_application(N, Args, Ctxt1)
end,
beside(lay_text_float("-"), beside(D, lay_text_float(".")));
binary ->
Expand Down Expand Up @@ -443,12 +443,10 @@ lay_no_comments(Node, Ctxt) ->
N = erl_syntax:macro_name(Node),
D = case erl_syntax:macro_arguments(Node) of
none -> lay(N, Ctxt1);
Args ->
As = lay_items(Args, set_prec(Ctxt1, max_prec()), fun lay/2),
lay_application(N, As, Ctxt1)
Args -> lay_application(N, Args, Ctxt1)
end,
D1 = beside(lay_text_float("?"), D),
maybe_parentheses(D1, 0, Ctxt); % must be conservative!
maybe_parentheses(D1, 0, Ctxt1);
parentheses ->
D = lay(erl_syntax:parentheses_body(Node), reset_prec(Ctxt)),
lay_parentheses(D, Ctxt);
Expand Down Expand Up @@ -571,7 +569,7 @@ lay_no_comments(Node, Ctxt) ->
[A] = Arguments,
D1 = lay(A, reset_prec(Ctxt)),
beside(text("["), beside(D1, text(", ...]")));
_ -> lay_type_application(Name, Arguments, Ctxt)
_ -> lay_application(Name, Arguments, Ctxt)
end;
bitstring_type ->
Ctxt1 = set_prec(Ctxt, max_prec()),
Expand Down Expand Up @@ -617,7 +615,7 @@ lay_no_comments(Node, Ctxt) ->
D2 = lay(Type, set_prec(Ctxt, PrecR)),
D3 = lay_follow_beside_text_float(D1, D2, Ctxt),
maybe_parentheses(D3, Prec, Ctxt);
false -> lay_type_application(Name, Args, Ctxt)
false -> lay_application(Name, Args, Ctxt)
end;
map_type ->
case erl_syntax:map_type_fields(Node) of
Expand Down Expand Up @@ -669,8 +667,8 @@ lay_no_comments(Node, Ctxt) ->
set_prec(Ctxt, PrecR), fun lay/2),
maybe_parentheses(Es, Prec, Ctxt);
user_type_application ->
lay_type_application(erl_syntax:user_type_application_name(Node),
erl_syntax:user_type_application_arguments(Node), Ctxt)
lay_application(erl_syntax:user_type_application_name(Node),
erl_syntax:user_type_application_arguments(Node), Ctxt)
end.

attribute_name(Node) ->
Expand Down Expand Up @@ -858,7 +856,7 @@ lay_type_par_text(Name, Value, Text, Ctxt) ->
D2 = lay(Value, Ctxt1),
par([D1, lay_text_float(Text), D2], Ctxt1#ctxt.break_indent).

lay_type_application(Name, Arguments, Ctxt) ->
lay_application(Name, Arguments, Ctxt) ->
{PrecL, Prec} = func_prec(), %
D1 = lay(Name, set_prec(Ctxt, PrecL)),
As = lay_items(Arguments, reset_prec(Ctxt), fun lay/2),
Expand Down Expand Up @@ -961,9 +959,6 @@ is_last_and_before_empty_line(H, [], #ctxt{empty_lines = EmptyLines}) ->
is_last_and_before_empty_line(H, [H2 | _], #ctxt{empty_lines = EmptyLines}) ->
get_pos(H2) - get_pos(H) >= 2 andalso lists:member(get_pos(H) + 1, EmptyLines).

lay_application(Name, Params, Ctxt) ->
beside(lay(Name, Ctxt), beside(text("("), beside(Params, lay_text_float(")")))).

get_pos(Node) ->
case erl_syntax:get_pos(Node) of
I when is_integer(I) -> I;
Expand Down
8 changes: 4 additions & 4 deletions test_app/after/src/macros.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@

-define(SOMEONE, ?NEW_PERSON("Someone", 44)).

is_adult(Person) -> Age = maps:get(age, Person, 0), Age >= (?ADULT_AGE).
is_adult(Person) -> Age = maps:get(age, Person, 0), Age >= ?ADULT_AGE.

-ifdef(TEST).

-include_lib("eunit/include/eunit.hrl").

is_adult_test() ->
?assert((is_adult(?NEW_PERSON("test1", 22)))),
?assertNot((is_adult(?NEW_PERSON("test2", 12)))),
?assert((is_adult(?SOMEONE))).
?assert(is_adult(?NEW_PERSON("test1", 22))),
?assertNot(is_adult(?NEW_PERSON("test2", 12))),
?assert(is_adult(?SOMEONE)).

-endif.

Expand Down
8 changes: 4 additions & 4 deletions test_app/after/src/syntax_tools_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ foo2(A, B) ->
?macro_string, ?macro_cond1, ?macro_cond2, ?macro_block(A), ?macro_if(A, B),
?macro_argument1(A), ?macro_argument1(begin A end), ?macro_block(<<"hello">>),
?macro_block("hello"), ?macro_block([$h, $e, $l, $l, $0]),
?macro_argument1((id(<<"hello">>))),
?macro_argument1(id(<<"hello">>)),
?macro_argument1(if A -> B;
true -> 3.14
end),
Expand All @@ -87,7 +87,7 @@ foo2(A, B) ->
C -> C
end),
?macro_argument1(receive M -> M after 100 -> 3 end),
?macro_argument1(try foo5(A) catch C:(?macro_simple5) -> {C, B} end),
?macro_argument1(try foo5(A) catch C:?macro_simple5 -> {C, B} end),
?macro_argument2(A, B)],
A, B, ok].

Expand All @@ -106,8 +106,8 @@ foo4(A, B, #state{c = C} = S) ->
Ls = foo3(),
S1 = #state{a = 1, b = 2},
[foo2(A, Ls), B, C, B(3, C), erlang:process_info(self()),
erlang:(?macro_simple5)(self()), A:(?MACRO_SIMPLE2)(), A:(?macro_simple1)(),
erlang:?macro_simple5(self()), A:?MACRO_SIMPLE2(), A:?macro_simple1(),
A:process_info(self()), A:B(3), S#state{a = 2, b = B, d = S1}].

foo5(A) -> try foo2(A, A) of R -> R catch error:(?macro_simple5) -> nope end.
foo5(A) -> try foo2(A, A) of R -> R catch error:?macro_simple5 -> nope end.

6 changes: 3 additions & 3 deletions test_app/after/src/type_specs.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

-wild(attribute).

-record(par, {a :: undefined | (?MODULE)}).
-record(par, {a :: undefined | ?MODULE}).

-record(r0, {}).

Expand Down Expand Up @@ -66,7 +66,7 @@
-spec type_specs:f(pair(r0(), r0())) -> pair(t(), t()).

f({R, R}) ->
_ = (?MODULE_STRING) ++ "hej",
_ = ?MODULE_STRING ++ "hej",
_ = <<"foo">>,
_ = R#r.f1,
_ = R#r{f1 = 17, f2 = b},
Expand All @@ -82,5 +82,5 @@ b() -> case foo:bar() of #{a := 2} -> 19 end.
(X, Y) -> {atom(), float()} when X :: atom(), Y :: float();
(integer(), atom()) -> {integer(), atom()}.

c(A, B) -> _ = (?I), {A, B}.
c(A, B) -> _ = ?I, {A, B}.

0 comments on commit a7894a6

Please sign in to comment.