From 23cee9d25457e41da771fa03f07996cd2883854e Mon Sep 17 00:00:00 2001 From: Jesse Herrick Date: Fri, 6 Jun 2025 20:29:18 -0400 Subject: [PATCH 1/2] Keep if-s with else nil (explicit is better) and format the library --- README.md | 1 - lib/style/blocks.ex | 8 ++++---- lib/style/comment_directives.ex | 4 ++-- lib/zipper.ex | 2 +- test/style/blocks_test.exs | 6 ++++-- test/style/comment_directives_test.exs | 1 + test/zipper_test.exs | 2 +- 7 files changed, 13 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index a9c2ec7..78a1ed8 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,6 @@ Other ways Styler can change your program: - [config file sorting](https://hexdocs.pm/styler/mix_configs.html#this-can-break-your-program) - and likely other ways. stay safe out there! ->>>>>>> upstream/main ## Thanks & Inspiration ### [Sourceror](https://github.com/doorgan/sourceror/) diff --git a/lib/style/blocks.ex b/lib/style/blocks.ex index 9c308ba..b6dd297 100644 --- a/lib/style/blocks.ex +++ b/lib/style/blocks.ex @@ -25,11 +25,11 @@ defmodule Styler.Style.Blocks do * Credo.Check.Refactor.WithClauses """ - @behaviour Styler.Style - alias Styler.Style alias Styler.Zipper + @behaviour Styler.Style + defguardp is_negator(n) when elem(n, 0) in [:!, :not, :!=, :!==] # case statement with exactly 2 `->` cases @@ -144,8 +144,8 @@ defmodule Styler.Style.Blocks do {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx} # drop `else: nil` - [head, [do_block, {_, {:__block__, _, [nil]}}]] -> - {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx} + # [head, [do_block, {_, {:__block__, _, [nil]}}]] -> + # {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx} [head, [do_, else_]] -> if Style.max_line(do_) > Style.max_line(else_) do diff --git a/lib/style/comment_directives.ex b/lib/style/comment_directives.ex index 95edec5..c05aa0b 100644 --- a/lib/style/comment_directives.ex +++ b/lib/style/comment_directives.ex @@ -15,11 +15,11 @@ defmodule Styler.Style.CommentDirectives do `# styler:sort` maintains sorting of wordlists (by string comparison) and lists (string comparison of code representation) """ - @behaviour Styler.Style - alias Styler.Style alias Styler.Zipper + @behaviour Styler.Style + def run(zipper, ctx) do {zipper, comments} = ctx.comments diff --git a/lib/zipper.ex b/lib/zipper.ex index e0b88d0..11288ad 100644 --- a/lib/zipper.ex +++ b/lib/zipper.ex @@ -178,7 +178,7 @@ defmodule Styler.Zipper do """ @spec prepend_siblings(zipper, [tree]) :: zipper def prepend_siblings({node, nil}, siblings), do: {:__block__, [], siblings ++ [node]} |> zip() |> down() |> rightmost() - def prepend_siblings({tree, {l, p, r}}, siblings), do: {tree, {Enum.reverse(siblings, l), p , r}} + def prepend_siblings({tree, {l, p, r}}, siblings), do: {tree, {Enum.reverse(siblings, l), p, r}} @doc """ Inserts the item as the right sibling of the node at this zipper, without diff --git a/test/style/blocks_test.exs b/test/style/blocks_test.exs index a4e97c4..8f6d078 100644 --- a/test/style/blocks_test.exs +++ b/test/style/blocks_test.exs @@ -734,12 +734,14 @@ defmodule Styler.Style.BlocksTest do end describe "if" do - test "drops else nil" do - assert_style("if a, do: b, else: nil", "if a, do: b") + test "keeps else nil" do + assert_style("if a, do: b, else: nil", "if a, do: b, else: nil") assert_style("if a do b else nil end", """ if a do b + else + nil end """) diff --git a/test/style/comment_directives_test.exs b/test/style/comment_directives_test.exs index 68b6f4f..c849e82 100644 --- a/test/style/comment_directives_test.exs +++ b/test/style/comment_directives_test.exs @@ -10,6 +10,7 @@ defmodule Styler.Style.CommentDirectivesTest do @moduledoc false + use Styler.StyleCase, async: true describe "sort" do diff --git a/test/zipper_test.exs b/test/zipper_test.exs index 81dcf6e..30d80d5 100644 --- a/test/zipper_test.exs +++ b/test/zipper_test.exs @@ -70,7 +70,7 @@ defmodule StylerTest.ZipperTest do {1, {[], {{:foo, [], [1, 2]}, nil}, [2]}} assert {{:., [], [:a, :b]}, [], [1, 2]} |> Zipper.zip() |> Zipper.down() == - {{:., [], [:a, :b]}, {[],{{{:., [], [:a, :b]}, [], [1, 2]}, nil}, [1, 2]}} + {{:., [], [:a, :b]}, {[], {{{:., [], [:a, :b]}, [], [1, 2]}, nil}, [1, 2]}} end end From 896746ae4d5b69830ec35fa956924fb6f9ad2246 Mon Sep 17 00:00:00 2001 From: Jesse Herrick Date: Fri, 6 Jun 2025 20:36:17 -0400 Subject: [PATCH 2/2] Add a comment about why we're keeping else: nil --- lib/style/blocks.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/style/blocks.ex b/lib/style/blocks.ex index b6dd297..ff95165 100644 --- a/lib/style/blocks.ex +++ b/lib/style/blocks.ex @@ -143,6 +143,8 @@ defmodule Styler.Style.Blocks do [head, [do_block, {_, {:__block__, _, []}}]] -> {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx} + # We like keeping `else: nil` even if it's the same effect due to the explicitness. + # # drop `else: nil` # [head, [do_block, {_, {:__block__, _, [nil]}}]] -> # {:cont, Zipper.replace(zipper, {:if, m, [head, [do_block]]}), ctx}