Skip to content

Commit

Permalink
More work on removing slot :index, :name and :for
Browse files Browse the repository at this point in the history
  • Loading branch information
msaraiva committed Sep 18, 2024
1 parent b0bab5b commit 9d8cedd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 57 deletions.
4 changes: 1 addition & 3 deletions lib/surface/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ defmodule Surface.AST.Slot do
## Properties
* `:name` - the slot name
* `:index` - the index of the slotable entry assigned to this slot
* `:for` - the slotable entry assigned for this slot
* `:default` - a list of AST nodes representing the default content for this slot
* `:arg` - quoted expression representing the argument for this slot
Expand All @@ -341,12 +340,11 @@ defmodule Surface.AST.Slot do
* `:meta` - compilation meta data
* `:directives` - directives associated with this slot
"""
defstruct [:name, :as, :for, :index, :arg, :generator_value, :context_put, :default, :meta, directives: []]
defstruct [:name, :as, :for, :arg, :generator_value, :context_put, :default, :meta, directives: []]

@type t :: %__MODULE__{
name: binary(),
as: atom(),
index: any(),
for: any(),
directives: list(Surface.AST.Directive.t()),
meta: Surface.AST.Meta.t(),
Expand Down
54 changes: 8 additions & 46 deletions lib/surface/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -564,43 +564,28 @@ defmodule Surface.Compiler do
]
end

# TODO: Validate attributes with custom messages

has_root? = has_attribute?(attributes, :root)
has_for? = !has_root? and has_attribute?(attributes, "for")

name =
extract_name_from_root(attributes) || attribute_value_as_atom(attributes, "name", nil) ||
extract_name_from_for(attributes)
name = extract_name_from_root(attributes)

name =
if !name and !has_for? and !has_root? do
if !name and !has_root? do
:default
else
name
end

default_syntax? = not has_attribute?(attributes, "name") && not has_for? && not has_root?
default_syntax? = not has_root?

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

for_ast =
cond do
has_for? ->
attribute_value_as_ast(attributes, "for", :any, %Surface.AST.Literal{value: nil}, compile_meta)

has_root? ->
render_slot_args.slot

true ->
nil
slot_entry_ast =
if has_root? do
render_slot_args.slot
end

index = attribute_value_as_ast(attributes, "index", %Surface.AST.Literal{value: 0}, compile_meta)

{:ok, directives, attrs} = collect_directives(@slot_directive_handlers, attributes, meta)
validate_slot_attrs!(attrs, meta.caller)

Expand All @@ -620,7 +605,7 @@ defmodule Surface.Compiler do
maybe_warn_required_slot_with_default_value(
slot,
children,
for_ast,
slot_entry_ast,
meta
)

Expand Down Expand Up @@ -666,8 +651,7 @@ defmodule Surface.Compiler do
%AST.Slot{
name: name,
as: if(slot, do: slot[:opts][:as]),
index: index,
for: for_ast,
for: slot_entry_ast,
directives: directives,
default: to_ast(children, compile_meta),
arg: arg,
Expand Down Expand Up @@ -960,14 +944,6 @@ defmodule Surface.Compiler do
node
end

defp attribute_value_as_atom(attributes, attr_name, default) do
Enum.find_value(attributes, default, fn {name, value, _} ->
if name == attr_name do
String.to_atom(value)
end
end)
end

defp attribute_raw_value(attributes, attr_name, default) do
Enum.find_value(attributes, default, fn
{^attr_name, {:attribute_expr, expr, _}, _} ->
Expand All @@ -978,20 +954,6 @@ defmodule Surface.Compiler do
end)
end

defp extract_name_from_for(attributes) do
with value when is_binary(value) <- attribute_raw_value(attributes, "for", nil),
{:ok, {:@, _, [{assign_name, _, _}]}} when is_atom(assign_name) <- Code.string_to_quoted(value) do
assign_name
else
{:error, _} ->
# TODO: raise
nil

_ ->
nil
end
end

defp extract_name_from_root(attributes) do
with value when is_binary(value) <- attribute_raw_value(attributes, :root, nil),
{:ok, [{:@, _, [{assign_name, _, _}]} | _rest]} <-
Expand Down
9 changes: 1 addition & 8 deletions lib/surface/compiler/eex_engine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ defmodule Surface.Compiler.EExEngine do
%AST.Slot{
name: provided_name,
as: slot_as,
index: index_ast,
for: slot_for_ast,
arg: arg_expr,
generator_value: generator_value_ast,
Expand All @@ -207,12 +206,6 @@ defmodule Surface.Compiler.EExEngine do
buffer,
state
) do
slot_index =
case index_ast do
%AST.AttributeExpr{value: expr} -> expr
%AST.Literal{value: value} -> value
end

slot_for =
case slot_for_ast do
%AST.AttributeExpr{value: expr} -> expr
Expand All @@ -232,7 +225,7 @@ defmodule Surface.Compiler.EExEngine do
slot_assign = {:@, [], [{slot_name, [], nil}]}

quote do
Enum.at(List.wrap(unquote(slot_assign)), unquote(slot_index))
Enum.at(List.wrap(unquote(slot_assign)), 0)
end
end

Expand Down

0 comments on commit 9d8cedd

Please sign in to comment.