Skip to content

Commit

Permalink
Merge pull request #250 from inaka/elbrujohalcon.172.consider_more_de…
Browse files Browse the repository at this point in the history
…fault_operator

Fix #172: Add many more operators to the default of operator_spaces
  • Loading branch information
elbrujohalcon committed Jun 16, 2022
2 parents 5712823 + ffff991 commit 8cf1d5f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
10 changes: 9 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,15 @@
default(macro_names) ->
#{regex => "^([A-Z][A-Z_0-9]+)$"};
default(operator_spaces) ->
#{rules => [{right, ","}, {right, "++"}, {left, "++"}]};
#{rules =>
[{right, "++"}, {left, "++"}, {right, "="}, {left, "="}, {right, "+"}, {left, "+"},
{right, "-"}, {left, "-"}, {right, "*"}, {left, "*"}, {right, "/"}, {left, "/"},
{right, "=<"}, {left, "=<"}, {right, "<"}, {left, "<"}, {right, ">"}, {left, ">"},
{right, ">="}, {left, ">="}, {right, "=="}, {left, "=="}, {right, "=:="}, {left, "=:="},
{right, "/="}, {left, "/="}, {right, "=/="}, {left, "=/="}, {right, "--"}, {left, "--"},
{right, "=>"}, {left, "=>"}, {right, ":="}, {left, ":="}, {right, "<-"}, {left, "<-"},
{right, "<="}, {left, "<="}, {right, "||"}, {left, "||"}, {right, "|"}, {left, "|"},
{right, "::"}, {left, "::"}, {right, "->"}, {left, "->"}, {right, ","}]};
default(no_space) ->
#{rules => [{right, "("}, {left, ")"}, {left, ","}]};
default(nesting_level) ->
Expand Down
48 changes: 48 additions & 0 deletions test/examples/fail_operator_spaces.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
, windows_newlines/0
, this/0
, this/1
, pass_more_operators/0
, fail_more_operators/0
]).

%% No space before and after coma,on a comment.
Expand Down Expand Up @@ -102,3 +104,49 @@ this()
this(shouldnt_either)
-> A = 1
- 2, A.


-spec pass_more_operators() -> R when R :: tuple().
pass_more_operators() ->
D = "Elvis should not complain this function "
"since operators are properly spaced out: "
"=,+,-,*,/,=<,<,>,>=,==,=:=,/=,=/=,--,=>,:=,<-,<=,||,|,::,->",
X = 1 + 2 - 3 * 4 / 5,
M = #{d => D, x => X},
{
X =< 1,
X < 2,
X > 3,
X >= 4,
X == 5,
X =:= 6,
X /= 7,
X =/= 8,
D -- "",
M#{d => D, x := X},
[A || A <- D],
<< <<A>> || <<A>> <= list_to_binary(D) >>,
[X | D]
}.

-spec fail_more_operators()->R when R::tuple().
fail_more_operators()->
D="Elvis should complain this function "
"since operators have no space around them.",
X=1+2-3*4/5,
M=#{d=>D, x=>X},
{
X=<1,
X<2,
X>3,
X>=4,
X==5,
X=:=6,
X/=7,
X=/=8,
D--"",
M#{d=>D, x:=X},
[A||A<- D],
<<<<$>>>||<<$>>><=<<$>>>>>,
[X|D]
}.
2 changes: 1 addition & 1 deletion test/examples/pass_line_length_elvis_attr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function_5() ->
io:format("This line is 90 characters long and should be detected ~p!!!!!!!!!!!!!!!!!!!!!!!!!!", [yeah]),
io:format("This line is 90 characters long and should be detected ~p and these two escaped ~p!!!!!!!!!!!!!!!!!!", [yeah, no]).

function_6(Config, AntPositions)->
function_6(Config, AntPositions) ->
gb_trees:from_orddict([{Pos, #{pos => Pos, state => model:random_ant_state(Config)}} || Pos <- lists:sort(AntPositions)]). % {Pozycja, CałyAgent} - ew. do zmiany, jest zbalansowany [DG]

function_7() ->
Expand Down
45 changes: 26 additions & 19 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -411,35 +411,42 @@ verify_operator_spaces(Config) ->
[_] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, AppendOptions, Path),

SumOperation = #{rules => [{right, "+"}, {left, "+"}]},
[_, _] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, SumOperation, Path),
[_, _, _, _] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, SumOperation, Path),

MinusOperation = #{rules => [{right, "-"}, {left, "-"}]},
[] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, MinusOperation, Path),
[_, _] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, MinusOperation, Path),

Arrow = #{rules => [{left, "->"}]},
[] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, Arrow, Path),
[_, _] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, Arrow, Path),

BarOptions = #{rules => [{right, "|"}, {left, "|"}]},
[_, _] = elvis_core_apply_rule(Config, elvis_style, operator_spaces, BarOptions, Path),
[_, _, _, _] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, BarOptions, Path),

ComprehensionOperation = #{rules => [{right, "||"}, {left, "||"}]},
[_, _] =
[_, _, _, _, _, _] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, ComprehensionOperation, Path),

AllOptions =
#{rules =>
[{right, ","},
{right, "++"},
{left, "++"},
{right, "+"},
{left, "+"},
{left, "->"},
{right, "|"},
{left, "|"},
{right, "||"},
{left, "||"}]},
[_, _, _, _, _, _, _, _, _, _] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, AllOptions, Path).
DefaultOptions = #{},
[#{info := [right, "," | _]}, #{info := [right, "," | _]}, #{info := [left, "++" | _]},
#{info := [right, "," | _]}, #{info := [left, "+" | _]}, #{info := [right, "+" | _]},
#{info := [right, "|" | _]}, #{info := [left, "|" | _]}, #{info := [right, "||" | _]},
#{info := [left, "||" | _]}, #{info := [right, "::" | _]}, #{info := [left, "::" | _]},
#{info := [right, "->" | _]}, #{info := [left, "->" | _]}, #{info := [left, "->" | _]},
#{info := [right, "+" | _]}, #{info := [left, "+" | _]}, #{info := [right, "-" | _]},
#{info := [left, "-" | _]}, #{info := [right, "*" | _]}, #{info := [left, "*" | _]},
#{info := [right, "/" | _]}, #{info := [left, "/" | _]}, #{info := [right, "=<" | _]},
#{info := [left, "=<" | _]}, #{info := [right, "<" | _]}, #{info := [left, "<" | _]},
#{info := [right, ">" | _]}, #{info := [left, ">" | _]}, #{info := [right, ">=" | _]},
#{info := [left, ">=" | _]}, #{info := [right, "==" | _]}, #{info := [left, "==" | _]},
#{info := [right, "=:=" | _]}, #{info := [left, "=:=" | _]}, #{info := [right, "/=" | _]},
#{info := [left, "/=" | _]}, #{info := [right, "=/=" | _]}, #{info := [left, "=/=" | _]},
#{info := [right, "--" | _]}, #{info := [left, "--" | _]}, #{info := [right, "||" | _]},
#{info := [left, "||" | _]}, #{info := [right, "||" | _]}, #{info := [left, "||" | _]},
#{info := [right, "|" | _]}, #{info := [left, "|" | _]}] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, DefaultOptions, Path).

-spec verify_no_space(config()) -> any().
verify_no_space(Config) ->
Expand Down

0 comments on commit 8cf1d5f

Please sign in to comment.