Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use lib_elixir instead of vendoring elixir modules manually #162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 18 additions & 46 deletions lib/sourceror.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Sourceror do
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)

alias Sourceror.LibElixir
alias Sourceror.TraversalState

@line_fields ~w[closing do end end_of_expression]a
Expand All @@ -24,27 +25,17 @@ defmodule Sourceror do

@type traversal_function :: (Macro.t(), TraversalState.t() -> {Macro.t(), TraversalState.t()})

@code_module (if Version.match?(System.version(), "~> 1.13") do
Code
else
Sourceror.Code
end)

@doc """
A wrapper around `Code.string_to_quoted_with_comments!/2` for compatibility
with pre 1.13 Elixir versions.
"""
defmacro string_to_quoted!(string, opts) do
map_literal_fix? = Version.match?(System.version(), "< 1.17.0")

quote bind_quoted: [
code_module: @code_module,
code_module: LibElixir.Code,
string: string,
opts: opts,
map_literal_fix?: map_literal_fix?
opts: opts
] do
code_module.string_to_quoted_with_comments!(string, opts)
|> Sourceror.map_literal_fix(map_literal_fix?)
end
end

Expand All @@ -53,50 +44,21 @@ defmodule Sourceror do
with pre 1.13 Elixir versions.
"""
defmacro string_to_quoted(string, opts) do
map_literal_fix? = Version.match?(System.version(), "< 1.17.0")

quote bind_quoted: [
code_module: @code_module,
code_module: LibElixir.Code,
string: string,
opts: opts,
map_literal_fix?: map_literal_fix?
opts: opts
] do
code_module.string_to_quoted_with_comments(string, opts)
|> Sourceror.map_literal_fix(map_literal_fix?)
end
end

@doc false
def map_literal_fix(result, false),
do: result

def map_literal_fix({:error, reason}, _),
do: {:error, reason}

def map_literal_fix({:ok, quoted, comments}, true) do
{quoted, comments} = map_literal_fix({quoted, comments}, true)
{:ok, quoted, comments}
end

def map_literal_fix({quoted, comments}, true) do
quoted =
Macro.postwalk(quoted, fn
{:%{}, meta, args} ->
{:%{}, Keyword.replace(meta, :column, meta[:column] - 1), args}

quoted ->
quoted
end)

{quoted, comments}
end

@doc """
A wrapper around `Code.quoted_to_algebra/2` for compatibility with pre 1.13
Elixir versions.
"""
defmacro quoted_to_algebra(quoted, opts) do
quote bind_quoted: [code_module: @code_module, quoted: quoted, opts: opts] do
quote bind_quoted: [code_module: LibElixir.Code, quoted: quoted, opts: opts] do
if opts |> Keyword.get(:quoted_to_algebra) |> is_function(2) do
opts[:quoted_to_algebra].(quoted, opts)
else
Expand Down Expand Up @@ -130,6 +92,12 @@ defmodule Sourceror do
def parse_string!(source) do
{quoted, comments} = string_to_quoted!(source, to_quoted_opts())
Sourceror.Comments.merge_comments(quoted, comments)
rescue
error in Sourceror.LibElixir.SyntaxError ->
reraise Map.put(error, :__struct__, SyntaxError), __STACKTRACE__

error in Sourceror.LibElixir.TokenMissingError ->
reraise Map.put(error, :__struct__, TokenMissingError), __STACKTRACE__
end

defp to_quoted_opts do
Expand Down Expand Up @@ -539,7 +507,11 @@ defmodule Sourceror do
end

def get_start_position({_, meta, _}, default) do
position = Keyword.take(meta, [:line, :column])
line = Keyword.get(meta, :line, default[:line])
column = Keyword.get(meta, :column, default[:column])

position = [line: line, column: column]

Keyword.merge(default, position)
end

Expand All @@ -565,7 +537,7 @@ defmodule Sourceror do
...> end
...> "\"" |> Sourceror.parse_string!()
iex> Sourceror.get_end_position(quoted)
[line: 3, column: 1]
[line: 3, column: 3]

iex> quoted = ~S"\""
...> foo(
Expand Down
2 changes: 1 addition & 1 deletion lib/sourceror/identifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ defmodule Sourceror.Identifier do
iex> is_atomic_literal({:__block__, [], [{:node, [], nil}]})
false

iex> is_atomic_literal('foo')
iex> is_atomic_literal(~c'foo')
false
"""
@spec is_atomic_literal(Macro.t()) :: Macro.t()
Expand Down
Loading
Loading