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

[Closes #236] Operator space false positive #259

Merged
merged 8 commits into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from 7 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
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()}) ->
undefined | {ok, map()}.
Copy link
Member

Choose a reason for hiding this comment

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

This spec says it returns undefined, but it returns not_found and it's converted to undefined elvis_style.erl@609

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} -> [];
Copy link
Member

Choose a reason for hiding this comment

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

Why matching all this cases? If I'm not mistaken it would either be undefined or any of your valid options. Wouldn't matching undefined and have an otherwise default behaviour save lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to check for those types of nodes or tokens where an operator without a space is acceptable. An alternative would be to match for all that shouldn't have it, but in either case there needs to be an explicit specification of the valid (or invalid) types of nodes/tokens.

_ ->
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).
Copy link
Member

Choose a reason for hiding this comment

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

Are you using 2 or 4 spaces? Line 25 is using 4.


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],
Copy link
Member

Choose a reason for hiding this comment

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

Why exclude all if it has arity 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a comprehensive exclusion list.

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, ""}.