From 693565ddda56295845f92f8290ebb2a2b64be9f5 Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Sat, 12 Jan 2019 22:10:21 +0000 Subject: [PATCH] syntax error on empty variable tag --- CHANGELOG.md | 4 ++++ Sources/Extension.swift | 2 +- Sources/Node.swift | 5 ++++- Tests/StencilTests/EnvironmentSpec.swift | 7 ++++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 558e8cd..4f87233 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,10 @@ _None_ [Ilya Puchka](https://github.com/ilyapuchka) [#254](https://github.com/stencilproject/Stencil/pull/254) +- Throw syntax error on empty variable tags (`{{ }}`) instead `fatalError`. + [Ilya Puchka](https://github.com/ilyapuchka) + [#263](https://github.com/stencilproject/Stencil/pull/263) + ### Internal Changes - `Token` type converted to struct to allow computing token components only once. diff --git a/Sources/Extension.swift b/Sources/Extension.swift index e994e6e..a91b4ab 100644 --- a/Sources/Extension.swift +++ b/Sources/Extension.swift @@ -89,7 +89,7 @@ enum Filter: FilterType { switch self { case let .simple(filter): if !arguments.isEmpty { - throw TemplateSyntaxError("cannot invoke filter with an argument") + throw TemplateSyntaxError("Can't invoke filter with an argument") } return try filter(value) case let .arguments(filter): diff --git a/Sources/Node.swift b/Sources/Node.swift index 8885ff6..d06d3ca 100644 --- a/Sources/Node.swift +++ b/Sources/Node.swift @@ -84,7 +84,10 @@ public class VariableNode: NodeType { elseExpression = nil } - let filter = try parser.compileResolvable(components[0], containedIn: token) + guard let resolvable = components.first else { + throw TemplateSyntaxError(reason: "Missing variable name", token: token) + } + let filter = try parser.compileResolvable(resolvable, containedIn: token) return VariableNode(variable: filter, token: token, condition: condition, elseExpression: elseExpression) } diff --git a/Tests/StencilTests/EnvironmentSpec.swift b/Tests/StencilTests/EnvironmentSpec.swift index 2acfa30..f1bf469 100644 --- a/Tests/StencilTests/EnvironmentSpec.swift +++ b/Tests/StencilTests/EnvironmentSpec.swift @@ -133,6 +133,11 @@ final class EnvironmentTests: XCTestCase { token: "name|unknown" ) } + + it("reports error in variable tag") { + self.template = "{{ }}" + try self.expectError(reason: "Missing variable name", token: " ") + } } func testRenderingError() { @@ -153,7 +158,7 @@ final class EnvironmentTests: XCTestCase { it("reports passing argument to simple filter") { self.template = "{{ name|uppercase:5 }}" - try self.expectError(reason: "cannot invoke filter with an argument", token: "name|uppercase:5") + try self.expectError(reason: "Can't invoke filter with an argument", token: "name|uppercase:5") } it("reports rendering error in custom tag") {