Skip to content

Commit

Permalink
Fix false positive on SpaceAroundOperators
Browse files Browse the repository at this point in the history
Refs #797
  • Loading branch information
René Föhring committed Oct 24, 2020
1 parent bd42ff0 commit 85c1755
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators.SpaceHelper do
Examples:
x..-1 # .. is the operator here and there is usually no space after that
"""
def usually_no_space_after?({:"(", _}, {:dual_op, _, :-}, {:identifier, _, _}), do: true
def usually_no_space_after?({:"(", _}, {:dual_op, _, :-}, {:number, _, _}), do: true
def usually_no_space_after?({:"(", _}, {:dual_op, _, :-}, {:int, _, _}), do: true
def usually_no_space_after?({:"(", _}, {:dual_op, _, :-}, {:float, _, _}), do: true
def usually_no_space_after?({:"(", _}, {:dual_op, _, :-}, {:flt, _, _}), do: true
def usually_no_space_after?({:",", _}, {:dual_op, _, :-}, {:identifier, _, _}), do: true
def usually_no_space_after?({:",", _}, {:dual_op, _, :-}, {:number, _, _}), do: true
def usually_no_space_after?({:",", _}, {:dual_op, _, :-}, {:int, _, _}), do: true
def usually_no_space_after?({:",", _}, {:dual_op, _, :-}, {:float, _, _}), do: true
def usually_no_space_after?({:",", _}, {:dual_op, _, :-}, {:flt, _, _}), do: true
def usually_no_space_after?({_, _, :^}, {_, _, :-}, _), do: true
def usually_no_space_after?({_, _, :=}, {_, _, :-}, _), do: true
def usually_no_space_after?({_, _, :..}, {_, _, :-}, _), do: true
Expand Down
137 changes: 62 additions & 75 deletions test/credo/check/consistency/space_around_operators/collector_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,94 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators.CollectorTest do

alias Credo.Check.Consistency.SpaceAroundOperators.Collector

@with_space """
defmodule Credo.Sample1 do
defmodule InlineModule do
def foobar do
4 + 3
4 - 3
4 * 3
a = 3
4 && 3
"4" <> "3"
4 == 3
[4] ++ [3]
4 == 3
4 > 3
4 >= 3
4 <= 3
range = -999..-1
for op <- [:{}, :%{}, :^, :|, :<>] do
test "it should report correct frequencies for operators surrounded by spaces" do
result =
"""
defmodule Credo.Sample1 do
defmodule InlineModule do
def foobar do
4 + 3
4 - 3
4 * 3
a = 3
4 && 3
"4" <> "3"
4 == 3
[4] ++ [3]
4 == 3
4 > 3
4 >= 3
4 <= 3
range = -999..-1
for op <- [:{}, :%{}, :^, :|, :<>] do
end
something = removed != []
Enum.map(dep.deps, &(&1.app)) ++ current_breadths
&function_capture/1
&:erlang_module.function_capture/3
&Elixir.function_capture/3
&@module.blah/1
|> my_func(&Some.Deep.Module.is_something/1)
end
end
something = removed != []
Enum.map(dep.deps, &(&1.app)) ++ current_breadths
&function_capture/1
&:erlang_module.function_capture/3
&Elixir.function_capture/3
&@module.blah/1
|> my_func(&Some.Deep.Module.is_something/1)
end
end
end
"""
@without_space """
defmodule Credo.Sample2 do
def foobar do
1+2
end
end
"""
@mixed """
defmodule Credo.Sample3 do
def foobar do
1+ 2
3 *4
end
end
"""
@with_spaces_special_cases ~S"""
defmodule Credo.Sample2 do
defmodule InlineModule do
def foobar do
child_sources = Enum.drop(child_sources, -1)
TestRepo.all(from p in Post, where: field(p, ^field) = datetime_add(^inserted_at, ^-3, ^"week"))
{literal(-number, type, vars), params}
{time2, _} = :timer.tc(&flush/0, [])
{{:{}, [], [:==, [], [to_escaped_field(field), value]]}, params}
<<_, unquoted::binary-size(size), _>> = quoted
args = Enum.map_join ix+1..ix+length, ",", &"$#{&1}"
end
defp do_underscore(<<?-, t :: binary>>, _) do
end
"""
|> to_source_file()
|> Collector.collect_matches([])

def escape({:-, _, [number]}, type, params, vars, _env) when is_number(number) do
end
end
assert %{with_space: 18} == result
end
"""

test "it should report correct frequencies for operators surrounded by spaces" do
test "it should report correct frequencies for operators surrounded by spaces /2" do
result =
@with_space
"""
a = b + c + compare_fn.(-d, 0)
"""
|> to_source_file()
|> Collector.collect_matches([])

assert %{with_space: 18} == result
assert %{with_space: 3} == result
end

test "it should report correct frequencies for operators not surrounded by spaces" do
test "it should report correct frequencies for operators surrounded by spaces /3" do
result =
@without_space
"""
a = b + c + compare_fn.(-d, 0)
"""
|> to_source_file()
|> Collector.collect_matches([])

assert %{without_space: 1} == result
assert %{with_space: 3} == result
end

test "it should report correct frequencies for mixed cases" do
test "it should report correct frequencies for operators not surrounded by spaces" do
result =
@mixed
"""
defmodule Credo.Sample2 do
def foobar do
1+2
end
end
"""
|> to_source_file()
|> Collector.collect_matches([])

assert %{with_space: 2, without_space: 2} == result
assert %{without_space: 1} == result
end

@tag :to_be_implemented
test "it should NOT report without_space for special cases" do
test "it should report correct frequencies for mixed cases" do
result =
@with_spaces_special_cases
"""
defmodule Credo.Sample3 do
def foobar do
1+ 2
3 *4
end
end
"""
|> to_source_file()
|> Collector.collect_matches([])

assert %{} == Map.delete(result, :with_spaces)
assert %{with_space: 2, without_space: 2} == result
end
end

0 comments on commit 85c1755

Please sign in to comment.