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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep module callback ordering relative to use directives. #121

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ defmodule Styler.Style.ModuleDirectives do
alias Styler.Zipper

@directives ~w(alias import require use)a
@attr_directives ~w(moduledoc shortdoc behaviour)a
@callback_attrs ~w(before_compile after_compile after_verify)a
@attr_directives ~w(moduledoc shortdoc behaviour)a ++ @callback_attrs

@moduledoc_false {:@, [line: nil], [{:moduledoc, [line: nil], [{:__block__, [line: nil], [false]}]}]}

Expand Down Expand Up @@ -142,6 +143,9 @@ defmodule Styler.Style.ModuleDirectives do

directives =
Enum.group_by(directives, fn
# callbacks are essentially the same as `use` -- they invoke macros.
# so, we need to group / order them with `use` statements to make sure we don't break things!
{:@, _, [{callback, _, _}]} when callback in @callback_attrs -> :use
{:@, _, [{attr_name, _, _}]} -> :"@#{attr_name}"
{directive, _, _} -> directive
end)
Expand Down
11 changes: 11 additions & 0 deletions test/style/module_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ defmodule Styler.Style.ModuleDirectivesTest do
)
end

test "groups module callbacks with uses, keeping ordering" do
assert_style """
defmacro __using__(_) do
quote do
@after_compile Foo
use Bar
end
end
"""
end

test "interwoven directives w/o the context of a module" do
assert_style(
"""
Expand Down