Skip to content

Commit

Permalink
Pipes: fix block extraction of module-qualified macros. Closes #91
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Dec 1, 2023
1 parent 3d924fe commit 0c76518
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

* charlists: rewrite empty charlist to use sigil (`''` => `~c""`)
* pipes: don't blow up extracting fully-qualified macros (`Foo.bar do end |> foo()`, #91, h/t @NikitaNaumenko)

## v0.10.2

Expand Down
9 changes: 8 additions & 1 deletion lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@ defmodule Styler.Style.Pipes do
#
# block_result
# |> ...
variable = {:"#{fun}_result", [line: line], nil}
var_name =
case fun do
fun when is_atom(fun) -> fun
{:., _, [{:__aliases__, _, _}, fun]} when is_atom(fun) -> fun
_ -> "block"
end

variable = {:"#{var_name}_result", [line: line], nil}
new_assignment = {:=, [line: line], [variable, lhs]}
{variable, new_assignment}
else
Expand Down
25 changes: 25 additions & 0 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ defmodule Styler.Style.PipesTest do
)
end

test "block extraction: names aliased modules" do

Check failure on line 128 in test/style/pipes_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.14.2/OTP25.1.2

test block pipe starts block extraction: names aliased modules (Styler.Style.PipesTest)

Check failure on line 128 in test/style/pipes_test.exs

View workflow job for this annotation

GitHub Actions / Ex1.15.7/OTP25.1.2

test block pipe starts block extraction: names aliased modules (Styler.Style.PipesTest)
assert_style(
"""
Foo.bar do
:ok
end
|> case do
:ok -> :ok
_ -> :error
end
""",
"""
bar_result =
Foo.bar do
:ok
end
case tx_result do
:ok -> :ok
_ -> :error
end
"""
)
end

test "macro with arg and do block" do
assert_style("""
"baz"
Expand Down

0 comments on commit 0c76518

Please sign in to comment.