-
Notifications
You must be signed in to change notification settings - Fork 225
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
Allow conditions in variable node #243
Conversation
Thoughts on |
From implementation point of view it is easier to have it as extension of variable node, not a variation of if node. We can also add support for this in other nodes, like include, extend, filter |
self.elseExpression = nil | ||
} | ||
|
||
init(variable: Resolvable, token: Token? = nil, condition: Expression?, elseExpression: Resolvable?) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not add the condition
and elseExpression
parameters to the public init above? (and make them nil
by default)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expression
is not public, at least in this branch. TBH I don't even know why VariableNode
needs to be public, but didn't want to make unrelated changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
} | ||
|
||
public init(variable: String, token: Token? = nil) { | ||
self.variable = Variable(variable) | ||
self.token = token | ||
self.condition = nil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
Sources/Node.swift
Outdated
} | ||
|
||
public func render(_ context: Context) throws -> String { | ||
if let condition = self.condition { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be one if
statement. I'd also move the rest into an else
clause, for clarity purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean? it needs to be anwrapped here so we don't need to dance with optional chaining later. And it should continue execution if this is nil or if it evaluates to true
, that's why the rest is not in else. It's kind of guard but using if
s. Can be merged into one though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now I read This can be one
again and see that it's what you mean)
var components = token.components() | ||
|
||
func hasToken(_ token: String, at index: Int) -> Bool { | ||
return components.count > (index + 1) && components[index] == token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nitpick, but I think this is clearer:
https://github.com/SwiftGen/StencilSwiftKit/blob/master/Sources/StencilSwiftKit/MapNode.swift#L21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find ~=
harder to grasp than >
🤷♂️
Sources/Node.swift
Outdated
let elseExpression: Resolvable? | ||
|
||
if hasToken("if", at: 1) { | ||
let components = Array(components.suffix(from: 2)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to convert this into an Array
? #226 has made me more conscious of performance issues, and this causes an allocation/copy that may not be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, we can do it later in else
branch
Resolves #241