Skip to content

Commit

Permalink
Merge pull request #215 from dvaergiller/trailing_whitespace
Browse files Browse the repository at this point in the history
Add rule checking for trailing whitespace
  • Loading branch information
Brujo Benavides committed Apr 7, 2015
2 parents 70fa2bf + 623af81 commit 5ad8afd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ environment values in your [configuration][config] file:
filter => "*.erl",
rules => [{elvis_style, line_length, #{limit => 80}},
{elvis_style, no_tabs},
{elvis_style, no_trailing_whitespace},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
Expand Down
1 change: 1 addition & 0 deletions config/elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
rules => [{elvis_style, line_length, #{limit => 80,
skip_comments => false}},
{elvis_style, no_tabs},
{elvis_style, no_trailing_whitespace},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
Expand Down
1 change: 1 addition & 0 deletions config/test.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
rules => [{elvis_style, line_length, #{limit => 80,
skip_comments => false}},
{elvis_style, no_tabs},
{elvis_style, no_trailing_whitespace},
{elvis_style, macro_names},
{elvis_style, macro_module_names},
{elvis_style, operator_spaces, #{rules => [{right, ","},
Expand Down
33 changes: 33 additions & 0 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
-export([
line_length/3,
no_tabs/3,
no_trailing_whitespace/3,
macro_names/3,
macro_module_names/3,
operator_spaces/3,
Expand All @@ -21,6 +22,9 @@

-define(NO_TABS_MSG, "Line ~p has a tab at column ~p.").

-define(NO_TRAILING_WHITESPACE_MSG,
"Line ~b has ~b trailing whitespace characters.").

-define(INVALID_MACRO_NAME_MSG,
"Invalid macro name ~s on line ~p. Use UPPER_CASE.").

Expand Down Expand Up @@ -105,6 +109,15 @@ no_tabs(_Config, Target, _RuleConfig) ->
{Src, _} = elvis_file:src(Target),
elvis_utils:check_lines(Src, fun check_no_tabs/3, []).

-spec no_trailing_whitespace(elvis_config:config(),
elvis_file:file(),
map()) ->
[elvis_result:item()].
no_trailing_whitespace(_Config, Target, RuleConfig) ->
{Src, _} = elvis_file:src(Target),
elvis_utils:check_lines(Src, fun check_no_trailing_whitespace/3,
RuleConfig).

-spec macro_names(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
Expand Down Expand Up @@ -393,6 +406,26 @@ check_no_tabs(Line, Num, _Args) ->
{ok, Result}
end.

-spec check_no_trailing_whitespace(binary(), integer(), map()) ->
no_result | {ok, elvis_result:item()}.
check_no_trailing_whitespace(Line, Num, RuleConfig) ->
Regex =
case RuleConfig of
%% Lookbehind assertion: http://erlang.org/doc/man/re.html#sect17
#{ignore_empty_lines := true} -> "(?<=\\S)\\s+$";
_AnythingElse -> "\\s+$"
end,

case re:run(Line, Regex) of
nomatch ->
no_result;
{match, [PosLen]} ->
Msg = ?NO_TRAILING_WHITESPACE_MSG,
Info = [Num, size(binary:part(Line, PosLen))],
Result = elvis_result:new(item, Msg, Info, Num),
{ok, Result}
end.

-spec check_macro_names(binary(), integer(), [term()]) ->
no_result | {ok, elvis_result:item()}.
check_macro_names(Line, Num, _Args) ->
Expand Down
21 changes: 21 additions & 0 deletions test/examples/fail_no_trailing_whitespace.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(fail_no_trailing_whitespace).

-export([one/0, two/0, three/0, four/0, five/0]).

one() ->
%% Following line ends with a tab
not_ok.

two() ->
%% Following line ends with a space
not_ok.

three() ->
%% Previous line ends with a space
not_ok.

four() -> %% Previous (supposedly blank) line has a space
not_ok.

five() ->
ok. %% This function should be fine
20 changes: 20 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-export([
verify_line_length_rule/1,
verify_no_tabs_rule/1,
verify_no_trailing_whitespace_rule/1,
verify_macro_names_rule/1,
verify_macro_module_names/1,
verify_operator_spaces/1,
Expand Down Expand Up @@ -94,6 +95,25 @@ verify_no_tabs_rule(_Config) ->

[_, _] = elvis_style:no_tabs(ElvisConfig, Path, #{}).

-spec verify_no_trailing_whitespace_rule(config()) -> any().
verify_no_trailing_whitespace_rule(_Config) ->
ElvisConfig = elvis_config:default(),
SrcDirs = elvis_config:dirs(ElvisConfig),

File = "fail_no_trailing_whitespace.erl",
{ok, Path} = elvis_test_utils:find_file(SrcDirs, File),

do_verify_no_trailing_whitespace(Path, ElvisConfig,
#{ignore_empty_lines => true}, 3),
do_verify_no_trailing_whitespace(Path, ElvisConfig,
#{ignore_empty_lines => false}, 4),
do_verify_no_trailing_whitespace(Path, ElvisConfig, #{}, 4).

do_verify_no_trailing_whitespace(Path, Config, RuleConfig, ExpectedNumItems) ->
Items = elvis_style:no_trailing_whitespace(Config, Path, RuleConfig),
length(Items) == ExpectedNumItems orelse
ct:fail("Expected ~b error items. Got: ~p", [ExpectedNumItems, Items]).

-spec verify_macro_names_rule(config()) -> any().
verify_macro_names_rule(_Config) ->
ElvisConfig = elvis_config:default(),
Expand Down

0 comments on commit 5ad8afd

Please sign in to comment.