Skip to content

Commit

Permalink
Merge pull request #85 from ilyapuchka/call-with-filter
Browse files Browse the repository at this point in the history
Compile filters in call node
  • Loading branch information
AliSoftware authored Apr 17, 2018
2 parents f15c445 + c77a23a commit c85c2d0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

---

## Master

### Bug fixes

* Fixed using filter expression in call node.
[Ilya Puchka](https://github.com/ilyapuchka)
[#85](https://github.com/SwiftGen/StencilSwiftKit/pull/85)

## 2.5.0

### New Features
Expand Down
4 changes: 2 additions & 2 deletions Documentation/tag-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ See the documentation for the [macro tag](tag-macro.md) for more information.
{% endmacro %}
{# calling test2 #}
{% call test2 "hey" 123 "world" %}
{% call test2 "hey" 123 "world"|capitalise %}
```

Will output:
Expand All @@ -58,7 +58,7 @@ Will output:
Received parameters in test2:
- a = "hey"
- b = "123"
- c = "world"
- c = "World"
// calling test1
Hello world! (inside test)
Expand Down
8 changes: 4 additions & 4 deletions Sources/CallMacroNodes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct CallableBlock: NodeType {
let parameters: [String]
let nodes: [NodeType]

func context(_ context: Context, arguments: [Variable]) throws -> [String: Any] {
func context(_ context: Context, arguments: [Resolvable]) throws -> [String: Any] {
var result = [String: Any]()

for (parameter, argument) in zip(parameters, arguments) {
Expand Down Expand Up @@ -61,20 +61,20 @@ class MacroNode: NodeType {

class CallNode: NodeType {
let variableName: String
let arguments: [Variable]
let arguments: [Resolvable]

class func parse(_ parser: TokenParser, token: Token) throws -> NodeType {
let components = token.components()
guard components.count >= 2 else {
throw TemplateSyntaxError("'call' tag takes at least one argument, the name of the block to call.")
}
let variable = components[1]
let arguments = Array(components.dropFirst(2)).map { Variable($0) }
let arguments = try Array(components.dropFirst(2)).map(parser.compileFilter)

return CallNode(variableName: variable, arguments: arguments)
}

init(variableName: String, arguments: [Variable]) {
init(variableName: String, arguments: [Resolvable]) {
self.variableName = variableName
self.arguments = arguments
}
Expand Down
5 changes: 3 additions & 2 deletions Tests/StencilSwiftKitTests/CallNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CallNodeTests: XCTestCase {
}

XCTAssertEqual(node.variableName, "myFunc")
XCTAssertEqual(node.arguments, [])
XCTAssertEqual(node.arguments.count, 0)
}

func testParserWithArguments() {
Expand All @@ -38,7 +38,8 @@ class CallNodeTests: XCTestCase {
}

XCTAssertEqual(node.variableName, "myFunc")
XCTAssertEqual(node.arguments, [Variable("a"), Variable("b"), Variable("c")])
let variables = node.arguments.flatMap { $0 as? FilterExpression }.flatMap { $0.variable }
XCTAssertEqual(variables, [Variable("a"), Variable("b"), Variable("c")])
}

func testParserFail() {
Expand Down
11 changes: 11 additions & 0 deletions Tests/StencilSwiftKitTests/MacroNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,15 @@ class MacroNodeTests: XCTestCase {
XCTAssertEqual(result["p2"] as? Int, 2)
XCTAssertEqual(result["p3"] as? String, "hello")
}

func testCallableBlockWithFilterExpressionParameter() throws {
let block = CallableBlock(parameters: ["greeting"], nodes: [])

let parser = TokenParser(tokens: [], environment: stencilSwiftEnvironment())
let arguments = try [FilterExpression(token: "greet|uppercase", parser: parser)]
let context = Context(dictionary: ["greet": "hello"])

let result = try block.context(context, arguments: arguments)
XCTAssertEqual(result["greeting"] as? String, "HELLO")
}
}

0 comments on commit c85c2d0

Please sign in to comment.