Skip to content

Commit

Permalink
Don't add the scope_id attr when the caller is not a component
Browse files Browse the repository at this point in the history
  • Loading branch information
msaraiva committed Feb 8, 2023
1 parent e9edb4e commit 85d0e7b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/surface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ defmodule Surface do
indentation = meta[:indentation] || 0
column = meta[:column] || 1

caller_is_surface_component =
Module.open?(__CALLER__.module) &&
Module.get_attribute(__CALLER__.module, :component_type) != nil
component_type =
if Module.open?(__CALLER__.module) do
Module.get_attribute(__CALLER__.module, :component_type)
end

caller_is_surface_component = component_type != nil

string
|> Surface.Compiler.compile(line, __CALLER__, __CALLER__.file,
checks: [no_undefined_assigns: caller_is_surface_component],
caller_spec: %Surface.Compiler.CallerSpec{type: component_type},
indentation: indentation,
column: column
)
Expand All @@ -131,7 +135,7 @@ defmodule Surface do
The code must be passed with the `do` block using the `~F` sigil.
Optional `line` and `file` metadata can be passed using `opts`.
Optional `line`, `file` and `caller` metadata can be passed using `opts`.
## Example
Expand Down Expand Up @@ -161,10 +165,11 @@ defmodule Surface do

line = Keyword.get(opts, :line, default_line)
file = Keyword.get(opts, :file, __CALLER__.file)
caller = Keyword.get(opts, :caller, quote(do: __ENV__))
indentation = Keyword.get(string_meta, :indentation, 0)

quote do
Surface.Compiler.compile(unquote(code), unquote(line), __ENV__, unquote(file),
Surface.Compiler.compile(unquote(code), unquote(line), unquote(var!(caller)), unquote(file),
checks: [no_undefined_assigns: false],
indentation: unquote(indentation),
column: 1,
Expand Down
26 changes: 19 additions & 7 deletions lib/surface/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,24 @@ defmodule Surface.Compiler do
"wbr"
]

# TODO: Add all relevant information from the caller (when it's a component), e.g. props, data, style, etc.
# Make the compiler use this struct instead of calls to Surface.API.*(caller.module)
defmodule CallerSpec do
defstruct [:type]

@type t :: %__MODULE__{type: module()}
end

defmodule CompileMeta do
defstruct [:line, :file, :caller, :checks, :variables, :module, :style]
defstruct [:line, :file, :caller, :checks, :variables, :module, :style, :caller_spec]

@type t :: %__MODULE__{
line: non_neg_integer(),
file: binary(),
caller: Macro.Env.t(),
variables: keyword(),
checks: Keyword.t(boolean()),
caller_spec: CallerSpec.t(),
style: map()
}
end
Expand Down Expand Up @@ -112,7 +121,8 @@ defmodule Surface.Compiler do
caller: caller,
checks: opts[:checks] || [],
variables: opts[:variables],
style: style
style: style,
caller_spec: opts[:caller_spec] || %CallerSpec{}
}

tokens
Expand Down Expand Up @@ -859,8 +869,10 @@ defmodule Surface.Compiler do
node
end

defp maybe_add_caller_scope_id_attr_to_root_node(%type{} = node, _style) when type in [AST.Tag, AST.VoidTag] do
is_caller_a_component? = !!Helpers.get_module_attribute(node.meta.caller.module, :component_type, nil)
defp maybe_add_caller_scope_id_attr_to_root_node(%type{} = node, _style, %CallerSpec{} = caller_spec)
when type in [AST.Tag, AST.VoidTag] do
is_caller_a_component? = caller_spec.type != nil

# TODO: when support for `attr` is added, check for :css_class types instead
function_component? = node.meta.caller.function != {:render, 1}

Expand Down Expand Up @@ -899,7 +911,7 @@ defmodule Surface.Compiler do
%{node | attributes: attributes}
end

defp maybe_add_caller_scope_id_attr_to_root_node(node, _style) do
defp maybe_add_caller_scope_id_attr_to_root_node(node, _style, _caller_spec) do
node
end

Expand Down Expand Up @@ -1560,12 +1572,12 @@ defmodule Surface.Compiler do
end
end

defp maybe_transform_ast(nodes, %CompileMeta{style: style}) do
defp maybe_transform_ast(nodes, %CompileMeta{style: style, caller_spec: caller_spec}) do
Enum.map(nodes, fn node ->
node
|> maybe_add_data_s_attrs_to_root_node(style)
|> maybe_add_or_update_style_attr_of_root_node(style)
|> maybe_add_caller_scope_id_attr_to_root_node(style)
|> maybe_add_caller_scope_id_attr_to_root_node(style, caller_spec)
end)
end

Expand Down
1 change: 1 addition & 0 deletions lib/surface/compiler/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ defmodule Surface.Compiler.Helpers do
end
end

# TODO: remove this function and use the `caller_spec` field on the `CompileMeta` struct instead
def get_module_attribute(module, key, default) do
if Mix.env() == :test do
# If the template is compiled directly in a test module, get_attribute might fail,
Expand Down

0 comments on commit 85d0e7b

Please sign in to comment.