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 1 commit
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.
Copy link
Contributor

@AliSoftware AliSoftware Apr 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, just missed that remark though:
Missing two spaces at the end of the line after the . — for markdown formatting purposes

(we should def add a Dangerfile and rule for linting our CHANGELOG one day, just never had time to write one for our specific CHANGELOG formatting convention yet)

[Ilya Puchka](https://github.com/ilyapuchka)
[#85](https://github.com/SwiftGen/StencilSwiftKit/pull/85)

## 2.5.0

### New Features
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 })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally agree with this code style too when chaining flatMaps but our linter disagrees there, so maybe adapt the syntax here, like let variables = node.arguments.flatMap { ($0 as? FilterExpression)?.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")
}
}