Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
defmodule Credo.Check.Readability.MultiAlias do | ||
@moduledoc false | ||
|
||
@checkdoc """ | ||
Multi alias expansion makes module uses harder to search for in large code bases. | ||
# preferred | ||
alias Module.Foo | ||
alias Module.Bar | ||
# NOT preferred | ||
alias Module.{Foo, Bar} | ||
Like all `Readability` issues, this one is not a technical concern. | ||
But you can improve the odds of others reading and liking your code by making | ||
it easier to follow. | ||
""" | ||
@explanation [check: @checkdoc] | ||
|
||
use Credo.Check, base_priority: :low | ||
|
||
alias Credo.Code | ||
|
||
@doc false | ||
def run(source_file, params \\ []) do | ||
issue_meta = IssueMeta.for(source_file, params) | ||
|
||
Code.prewalk(source_file, &traverse(&1, &2, issue_meta)) | ||
end | ||
|
||
defp traverse( | ||
{:alias, _, [{{_, _, [{:__aliases__, opts, base_alias}, :{}]}, _, [multi_alias | _]}]} = | ||
ast, | ||
issues, | ||
issue_meta | ||
) do | ||
{:__aliases__, _, [module | []]} = multi_alias | ||
new_issue = issue_for(issue_meta, Keyword.get(opts, :line), base_alias, module) | ||
|
||
{ast, [new_issue | issues]} | ||
end | ||
|
||
defp traverse(ast, issues, _issue_meta), do: {ast, issues} | ||
|
||
defp issue_for(issue_meta, line_no, base_alias, trigger) do | ||
module = | ||
base_alias | ||
|> Module.concat() | ||
|> Module.concat(trigger) | ||
|> inspect() | ||
|
||
format_issue( | ||
issue_meta, | ||
message: "Avoid multi aliases to #{module}; consider removing.", | ||
trigger: trigger, | ||
line_no: line_no | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Credo.Check.Readability.MultiAliasTest do | ||
use Credo.TestHelper | ||
|
||
@described_check Credo.Check.Readability.MultiAlias | ||
|
||
# | ||
# cases NOT raising issues | ||
# | ||
|
||
test "it should NOT report violation" do | ||
""" | ||
defmodule Test do | ||
alias App.Module1 | ||
alias App.Module2 | ||
end | ||
""" | ||
|> to_source_file | ||
|> refute_issues(@described_check) | ||
end | ||
|
||
# | ||
# cases raising issues | ||
# | ||
|
||
test "it should report a violation" do | ||
""" | ||
defmodule CredoSampleModule do | ||
alias App.Module2.{Module3} | ||
end | ||
""" | ||
|> to_source_file | ||
|> assert_issue(@described_check) | ||
end | ||
|
||
test "it should report a violation for multiple expansions" do | ||
""" | ||
defmodule CredoSampleModule do | ||
alias App.{Module1, Module2} | ||
end | ||
""" | ||
|> to_source_file | ||
|> assert_issue(@described_check) | ||
end | ||
end |