From cb6aab033185b8d266b6568606712d6d79624868 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Thu, 23 May 2024 11:45:30 -0400 Subject: [PATCH 1/2] Map start position --- test/sourceror_test.exs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/sourceror_test.exs b/test/sourceror_test.exs index 78dce15..aecee6c 100644 --- a/test/sourceror_test.exs +++ b/test/sourceror_test.exs @@ -532,6 +532,9 @@ defmodule SourcerorTest do quoted = Sourceror.parse_string!("foo(:bar)") assert Sourceror.get_start_position(quoted) == [line: 1, column: 1] + + quoted = Sourceror.parse_string!("%{a: 1}") + assert Sourceror.get_start_position(quoted) == [line: 1, column: 1] end end From cdc12e3f9abb1bcef2f09b599b196a2174293c53 Mon Sep 17 00:00:00 2001 From: Brian Cardarella Date: Thu, 23 May 2024 19:43:43 -0400 Subject: [PATCH 2/2] Fix for map literal --- lib/sourceror.ex | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/lib/sourceror.ex b/lib/sourceror.ex index a14f77d..8421faa 100644 --- a/lib/sourceror.ex +++ b/lib/sourceror.ex @@ -45,8 +45,16 @@ defmodule Sourceror do with pre 1.13 Elixir versions. """ defmacro string_to_quoted!(string, opts) do - quote bind_quoted: [code_module: @code_module, string: string, opts: opts] do + map_literal_fix? = Version.match?(System.version(), "< 1.17.0") + + quote bind_quoted: [ + code_module: @code_module, + string: string, + opts: opts, + map_literal_fix?: map_literal_fix? + ] do code_module.string_to_quoted_with_comments!(string, opts) + |> Sourceror.map_literal_fix(map_literal_fix?) end end @@ -55,11 +63,44 @@ defmodule Sourceror do with pre 1.13 Elixir versions. """ defmacro string_to_quoted(string, opts) do - quote bind_quoted: [code_module: @code_module, string: string, opts: opts] do + map_literal_fix? = Version.match?(System.version(), "< 1.17.0") + + quote bind_quoted: [ + code_module: @code_module, + string: string, + opts: opts, + map_literal_fix?: map_literal_fix? + ] 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.