From 4f1a5b3e3d0f65bdf86042387959890a740273bd Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Sun, 12 Aug 2018 22:25:25 +0100 Subject: [PATCH] store reference to token when parsing range variable --- Sources/Parser.swift | 2 +- Sources/Variable.swift | 13 ++++++++++++- Tests/StencilTests/VariableSpec.swift | 8 +++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Sources/Parser.swift b/Sources/Parser.swift index 5dde40c..fd984a4 100644 --- a/Sources/Parser.swift +++ b/Sources/Parser.swift @@ -148,7 +148,7 @@ public class TokenParser { } public func compileResolvable(_ token: String, containedIn containingToken: Token) throws -> Resolvable { - return try RangeVariable(token, parser: self) + return try RangeVariable(token, parser: self, containedIn: containingToken) ?? compileFilter(token, containedIn: containingToken) } diff --git a/Sources/Variable.swift b/Sources/Variable.swift index 1c54a55..1da7439 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -50,7 +50,7 @@ public struct Variable : Equatable, Resolvable { // Split the lookup string and resolve references if possible fileprivate func lookup(_ context: Context) throws -> [String] { - var keyPath = KeyPath(variable, in: context) + let keyPath = KeyPath(variable, in: context) return try keyPath.parse() } @@ -138,6 +138,7 @@ public struct RangeVariable: Resolvable { public let from: Resolvable public let to: Resolvable + @available(*, deprecated, message: "Use init?(_:parser:containedIn:)") public init?(_ token: String, parser: TokenParser) throws { let components = token.components(separatedBy: "...") guard components.count == 2 else { @@ -148,6 +149,16 @@ public struct RangeVariable: Resolvable { self.to = try parser.compileFilter(components[1]) } + public init?(_ token: String, parser: TokenParser, containedIn containingToken: Token) throws { + let components = token.components(separatedBy: "...") + guard components.count == 2 else { + return nil + } + + self.from = try parser.compileFilter(components[0], containedIn: containingToken) + self.to = try parser.compileFilter(components[1], containedIn: containingToken) + } + public func resolve(_ context: Context) throws -> Any? { let fromResolved = try from.resolve(context) let toResolved = try to.resolve(context) diff --git a/Tests/StencilTests/VariableSpec.swift b/Tests/StencilTests/VariableSpec.swift index 7b386bc..ac85f1b 100644 --- a/Tests/StencilTests/VariableSpec.swift +++ b/Tests/StencilTests/VariableSpec.swift @@ -292,7 +292,9 @@ func testVariable() { }() func makeVariable(_ token: String) throws -> RangeVariable? { - return try RangeVariable(token, parser: TokenParser(tokens: [], environment: context.environment)) + let token = Token.variable(value: token, at: .unknown) + let parser = TokenParser(tokens: [token], environment: context.environment) + return try RangeVariable(token.contents, parser: parser, containedIn: token) } $0.it("can resolve closed range as array") { @@ -321,11 +323,11 @@ func testVariable() { } $0.it("throws is left range value is missing") { - try expect(makeVariable("...1")).toThrow() + try expect(makeVariable("...1")).toThrow() } $0.it("throws is right range value is missing") { - try expect(makeVariable("1...")).toThrow() + try expect(makeVariable("1...")).toThrow() } }