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

fix: use drop valueOf when evaluated as solitary condition #705

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 11 additions & 1 deletion src/render/boolean.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isTruthy, isFalsy } from './boolean'
import { Context } from '../context'
import { Drop } from '..'

describe('boolean Shopify', function () {
describe('.isTruthy()', function () {
Expand All @@ -8,7 +9,13 @@ describe('boolean Shopify', function () {
jsTruthy: false
}
} as unknown as Context
//

class BooleanDrop extends Drop {
public valueOf () {
return false
}
}

// Spec: https://shopify.github.io/liquid/basics/truthy-and-falsy/
it('true is truthy', function () {
expect(isTruthy(true, ctx)).toBeTruthy()
Expand Down Expand Up @@ -40,6 +47,9 @@ describe('boolean Shopify', function () {
it('[] is truthy', function () {
expect(isTruthy([], ctx)).toBeTruthy()
})
it('drop valueOf determines truthy', function () {
expect(isTruthy(new BooleanDrop(), ctx)).toBeFalsy()
})
})
})

Expand Down
3 changes: 3 additions & 0 deletions src/render/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Context } from '../context/context'
import { toValue } from '../util'

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

export function isFalsy (val: any, ctx: Context): boolean {
val = toValue(val)

if (ctx.opts.jsTruthy) {
return !val
} else {
Expand Down
14 changes: 13 additions & 1 deletion test/integration/tags/if.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Liquid } from '../../../src/liquid'
import { Liquid, Drop } from '../../../src'

describe('tags/if', function () {
const liquid = new Liquid()
Expand All @@ -9,6 +9,12 @@ describe('tags/if', function () {
emptyArray: []
}

class BooleanDrop extends Drop {
public valueOf () {
return false
}
}

it('should throw if not closed', function () {
const src = '{% if false%}yes'
return expect(liquid.parseAndRender(src, scope))
Expand Down Expand Up @@ -143,6 +149,12 @@ describe('tags/if', function () {
const html = await liquid.parseAndRender(src, scope)
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', () => {
const engine = new Liquid()
const result = engine.parseAndRenderSync('{% if false %}don\'t show' +
Expand Down
Loading