Skip to content

Commit

Permalink
Merge pull request #947 from hoyon/forbidden-module-nested-alias
Browse files Browse the repository at this point in the history
Make Credo.Check.Warning.ForbiddenModule work for grouped aliases
  • Loading branch information
rrrene authored Feb 5, 2022
2 parents fa82efd + 7e47e38 commit 34e2d14
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
38 changes: 33 additions & 5 deletions lib/credo/check/warning/forbidden_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,42 @@ defmodule Credo.Check.Warning.ForbiddenModule do
defp traverse(ast = {:__aliases__, meta, modules}, issues, forbidden_modules, issue_meta) do
module = Name.full(modules)

issues = put_issue_if_forbidden(issues, issue_meta, meta[:line], module, forbidden_modules)

{ast, issues}
end

defp traverse(
ast = {:alias, _meta, [{{_, _, [{:__aliases__, _opts, base_alias}, :{}]}, _, aliases}]},
issues,
forbidden_modules,
issue_meta
) do
modules =
Enum.map(aliases, fn {:__aliases__, meta, module} ->
{Name.full([base_alias, module]), meta[:line]}
end)

issues =
Enum.reduce(modules, issues, fn {module, line}, issues ->
put_issue_if_forbidden(issues, issue_meta, line, module, forbidden_modules)
end)

{ast, issues}
end

defp traverse(ast, issues, _, _), do: {ast, issues}

defp put_issue_if_forbidden(issues, issue_meta, line_no, module, forbidden_modules) do
forbidden_module_names = Enum.map(forbidden_modules, &elem(&1, 0))

if found_module?(forbidden_module_names, module) do
{ast, [issue_for(issue_meta, meta[:line], module, forbidden_modules) | issues]}
[issue_for(issue_meta, line_no, module, forbidden_modules) | issues]
else
{ast, issues}
issues
end
end

defp traverse(ast, issues, _, _), do: {ast, issues}

defp found_module?(forbidden_module_names, module) do
Enum.member?(forbidden_module_names, module)
end
Expand All @@ -68,6 +93,9 @@ defmodule Credo.Check.Warning.ForbiddenModule do
end

defp message(forbidden_modules, module) do
Enum.find_value(forbidden_modules, fn {^module, message} -> message end)
Enum.find_value(forbidden_modules, fn
{^module, message} -> message
_ -> nil
end)
end
end
24 changes: 24 additions & 0 deletions test/credo/check/warning/forbidden_module_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ defmodule Credo.Check.Warning.ForbiddenModuleTest do
|> assert_issue()
end

test "it should report on grouped aliases" do
"""
defmodule CredoSampleModule do
alias CredoSampleModule.{AllowedModule, ForbiddenModule, ForbiddenModule2}
def some_function, do: ForbiddenModule.another_function()
end
"""
|> to_source_file
|> run_check(@described_check, modules: [CredoSampleModule.ForbiddenModule, CredoSampleModule.ForbiddenModule2])
|> assert_issues()
end

test "it should report on import" do
"""
defmodule CredoSampleModule do
Expand Down Expand Up @@ -103,4 +115,16 @@ defmodule Credo.Check.Warning.ForbiddenModuleTest do
assert issue.message == expected_message
end)
end

test "it should work with multiple forbidden modules" do
"""
defmodule CredoSampleModule do
def some_function, do: CredoSampleModule.ForbiddenModule.another_function()
def some_function2, do: CredoSampleModule.ForbiddenModule2.another_function()
end
"""
|> to_source_file
|> run_check(@described_check, modules: [CredoSampleModule.ForbiddenModule, CredoSampleModule.ForbiddenModule2])
|> assert_issues()
end
end

0 comments on commit 34e2d14

Please sign in to comment.