Skip to content

Commit

Permalink
Remove deprecated slot props for, name and ìndex
Browse files Browse the repository at this point in the history
  • Loading branch information
msaraiva committed Sep 18, 2024
1 parent 4e8cc9f commit b0bab5b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 100 deletions.
65 changes: 2 additions & 63 deletions lib/surface/compiler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ defmodule Surface.Compiler do
Surface.Directive.For
]

@valid_slot_props [:root, "for", "name", "index", "generator_value", "context_put"]
@valid_slot_props [:root, "generator_value", "context_put"]

@directive_prefixes [":", "s-"]

Expand Down Expand Up @@ -1523,67 +1523,6 @@ defmodule Surface.Compiler do
Enum.each(attrs, &validate_slot_attr!(&1, caller))
end

defp validate_slot_attr!({"for", value, _meta}, caller) do
{:attribute_expr, expr, expr_meta} = value

message = """
property `for` has been deprecated. Please use the root prop instead. Examples:
Rendering the slot:
<#slot {#{expr}}/>
Iterating over the slot items:
{#for item <- #{expr}}
<#slot {item}/>
{/for}
"""

Surface.IOHelper.warn(message, caller, expr_meta.line)
:ok
end

defp validate_slot_attr!({"name", value, meta}, caller) do
message = """
properties `name` and `index` have been deprecated. Please use root prop instead. Examples:
Rendering the slot:
<#slot {@#{value}}/>
Iterating over the slot items:
{#for item <- @#{value}}
<#slot {item}/>
{/for}
"""

Surface.IOHelper.warn(message, caller, meta.line)

:ok
end

defp validate_slot_attr!({"index", _value, meta}, caller) do
message = """
properties `name` and `index` have been deprecated. Please use root prop instead. Examples:
Rendering the slot:
<#slot {@slot_name}/>
Iterating over the slot items:
{#for item <- @slot_name}
<#slot {item}/>
{/for}
"""

Surface.IOHelper.warn(message, caller, meta.line)

:ok
end

defp validate_slot_attr!({name, _, _meta}, _caller) when name in @valid_slot_props do
:ok
end
Expand All @@ -1598,7 +1537,7 @@ defmodule Surface.Compiler do
message = """
invalid #{type} `#{name}` for <#slot>.
Slots only accept the root prop, `for`, `name`, `index`, `generator_value`, `:if` and `:for`.
Slots only accept the root prop, `generator_value`, `:if` and `:for`.
"""

IOHelper.compile_error(message, file, line)
Expand Down
39 changes: 2 additions & 37 deletions test/surface/integrations/slot_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ defmodule Surface.SlotSyncTest do
message = ~r"""
code:10: invalid directive `:attrs` for <#slot>.
Slots only accept the root prop, `for`, `name`, `index`, `generator_value`, `:if` and `:for`.
Slots only accept the root prop, `generator_value`, `:if` and `:for`.
"""

assert_raise(CompileError, message, fn ->
Expand Down Expand Up @@ -1567,7 +1567,7 @@ defmodule Surface.SlotSyncTest do
message = ~r"""
code:11: invalid attribute `let` for <#slot>.
Slots only accept the root prop, `for`, `name`, `index`, `generator_value`, `:if` and `:for`.
Slots only accept the root prop, `generator_value`, `:if` and `:for`.
"""

assert_raise(CompileError, message, fn ->
Expand Down Expand Up @@ -1637,41 +1637,6 @@ defmodule Surface.SlotSyncTest do
end)
end

test "outputs compile warning when using deprecated for property" do
component_code = """
defmodule UsingDeprecatedArgsOption do
use Surface.Component
slot default
def render(assigns) do
~F"\""
<#slot for={@default} />
"\""
end
end
"""

output =
capture_io(:standard_error, fn ->
{{:module, _, _, _}, _} = Code.eval_string(component_code, [], %{__ENV__ | file: "code.exs", line: 1})
end)

assert output =~ """
property `for` has been deprecated. Please use the root prop instead. Examples:
Rendering the slot:
<#slot {@default}/>
Iterating over the slot items:
{#for item <- @default}
<#slot {item}/>
{/for}
"""
end

test "outputs compile warning when adding arg attribute to the default slot in a slotable component" do
component_code = """
defmodule ColumnWithRenderAndSlotArg do
Expand Down

0 comments on commit b0bab5b

Please sign in to comment.