Skip to content

Commit

Permalink
Merge pull request #259 from inaka/jfacorro.236.operator_space_false_…
Browse files Browse the repository at this point in the history
…positive

[Closes #236] Operator space false positive
  • Loading branch information
HernanRivasAcosta committed Aug 27, 2015
2 parents e2c243d + b331d65 commit e316259
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PROJECT = elvis

DEPS = lager sync getopt jiffy ibrowse aleppo zipper egithub katana
TEST_DEPS = meck
TEST_DEPS = meck xref_runner

dep_lager = git https://github.com/basho/lager.git 2.0.3
dep_sync = git https://github.com/inaka/sync.git 0.1.3
Expand All @@ -12,7 +12,8 @@ dep_ibrowse = git https://github.com/cmullaparthi/ibrowse v4.1.2
dep_aleppo = git https://github.com/inaka/aleppo 0.9.1
dep_zipper = git https://github.com/inaka/zipper 0.1.2
dep_egithub = git https://github.com/inaka/erlang-github 0.1.7
dep_katana = git https://github.com/inaka/erlang-katana 0.2.7
dep_katana = git https://github.com/inaka/erlang-katana 0.2.10
dep_xref_runner = git https://github.com/inaka/xref_runner.git 0.2.2

include erlang.mk

Expand All @@ -32,7 +33,7 @@ escript: all
./elvis help

shell: app
erl -pa ebin -pa deps/*/ebin -s sync -s elvis -s lager -config config/elvis.config
erl -pa ebin -pa deps/*/ebin -name elvis@`hostname` -s sync -s elvis -s lager -config config/elvis.config

test-shell: build-ct-suites app
erl -pa ebin -pa deps/*/ebin -pa test -s sync -s elvis -s lager -config config/elvis.config
Expand Down
11 changes: 11 additions & 0 deletions src/elvis_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
find/2,
find/3,
find_by_location/2,
find_token/2,
code_zipper/1,
code_zipper/2
]).
Expand Down Expand Up @@ -128,6 +129,16 @@ is_at_location(Node = #{attrs := #{location := {Line, NodeCol}}},
is_at_location(_, _) ->
false.

-spec find_token(ktn_code:tree_node(), {integer(), integer()}) ->
not_found | {ok, map()}.
find_token(Root, Location) ->
Fun = fun (Token) -> is_at_location(Token, Location) end,
Tokens = ktn_code:attr(tokens, Root),
case lists:filter(Fun, Tokens) of
[] -> not_found;
[Token | _] -> {ok, Token}
end.

%%% Processing functions

%% @doc Takes a node and returns all nodes where the nesting limit is exceeded.
Expand Down
17 changes: 11 additions & 6 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,17 @@ check_operator_spaces_rule(Line, Num, {Position, Operator}, Root) ->
not_found -> undefined;
{ok, Node} -> ktn_code:type(Node)
end,
case Type of
atom -> [];
binary_element -> [];
string -> [];
char -> [];
comment -> [];
TokenType = case elvis_code:find_token(Root, {Num, Col}) of
not_found -> undefined;
{ok, Token} -> ktn_code:type(Token)
end,
case {Type, TokenType} of
{atom, _} -> [];
{binary_element, _} -> [];
{string, _} -> [];
{char, _} -> [];
{comment, _} -> [];
{_, string} -> [];
_ ->
Msg = ?OPERATOR_SPACE_MSG,
Info = [Label, Operator, Num],
Expand Down
24 changes: 22 additions & 2 deletions test/examples/fail_operator_spaces.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-module(fail_operator_spaces).

-export([function1/2,function2/2, function3/2, function4/2, function5/0]).
-export([function1/2,function2/2, function3/2, function4/2, function5/0, tag_filters/2]).

%% No space before and after coma,on a comment.

Expand All @@ -23,4 +23,24 @@ function4(Should, <<_:10/binary, ",", _/binary>>) ->

function5() ->
User = #{name => <<"Juan">>, email => <<"juan@inaka.com">>},
<<"juan@inaka.com">> = maps:get(email,User).
<<"juan@inaka.com">> = maps:get(email,User).

tag_filters(DocName, #{conn := Conn} = State) ->
TableName = atom_to_list(DocName),
Sql = ["SELECT "
" 'tag' AS \"type\", "
" tag_name AS value, "
" COUNT(1) AS \"count\" "
"FROM ( "
" SELECT unnest(regexp_split_to_array(tags, ',')) AS tag_name"
" FROM ", TableName, " "
") AS tags "
"GROUP BY tag_name "
"ORDER BY tag_name "],
Values = [],
case {Conn, Sql, Values} of
{ok, Maps} ->
{ok, {raw, Maps}, State};
{error, Error} ->
{error, Error, State}
end.
26 changes: 26 additions & 0 deletions test/xref_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-module(xref_SUITE).

-type config() :: proplists:proplist().

-export(
[
all/0,
xref/1
]
).

-spec all() -> [atom()].
all() ->
ExcludedFuns = [all, module_info],
Exports = ?MODULE:module_info(exports),
[F || {F, 1} <- Exports, not lists:member(F, ExcludedFuns)].

-spec xref(config()) -> {comment, []}.
xref(_Config) ->
Dirs = [filename:absname("../../ebin")],
[] = xref_runner:check(undefined_function_calls, #{dirs => Dirs}),
[] = xref_runner:check(undefined_functions, #{dirs => Dirs}),
[] = xref_runner:check(locals_not_used, #{dirs => Dirs}),
[] = xref_runner:check(deprecated_function_calls, #{dirs => Dirs}),
[] = xref_runner:check(deprecated_functions, #{dirs => Dirs}),
{comment, ""}.

0 comments on commit e316259

Please sign in to comment.