Skip to content

Commit

Permalink
alias: fix single-module alias deletion newlines bug
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Dec 5, 2023
1 parent 09fa479 commit a8e99a8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## main

## v0.10.5

### Fixes

* alias: fix single-module alias deletion newlines bug

## v0.10.4

### Improvements
Expand Down
6 changes: 4 additions & 2 deletions lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ defmodule Styler.Style.ModuleDirectives do
imports = expand_and_sort(directives[:import] || [])
aliases = expand_and_sort(directives[:alias] || [])
requires = expand_and_sort(directives[:require] || [])
# Delete root level aliases ala `alias Foo` -> ``
filtered_aliases = Enum.reject(aliases, &match?({:alias, _, [{:__aliases__, _, [_]}]}, &1))

directives =
[
Expand All @@ -163,14 +165,14 @@ defmodule Styler.Style.ModuleDirectives do
behaviours,
uses,
imports,
aliases,
filtered_aliases,
requires
]
|> Enum.concat()
|> fix_line_numbers(List.first(nondirectives))

cond do
Enum.empty?(directives) ->
Enum.empty?(directives) and Enum.empty?(aliases) ->
parent

Enum.empty?(nondirectives) ->
Expand Down
4 changes: 0 additions & 4 deletions lib/style/single_node.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ defmodule Styler.Style.SingleNode do
alias Styler.Style
alias Styler.Zipper

# Delete root level alias
# `alias Foo` -> ``
def run({{:alias, _, [{:__aliases__, _, [_]}]}, _} = zipper, ctx), do: {:cont, Zipper.remove(zipper), ctx}

def run({node, meta}, ctx), do: {:cont, {style(node), meta}, ctx}

# as of 1.15, elixir's formatter takes care of this for us.
Expand Down
21 changes: 21 additions & 0 deletions test/style/module_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -415,4 +415,25 @@ defmodule Styler.Style.ModuleDirectivesTest do
)
end
end

test "Deletes root level alias" do
assert_style("alias Foo", "")

assert_style(
"""
alias unquote(Foo)
alias Foo
alias Bar, as: Bop
alias __MODULE__
require Foo
""",
"""
alias __MODULE__
alias Bar, as: Bop
alias unquote(Foo)
require Foo
"""
)
end
end
18 changes: 0 additions & 18 deletions test/style/single_node_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -293,22 +293,4 @@ defmodule Styler.Style.SingleNodeTest do
assert_style("Enum.reverse(foo, bar) ++ bar")
end
end

test "Delete root level alias" do
assert_style("alias Foo", "")

assert_style(
"""
alias unquote(Foo)
alias Foo
alias Bar, as: Bop
alias __MODULE__
""",
"""
alias __MODULE__
alias Bar, as: Bop
alias unquote(Foo)
"""
)
end
end

0 comments on commit a8e99a8

Please sign in to comment.