Skip to content

Commit

Permalink
initial spike
Browse files Browse the repository at this point in the history
  • Loading branch information
novaugust committed Nov 7, 2024
1 parent fe32a84 commit 8063005
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/style.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ defmodule Styler.Style do
end
end

def do_block?([{{:__block__, _, [:do]}, _body} | _]), do: true
def do_block?(_), do: false

@doc """
Returns a zipper focused on the nearest node where additional nodes can be inserted (a "block").
Expand Down
3 changes: 1 addition & 2 deletions lib/style/blocks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@ defmodule Styler.Style.Blocks do
def run({{:with, with_meta, children}, _} = zipper, ctx) when is_list(children) do
# a std lib `with` block will have at least one left arrow and a `do` body. anything else we skip ¯\_(ツ)_/¯
arrow_or_match? = &(left_arrow?(&1) || match?({:=, _, _}, &1))
do_block? = &match?([{{:__block__, _, [:do]}, _body} | _], &1)

if Enum.any?(children, arrow_or_match?) and Enum.any?(children, do_block?) do
if Enum.any?(children, arrow_or_match?) and Enum.any?(children, &Style.do_block?/1) do
{preroll, children} =
children
|> Enum.map(fn
Expand Down
10 changes: 10 additions & 0 deletions lib/style/pipes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ defmodule Styler.Style.Pipes do
end
end

# a(b |> c[, ...args])
# maybe pipeify it
def run({{f, m, [{:|>, _, _} = pipe | args]}, _} = zipper, ctx) do
if Enum.any?(args, &Style.do_block?/1) do
{:cont, zipper, ctx}
else
{:cont, Zipper.replace(zipper, {:|>, m, [pipe, {f, m, args}]}), ctx}
end
end

def run(zipper, ctx), do: {:cont, zipper, ctx}

defp fix_pipe_start({pipe, zmeta} = zipper) do
Expand Down
33 changes: 23 additions & 10 deletions test/style/pipes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,8 @@ defmodule Styler.Style.PipesTest do
end
end

describe "comments" do
test "unpiping doesn't move comment in anonymous function" do
describe "comments and..." do
test "unpiping" do
assert_style(
"""
aliased =
Expand Down Expand Up @@ -850,10 +850,9 @@ defmodule Styler.Style.PipesTest do
"""
)
end
end

test "optimizing with comments" do
assert_style(
test "optimizing" do
assert_style(
"""
a
|> Enum.map(fn b ->
Expand All @@ -873,9 +872,9 @@ defmodule Styler.Style.PipesTest do
end)
|> Enum.each(...)
"""
)
)

assert_style(
assert_style(
"""
a
|> Enum.map(fn b ->
Expand All @@ -895,9 +894,9 @@ defmodule Styler.Style.PipesTest do
end)
|> Enum.each(...)
"""
)
)

assert_style(
assert_style(
"""
a
|> Enum.map(fn b ->
Expand All @@ -917,6 +916,20 @@ defmodule Styler.Style.PipesTest do
end)
|> Enum.each(...)
"""
)
)
end
end

describe "pipifying" do
test "no false positives" do
pipe = "a() |> b() |> c()"
assert_style "fn -> #{pipe} end"
assert_style "if #{pipe}, do: ..."
assert_style "x\n\n#{pipe}"
end

test "pipifying" do
assert_style "d(a |> b |> c)", "a |> b() |> c() |> d()"
end
end
end

0 comments on commit 8063005

Please sign in to comment.