Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc_rules/elvis_style/operator_spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ compromised without adequate spacing.

`{left, "!"}, {right, "!"}` was added in [4.0.0](https://github.com/inaka/elvis_core/releases/tag/4.0.0)).

`{left, "?="}, {right, "?="}` was added in [4.1.0](https://github.com/inaka/elvis_core/releases/tag/4.1.0)).

## Example configuration

```erlang
Expand Down
32 changes: 19 additions & 13 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ default(operator_spaces) ->
{left, "->"},
{right, ","},
{left, "!"},
{right, "!"}
{right, "!"},
{right, "?="},
{left, "?="}
]
};
default(no_space) ->
Expand Down Expand Up @@ -871,7 +873,7 @@ operator_spaces(Config, Target, RuleConfig) ->
Zipper = elvis_code:code_zipper(Root),
OpNodes = zipper:filter(fun is_operator_node/1, Zipper),

PunctuationTokens = elvis_code:find_by_types_in_tokens(?PUNCTUATION_SYMBOLS, Root),
PunctuationTokens = elvis_code:find_by_types_in_tokens(['=' | ?PUNCTUATION_SYMBOLS], Root),

Lines = elvis_utils:split_all_lines(Src),
AllNodes = OpNodes ++ PunctuationTokens,
Expand All @@ -883,7 +885,14 @@ operator_spaces(Config, Target, RuleConfig) ->
%% @doc Returns true when the node is an operator with more than one operand
-spec is_operator_node(ktn_code:tree_node()) -> boolean().
is_operator_node(Node) ->
ktn_code:type(Node) =:= op andalso length(ktn_code:content(Node)) > 1.
NodeType = ktn_code:type(Node),
OpOrMatch = [op | match_operators()],
ExtraOpsTypes = [map_field_exact, generate, b_generate, map_field_assoc],
(length(ktn_code:content(Node)) > 1 andalso lists:member(NodeType, OpOrMatch)) orelse
lists:member(NodeType, ExtraOpsTypes).

match_operators() ->
[match, maybe_match].

-type no_space_config() ::
#{ignore => [ignorable()], rules => [{right | left, string()}]}.
Expand Down Expand Up @@ -1915,11 +1924,11 @@ is_match_in_condition(Node) ->
ktn_code:content(Node)
)).

is_match(Node) ->
lists:member(ktn_code:type(Node), match_operators()).

has_match_child(Node) ->
IsMatch = fun(InnerNode) ->
ktn_code:type(InnerNode) =:= match orelse ktn_code:type(InnerNode) =:= maybe_match
end,
lists:any(IsMatch, ktn_code:content(Node)).
lists:any(fun is_match/1, ktn_code:content(Node)).

-type numeric_format_config() ::
#{
Expand Down Expand Up @@ -2654,12 +2663,9 @@ check_parent_match_or_macro(Zipper) ->
false;
ParentZipper ->
Parent = zipper:node(ParentZipper),
case ktn_code:type(Parent) of
match ->
zipper:down(ParentZipper) =:= Zipper;
macro ->
zipper:down(ParentZipper) =:= Zipper;
maybe_match ->
IsMatchOrMacro = lists:member(ktn_code:type(Parent), [macro | match_operators()]),
case IsMatchOrMacro of
true ->
zipper:down(ParentZipper) =:= Zipper;
_ ->
check_parent_match_or_macro(ParentZipper)
Expand Down
12 changes: 12 additions & 0 deletions test/examples/fail_operator_spaces.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%% Another initial comment
-module(fail_operator_spaces).

-feature(maybe_expr, enable).

-dialyzer({nowarn_function, function7/0}).

-export([ function1/2
Expand All @@ -21,6 +23,7 @@
, pass_more_operators/0
, fail_more_operators/0
, fail_no_space_excl/0
, fail_maybe/0
]).

%% No space before and after coma,on a comment.
Expand Down Expand Up @@ -154,3 +157,12 @@ fail_more_operators()->

fail_no_space_excl() ->
self()!'a'.

fail_maybe() ->
maybe
A = 2,
A?=throw(error)
else
_ ->
ok
end.
21 changes: 20 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ verify_operator_spaces(Config) ->
#{info := [right, "->" | _]},
#{info := [left, "->" | _]},
#{info := [left, "->" | _]},
#{info := [right, "=" | _]},
#{info := [left, "=" | _]},
#{info := [right, "=" | _]},
#{info := [left, "=" | _]},
#{info := [right, "+" | _]},
#{info := [left, "+" | _]},
#{info := [right, "-" | _]},
Expand All @@ -684,6 +688,12 @@ verify_operator_spaces(Config) ->
#{info := [left, "*" | _]},
#{info := [right, "/" | _]},
#{info := [left, "/" | _]},
#{info := [right, "=" | _]},
#{info := [left, "=" | _]},
#{info := [right, "=>" | _]},
#{info := [right, "=>" | _]},
#{info := [left, "=>" | _]},
#{info := [left, "=>" | _]},
#{info := [right, "=<" | _]},
#{info := [left, "=<" | _]},
#{info := [right, "<" | _]},
Expand All @@ -702,14 +712,23 @@ verify_operator_spaces(Config) ->
#{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 := [left, "!" | _]},
#{info := [right, "!" | _]}
#{info := [right, "!" | _]},
#{info := [right, "?=" | _]},
#{info := [left, "?=" | _]}
] =
elvis_core_apply_rule(Config, elvis_style, operator_spaces, DefaultOptions, Path).

Expand Down