diff --git a/src/nodes/function.jl b/src/nodes/function.jl index bb3d8ee..b3ce6d0 100644 --- a/src/nodes/function.jl +++ b/src/nodes/function.jl @@ -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 @@ -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. diff --git a/test/runtests.jl b/test/runtests.jl index dfc6453..4cda179 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 diff --git a/test/templates.jl b/test/templates.jl index 5e1770a..5353e5b 100644 --- a/test/templates.jl +++ b/test/templates.jl @@ -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 diff --git a/test/templates/basic/splat-args-2.1.txt b/test/templates/basic/splat-args-2.1.txt new file mode 100644 index 0000000..3b294b7 --- /dev/null +++ b/test/templates/basic/splat-args-2.1.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat-args-2.2.txt b/test/templates/basic/splat-args-2.2.txt new file mode 100644 index 0000000..711cdd1 --- /dev/null +++ b/test/templates/basic/splat-args-2.2.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat-args-2.3.txt b/test/templates/basic/splat-args-2.3.txt new file mode 100644 index 0000000..568bfc6 --- /dev/null +++ b/test/templates/basic/splat-args-2.3.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat-args.1.txt b/test/templates/basic/splat-args.1.txt new file mode 100644 index 0000000..f63a97d --- /dev/null +++ b/test/templates/basic/splat-args.1.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat-args.2.txt b/test/templates/basic/splat-args.2.txt new file mode 100644 index 0000000..e2d1b04 --- /dev/null +++ b/test/templates/basic/splat-args.2.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat-args.3.txt b/test/templates/basic/splat-args.3.txt new file mode 100644 index 0000000..669b607 --- /dev/null +++ b/test/templates/basic/splat-args.3.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/templates/basic/splat.html b/test/templates/basic/splat.html new file mode 100644 index 0000000..c64ab51 --- /dev/null +++ b/test/templates/basic/splat.html @@ -0,0 +1,34 @@ + + + + + + + \ No newline at end of file