Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support function components #473

Merged
merged 9 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions lib/surface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ defmodule Surface do

@doc false
def default_props(module) do
Enum.map(module.__props__(), fn %{name: name, opts: opts} -> {name, opts[:default]} end)
props = if function_exported?(module, :__props__, 0), do: module.__props__(), else: []
Enum.map(props, fn %{name: name, opts: opts} -> {name, opts[:default]} end)
end

@doc false
Expand Down Expand Up @@ -230,26 +231,31 @@ defmodule Surface do
|> Keyword.merge(dynamic_props)
|> Keyword.merge(static_props)

slot_assigns =
module
|> map_slots_to_assigns(slot_props)

Map.new(
props ++
slot_assigns ++
[
__surface__: %{
slots: Map.new(slots),
provided_templates: Keyword.keys(slot_props)
},
__context__: context
]
)
slot_assigns = map_slots_to_assigns(module, slot_props)

if module do
Map.new(
props ++
slot_assigns ++
[
__surface__: %{
slots: Map.new(slots),
provided_templates: Keyword.keys(slot_props)
},
__context__: context
]
)
else
# Function components don't support slots nor contexts
Map.new(props)
end
end

defp map_slots_to_assigns(module, slot_props) do
slots = if function_exported?(module, :__slots__, 0), do: module.__slots__(), else: []

mapping =
module.__slots__()
slots
|> Enum.map(fn %{name: name, opts: opts} -> {name, Keyword.get(opts, :as)} end)
|> Enum.filter(fn value -> not match?({_, nil}, value) end)

Expand Down
47 changes: 35 additions & 12 deletions lib/surface/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ defmodule Surface.API do
# Any caller component can hold other components with slots
Module.register_attribute(__MODULE__, :assigned_slots_by_parent, accumulate: false)

Module.put_attribute(__MODULE__, :changes_context?, false)
Module.put_attribute(__MODULE__, :gets_context?, false)
Module.register_attribute(__MODULE__, :changes_context?, accumulate: true)
Module.register_attribute(__MODULE__, :gets_context?, accumulate: true)

for func <- unquote(include) do
Module.register_attribute(__MODULE__, func, accumulate: true)
Expand Down Expand Up @@ -295,20 +295,43 @@ defmodule Surface.API do
end

defp quoted_context_funcs(env) do
changes_context? = Module.get_attribute(env.module, :changes_context?)
gets_context? = Module.get_attribute(env.module, :gets_context?)
funs_changing =
env.module
|> Module.get_attribute(:changes_context?, [])
|> MapSet.new()

quote do
@doc false
def __changes_context__?() do
unquote(changes_context?)
funs_getting =
env.module
|> Module.get_attribute(:gets_context?, [])
|> MapSet.new()

quoted_changing =
for fun <- funs_changing do
quote do
@doc false
def __changes_context__?(unquote(fun)), do: true
end
end

@doc false
def __gets_context__?() do
unquote(gets_context?)
quoted_changing_fallback =
quote do
def __changes_context__?(_fun), do: false
end
end

quoted_getting =
for fun <- funs_getting do
quote do
@doc false
def __gets_context__?(unquote(fun)), do: true
end
end

quoted_getting_fallback =
quote do
def __gets_context__?(_fun), do: false
end

List.flatten([quoted_changing, quoted_changing_fallback, quoted_getting, quoted_getting_fallback])
end

defp validate_assigns!(env) do
Expand Down
89 changes: 70 additions & 19 deletions lib/surface/ast.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ defmodule Surface.AST.Meta do
* `:node_alias` - the alias used inside the source code (e.g. `LivePatch`)
* `:file` - the file from which the source was extracted
* `:caller` - a Macro.Env struct representing the caller
* `:function_component?` - indicates it's a function component or not
"""
@derive {Inspect, only: [:line, :column, :module, :node_alias, :file, :checks]}
defstruct [:line, :column, :module, :node_alias, :file, :caller, :checks]
@derive {Inspect, only: [:line, :column, :module, :node_alias, :file, :checks, :function_component?]}
defstruct [:line, :column, :module, :node_alias, :file, :caller, :checks, :function_component?]

@type t :: %__MODULE__{
line: non_neg_integer(),
Expand All @@ -106,7 +107,8 @@ defmodule Surface.AST.Meta do
node_alias: binary() | nil,
caller: Macro.Env.t(),
file: binary(),
checks: Keyword.t(boolean())
checks: Keyword.t(boolean()),
function_component?: boolean()
}

def quoted_caller_cid(meta) do
Expand Down Expand Up @@ -425,7 +427,7 @@ end

defmodule Surface.AST.Component do
@moduledoc """
An AST node representing a standard HTML tag
An AST node representing a component

## Properties
* `:module` - the component module
Expand Down Expand Up @@ -453,6 +455,38 @@ defmodule Surface.AST.Component do
}
end

defmodule Surface.AST.FunctionComponent do
@moduledoc """
An AST node representing a function component

## Properties
* `:module` - the component module
* `:fun` - the render function
* `:type` - the type of function (:local or :remote)
* `:props` - the props for this component
* `:directives` - any directives to be applied to this tag
* `:children` - the tag children
* `:meta` - compilation meta data
* `:debug` - keyword list indicating when debug information should be printed during compilation
"""
defstruct [:module, :fun, :type, :props, :dynamic_props, :templates, :meta, debug: [], directives: []]

@type t :: %__MODULE__{
module: module() | Surface.AST.AttributeExpr.t(),
fun: atom() | Surface.AST.AttributeExpr.t() | nil,
debug: list(atom()),
type: :local | :remote | :dynamic,
props: list(Surface.AST.Attribute.t()),
dynamic_props: Surface.AST.DynamicAttribute.t(),
directives: list(Surface.AST.Directive.t()),
templates: %{
:default => list(Surface.AST.Template.t() | Surface.AST.SlotableComponent.t()),
optional(atom()) => list(Surface.AST.Template.t() | Surface.AST.SlotableComponent.t())
},
meta: Surface.AST.Meta.t()
}
end

defmodule Surface.AST.MacroComponent do
@moduledoc """
An AST node representing a macro component
Expand Down Expand Up @@ -524,30 +558,47 @@ defmodule Surface.AST.SlotableComponent do
end

defmodule Surface.AST do
alias __MODULE__

@type t ::
Surface.AST.Literal.t()
| Surface.AST.Interpolation.t()
| Surface.AST.Expr.t()
| Surface.AST.Tag.t()
| Surface.AST.VoidTag.t()
| Surface.AST.Template.t()
| Surface.AST.Slot.t()
| Surface.AST.If.t()
| Surface.AST.For.t()
| Surface.AST.Container.t()
| Surface.AST.Component.t()
| Surface.AST.MacroComponent.t()
| Surface.AST.SlotableComponent.t()
| Surface.AST.Error.t()
AST.Literal.t()
| AST.Interpolation.t()
| AST.Expr.t()
| AST.Tag.t()
| AST.VoidTag.t()
| AST.Template.t()
| AST.Slot.t()
| AST.If.t()
| AST.For.t()
| AST.Container.t()
| AST.Component.t()
| AST.MacroComponent.t()
| AST.SlotableComponent.t()
| AST.Error.t()

def find_attribute_value(attributes, name) do
Enum.find_value(attributes, fn
%Surface.AST.Attribute{name: ^name, value: value} -> value
%AST.Attribute{name: ^name, value: value} -> value
_ -> nil
end)
end

def has_attribute?(attributes, name) do
Enum.any?(attributes, fn %{name: attr_name} -> attr_name == name end)
end

def pop_attributes_values_as_map(attributes, names) do
initial = {Map.new(names, &{&1, nil}), []}

{map, others} =
Enum.reduce(attributes, initial, fn %AST.Attribute{name: name, value: value} = attr, {map, others} ->
if name in names do
{Map.put(map, name, value), others}
else
{map, [attr | others]}
end
end)

{map, Enum.reverse(others)}
end
end
Loading