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

Rules for warning about seqbind #67

Merged
merged 3 commits into from
Oct 31, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
{test, [
{deps, [ {katana_test, {git, "https://github.com/inaka/katana-test.git", {tag, "0.0.6"}}}
, {mixer, {git, "https://github.com/inaka/mixer.git", {tag, "0.1.5"}}}
, {seqbind, {git, "https://github.com/lpgauth/seqbind.git", {branch, "master"}}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use "master" branch, either choose a tag or use a commit SHA.

, {meck, "0.8.3"}
]}
]},
Expand Down
68 changes: 67 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
max_module_length/3,
max_function_length/3,
no_debug_call/3,
no_nested_try_catch/3
no_nested_try_catch/3,
no_seqbind/3,
no_useless_seqbind/3
]).

-define(LINE_LENGTH_MSG, "Line ~p is too long: ~s.").
Expand Down Expand Up @@ -114,6 +116,12 @@
-define(NO_NESTED_TRY_CATCH,
"Nested try...catch block starting at line ~p.").

-define(NO_SEQBIND,
"Declaration of seqbind at line ~p.").

-define(NO_USELESS_SEQBIND,
"Module declares seqbind on line ~p, but no seq-bindings are used.").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -1182,3 +1190,61 @@ check_nested_try_catchs(ResultFun, TryExp) ->
false
end,
elvis_code:find(Predicate, TryExp)).


%% Disallow `-compile({parse_trans, seqbind})`
-spec no_seqbind(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
[elvis_result:item()].
no_seqbind(Config, Target, _RuleConfig) ->
{Root, _} = elvis_file:parse_tree(Config, Target),
ResultFun = result_node_line_fun(?NO_SEQBIND),
SeqbindDecls = elvis_code:find(fun is_seqbind_declaration/1, Root),
lists:map(ResultFun, SeqbindDecls).


is_seqbind_declaration(Node) ->
case ktn_code:type(Node) of
compile -> declares_seqbind(ktn_code:attr(value, Node));
_ -> false
end.


declares_seqbind(Decls) when is_list(Decls) ->
lists:keyfind(parse_transform, 1, Decls) =:= {parse_transform, seqbind};
declares_seqbind(Singleton) ->
Singleton =:= {parse_transform, seqbind}.


%% Warn when `-compile({parse_trans, seqbind})` is declared,
%% but no seq-bindings (i.e., VarName@) are used in the module.
-spec no_useless_seqbind(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
[elvis_result:item()].
no_useless_seqbind(Config, Target, _RuleConfig) ->
{Root, _} = elvis_file:parse_tree(Config, Target),
ResultFun = result_node_line_fun(?NO_USELESS_SEQBIND),
SeqbindDecls = elvis_code:find(fun is_seqbind_declaration/1, Root),
case SeqbindDecls of
[] -> [];
_ ->
case uses_seq_bindings(Root) of
true -> [];
false -> lists:map(ResultFun, SeqbindDecls)
end
end.


uses_seq_bindings(Root) ->
SeqBindings = elvis_code:find(
fun(Node) ->
ktn_code:type(Node) =:= var andalso
lists:last(ktn_code:attr(text, Node)) =:= $@
end,
Root),
case SeqBindings of
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a case, you can do SeqBindings /= [] directly.

[] -> false;
_ -> true
end.
5 changes: 5 additions & 0 deletions test/examples/fail_no_seqbind.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(fail_no_seqbind).

-compile({parse_transform, seqbind}).
-compile([{parse_transform, seqbind}]).
-compile([export_all, {parse_transform, seqbind}]).
10 changes: 10 additions & 0 deletions test/examples/fail_no_useless_seqbind.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-module(fail_no_useless_seqbind).

-compile({parse_transform, seqbind}).

-export([demo/0]).

demo() ->
X1 = 10,
X2 = X1 + 1,
X2.
10 changes: 10 additions & 0 deletions test/examples/pass_no_useless_seqbind.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-module(pass_no_useless_seqbind).

-compile({parse_transformer, seqbind}).

-export([demo/0]).

demo() ->
X@ = 10,
X@ = X@ + 1,
X@.
23 changes: 23 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
verify_max_function_length/1,
verify_no_debug_call/1,
verify_no_nested_try_catch/1,
verify_no_seqbind/1,
verify_no_useless_seqbind/1,
%% Non-rule
results_are_ordered_by_line/1
]).
Expand Down Expand Up @@ -552,6 +554,27 @@ verify_no_nested_try_catch(_Config) ->
#{line_num := 35}
] = elvis_style:no_nested_try_catch(ElvisConfig, File, #{}).

-spec verify_no_seqbind(config()) -> any().
verify_no_seqbind(_Config) ->
ElvisConfig = elvis_config:default(),
SrcDirs = ["../../test/examples"],
File = "fail_no_seqbind.erl",
{ok, Path} = elvis_test_utils:find_file(SrcDirs, File),
[
#{line_num := 3},
#{line_num := 4},
#{line_num := 5}
] = elvis_style:no_seqbind(ElvisConfig, Path, #{}).

-spec verify_no_useless_seqbind(config()) -> any().
verify_no_useless_seqbind(_Config) ->
ElvisConfig = elvis_config:default(),
SrcDirs = ["../../test/examples"],
{ok, PassPath} = elvis_test_utils:find_file(SrcDirs, "pass_no_useless_seqbind.erl"),
{ok, FailPath} = elvis_test_utils:find_file(SrcDirs, "fail_no_useless_seqbind.erl"),
[] = elvis_style:no_useless_seqbind(ElvisConfig, PassPath, #{}),
[#{line_num := 3}] = elvis_style:no_useless_seqbind(ElvisConfig, FailPath, #{}).

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