Skip to content

Commit

Permalink
Update lib_elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
doorgan committed Aug 14, 2024
1 parent b7e42b4 commit 6361ddb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
43 changes: 8 additions & 35 deletions lib/sourceror.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ 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: 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 @@ -48,44 +44,15 @@ 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: 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.
Expand Down Expand Up @@ -125,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
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ defmodule Sourceror.MixProject do
{:ex_check, "~> 0.15.0", only: [:dev], runtime: false},
{:ex_doc, ">= 0.0.0", only: :dev, runtime: false},
{:excoveralls, "~> 0.15", only: [:test]},
{:lib_elixir, github: "zachallaun/lib_elixir", runtime: false},
{:lib_elixir,
github: "zachallaun/lib_elixir",
ref: "dda978a0ac1dc48d075aaec97c3639fc16ab5f13",
runtime: false},
{:sobelow, "~> 0.11", only: :dev}
]
end
Expand Down
4 changes: 2 additions & 2 deletions test/sourceror_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ defmodule SourcerorTest do
end

test "raises on invalid string" do
assert_raise Sourceror.LibElixir.SyntaxError, fn ->
assert_raise SyntaxError, fn ->
Sourceror.parse_string!(":ok end")
end

assert_raise Sourceror.LibElixir.TokenMissingError, fn ->
assert_raise TokenMissingError, fn ->
Sourceror.parse_string!("do :ok")
end
end
Expand Down

0 comments on commit 6361ddb

Please sign in to comment.