From 9d8cedd2d45b435bf5e9b957ceea7f451de9c4f4 Mon Sep 17 00:00:00 2001 From: Marlus Saraiva Date: Wed, 18 Sep 2024 18:30:59 -0300 Subject: [PATCH] More work on removing slot :index, :name and :for --- lib/surface/ast.ex | 4 +-- lib/surface/compiler.ex | 54 +++++------------------------- lib/surface/compiler/eex_engine.ex | 9 +---- 3 files changed, 10 insertions(+), 57 deletions(-) diff --git a/lib/surface/ast.ex b/lib/surface/ast.ex index aab99894..ae3f7c13 100644 --- a/lib/surface/ast.ex +++ b/lib/surface/ast.ex @@ -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 @@ -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(), diff --git a/lib/surface/compiler.ex b/lib/surface/compiler.ex index 792314ad..dd3ad565 100644 --- a/lib/surface/compiler.ex +++ b/lib/surface/compiler.ex @@ -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) @@ -620,7 +605,7 @@ defmodule Surface.Compiler do maybe_warn_required_slot_with_default_value( slot, children, - for_ast, + slot_entry_ast, meta ) @@ -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, @@ -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, _}, _} -> @@ -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]} <- diff --git a/lib/surface/compiler/eex_engine.ex b/lib/surface/compiler/eex_engine.ex index 64bcdb8e..d4c33926 100644 --- a/lib/surface/compiler/eex_engine.ex +++ b/lib/surface/compiler/eex_engine.ex @@ -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, @@ -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 @@ -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