Ensure escaping works within struct fields, closes #14817
This commit is contained in:
@@ -38,7 +38,8 @@ expand_struct(Meta, Left, {'%{}', MapMeta, MapArgs}, S, #{context := Context} =
|
||||
Keys = ['__struct__'] ++ AssocKeys,
|
||||
WithoutKeys = lists:sort(maps:to_list(maps:without(Keys, Struct))),
|
||||
StructAssocs = elixir_quote:escape(WithoutKeys, none, false),
|
||||
{{'%', Meta, [ELeft, {'%{}', MapMeta, StructAssocs ++ Assocs}]}, SE, EE};
|
||||
{EStructAssocs, SA, EA} = elixir_expand:expand(StructAssocs, SE, EE),
|
||||
{{'%', Meta, [ELeft, {'%{}', MapMeta, EStructAssocs ++ Assocs}]}, SA, EA};
|
||||
|
||||
{'%{}', MapMeta, Assocs} ->
|
||||
_ = load_struct_info(Meta, ELeft, Assocs, EE),
|
||||
|
||||
@@ -176,6 +176,17 @@ defmodule MacroTest do
|
||||
} = Macro.escape(~r/foo/)
|
||||
end
|
||||
|
||||
@tag :re_import
|
||||
test "escape works within structs fields" do
|
||||
defmodule Test do
|
||||
# Structs have their own escape logic, which now needs to be expanded
|
||||
defstruct my_regex: ~r/^hi$/
|
||||
def init, do: %__MODULE__{}
|
||||
end
|
||||
|
||||
assert %Regex{} = Test.init().my_regex
|
||||
end
|
||||
|
||||
defmodule EscapedStruct do
|
||||
defstruct [:ast, :ref]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user