fix(variable): checking values for nil when using mirror (#144)
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
incorrect amount of arguments.
|
incorrect amount of arguments.
|
||||||
- Fixes a potential crash when using incomplete tokens in a template for
|
- Fixes a potential crash when using incomplete tokens in a template for
|
||||||
example, `{%%}` or `{{}}`.
|
example, `{%%}` or `{{}}`.
|
||||||
|
- Fixes evaluating nil properties as true
|
||||||
|
|
||||||
|
|
||||||
## 0.9.0
|
## 0.9.0
|
||||||
|
|||||||
@@ -105,6 +105,10 @@ public struct Variable : Equatable, Resolvable {
|
|||||||
|
|
||||||
if current == nil {
|
if current == nil {
|
||||||
return nil
|
return nil
|
||||||
|
// mirror returns non-nil value even for nil-containing properties
|
||||||
|
// so we have to check if its value is actually nil or not
|
||||||
|
} else if let current = current, String(describing: current) == "nil" {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -253,5 +253,22 @@ func testIfNode() {
|
|||||||
let result = try renderNodes(nodes, Context(dictionary: ["value": "test"]))
|
let result = try renderNodes(nodes, Context(dictionary: ["value": "test"]))
|
||||||
try expect(result) == "true"
|
try expect(result) == "true"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates nil properties as false") {
|
||||||
|
let tokens: [Token] = [
|
||||||
|
.block(value: "if instance.value"),
|
||||||
|
.text(value: "true"),
|
||||||
|
.block(value: "endif")
|
||||||
|
]
|
||||||
|
|
||||||
|
let parser = TokenParser(tokens: tokens, environment: Environment())
|
||||||
|
let nodes = try parser.parse()
|
||||||
|
|
||||||
|
struct SomeType {
|
||||||
|
let value: String? = nil
|
||||||
|
}
|
||||||
|
let result = try renderNodes(nodes, Context(dictionary: ["instance": SomeType()]))
|
||||||
|
try expect(result) == ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user