Skip to content

Commit 9d8cedd

Browse files
committed
More work on removing slot :index, :name and :for
1 parent b0bab5b commit 9d8cedd

File tree

3 files changed

+10
-57
lines changed

3 files changed

+10
-57
lines changed

lib/surface/ast.ex

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ defmodule Surface.AST.Slot do
332332
333333
## Properties
334334
* `:name` - the slot name
335-
* `:index` - the index of the slotable entry assigned to this slot
336335
* `:for` - the slotable entry assigned for this slot
337336
* `:default` - a list of AST nodes representing the default content for this slot
338337
* `:arg` - quoted expression representing the argument for this slot
@@ -341,12 +340,11 @@ defmodule Surface.AST.Slot do
341340
* `:meta` - compilation meta data
342341
* `:directives` - directives associated with this slot
343342
"""
344-
defstruct [:name, :as, :for, :index, :arg, :generator_value, :context_put, :default, :meta, directives: []]
343+
defstruct [:name, :as, :for, :arg, :generator_value, :context_put, :default, :meta, directives: []]
345344

346345
@type t :: %__MODULE__{
347346
name: binary(),
348347
as: atom(),
349-
index: any(),
350348
for: any(),
351349
directives: list(Surface.AST.Directive.t()),
352350
meta: Surface.AST.Meta.t(),

lib/surface/compiler.ex

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -564,43 +564,28 @@ defmodule Surface.Compiler do
564564
]
565565
end
566566

567-
# TODO: Validate attributes with custom messages
568-
569567
has_root? = has_attribute?(attributes, :root)
570-
has_for? = !has_root? and has_attribute?(attributes, "for")
571-
572-
name =
573-
extract_name_from_root(attributes) || attribute_value_as_atom(attributes, "name", nil) ||
574-
extract_name_from_for(attributes)
568+
name = extract_name_from_root(attributes)
575569

576570
name =
577-
if !name and !has_for? and !has_root? do
571+
if !name and !has_root? do
578572
:default
579573
else
580574
name
581575
end
582576

583-
default_syntax? = not has_attribute?(attributes, "name") && not has_for? && not has_root?
577+
default_syntax? = not has_root?
584578

585579
render_slot_args =
586580
if has_root? do
587581
attribute_value_as_ast(attributes, :root, :render_slot, %Surface.AST.Literal{value: nil}, compile_meta)
588582
end
589583

590-
for_ast =
591-
cond do
592-
has_for? ->
593-
attribute_value_as_ast(attributes, "for", :any, %Surface.AST.Literal{value: nil}, compile_meta)
594-
595-
has_root? ->
596-
render_slot_args.slot
597-
598-
true ->
599-
nil
584+
slot_entry_ast =
585+
if has_root? do
586+
render_slot_args.slot
600587
end
601588

602-
index = attribute_value_as_ast(attributes, "index", %Surface.AST.Literal{value: 0}, compile_meta)
603-
604589
{:ok, directives, attrs} = collect_directives(@slot_directive_handlers, attributes, meta)
605590
validate_slot_attrs!(attrs, meta.caller)
606591

@@ -620,7 +605,7 @@ defmodule Surface.Compiler do
620605
maybe_warn_required_slot_with_default_value(
621606
slot,
622607
children,
623-
for_ast,
608+
slot_entry_ast,
624609
meta
625610
)
626611

@@ -666,8 +651,7 @@ defmodule Surface.Compiler do
666651
%AST.Slot{
667652
name: name,
668653
as: if(slot, do: slot[:opts][:as]),
669-
index: index,
670-
for: for_ast,
654+
for: slot_entry_ast,
671655
directives: directives,
672656
default: to_ast(children, compile_meta),
673657
arg: arg,
@@ -960,14 +944,6 @@ defmodule Surface.Compiler do
960944
node
961945
end
962946

963-
defp attribute_value_as_atom(attributes, attr_name, default) do
964-
Enum.find_value(attributes, default, fn {name, value, _} ->
965-
if name == attr_name do
966-
String.to_atom(value)
967-
end
968-
end)
969-
end
970-
971947
defp attribute_raw_value(attributes, attr_name, default) do
972948
Enum.find_value(attributes, default, fn
973949
{^attr_name, {:attribute_expr, expr, _}, _} ->
@@ -978,20 +954,6 @@ defmodule Surface.Compiler do
978954
end)
979955
end
980956

981-
defp extract_name_from_for(attributes) do
982-
with value when is_binary(value) <- attribute_raw_value(attributes, "for", nil),
983-
{:ok, {:@, _, [{assign_name, _, _}]}} when is_atom(assign_name) <- Code.string_to_quoted(value) do
984-
assign_name
985-
else
986-
{:error, _} ->
987-
# TODO: raise
988-
nil
989-
990-
_ ->
991-
nil
992-
end
993-
end
994-
995957
defp extract_name_from_root(attributes) do
996958
with value when is_binary(value) <- attribute_raw_value(attributes, :root, nil),
997959
{:ok, [{:@, _, [{assign_name, _, _}]} | _rest]} <-

lib/surface/compiler/eex_engine.ex

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ defmodule Surface.Compiler.EExEngine do
196196
%AST.Slot{
197197
name: provided_name,
198198
as: slot_as,
199-
index: index_ast,
200199
for: slot_for_ast,
201200
arg: arg_expr,
202201
generator_value: generator_value_ast,
@@ -207,12 +206,6 @@ defmodule Surface.Compiler.EExEngine do
207206
buffer,
208207
state
209208
) do
210-
slot_index =
211-
case index_ast do
212-
%AST.AttributeExpr{value: expr} -> expr
213-
%AST.Literal{value: value} -> value
214-
end
215-
216209
slot_for =
217210
case slot_for_ast do
218211
%AST.AttributeExpr{value: expr} -> expr
@@ -232,7 +225,7 @@ defmodule Surface.Compiler.EExEngine do
232225
slot_assign = {:@, [], [{slot_name, [], nil}]}
233226

234227
quote do
235-
Enum.at(List.wrap(unquote(slot_assign)), unquote(slot_index))
228+
Enum.at(List.wrap(unquote(slot_assign)), 0)
236229
end
237230
end
238231

0 commit comments

Comments
 (0)