Skip to content

Commit 78f35eb

Browse files
committed
Fix rewrite of single-clause case statement with assignment parent. Closes #247
1 parent 81bb7ef commit 78f35eb

4 files changed

Lines changed: 63 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ they can and will change without that change being reflected in Styler's semanti
55

66
## main
77

8+
## 1.9.1
9+
10+
### Fix
11+
12+
- fixes rewrites of single-clause case statement with assignment parent (Closes #247, h/t @vasspilka)
13+
814
## 1.9.0
915

1016
This was a weird one, but I found myself often writing `to_timeout` with plural units and then having to go back and fix

lib/style/blocks.ex

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,25 @@ defmodule Styler.Style.Blocks do
5757

5858
zipper =
5959
case Zipper.up(zipper) do
60-
{{:=, am, [parent_lhs, _single_clause_case]}, _} = zipper ->
61-
# this was a `x = case head, do: (lhs -> rhs)`. make it `x = lhs = head; rhs`
60+
{{:=, am, [parent_lhs, _case_statement_rhs]}, _} = zipper ->
61+
# this was a `x = case head, do: (lhs -> rhs)`. make it `x = lhs = head; x = rhs`
6262
meta = [line: am[:line]]
63-
Zipper.replace(zipper, {:=, meta, [parent_lhs, {:=, meta, [lhs, head]}]})
63+
# change the if there are multiple clauses in the case, have the final one be the new rhs of parent_lhs
64+
# the meta for the final equality has an incorrect line, but it shouldn't mess with comments so leaving it be
65+
siblings = List.update_at(rhs, -1, &{:=, meta, [parent_lhs, &1]})
66+
67+
zipper
68+
|> Zipper.replace({:=, meta, [lhs, head]})
69+
|> Zipper.insert_siblings(siblings)
6470

6571
_ ->
6672
zipper
6773
|> Style.find_nearest_block()
6874
|> Zipper.replace({:=, [line: m[:line]], [lhs, head]})
75+
|> Zipper.insert_siblings(rhs)
6976
end
7077

71-
{:cont, Zipper.insert_siblings(zipper, rhs), ctx}
78+
{:cont, zipper, ctx}
7279
end
7380

7481
def run({{:cond, _, [[{do_, clauses}]]}, _} = zipper, ctx) do

test/style/blocks_test.exs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,54 @@ defmodule Styler.Style.BlocksTest do
100100
test "when already an assignment" do
101101
assert_style(
102102
"""
103-
m
103+
preroll
104104
105-
x =
106-
case a do
107-
b -> c
105+
assignment =
106+
case head do
107+
lhs -> body
108108
end
109109
""",
110110
"""
111-
m
111+
preroll
112112
113-
x = b = a
114-
c
113+
lhs = head
114+
assignment = body
115+
"""
116+
)
117+
118+
assert_style(
119+
"""
120+
# assignmet
121+
assignment =
122+
# case head
123+
case head do
124+
# lhs
125+
lhs ->
126+
# body
127+
body
128+
# clauses
129+
fn ->
130+
look
131+
im(a)
132+
# big function
133+
big function
134+
end
135+
end
136+
""",
137+
"""
138+
# assignmet
139+
# case head
140+
# lhs
141+
lhs = head
142+
# body
143+
body
144+
# clauses
145+
assignment = fn ->
146+
look
147+
im(a)
148+
# big function
149+
big(function)
150+
end
115151
"""
116152
)
117153
end

test/style/pipes_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ defmodule Styler.Style.PipesTest do
283283
assert_style(
284284
"""
285285
case x do
286-
x -> x
286+
y -> z
287287
end
288288
|> foo()
289289
""",
290290
"""
291-
case_result = x = x
292-
x
291+
y = x
292+
case_result = z
293293
foo(case_result)
294294
"""
295295
)

0 commit comments

Comments
 (0)