Skip to content

Commit

Permalink
1.17+
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Feb 20, 2025
1 parent 7c879af commit db5ba14
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ they can and will change without that change being reflected in Styler's semanti

#### Ex1.17+

Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`
- Replace `:timer.units(x)` with the new `to_timeout(unit: x)` for `hours|minutes|seconds`
- Handle `, else: (_ -> x)` bugs introduced by `(_ -> x)` being termed a literal (#219, h/t @iamhassangm)

#### Ex1.18+

Expand Down Expand Up @@ -48,6 +49,7 @@ This release taught Styler to try just that little bit harder when doing alias l
### Fixes

- `pipes`: handle pipifying when the first arg is itself a pipe: `c(a |> b, d)` => `a |> b() |> c(d)` (#214, h/t @kybishop)
- `with`: correctly handle a stabby `with` `, else: (_ -> :ok)` being rewritten to a case (#219, h/t @iamhassangm)

## 1.3.3

Expand Down
24 changes: 21 additions & 3 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,28 @@ defmodule Styler.Style.Blocks do
def run({{:with, m, [{:<-, am, [success, single_statement]}, [body, elses]]}, zm}, ctx) do
{{:__block__, do_meta, [:do]}, body} = body
{{:__block__, _, [:else]}, elses} = elses

elses =
case elses do
# unwrap a stab ala `, else: (_ -> :ok)`. these became literals in 1.17
{:__block__, _, [[{:->, _, _}] = stab]} -> stab
elses -> elses
end

# drops keyword formatting etc
do_meta = [line: do_meta[:line]]
clauses = [{{:__block__, am, [:do]}, [{:->, do_meta, [[success], body]} | elses]}]
# fun fact: i added the do/end meta just because i noticed it was missing while debugging something
# and it fixed the bug 🤷
case_meta = [do: [line: do_meta], end: [line: Style.max_line(elses)], line: m[:line]]
end_line = Style.max_line(elses) + 1

# fun fact: i added the detailed meta just because i noticed it was missing while debugging something ...
# ... and it fixed the bug 🤷
case_meta = [
end_of_expression: [newlines: 1, line: end_line],
do: do_meta,
end: [line: end_line],
line: m[:line]
]

# recurse in case this new case should be rewritten to a `if`, etc
run({{:case, case_meta, [single_statement, clauses]}, zm}, ctx)
end
Expand Down

0 comments on commit db5ba14

Please sign in to comment.