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

Ignore Ecto.Query functions in ABCSize when Ecto.Query is imported. Fixes #561. #735

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/credo/check/refactor/abc_size.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule Credo.Check.Refactor.ABCSize do
max_size: 30,
excluded_functions: []
]
@ecto_functions ["where", "from", "select", "join"]
@def_ops [:def, :defp, :defmacro]
@branch_ops [:.]
@condition_ops [:if, :unless, :for, :try, :case, :cond, :and, :or, :&&, :||]
Expand All @@ -31,16 +32,34 @@ defmodule Credo.Check.Refactor.ABCSize do

@doc false
def run(source_file, params \\ []) do
ignore_ecto? = imports_ecto_query?(source_file)
issue_meta = IssueMeta.for(source_file, params)
max_abc_size = Params.get(params, :max_size, @default_params)
excluded_functions = Params.get(params, :excluded_functions, @default_params)

excluded_functions =
if ignore_ecto? do
@ecto_functions ++ excluded_functions
else
excluded_functions
end

Credo.Code.prewalk(
source_file,
&traverse(&1, &2, issue_meta, max_abc_size, excluded_functions)
)
end

defp imports_ecto_query?(source_file),
do: Credo.Code.prewalk(source_file, &traverse_for_ecto/2, false)

defp traverse_for_ecto(_, true), do: {nil, true}

defp traverse_for_ecto({:import, _, [{:__aliases__, _, [:Ecto, :Query]} | _]}, false),
do: {nil, true}

defp traverse_for_ecto(ast, false), do: {ast, false}

defp traverse(
{:defmacro, _, [{:__using__, _, _}, _]} = ast,
issues,
Expand Down
43 changes: 43 additions & 0 deletions test/credo/check/refactor/abc_size_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,47 @@ defmodule Credo.Check.Refactor.ABCSizeTest do

assert rounded_abc_size(source, ["where", "join", "select", "distinct"]) == 1
end

test "it should NOT count ecto functions when Ecto.Query is imported" do
source = """
defmodule CredoEctoQueryModule do
import Ecto.Query

def fun() do
Favorite
|> where(user_id: ^user.id)
|> join(:left, [f], t in Template, f.entity_id == t.id and f.entity_type == "template")
|> join(:left, [f, t], d in Document, f.entity_id == d.id and f.entity_type == "document")
|> join(:left, [f, t, d], dt in Template, dt.id == d.template_id)
|> join(:left, [f, t, d, dt], c in Category, c.id == t.category_id or c.id == dt.category_id)
|> select([f, t, d, dt, c], c)
|> distinct(true)
|> Repo.all()
end
end
"""
|> to_source_file
|> refute_issues(@described_check, max_size: 3)
end

test "it SHOULD count ecto functions when Ecto.Query is NOT imported" do
source = """
defmodule CredoEctoQueryModule do

def fun() do
Favorite
|> where(user_id: ^user.id)
|> join(:left, [f], t in Template, f.entity_id == t.id and f.entity_type == "template")
|> join(:left, [f, t], d in Document, f.entity_id == d.id and f.entity_type == "document")
|> join(:left, [f, t, d], dt in Template, dt.id == d.template_id)
|> join(:left, [f, t, d, dt], c in Category, c.id == t.category_id or c.id == dt.category_id)
|> select([f, t, d, dt, c], c)
|> distinct(true)
|> Repo.all()
end
end
"""
|> to_source_file
|> assert_issue(@described_check, max_size: 3)
end
end