Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new AliasOrder check #547

Merged
merged 1 commit into from Jun 4, 2018
Merged

Conversation

remi
Copy link
Contributor

@remi remi commented Jun 3, 2018

There are already tests for this check, waiting to be implemented. They now all pass.

@rrrene This is my first time implementing a check as complex as this one, so I鈥檓 looking for feedback if I should have done things differently.

Thank you! 馃槉

@remi remi force-pushed the feature/alias-order-check branch 5 times, most recently from f2ed576 to 35da983 Compare June 3, 2018 18:09
@rrrene
Copy link
Owner

rrrene commented Jun 3, 2018

@remiprev 馃憤 that looks pretty good already!

One thing I overlooked in the tests is supporting multi-alias syntax like

      alias Credo.Priority
      alias Credo.CLI.{Sorter,Filename,Options}

Could you add those to the tests and ensure that Multi-Alias is ordered as well?

@remi remi force-pushed the feature/alias-order-check branch 3 times, most recently from 0242438 to 098e5b6 Compare June 3, 2018 18:37
@remi
Copy link
Contributor Author

remi commented Jun 3, 2018

@rrrene Done! 馃帀 I added two new tests and made them pass.

@remi remi force-pushed the feature/alias-order-check branch 2 times, most recently from f4c9104 to 7dcea78 Compare June 3, 2018 20:49
Copy link
Owner

@rrrene rrrene left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! A couple of annotations below. 馃憤

end

group
|> (&apply(Enum, chunk_method, [&1, 2, 1])).()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a module called Credo.Backports.Enum where you can add a chunk_every function and delegate it based on the user's Elixir version (hint: chunk_every was introduced in Elixir 1.5).

format_issue(
issue_meta,
message: "The alias `#{trigger}` is not alphabetically ordered among its group.",
trigger: trigger,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trigger has to be the substring which actually causes the issue in the given line.

Third party tools, like editor integrations, use this to highlight the portion of the source code that is causing the issue.

In your implementation, you expand all the multi-alias members to check their order, which is fine. But you can not use the expanded version for the trigger field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback, the code is now much cleaner! There鈥檚 only this comment left and I want to make sure I get the specs right before I start implementing it 馃槃


defmodule Text do
  alias Module2.Module20
  alias Module1.Module10
end

The trigger should be "Module1.Module10".


defmodule Text do
  alias Module1.Module10
  alias Module1.Module09.Module009
end

The trigger should be "Module09.Module009".


defmodule Text do
  alias Module1.Module10
  alias Module2.{Module20, Module19}
end

The trigger should be "Module19".


defmodule Text do
  alias Module1.Module10.Module100
  alias Module2.Module20.{Module202, Module201, Module203}
  alias Module3.Module30.Module300
end

The trigger should be "Module201".


Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rrrene I modified my implementation to fix trigger. We now refrain from converting AST modules into strings except when we really need them.

I also fixed a bug where line_no was not computed properly when multi-alias expansion was used on multiple lines.

Let me know if you鈥檇 like me to fix something else!

end

defp find_alias_groups(
{:alias, _, [{:__aliases__, [line: line, column: _], mod_list}]} = ast,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not match on [line: line, column: _] like this, because it is a keyword list and it will only match if both and only both values are present.

If, at some point in the future, we get another field in the metadata of the AST, our code would be incompatible between Elixir versions (this actually happened in the past when column: was introduced).

),
do: process_single_alias(ast, line, mod_list, aliases)

defp find_alias_groups({:alias, _, [{:__aliases__, [line: line], mod_list}]} = ast, aliases),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: metadata match.

defp find_alias_groups(
{:alias, _,
[
{{:., _, [{:__aliases__, [line: line, column: _], mod_list}, :{}]}, _, multi_mod_list}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: metadata match.

defp find_alias_groups(
{:alias, _,
[
{{:., _, [{:__aliases__, [line: line], mod_list}, :{}]}, _, multi_mod_list}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: metadata match.

end

defp find_alias_groups(
{:alias, _, [{:__aliases__, [line: line, column: _], mod_list}]} = ast,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not match on [line: line, column: _] like this, because it is a keyword list and it will only match if both and only both values are present.

If, at some point in the future, we get another field in the metadata of the AST, our code would be incompatible between Elixir versions (this actually happened in the past when column: was introduced).

That's why we have do this by hand: Example from the AliasUsage Check

@remi remi force-pushed the feature/alias-order-check branch 5 times, most recently from 4c9c7ad to c46ed4d Compare June 3, 2018 23:44
@remi remi force-pushed the feature/alias-order-check branch from c46ed4d to c53aed7 Compare June 3, 2018 23:47
@rrrene rrrene merged commit 9be44fb into rrrene:master Jun 4, 2018
@rrrene
Copy link
Owner

rrrene commented Jun 4, 2018

Thanks @remiprev 馃憤

@remi remi deleted the feature/alias-order-check branch June 4, 2018 20:37
@remi
Copy link
Contributor Author

remi commented Jun 4, 2018

My pleasure! Do you need me to create another pull request to add a line in CHANGELOG.md?

@rrrene
Copy link
Owner

rrrene commented Jun 4, 2018

@remiprev No need, I'm already on it ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants