-
Notifications
You must be signed in to change notification settings - Fork 425
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
Add correct column metadata to all warning checks #1127
Changes from 6 commits
f38a299
db720f8
47b2000
346953b
4f37ddb
d8011ee
450f9e9
636cd9d
cdee25f
7a4d7d9
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 |
---|---|---|
|
@@ -29,9 +29,12 @@ defmodule Credo.Check.Warning.ForbiddenModule do | |
|
||
modules = | ||
if Keyword.keyword?(modules) do | ||
Enum.map(modules, fn {key, value} -> {Name.full(key), value} end) | ||
Map.new(modules, fn {key, value} -> {Name.full(key), value} end) | ||
else | ||
Enum.map(modules, fn key -> {Name.full(key), nil} end) | ||
Map.new(modules, fn module -> | ||
full = Name.full(module) | ||
{full, "The `#{Name.full(module)}` module is not allowed."} | ||
end) | ||
end | ||
|
||
Credo.Code.prewalk( | ||
|
@@ -43,9 +46,11 @@ defmodule Credo.Check.Warning.ForbiddenModule do | |
defp traverse({:__aliases__, meta, modules} = ast, 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} | ||
if found_module?(module, forbidden_modules) do | ||
{ast, [issue_for(issue_meta, meta, module, forbidden_modules[module]) | issues]} | ||
else | ||
{ast, issues} | ||
end | ||
end | ||
|
||
defp traverse( | ||
|
@@ -54,51 +59,36 @@ defmodule Credo.Check.Warning.ForbiddenModule do | |
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) | ||
Enum.reduce(aliases, issues, fn {:__aliases__, meta, module}, issues -> | ||
full_name = Name.full([base_alias, module]) | ||
|
||
if found_module?(full_name, forbidden_modules) 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. Isn't this exactly the same as 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's an artifact of the map initializing with nil values at first, while is_map_key would return true here. |
||
message = forbidden_modules[full_name] | ||
trigger = Name.full(module) | ||
[issue_for(issue_meta, meta, trigger, message) | issues] | ||
else | ||
issues | ||
end | ||
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)) | ||
defp found_module?(module, forbidden_modules) when is_map_key(forbidden_modules, module), | ||
do: true | ||
|
||
if found_module?(forbidden_module_names, module) do | ||
[issue_for(issue_meta, line_no, module, forbidden_modules) | issues] | ||
else | ||
issues | ||
end | ||
end | ||
|
||
defp found_module?(forbidden_module_names, module) do | ||
Enum.member?(forbidden_module_names, module) | ||
end | ||
|
||
defp issue_for(issue_meta, line_no, module, forbidden_modules) do | ||
trigger = Name.full(module) | ||
message = message(forbidden_modules, module) || "The `#{trigger}` module is not allowed." | ||
defp found_module?(_, _), do: false | ||
|
||
defp issue_for(issue_meta, meta, trigger, message) 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. Again: Why the refactor to get the message (and trigger) as an argument instead of looking it up here? 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. The trigger would need to be different if it's coming from a group alias, while the message is always based on the full name so far, since I didn't know if changing the message would be some kind of a compatibility problem decided to keep it consistent as it was. But I don't mind reworking it like this if you prefer? 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 the trigger is different for different scenarios, we can pass it in. |
||
format_issue( | ||
issue_meta, | ||
message: message, | ||
trigger: trigger, | ||
line_no: line_no | ||
line_no: meta[:line], | ||
column: meta[:column] | ||
) | ||
end | ||
|
||
defp message(forbidden_modules, module) do | ||
Enum.find_value(forbidden_modules, fn | ||
{^module, message} -> message | ||
_ -> nil | ||
end) | ||
end | ||
end |
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.
Why did you choose to put the fallback message here instead of where it was? To me seems unclear.
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.
Hmm I did it because issue_for was getting too many arguments since I would need the trigger to be the short name in some cases, and then I'd need to pass in the full name as well in this scenario. I can revert it though
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.
I think we can tolerate 5, 6 or even 7 arguments in a function in functional programming 🙂
Especially if it makes the flow & origin of information more clear.