Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile filters in call node #85

Merged
merged 4 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}