Skip to content

Commit a7da93f

Browse files
authored
fix: use drop valueOf when evaluated as condition (#705)
1 parent 513940d commit a7da93f

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/render/boolean.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isTruthy, isFalsy } from './boolean'
22
import { Context } from '../context'
3+
import { Drop } from '..'
34

45
describe('boolean Shopify', function () {
56
describe('.isTruthy()', function () {
@@ -8,7 +9,13 @@ describe('boolean Shopify', function () {
89
jsTruthy: false
910
}
1011
} as unknown as Context
11-
//
12+
13+
class BooleanDrop extends Drop {
14+
public valueOf () {
15+
return false
16+
}
17+
}
18+
1219
// Spec: https://shopify.github.io/liquid/basics/truthy-and-falsy/
1320
it('true is truthy', function () {
1421
expect(isTruthy(true, ctx)).toBeTruthy()
@@ -40,6 +47,9 @@ describe('boolean Shopify', function () {
4047
it('[] is truthy', function () {
4148
expect(isTruthy([], ctx)).toBeTruthy()
4249
})
50+
it('drop valueOf determines truthy', function () {
51+
expect(isTruthy(new BooleanDrop(), ctx)).toBeFalsy()
52+
})
4353
})
4454
})
4555

src/render/boolean.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Context } from '../context/context'
2+
import { toValue } from '../util'
23

34
export function isTruthy (val: any, ctx: Context): boolean {
45
return !isFalsy(val, ctx)
56
}
67

78
export function isFalsy (val: any, ctx: Context): boolean {
9+
val = toValue(val)
10+
811
if (ctx.opts.jsTruthy) {
912
return !val
1013
} else {

test/integration/tags/if.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Liquid } from '../../../src/liquid'
1+
import { Liquid, Drop } from '../../../src'
22

33
describe('tags/if', function () {
44
const liquid = new Liquid()
@@ -9,6 +9,12 @@ describe('tags/if', function () {
99
emptyArray: []
1010
}
1111

12+
class BooleanDrop extends Drop {
13+
public valueOf () {
14+
return false
15+
}
16+
}
17+
1218
it('should throw if not closed', function () {
1319
const src = '{% if false%}yes'
1420
return expect(liquid.parseAndRender(src, scope))
@@ -143,6 +149,12 @@ describe('tags/if', function () {
143149
const html = await liquid.parseAndRender(src, scope)
144150
return expect(html).toBe('success')
145151
})
152+
it('should support drop as condition variable', async () => {
153+
const src = `{% if drop %}yes{% else %}no{% endif %}`
154+
const scope = { drop: new BooleanDrop() }
155+
const html = await liquid.parseAndRender(src, scope)
156+
return expect(html).toBe('no')
157+
})
146158
it('should not render anything after an else branch even when first else branch is empty', () => {
147159
const engine = new Liquid()
148160
const result = engine.parseAndRenderSync('{% if false %}don\'t show' +

0 commit comments

Comments
 (0)