Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #640 from mirego/feature/unnecessary-alias-expansion
Add UnnecessaryAliasExpansion check
- Loading branch information
Showing
3 changed files
with
95 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
48 changes: 48 additions & 0 deletions
48
lib/credo/check/readability/unnecessary_alias_expansion.ex
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,48 @@ | ||
defmodule Credo.Check.Readability.UnnecessaryAliasExpansion do | ||
@moduledoc false | ||
|
||
@checkdoc """ | ||
Alias expansion is useful but when aliasing a single module, | ||
it can be harder to read with unnecessary braces. | ||
# preferred | ||
alias ModuleA.Foo | ||
alias ModuleA.{Foo, Bar} | ||
# NOT preferred | ||
alias ModuleA.{Foo} | ||
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, [child]}]}]} = ast, issues, issue_meta) do | ||
{ast, issues ++ [issue_for(issue_meta, Keyword.get(opts, :line), child)]} | ||
end | ||
|
||
defp traverse(ast, issues, _issue_meta), do: {ast, issues} | ||
|
||
defp issue_for(issue_meta, line_no, trigger) do | ||
format_issue( | ||
issue_meta, | ||
message: "Unnecessary alias expansion for #{trigger}, consider removing braces.", | ||
trigger: trigger, | ||
line_no: line_no | ||
) | ||
end | ||
end |
46 changes: 46 additions & 0 deletions
46
test/credo/check/readability/unnecessary_alias_expansion_test.exs
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,46 @@ | ||
defmodule Credo.Check.Readability.UnnecessaryAliasExpansionTest do | ||
use Credo.TestHelper | ||
|
||
@described_check Credo.Check.Readability.UnnecessaryAliasExpansion | ||
|
||
# | ||
# cases NOT raising issues | ||
# | ||
|
||
test "it should NOT report violation" do | ||
""" | ||
defmodule Test do | ||
alias App.Module1 | ||
alias App.{Module2, Module3} | ||
end | ||
""" | ||
|> to_source_file | ||
|> refute_issues(@described_check) | ||
end | ||
|
||
# | ||
# cases raising issues | ||
# | ||
|
||
test "it should report a violation" do | ||
""" | ||
defmodule CredoSampleModule do | ||
alias App.Module1 | ||
alias App.Module2.{Module3} | ||
end | ||
""" | ||
|> to_source_file | ||
|> assert_issue(@described_check) | ||
end | ||
|
||
test "it should report a violation for double expansion" do | ||
""" | ||
defmodule CredoSampleModule do | ||
alias App.Module1 | ||
alias App.{Module2}.{Module3} | ||
end | ||
""" | ||
|> to_source_file | ||
|> assert_issue(@described_check) | ||
end | ||
end |