Skip to content

Commit 90cd4ab

Browse files
committed
Extract shared pipe injection helper
1 parent 9a7fe8b commit 90cd4ab

3 files changed

Lines changed: 22 additions & 28 deletions

File tree

lib/ex_dna/ast/normalizer.ex

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ defmodule ExDNA.AST.Normalizer do
3434
and library guards like `Integer.is_even/1`.
3535
"""
3636

37+
alias ExDNA.AST.Pipe
38+
3739
@type option :: {:literal_mode, :keep | :abstract} | {:normalize_pipes, boolean()}
3840

3941
@doc """
@@ -100,7 +102,7 @@ defmodule ExDNA.AST.Normalizer do
100102
defp fused_walk({:|>, _meta, [left, right]}, true = pipes, env) do
101103
{left_n, env} = fused_walk(left, pipes, env)
102104
{right_n, env} = fused_walk(right, pipes, env)
103-
{inject_first_arg(right_n, left_n), env}
105+
{Pipe.inject_first_arg(right_n, left_n), env}
104106
end
105107

106108
# Variable node
@@ -156,20 +158,6 @@ defmodule ExDNA.AST.Normalizer do
156158
{Enum.reverse(reversed), env}
157159
end
158160

159-
# --- Pipe injection (from PipeNormalizer, inlined for the fused walk) ---
160-
161-
defp inject_first_arg({call, meta, args}, first_arg) when is_list(args) do
162-
{call, meta, [first_arg | args]}
163-
end
164-
165-
defp inject_first_arg({call, meta, nil}, first_arg) do
166-
{call, meta, [first_arg]}
167-
end
168-
169-
defp inject_first_arg(other, first_arg) do
170-
{other, [], [first_arg]}
171-
end
172-
173161
# --- Sigil expansion ---
174162

175163
defp expand_sigil_w(content, modifier) do

lib/ex_dna/ast/pipe.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
defmodule ExDNA.AST.Pipe do
2+
@moduledoc false
3+
4+
@spec inject_first_arg(Macro.t(), Macro.t()) :: Macro.t()
5+
def inject_first_arg({call, meta, args}, first_arg) when is_list(args) do
6+
{call, meta, [first_arg | args]}
7+
end
8+
9+
def inject_first_arg({call, meta, nil}, first_arg) do
10+
{call, meta, [first_arg]}
11+
end
12+
13+
def inject_first_arg(other, first_arg) do
14+
{other, [], [first_arg]}
15+
end
16+
end

lib/ex_dna/ast/pipe_normalizer.ex

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ defmodule ExDNA.AST.PipeNormalizer do
88
don't prevent clone detection.
99
"""
1010

11+
alias ExDNA.AST.Pipe
12+
1113
@doc """
1214
Convert all pipe expressions in an AST to nested function calls.
1315
"""
@@ -17,20 +19,8 @@ defmodule ExDNA.AST.PipeNormalizer do
1719
end
1820

1921
defp flatten_pipe({:|>, _meta, [left, right]}) do
20-
inject_first_arg(right, left)
22+
Pipe.inject_first_arg(right, left)
2123
end
2224

2325
defp flatten_pipe(other), do: other
24-
25-
defp inject_first_arg({call, meta, args}, first_arg) when is_list(args) do
26-
{call, meta, [first_arg | args]}
27-
end
28-
29-
defp inject_first_arg({call, meta, nil}, first_arg) do
30-
{call, meta, [first_arg]}
31-
end
32-
33-
defp inject_first_arg(other, first_arg) do
34-
{other, [], [first_arg]}
35-
end
3626
end

0 commit comments

Comments
 (0)