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

de-alias when sorting module directives. #155

Merged
merged 5 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 1 addition & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,42 +108,7 @@ Once styled the first time, future styling formats shouldn't take noticeably mor

### Troubleshooting: Compilation broke due to Module Directive rearrangement

Sometimes naively moving Module Directives around can break compilation.

Here's helpers on how to manually fix that and have a happy styling for the rest of
your codebase's life.

#### Alias dependency

If you have an alias that, for example, a `@behaviour` relies on, compilation will break after your first run.
This requires one-time manual fixing to get your repo in line with Styler's standards.

For example, if your code was this:
```elixir
defmodule MyModule do
@moduledoc "Implements MyBehaviour!"
alias Deeply.Nested.MyBehaviour
@behaviour MyBehaviour
...
end
```

then Styler will style the file like this, which cannot compile due to `MyBehaviour` not being defined.

```elixir
defmodule MyModule do
@moduledoc "Implements MyBehaviour!"
@behaviour MyBehaviour # <------ compilation error, MyBehaviour is not defined!

alias Deeply.Nested.MyBehaviour

...
end
```

A simple solution is to manually expand the alias with a find-replace-all like:
`@behaviour MyBehaviour` -> `@behaviour Deeply.Nested.MyBehaviour`. It's important to specify that you only want to
find-replace with the `@behaviour` prefix or you'll unintentially expand `MyBehaviour` everywhere in the codebase.
Styler naively moves module attributes, which can break compilation. For now, the only fix is some elbow grease.

#### Module Attribute dependency

Expand Down
44 changes: 43 additions & 1 deletion lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Styler.Style.ModuleDirectives do

**This can break your code.**

### Strict Layout: alias referents
### Strict Layout

Modules directives are sorted into the following order:

Expand Down Expand Up @@ -229,6 +229,7 @@ defmodule Styler.Style.ModuleDirectives do
aliases,
requires
]
|> Stream.map(&dealias(&1, aliases))
|> Enum.concat()
|> Style.fix_line_numbers(List.first(nondirectives))

Expand Down Expand Up @@ -367,6 +368,47 @@ defmodule Styler.Style.ModuleDirectives do
|> Zipper.node()
end

# these are just lazy hacks so i can Enum.map the list of directives while not doing weird stuff to the aliases themselves
defp dealias([], _), do: []
defp dealias([{:alias, _, _} | _] = aliases, _), do: aliases
defp dealias([{:require, _, _} | _] = requires, _), do: requires
defp dealias(directives, []), do: directives

defp dealias(non_aliases, aliases) do
latest_non_aliases = non_aliases |> Stream.map(fn {_, meta, _} -> meta[:line] end) |> Enum.max()
earliest_alias = aliases |> Stream.map(fn {_, meta, _} -> meta[:line] end) |> Enum.min()
novaugust marked this conversation as resolved.
Show resolved Hide resolved

if earliest_alias >= latest_non_aliases do
non_aliases
novaugust marked this conversation as resolved.
Show resolved Hide resolved
else
Enum.map(non_aliases, fn {_, meta, _} = ast ->
line = meta[:line]

if line < earliest_alias do
ast
else
dealiases =
aliases
|> Enum.filter(fn {_, meta, _} -> meta[:line] < line end)
|> Map.new(fn {:alias, _, [{:__aliases__, _, aliases}]} -> {List.last(aliases), aliases} end)

ast
|> Zipper.zip()
|> Zipper.traverse(fn
{{:__aliases__, meta, [first | rest]}, _} = zipper ->
if dealias = dealiases[first],
do: Zipper.replace(zipper, {:__aliases__, meta, dealias ++ rest}),
else: zipper

zipper ->
zipper
end)
|> Zipper.node()
end
end)
end
end

defp expand_and_sort(directives) do
# sorting is done with `downcase` to match Credo
directives
Expand Down
35 changes: 18 additions & 17 deletions test/style/module_directives/alias_lifting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,24 @@ defmodule Styler.Style.ModuleDirectives.AliasLiftingTest do

test "lifts in modules with only-child bodies" do
assert_style """
defmodule A do
def lift_me() do
A.B.C.foo()
A.B.C.baz()
end
end
""","""
defmodule A do
@moduledoc false
alias A.B.C

def lift_me do
C.foo()
C.baz()
end
end
"""
defmodule A do
def lift_me() do
A.B.C.foo()
A.B.C.baz()
end
end
""",
"""
defmodule A do
@moduledoc false
alias A.B.C

def lift_me do
C.foo()
C.baz()
end
end
"""
end

describe "comments stay put" do
Expand Down
34 changes: 32 additions & 2 deletions test/style/module_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ defmodule Styler.Style.ModuleDirectivesTest do
@behaviour Lawful

use B
use A
use A.A

import A
import A.A
import C

alias A.A
Expand Down Expand Up @@ -548,4 +548,34 @@ defmodule Styler.Style.ModuleDirectivesTest do

assert_style "@derive Inspect"
end

test "de-aliases use/behaviour/import/moduledoc" do
assert_style """
defmodule MyModule do
alias A.B.C
@moduledoc "Implements \#{C.foo()}!"
alias D.F.C
import C
alias G.H.C
@behaviour C
alias Z.X.C
use SomeMacro, with: C
end
""",
"""
defmodule MyModule do
@moduledoc "Implements \#{A.B.C.foo()}!"
@behaviour G.H.C

use SomeMacro, with: Z.X.C

import D.F.C

alias A.B.C
alias D.F.C
alias G.H.C
alias Z.X.C
end
"""
end
end