Skip to content

Commit

Permalink
style:sort - dont dedupe
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 23, 2024
1 parent b13d045 commit 0e8485b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
11 changes: 0 additions & 11 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ defmodule Styler.Style do
@doc "prewalks ast and sets all meta to `nil`. useful for comparing AST without meta (line numbers, etc) interfering"
def without_meta(ast), do: update_all_meta(ast, fn _ -> nil end)

@doc "sorts a list of nodes according to their string representations"
def sort(ast, opts \\ []) when is_list(ast) do
format = if opts[:format] == :downcase, do: &String.downcase/1, else: & &1

ast
|> Enum.map(&{&1, &1 |> Macro.to_string() |> format.()})
|> Enum.uniq_by(&elem(&1, 1))
|> List.keysort(1)
|> Enum.map(&elem(&1, 0))
end

@doc """
Returns the current node (wrapped in a `__block__` if necessary) if it's a valid place to insert additional nodes
"""
Expand Down
4 changes: 2 additions & 2 deletions lib/style/comment_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ defmodule Styler.Style.CommentDirectives do

@behaviour Styler.Style

alias Styler.Style
alias Styler.Zipper

def run(zipper, ctx) do
Expand All @@ -39,7 +38,8 @@ defmodule Styler.Style.CommentDirectives do
{:halt, zipper, ctx}
end

defp sort({:__block__, meta, [list]}) when is_list(list), do: {:__block__, meta, [Style.sort(list)]}
defp sort({:__block__, meta, [list]}) when is_list(list), do: {:__block__, meta, [sort(list)]}
defp sort(list) when is_list(list), do: Enum.sort_by(list, &Macro.to_string/1)

defp sort({:sigil_w, sm, [{:<<>>, bm, [string]}, modifiers]}) do
# ew. gotta be a better way.
Expand Down
5 changes: 4 additions & 1 deletion lib/style/module_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,10 @@ defmodule Styler.Style.ModuleDirectives do
defp sort(directives) do
# sorting is done with `downcase` to match Credo
directives
|> Style.sort(format: :downcase)
|> Enum.map(&{&1, &1 |> Macro.to_string() |> String.downcase()})
|> Enum.uniq_by(&elem(&1, 1))
|> List.keysort(1)
|> Enum.map(&elem(&1, 0))
|> Style.reset_newlines()
end
end
2 changes: 2 additions & 0 deletions test/style/comment_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Styler.Style.CommentDirectivesTest do
[
:c,
:b,
:c,
:a
]
""",
Expand All @@ -32,6 +33,7 @@ defmodule Styler.Style.CommentDirectivesTest do
[
:a,
:b,
:c,
:c
]
"""
Expand Down

0 comments on commit 0e8485b

Please sign in to comment.