Skip to content

Commit

Permalink
ignore no-paren, no-body function heads. closes #185
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Aug 7, 2024
1 parent 95fba17 commit 3769ffb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ they can and will change without that change being reflected in Styler's semanti

### Fixes

* don't blow up on `def function_head_with_no_body_nor_parens` (#185, h/t @ypconstante)
* fix `with` arrow replacement + redundant body removal creating invalid statements (#184, h/t @JesseHerrick)
* allow Kernel unary `!` and `not` as valid pipe starts (#183, h/t @nherzing)

Expand Down
19 changes: 13 additions & 6 deletions lib/style/defs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,20 @@ defmodule Styler.Style.Defs do
first_line = meta[:line]
last_line = head_meta[:closing][:line]

if first_line == last_line do
cond do
# weird `def fun`, nothing else
is_nil(last_line) ->
{:skip, zipper, ctx}

# Already collapsed
{:skip, zipper, ctx}
else
comments = Style.displace_comments(ctx.comments, first_line..last_line)
node = {def, meta, [Style.set_line(head, meta[:line])]}
{:skip, Zipper.replace(zipper, node), %{ctx | comments: comments}}
first_line == last_line ->
{:skip, zipper, ctx}

# I just felt like this clause deserved a comment too. It's my favorite one
true ->
comments = Style.displace_comments(ctx.comments, first_line..last_line)
node = {def, meta, [Style.set_line(head, first_line)]}
{:skip, Zipper.replace(zipper, node), %{ctx | comments: comments}}
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/style/defs_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ defmodule Styler.Style.DefsTest do
end

test "no body" do
assert_style "def no_body_nor_parens_yikes!"

assert_style(
"""
# Top comment
Expand Down

0 comments on commit 3769ffb

Please sign in to comment.