Skip to content

Commit

Permalink
Add Sourceror.strip_meta/1 (#158)
Browse files Browse the repository at this point in the history
* Add Sourceror.strip_meta/1

* mix format

---------

Co-authored-by: doorgan <[email protected]>
  • Loading branch information
bcardarella and doorgan committed Jul 23, 2024
1 parent 04ffeeb commit 2a0afcc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/sourceror.ex
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,19 @@ defmodule Sourceror do
|> Enum.join()
end

@doc """
A convenience utility function for stripping all meta data out of
a quoted expression. This is useful when debugging for readability of very large
ASTs.
"""
@spec strip_meta(quoted :: Macro.t()) :: Macro.t()
def strip_meta(quoted) do
Macro.prewalk(quoted, [], fn
{name, _meta, args}, acc -> {{name, {}, args}, acc}
other, acc -> {other, acc}
end)
end

defp do_patch_string(lines, [], seen, _), do: Enum.reverse(lines) ++ seen

defp do_patch_string([], _, seen, _), do: seen
Expand Down
18 changes: 18 additions & 0 deletions test/sourceror_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1226,4 +1226,22 @@ defmodule SourcerorTest do
"""
end
end

describe "strip_meta/1" do
test "will strip all meta data from AST" do
original = ~S"""
hello world do
:ok
end
"""

quoted = Sourceror.parse_string!(original)

assert Sourceror.strip_meta(quoted) == {
{:hello, {},
[{:world, {}, nil}, [{{:__block__, {}, [:do]}, {:__block__, {}, [:ok]}}]]},
[]
}
end
end
end

0 comments on commit 2a0afcc

Please sign in to comment.