-
Notifications
You must be signed in to change notification settings - Fork 420
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
Allowing to ignore modules on Credo.Check.Readability.AliasAs #987
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ defmodule Credo.Check.Readability.AliasAs do | |
use Credo.Check, | ||
base_priority: :low, | ||
tags: [:experimental], | ||
param_defaults: [ | ||
ignore: [] | ||
], | ||
explanations: [ | ||
check: """ | ||
Aliases which are not completely renamed using the `:as` option are easier to follow. | ||
|
@@ -30,29 +33,52 @@ defmodule Credo.Check.Readability.AliasAs do | |
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. | ||
""" | ||
""", | ||
params: [ | ||
ignore: "List of modules to ignore and allow to `alias Module, as: ...`" | ||
] | ||
] | ||
|
||
@doc false | ||
@impl true | ||
def run(%SourceFile{} = source_file, params) do | ||
ignore = Params.get(params, :ignore, __MODULE__) | ||
|
||
source_file | ||
|> Credo.Code.prewalk(&traverse(&1, &2, IssueMeta.for(source_file, params))) | ||
|> Credo.Code.prewalk(&traverse(&1, &2, IssueMeta.for(source_file, params), ignore)) | ||
|> Enum.reverse() | ||
end | ||
|
||
defp traverse(ast, issues, issue_meta), do: {ast, add_issue(issues, issue(ast, issue_meta))} | ||
defp traverse(ast, issues, issue_meta, ignore), | ||
do: {ast, add_issue(issues, issue(ast, issue_meta, ignore))} | ||
|
||
defp add_issue(issues, nil), do: issues | ||
defp add_issue(issues, issue), do: [issue | issues] | ||
|
||
defp issue({:alias, _, [{:__MODULE__, _, nil}, [as: {_, meta, _}]]}, issue_meta), | ||
do: issue_for(issue_meta, meta[:line], inspect(:__MODULE__)) | ||
defp issue({:alias, _, [{:__MODULE__, _, nil}, [as: {_, meta, _}]]}, issue_meta, ignore) do | ||
line = meta[:line] | ||
{Credo.IssueMeta, source_file, _check_params} = issue_meta | ||
{_def, module_name} = Check.scope_for(source_file, line: line) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems expensive to get the scope for each occurrence instead of passing the module name during traversal. |
||
module = Module.concat([module_name]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||
|
||
if :__MODULE__ not in ignore and module not in ignore do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
issue_for(issue_meta, line, inspect(:__MODULE__)) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
defp issue({:alias, _, [{_, _, original}, [as: {_, meta, _}]]}, issue_meta), | ||
do: issue_for(issue_meta, meta[:line], inspect(Module.concat(original))) | ||
defp issue({:alias, _, [{_, _, original}, [as: {_, meta, _}]]}, issue_meta, ignore) do | ||
module = Module.concat(original) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use |
||
|
||
if module not in ignore do | ||
issue_for(issue_meta, meta[:line], inspect(module)) | ||
else | ||
nil | ||
end | ||
end | ||
|
||
defp issue(_ast, _issue_meta), do: nil | ||
defp issue(_ast, _issue_meta, _ignore), do: nil | ||
|
||
defp issue_for(issue_meta, line_no, trigger) do | ||
format_issue( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the functions in
Credo.IssueMeta
to deal with IssueMeta.