Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #172: Add many more operators to the default of operator_spaces #250

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -412,35 +412,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