fix: use drop valueOf when evaluated as condition (#705)

This commit is contained in:
Adam Tanner
2024-06-05 08:28:21 +08:00
committed by GitHub
parent 513940dfa7
commit a7da93ff0f
3 changed files with 27 additions and 2 deletions
+11 -1
View File
@@ -1,5 +1,6 @@
import { isTruthy, isFalsy } from './boolean' import { isTruthy, isFalsy } from './boolean'
import { Context } from '../context' import { Context } from '../context'
import { Drop } from '..'
describe('boolean Shopify', function () { describe('boolean Shopify', function () {
describe('.isTruthy()', function () { describe('.isTruthy()', function () {
@@ -8,7 +9,13 @@ describe('boolean Shopify', function () {
jsTruthy: false jsTruthy: false
} }
} as unknown as Context } as unknown as Context
//
class BooleanDrop extends Drop {
public valueOf () {
return false
}
}
// Spec: https://shopify.github.io/liquid/basics/truthy-and-falsy/ // Spec: https://shopify.github.io/liquid/basics/truthy-and-falsy/
it('true is truthy', function () { it('true is truthy', function () {
expect(isTruthy(true, ctx)).toBeTruthy() expect(isTruthy(true, ctx)).toBeTruthy()
@@ -40,6 +47,9 @@ describe('boolean Shopify', function () {
it('[] is truthy', function () { it('[] is truthy', function () {
expect(isTruthy([], ctx)).toBeTruthy() expect(isTruthy([], ctx)).toBeTruthy()
}) })
it('drop valueOf determines truthy', function () {
expect(isTruthy(new BooleanDrop(), ctx)).toBeFalsy()
})
}) })
}) })
+3
View File
@@ -1,10 +1,13 @@
import { Context } from '../context/context' import { Context } from '../context/context'
import { toValue } from '../util'
export function isTruthy (val: any, ctx: Context): boolean { export function isTruthy (val: any, ctx: Context): boolean {
return !isFalsy(val, ctx) return !isFalsy(val, ctx)
} }
export function isFalsy (val: any, ctx: Context): boolean { export function isFalsy (val: any, ctx: Context): boolean {
val = toValue(val)
if (ctx.opts.jsTruthy) { if (ctx.opts.jsTruthy) {
return !val return !val
} else { } else {
+13 -1
View File
@@ -1,4 +1,4 @@
import { Liquid } from '../../../src/liquid' import { Liquid, Drop } from '../../../src'
describe('tags/if', function () { describe('tags/if', function () {
const liquid = new Liquid() const liquid = new Liquid()
@@ -9,6 +9,12 @@ describe('tags/if', function () {
emptyArray: [] emptyArray: []
} }
class BooleanDrop extends Drop {
public valueOf () {
return false
}
}
it('should throw if not closed', function () { it('should throw if not closed', function () {
const src = '{% if false%}yes' const src = '{% if false%}yes'
return expect(liquid.parseAndRender(src, scope)) return expect(liquid.parseAndRender(src, scope))
@@ -143,6 +149,12 @@ describe('tags/if', function () {
const html = await liquid.parseAndRender(src, scope) const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('success') return expect(html).toBe('success')
}) })
it('should support drop as condition variable', async () => {
const src = `{% if drop %}yes{% else %}no{% endif %}`
const scope = { drop: new BooleanDrop() }
const html = await liquid.parseAndRender(src, scope)
return expect(html).toBe('no')
})
it('should not render anything after an else branch even when first else branch is empty', () => { it('should not render anything after an else branch even when first else branch is empty', () => {
const engine = new Liquid() const engine = new Liquid()
const result = engine.parseAndRenderSync('{% if false %}don\'t show' + const result = engine.parseAndRenderSync('{% if false %}don\'t show' +