Skip to content

Commit

Permalink
Add support for ... props (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHatherly authored Nov 24, 2023
1 parent 6d208eb commit f398edb
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/nodes/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ const TEMPLATE_FUNCTION_TAG = "function"
struct Prop
name::String
value::String
vararg::Bool

function Prop(name, value)
return new(_restore_special_symbols(name), _restore_special_symbols(value))
vararg = false
if endswith(name, "...")
name == value || error("invalid syntax for '...' prop: $(name)...=$(value).")
name, _ = rsplit(name, "..."; limit = 2)
value = name
vararg = true
end
return new(_restore_special_symbols(name), _restore_special_symbols(value), vararg)
end
end

Expand All @@ -17,8 +25,12 @@ function expression(c::BuilderContext, p::Prop)
name = Symbol(p.name)
if p.name == p.value
# Required keyword with no default value or type.
return name
return p.vararg ? Expr(:(...), name) : name
else
# Gaurd against this condition, although it should have already been caught
# during initialization of `Prop`.
p.vararg && error("invalid syntax for '...' prop: $(name)...=$(p.value).")

expr = Meta.parse(p.value)
if MacroTools.@capture(expr, default_::Type_)
# Keyword with default value and type.
Expand Down
30 changes: 30 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ end
Templates.var"custom-elements-nested";
value = true,
)

@test_reference joinpath(basic, "splat-args.1.txt") render(
Templates.var"splat-args";
key = :value,
)

@test_reference joinpath(basic, "splat-args.2.txt") render(
Templates.var"splat-args";
)

@test_reference joinpath(basic, "splat-args.3.txt") render(
Templates.var"splat-args";
a = 1,
b = 2,
)

@test_reference joinpath(basic, "splat-args-2.1.txt") render(
Templates.var"splat-args-2";
)

@test_reference joinpath(basic, "splat-args-2.2.txt") render(
Templates.var"splat-args-2";
a = 2,
)

@test_reference joinpath(basic, "splat-args-2.3.txt") render(
Templates.var"splat-args-2";
a = 2,
b = 10,
)
end

@testset "complex" begin
Expand Down
1 change: 1 addition & 0 deletions test/templates.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ template"templates/basic/layout.html"
template"templates/basic/layout-usage.html"
template"templates/basic/special-symbols.html"
template"templates/basic/custom-elements.html"
template"templates/basic/splat.html"

module Complex

Expand Down
1 change: 1 addition & 0 deletions test/templates/basic/splat-args-2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li><p id="a">1</p></li></ul>
1 change: 1 addition & 0 deletions test/templates/basic/splat-args-2.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li><p id="a">2</p></li></ul>
1 change: 1 addition & 0 deletions test/templates/basic/splat-args-2.3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li><p id="a">2</p></li><li><p id="k">b</p><p id="v">10</p></li></ul>
1 change: 1 addition & 0 deletions test/templates/basic/splat-args.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li><p id="key">key</p><p id="value">value</p></li></ul>
1 change: 1 addition & 0 deletions test/templates/basic/splat-args.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul></ul>
1 change: 1 addition & 0 deletions test/templates/basic/splat-args.3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li><p id="key">a</p><p id="value">1</p></li><li><p id="key">b</p><p id="value">2</p></li></ul>
34 changes: 34 additions & 0 deletions test/templates/basic/splat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<function splat-args args...>
<ul>
<for iter="args" item="arg">
<li>
<p id="key">
<julia value="arg[1]" />
</p>
<p id="value">
<julia value="arg[2]" />
</p>
</li>
</for>
</ul>
</function>

<function splat-args-2 a="1" args...>
<ul>
<li>
<p id="a">
<julia value="a" />
</p>
</li>
<for iter="args" item="arg">
<li>
<p id="k">
<julia value="arg[1]" />
</p>
<p id="v">
<julia value="arg[2]" />
</p>
</li>
</for>
</ul>
</function>

0 comments on commit f398edb

Please sign in to comment.