Skip to content

Commit 941dd66

Browse files
authored
fix: enumerate Promises (e.g. in for & tablerow) (#237)
* fix: enumerate Promise<array> (e.g. in {% for ... %}) Previously, a Promise of an array was not being enumerated in {% for %}, for example. This is misleading since the library handles promises elsewhere (e.g. if you {% assign x = promiseArray %} and then {% for v in x %}, it worked just fine. This PR makes Promises of arrays handled by changing toEnumerable to handle then-ables. This affects other iterators, too, e.g. tablerow, so I put in a test for that as well.
1 parent 11ffd65 commit 941dd66

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/builtin/tags/for.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default {
3636
},
3737
render: function * (ctx: Context, emitter: Emitter) {
3838
const r = this.liquid.renderer
39-
let collection = toEnumerable(evalToken(this.collection, ctx))
39+
let collection = toEnumerable(yield evalToken(this.collection, ctx))
4040

4141
if (!collection.length) {
4242
yield r.renderTemplates(this.elseTemplates, ctx, emitter)

src/builtin/tags/tablerow.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
},
3131

3232
render: function * (ctx: Context, emitter: Emitter) {
33-
let collection = toEnumerable(evalToken(this.collection, ctx))
33+
let collection = toEnumerable(yield evalToken(this.collection, ctx))
3434
const hash = yield this.hash.render(ctx)
3535
const offset = hash.offset || 0
3636
const limit = (hash.limit === undefined) ? collection.length : hash.limit

test/integration/builtin/tags/for.ts

+8
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@ describe('tags/for', function () {
2020
nullProtoObj: Object.create(null),
2121
obj: { foo: 'bar', coo: 'haa' },
2222
alpha: ['a', 'b', 'c'],
23+
promiseArray: Promise.resolve(['a', 'b', 'c']),
2324
emptyArray: []
2425
}
2526
})
27+
2628
it('should support array', async function () {
2729
const src = '{%for c in alpha%}{{c}}{%endfor%}'
2830
const html = await liquid.parseAndRender(src, scope)
2931
return expect(html).to.equal('abc')
3032
})
3133

34+
it('should support promise of array', async function () {
35+
const src = '{%for c in promiseArray%}{{c}}{%endfor%}'
36+
const html = await liquid.parseAndRender(src, scope)
37+
return expect(html).to.equal('abc')
38+
})
39+
3240
it('should support object', async function () {
3341
const src = '{%for item in obj%}{{item[0]}},{{item[1]}}-{%else%}b{%endfor%}'
3442
const html = await liquid.parseAndRender(src, scope)

test/integration/builtin/tags/tablerow.ts

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ describe('tags/tablerow', function () {
1414
return expect(html).to.equal(dst)
1515
})
1616

17+
it('should support promises', async function () {
18+
const src = '{% tablerow i in promiseNumbers %}{{ i }}{% endtablerow %}'
19+
const ctx = {
20+
promiseNumbers: Promise.resolve([1,2,3])
21+
}
22+
const dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
23+
const html = await liquid.parseAndRender(src, ctx)
24+
return expect(html).to.equal(dst)
25+
})
26+
1727
it('should support cols', async function () {
1828
const src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
1929
const ctx = {

0 commit comments

Comments
 (0)