Skip to content

Commit 0332007

Browse files
authored
Expand {root} macros (#4145)
See #3842.
1 parent 58d0d6d commit 0332007

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/phoenix_live_view/tag_engine/compiler.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,11 @@ defmodule Phoenix.LiveView.TagEngine.Compiler do
527527
defp handle_tag_attrs(meta, attrs, substate, state) do
528528
Enum.reduce(attrs, substate, fn
529529
{:root, {:expr, _, _} = expr, _attr_meta}, substate ->
530-
ast = parse_expr!(expr, state.file)
530+
ast =
531+
case parse_expr!(expr, state.file) do
532+
{:@, _, _} = ast -> ast
533+
ast -> expand_with_line(ast, meta[:line], state.caller)
534+
end
531535

532536
# If we have a map of literal keys, we unpack it as a list
533537
# to simplify the downstream check.

test/phoenix_live_view/html_engine_test.exs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ defmodule Phoenix.LiveView.HTMLEngineTest do
4545
end
4646
end
4747

48+
defmacro test_attr_macro(a) do
49+
case a do
50+
:base -> quote do: [{"style", "display: flex;"}, {"other", "foo"}, {"another", @bar}]
51+
_ -> quote do: a
52+
end
53+
end
54+
4855
def assigns_component(assigns) do
4956
~H"{inspect(Map.delete(assigns, :__changed__))}"
5057
end
@@ -217,6 +224,15 @@ defmodule Phoenix.LiveView.HTMLEngineTest do
217224

218225
assert %Phoenix.LiveView.Rendered{static: ["<div", " d2=\"2\"", "></div>"]} =
219226
eval(template, assigns)
227+
228+
# macro is expanded
229+
template = ~S|<div {test_attr_macro(:base)} />|
230+
231+
assert %Phoenix.LiveView.Rendered{static: ["<div style=\"", "\" other=\"foo\"", "></div>"]} =
232+
eval(template, %{bar: "baz"})
233+
234+
assert render(template, %{bar: "baz"}) ==
235+
~S|<div style="display: flex;" other="foo" another="baz"></div>|
220236
end
221237

222238
test "optimizes attributes with literal string values" do

0 commit comments

Comments
 (0)