Skip to content

Commit

Permalink
Sort map update syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
emkguts committed Feb 28, 2025
1 parent 90120be commit 19e26a3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
5 changes: 5 additions & 0 deletions lib/style/comment_directives.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ defmodule Quokka.Style.CommentDirectives do
{{:defstruct, meta, [list]}, comments}
end

defp sort({:%{}, meta, [{:|, _, [var, keyword_list]}]}, comments) do
{{:__block__, meta, [keyword_list]}, comments} = sort({:__block__, meta, [keyword_list]}, comments)
{{:%{}, meta, [{:|, meta, [var, keyword_list]}]}, comments}
end

defp sort({:%{}, meta, list}, comments) when is_list(list) do
{{:__block__, meta, [list]}, comments} = sort({:__block__, meta, [list]}, comments)
{{:%{}, meta, list}, comments}
Expand Down
49 changes: 32 additions & 17 deletions test/style/comment_directives_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ defmodule Quokka.Style.CommentDirectivesTest do
use Quokka.StyleCase, async: true

describe "sort" do
test "test" do
Mimic.stub(Quokka.Config, :sort_all_maps?, fn -> true end)

assert_style(
"""
%{var | new_elem: val, another_elem: other_val}
""",
"""
%{var | another_elem: other_val, new_elem: val}
"""
)
end

test "sorts all maps if the config is set" do
Mimic.stub(Quokka.Config, :sort_all_maps?, fn -> true end)

Expand Down Expand Up @@ -51,24 +64,26 @@ defmodule Quokka.Style.CommentDirectivesTest do
test "sorts maps when config is set, map has comment, and there is a sort directive" do
Mimic.stub(Quokka.Config, :sort_all_maps?, fn -> true end)

assert_style("""
# quokka:sort
%{
c: 1,
b: 2,
assert_style(
"""
# quokka:sort
%{
c: 1,
b: 2,
# this needs to come last
a: 3
}
""",
"""
# quokka:sort
# this needs to come last
a: 3
}
""",
"""
# quokka:sort
# this needs to come last
%{
a: 3,
b: 2,
c: 1
}
""")
%{
a: 3,
b: 2,
c: 1
}
"""
)
end

test "we dont just sort by accident" do
Expand Down

0 comments on commit 19e26a3

Please sign in to comment.