Skip to content

Commit

Permalink
Fix #52: Allow ignoring specific rules in rebar.config (#56)
Browse files Browse the repository at this point in the history
* Fix #52: Allow ignoring specific rules in rebar.config

* Simplify
Brujo Benavides authored Jan 4, 2021
1 parent 6db579d commit f8ebf0a
Showing 4 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -49,8 +49,8 @@ The plugin supports the following configuration options in the `hank` section of
* `rules` (`[hank_rule:t()]`):
- This is the list of rules to apply to the analyzed code. Each rule is a module that should apply the `hank_rule` behavior.
- If this option is not defined, Hank will apply all [the default rules](src/rules).
* `ignore` (`[file:filename_all()]`):
- List of wildcard patterns representing the files that Hank will ignore when formatting.
* `ignore` (`[file:filename_all() | {file:filename_all(), hank_rule:t()}]`):
- List of wildcard patterns representing the files and rules that Hank will ignore when formatting. Tuple format is used to ignore only a specific rule in those files.
- You can also ignore a specific file adding the attribute `-hank ignore.` to it.
- And you can ignore specific rules adding the attribute `-hank [hank_rule:t()].` with the list of rules you want to ignore.

12 changes: 9 additions & 3 deletions src/hank.erl
Original file line number Diff line number Diff line change
@@ -5,16 +5,22 @@

%% @doc Runs a list of rules over a list of files and returns all the
%% dead code pieces it can find.
-spec analyze([file:filename()], [file:filename()], [hank_rule:t()], hank_context:t()) ->
-spec analyze([file:filename()],
[{file:filename(), hank_rule:t() | all}],
[hank_rule:t()],
hank_context:t()) ->
#{results => [hank_rule:result()], ignored => non_neg_integer()}.
analyze(Files, IgnoredFiles, Rules, Context) ->
ASTs = [{File, get_ast(File)} || File <- Files],
IgnoredRules =
[{File, IgnoredRule}
|| {File, AST} <- ASTs,
not lists:member(File, IgnoredFiles),
not lists:member({File, all}, IgnoredFiles),
IgnoredRule <- ignored_rules(AST, Rules)]
++ [{File, IgnoredRule} || File <- IgnoredFiles, IgnoredRule <- Rules],
++ [{File, Rule}
|| {File, IgnoredRule} <- IgnoredFiles,
Rule <- Rules,
IgnoredRule == all orelse IgnoredRule == Rule],
AllResults =
[Result#{rule => Rule}
|| Rule <- Rules, Result <- hank_rule:analyze(Rule, ASTs, Context)],
13 changes: 11 additions & 2 deletions src/rebar3_hank_prv.erl
Original file line number Diff line number Diff line change
@@ -43,8 +43,9 @@ do(State) ->
case proplists:get_value(ignore, rebar_state:get(State, hank, []), none) of
none ->
[];
Wildcards ->
[F || Wildcard <- Wildcards, F <- filelib:wildcard(Wildcard)]
IgnoreRules ->
[{F, Rule}
|| {Wildcard, Rule} <- normalize(IgnoreRules), F <- filelib:wildcard(Wildcard)]
end,
try hank:analyze(Files, IgnoredFiles, Rules, Context) of
#{results := [], ignored := 0} ->
@@ -87,3 +88,11 @@ get_rules(State) ->
Rules ->
Rules
end.

normalize(IgnoreRules) ->
lists:map(fun ({Wildcard, Rule}) ->
{Wildcard, Rule};
(Wildcard) ->
{Wildcard, all}
end,
IgnoreRules).
14 changes: 14 additions & 0 deletions test/ignore_SUITE.erl
Original file line number Diff line number Diff line change
@@ -31,6 +31,20 @@ rebar_config(_Config) ->
[{ignore, [binary_to_list(File) || #{file := File} <- Warnings]}]),
{ok, _} = rebar3_hank_prv:do(State2),

ct:comment("If we ignore some rules on the problematic files, we should "
"not get warnings for them"),
State3 =
rebar_state:set(State,
hank,
[{ignore,
[{binary_to_list(File), global_rejector}
|| #{file := File, text := <<" global_rejector">>} <- Warnings]}]),
Warnings3 = find_warnings(State3),
[] = [x || #{text := <<" global_rejector">>} <- Warnings3],
true = length(Warnings3) > 0,
[] = Warnings3 -- Warnings,
true = length(Warnings -- Warnings3) > 0,

{comment, ""}.

%% @doc No warning should be emitted for files with -hank ignore

0 comments on commit f8ebf0a

Please sign in to comment.