-
Notifications
You must be signed in to change notification settings - Fork 147
/
let.ex
39 lines (32 loc) · 1.02 KB
/
let.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
defmodule Surface.Directive.Let do
use Surface.Directive
def extract({":let", {:attribute_expr, value, expr_meta}, attr_meta}, meta) do
%AST.Directive{
module: __MODULE__,
name: :let,
value: directive_value(value, Helpers.to_meta(expr_meta, meta)),
meta: Helpers.to_meta(attr_meta, meta)
}
end
def extract(_, _), do: []
def process(%AST.Directive{value: %AST.AttributeExpr{value: value}}, %AST.Component{} = node) do
update_in(node.templates, fn
%{default: [first | others]} = tpls ->
Map.put(tpls, :default, [%{first | let: value} | others])
tpls ->
tpls
end)
end
def process(%AST.Directive{value: %AST.AttributeExpr{value: value}}, %type{} = node)
when type in [AST.Template, AST.SlotableComponent] do
%{node | let: value}
end
def process(_, node), do: node
defp directive_value(value, meta) do
AST.AttributeExpr.new(
Surface.TypeHandler.expr_to_quoted!(value, ":let", :bindings, meta),
value,
meta
)
end
end