From 40fc991d0d6c1daad3d1b76e3c4c196b5c527c86 Mon Sep 17 00:00:00 2001 From: Matthew Gieger Date: Fri, 1 Dec 2023 12:48:08 -0800 Subject: [PATCH] Delete root level alias --- lib/style/single_node.ex | 4 ++++ test/style/single_node_test.exs | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/style/single_node.ex b/lib/style/single_node.ex index ef3f6daf..0c42cb14 100644 --- a/lib/style/single_node.ex +++ b/lib/style/single_node.ex @@ -30,6 +30,10 @@ 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} # Our use of the `literal_encoder` option of `Code.string_to_quoted_with_comments!/2` creates diff --git a/test/style/single_node_test.exs b/test/style/single_node_test.exs index 40cc9a92..401fd77a 100644 --- a/test/style/single_node_test.exs +++ b/test/style/single_node_test.exs @@ -290,4 +290,20 @@ defmodule Styler.Style.SingleNodeTest do assert_style("Enum.reverse(foo, bar) ++ bar") end end + + test "Delete root level alias" do + assert_style( + """ + alias unquote(Foo) + alias Foo + alias Bar, as: Bop + alias __MODULE__ + """, + """ + alias __MODULE__ + alias Bar, as: Bop + alias unquote(Foo) + """ + ) + end end