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

Prevent crash for operators being analyzed at the left-most line position #184

Merged
merged 1 commit into from
Mar 13, 2021
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
6 changes: 4 additions & 2 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -998,9 +998,11 @@ character_at_location(Position, Lines, Operator, {LineNo, Col}, Encoding) ->
% If ColToCheck is greater than the length of OperatorLineStr variable, it
% means the end of line was reached so return " " to make the check pass,
% otherwise return the character at the given column.
% NOTE: text below only applies when the given Position is equal to `right`.
% NOTE: text below only applies when the given Position is equal to `right`,
% or Position is equal to `left` and Col is 1.
SpaceChar = $\s,
case {Position, (ColToCheck > length(OperatorLineStr))} of
case ColToCheck =:= 0 orelse {Position, (ColToCheck > length(OperatorLineStr))} of
true -> SpaceChar;
{right, true} -> SpaceChar;
_ -> lists:nth(ColToCheck, OperatorLineStr)
end.
Expand Down
13 changes: 13 additions & 0 deletions test/examples/fail_operator_spaces.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
, function7/0
, tag_filters/2
, unicode_characters/0
, this/0
, this/1
]).

%% No space before and after coma,on a comment.
Expand Down Expand Up @@ -75,3 +77,14 @@ unicode_characters() ->
<<"©"/utf8>> = <<"\\u00A9">>,
<<"ß"/utf8>> = <<"\\o337">>,
ok.

-spec this()
-> should_not_crash.
this()
-> should_not_crash.

-spec this(shouldnt_either)
-> -1.
this(shouldnt_either)
-> A = 1
- 2, A.
6 changes: 5 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,15 @@ verify_operator_spaces(Config) ->
MinusOperation = #{rules => [{right, "-"}, {left, "-"}]},
[] = 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),

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

Expand Down