Skip to content

Commit 0b6781f

Browse files
Add {left, "."}, {left, ";"} to no_space; add {right, ";"} to operator_spaces (#470)
* Add `left, .` to `no_space` (default) * Add `left, ;` to `no_space` (default) * Add `right, ;` to `operator_spaces` (default) * Act on self-review: more doc * Handle left-of better * Test further
1 parent 5eff7b9 commit 0b6781f

File tree

6 files changed

+72
-19
lines changed

6 files changed

+72
-19
lines changed

doc_rules/elvis_style/no_space.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ codebase.
3232

3333
- `rules :: [{right | left, string()}]`
3434
- default: `[{right, "("}, {left, ")"}, {left, ","}, {left, ":"}, {right, "#"}, {right, "?"},
35-
{right, "?"}]`
35+
{right, "?"}, {left, "."}, {left, ";"}]`
3636

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

39+
`{left, "."}, {left, ";"}` was added in [4.1.0](https://github.com/inaka/elvis_core/releases/tag/4.1.0).
40+
3941
## Example configuration
4042

4143
```erlang
@@ -46,6 +48,8 @@ codebase.
4648
, {right, "#"}
4749
, {right, "?"}
4850
, {right, "?"}
51+
, {left, "."}
52+
, {left, ";"}
4953
]
5054
}}
5155
```

doc_rules/elvis_style/operator_spaces.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ compromised without adequate spacing.
3434
, {right, "<="}, {left, "<="}, {right, "||"}, {left, "||"}
3535
, {right, "|"}, {left, "|"}, {right, "::"}, {left, "::"}
3636
, {right, "->"}, {left, "->"}, {right, ","}, {right, "!"}
37-
, {left, "!"}, {right, "?="}, {left, "?="}
37+
, {left, "!"}, {right, "?="}, {left, "?="}, {right, ";"}
3838
]
3939
```
4040

4141
`rules` was `[{right, ","}, {right, "++"}, {left, "++"}]` until [1.5.0](https://github.com/inaka/elvis_core/releases/tag/1.5.0).
4242

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

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

4747
## Example configuration
4848

@@ -59,7 +59,7 @@ compromised without adequate spacing.
5959
, {right, "<="}, {left, "<="}, {right, "||"}, {left, "||"}
6060
, {right, "|"}, {left, "|"}, {right, "::"}, {left, "::"}
6161
, {right, "->"}, {left, "->"}, {right, ","}, {right, "!"}
62-
, {left, "!"}, {right, "?="}, {left, "?="}
62+
, {left, "!"}, {right, "?="}, {left, "?="}, {right, ";"}
6363
]
6464
}}
6565
```

src/elvis_style.erl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,12 @@ default(operator_spaces) ->
343343
{right, "!"},
344344
{left, "!"},
345345
{right, "?="},
346-
{left, "?="}
346+
{left, "?="},
347+
{right, ";"}
347348
]
348349
};
349350
default(no_space) ->
351+
% ) one can happen at the start of lines; all others can't
350352
#{
351353
rules =>
352354
[
@@ -356,7 +358,9 @@ default(no_space) ->
356358
{left, ":"},
357359
{right, ":"},
358360
{right, "#"},
359-
{right, "?"}
361+
{right, "?"},
362+
{left, "."},
363+
{left, ";"}
360364
]
361365
};
362366
default(nesting_level) ->
@@ -2434,7 +2438,10 @@ macro_as_atom(false, [Type | OtherTypes], MacroNodeValue) ->
24342438
% _ is re:mp()
24352439

24362440
check_spaces(Lines, UnfilteredNodes, {Position, Text}, Encoding, {How0, _} = How) ->
2437-
FilterFun = fun(Node) -> ktn_code:attr(text, Node) =:= Text end,
2441+
FilterFun = fun(Node) ->
2442+
ktn_code:attr(text, Node) =:= Text orelse
2443+
(ktn_code:type(Node) =:= dot andalso Text =:= ".")
2444+
end,
24382445
Nodes = lists:filter(FilterFun, UnfilteredNodes),
24392446
SpaceChar = $\s,
24402447
FlatFun =
@@ -2508,16 +2515,20 @@ character_at_location(
25082515
SpaceChar = $\s,
25092516

25102517
case {ColToCheck, Position, length(TextLineStr)} of
2511-
{0, _, _} when How =:= should_have ->
2518+
{0, _, _} when Text =/= ")" ->
25122519
SpaceChar;
2513-
{0, _, _} when How =:= should_not_have ->
2514-
"";
25152520
{_, right, LenLine} when How =:= should_have, ColToCheck > LenLine ->
25162521
SpaceChar;
25172522
{_, right, LenLine} when How =:= should_not_have, ColToCheck > LenLine ->
25182523
"";
2519-
_ when How =:= should_have; TextRegex =:= nomatch ->
2524+
_ when How =:= should_have; TextRegex =:= nomatch, ColToCheck > 1 ->
25202525
lists:nth(ColToCheck, TextLineStr);
2526+
_ when
2527+
How =:= should_not_have,
2528+
ColToCheck > 1,
2529+
(Text =:= ":" orelse Text =:= "." orelse Text =:= ";")
2530+
->
2531+
lists:nth(ColToCheck - 1, TextLineStr);
25212532
_ ->
25222533
""
25232534
end.

test/examples/fail_no_space.erl

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
, unicode_characters/0
1818
, windows_newlines/0
1919
, this/0
20-
, this/2
21-
, use_record/0
20+
, this/2
21+
, use_record/0, dot_alone_1/0, dot_alone_2/0, dot_alone/1, colon_hurrah/0, colon_hurrah_2/0
2222
]).
2323

2424
-define(MACRO, "Brujo loves these").
@@ -91,7 +91,7 @@ tag_filters(DocName, #{conn := Conn} = State ) ->
9191
Values = [],
9292
case {Conn, Sql, Values} of
9393
{ok, Maps, _} ->
94-
{ok, {raw, Maps}, State};
94+
{ok, {raw, Maps}, State} ;
9595
{error, Error, _} ->
9696
{error, Error, State}
9797
end.
@@ -118,4 +118,29 @@ this(shouldnt_either, _A)
118118
- 2, A, $ .
119119

120120
use_record() ->
121-
# a{one = 1, two = ? MACRO}.
121+
# a{one = 1, two = ? MACRO} .
122+
123+
dot_alone_1() ->
124+
"The dot is in the next line"
125+
.
126+
127+
dot_alone_2() ->
128+
"The dot is in the next line"
129+
.
130+
131+
dot_alone(a) ->
132+
"The ; is in the next line"
133+
;
134+
dot_alone(b) ->
135+
"The ; is in the next line"
136+
;
137+
dot_alone(c) ->
138+
ok.
139+
140+
colon_hurrah() ->
141+
m
142+
:a().
143+
144+
colon_hurrah_2() ->
145+
m
146+
:a().

test/examples/fail_operator_spaces.erl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function8() ->
6868
function9() ->
6969
[X|| X <- [fail]] ++ [X ||X <- [fail]] ++ [X || X <- [notfail]].
7070

71-
tag_filters(DocName, #{conn := Conn} = State) ->
71+
tag_filters(DocName, #{conn := Conn} = State) when is_list(DocName);is_binary(DocName) ->
7272
TableName = atom_to_list(DocName),
7373
Sql = ["SELECT "
7474
" 'tag' AS \"type\", "
@@ -82,10 +82,10 @@ tag_filters(DocName, #{conn := Conn} = State) ->
8282
"ORDER BY tag_name "],
8383
Values = [],
8484
case {Conn, Sql, Values} of
85-
{ok, Maps, _} ->
85+
{ok, Maps, _} when Maps=:=#{};Conn=:=established ->
8686
{ok, {raw, Maps}, State};
8787
{error, Error, _} ->
88-
{error, Error, State}
88+
{error, Error, State};{finally, this, _} -> nok
8989
end.
9090

9191
unicode_characters() ->

test/style_SUITE.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,9 @@ verify_operator_spaces(Config) ->
652652
#{info := [left, "|" | _]},
653653
#{info := [right, "||" | _]},
654654
#{info := [left, "||" | _]},
655+
#{info := [right, ";" | _]},
656+
#{info := [right, ";" | _]},
657+
#{info := [right, ";" | _]},
655658
#{info := [right, "::" | _]},
656659
#{info := [left, "::" | _]},
657660
#{info := [right, "->" | _]},
@@ -720,15 +723,25 @@ verify_no_space(Config) ->
720723
Path1 = "fail_no_space." ++ Ext,
721724
[
722725
#{info := [right, "(", 3]},
726+
#{info := [left, ",", 20]},
723727
#{info := [right, "(", 36]},
724728
#{info := [right, "(", 52]},
725729
#{info := [left, ")", 52]},
726730
#{info := [left, ",", 76]},
727731
#{info := [left, ")", 79]},
732+
#{info := [left, ";", 94]},
728733
#{info := [right, "(", 109]},
729734
#{info := [left, ")", 109]},
735+
#{info := [left, ".", 118]},
730736
#{info := [right, "#", 121]},
731-
#{info := [right, "?", 121]}
737+
#{info := [right, "?", 121]},
738+
#{info := [left, ".", 121]},
739+
#{info := [left, ".", 125]},
740+
#{info := [left, ".", 129]},
741+
#{info := [left, ";", 133]},
742+
#{info := [left, ";", 136]},
743+
#{info := [left, ":", 142]},
744+
#{info := [left, ":", 146]}
732745
] =
733746
elvis_core_apply_rule(
734747
Config,

0 commit comments

Comments
 (0)