Skip to content

Commit 3dc4290

Browse files
committedJul 7, 2022
fix: stack overflow on large number of templates, #513
1 parent 2f87708 commit 3dc4290

File tree

16 files changed

+173
-181
lines changed

16 files changed

+173
-181
lines changed
 

‎docs/themes/navy/layout/partial/all-contributors.swig

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<td align="center"><a href="https://github.com/ameyaapte1"><img src="https://avatars.githubusercontent.com/u/16054747?v=4?s=100" width="100px;" alt=""/></a></td>
5656
<td align="center"><a href="https://github.com/tbdrz"><img src="https://avatars.githubusercontent.com/u/50599116?v=4?s=100" width="100px;" alt=""/></a></td>
5757
<td align="center"><a href="http://santialbo.com"><img src="https://avatars.githubusercontent.com/u/1557563?v=4?s=100" width="100px;" alt=""/></a></td>
58+
<td align="center"><a href="https://github.com/YahangWu"><img src="https://avatars.githubusercontent.com/u/12295975?v=4?s=100" width="100px;" alt=""/></a></td>
5859
</tr>
5960
</table>
6061

‎src/cache/cache.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import type { Template } from '../template/template'
2+
13
export interface Cache<T> {
24
write (key: string, value: T): void | Promise<void>;
35
read (key: string): T | undefined | Promise<T | undefined>;
46
remove (key: string): void | Promise<void>;
57
}
8+
9+
export type LiquidCache = Cache<Template[] | Promise<Template[]>>

‎src/liquid-options.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { snakeCase, forOwn, isArray, isString, isFunction } from './util/underscore'
2-
import { Template } from './template/template'
3-
import { Cache } from './cache/cache'
2+
import { LiquidCache } from './cache/cache'
43
import { LRU } from './cache/lru'
54
import { FS } from './fs/fs'
65
import * as fs from './fs/node'
76
import { defaultOperators, Operators } from './render/operator'
87
import { createTrie, Trie } from './util/operator-trie'
9-
import { Thenable } from './util/async'
108
import * as builtinFilters from './builtin/filters'
119
import { assert, FilterImplOptions } from './types'
1210

@@ -32,7 +30,7 @@ export interface LiquidOptions {
3230
/** Add a extname (if filepath doesn't include one) before template file lookup. Eg: setting to `".html"` will allow including file by basename. Defaults to `""`. */
3331
extname?: string;
3432
/** Whether or not to cache resolved templates. Defaults to `false`. */
35-
cache?: boolean | number | Cache<Thenable<Template[]>>;
33+
cache?: boolean | number | LiquidCache;
3634
/** Use Javascript Truthiness. Defaults to `false`. */
3735
jsTruthy?: boolean;
3836
/** If set, treat the `filepath` parameter in `{%include filepath %}` and `{%layout filepath%}` as a variable, otherwise as a literal value. Defaults to `true`. */
@@ -104,7 +102,7 @@ interface NormalizedOptions extends LiquidOptions {
104102
root?: string[];
105103
partials?: string[];
106104
layouts?: string[];
107-
cache?: Cache<Thenable<Template[]>>;
105+
cache?: LiquidCache;
108106
outputEscape?: OutputEscape;
109107
operatorsTrie?: Trie;
110108
}
@@ -116,7 +114,7 @@ export interface NormalizedFullOptions extends NormalizedOptions {
116114
relativeReference: boolean;
117115
jekyllInclude: boolean;
118116
extname: string;
119-
cache: undefined | Cache<Thenable<Template[]>>;
117+
cache?: LiquidCache;
120118
jsTruthy: boolean;
121119
dynamicPartials: boolean;
122120
fs: FS;
@@ -180,7 +178,7 @@ export function normalize (options: LiquidOptions): NormalizedFullOptions {
180178
if (!options.hasOwnProperty('layouts')) options.layouts = options.root
181179
}
182180
if (options.hasOwnProperty('cache')) {
183-
let cache: Cache<Thenable<Template[]>> | undefined
181+
let cache: LiquidCache | undefined
184182
if (typeof options.cache === 'number') cache = options.cache > 0 ? new LRU(options.cache) : undefined
185183
else if (typeof options.cache === 'object') cache = options.cache
186184
else cache = options.cache ? new LRU(1024) : undefined

‎src/parser/parser.ts

+13-16
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import { Output } from '../template/output'
88
import { HTML } from '../template/html'
99
import { Template } from '../template/template'
1010
import { TopLevelToken } from '../tokens/toplevel-token'
11-
import { Cache } from '../cache/cache'
11+
import { LiquidCache } from '../cache/cache'
1212
import { Loader, LookupType } from '../fs/loader'
13+
import { toPromise } from '../util/async'
1314
import { FS } from '../fs/fs'
14-
import { toThenable, Thenable } from '../util/async'
1515

1616
export default class Parser {
1717
public parseFile: (file: string, sync?: boolean, type?: LookupType, currentFile?: string) => Generator<unknown, Template[], Template[] | string>
1818

1919
private liquid: Liquid
2020
private fs: FS
21-
private cache: Cache<Thenable<Template[]>> | undefined
21+
private cache?: LiquidCache
2222
private loader: Loader
2323

2424
public constructor (liquid: Liquid) {
@@ -58,21 +58,18 @@ export default class Parser {
5858
return new ParseStream(tokens, (token, tokens) => this.parseToken(token, tokens))
5959
}
6060
private * _parseFileCached (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string): Generator<unknown, Template[], Template[]> {
61-
const key = this.loader.shouldLoadRelative(file)
62-
? currentFile + ',' + file
63-
: type + ':' + file
64-
const tpls = yield this.cache!.read(key)
61+
const cache = this.cache!
62+
const key = this.loader.shouldLoadRelative(file) ? currentFile + ',' + file : type + ':' + file
63+
const tpls = yield cache.read(key)
6564
if (tpls) return tpls
6665

67-
const task = toThenable(this._parseFile(file, sync, type, currentFile))
68-
this.cache!.write(key, task)
69-
try {
70-
return yield task
71-
} catch (e) {
72-
// remove cached task if failed
73-
this.cache!.remove(key)
74-
}
75-
return []
66+
const task = this._parseFile(file, sync, type, currentFile)
67+
// sync mode: exec the task and cache the result
68+
// async mode: cache the task before exec
69+
const taskOrTpl = sync ? yield task : toPromise(task)
70+
cache.write(key, taskOrTpl as any)
71+
// note: concurrent tasks will be reused, cache for failed task is removed until its end
72+
try { return yield taskOrTpl } catch (err) { cache.remove(key); throw err }
7673
}
7774
private * _parseFile (file: string, sync?: boolean, type: LookupType = LookupType.Root, currentFile?: string): Generator<unknown, Template[], string> {
7875
const filepath = yield this.loader.lookup(file, type, sync, currentFile)

‎src/render/render.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { Template } from '../template/template'
44
import { Emitter } from '../emitters/emitter'
55
import { SimpleEmitter } from '../emitters/simple-emitter'
66
import { StreamedEmitter } from '../emitters/streamed-emitter'
7-
import { toThenable } from '../util/async'
7+
import { toPromise } from '../util/async'
88
import { KeepingTypeEmitter } from '../emitters/keeping-type-emitter'
99

1010
export class Render {
1111
public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream {
1212
const emitter = new StreamedEmitter()
13-
Promise.resolve().then(() => toThenable(this.renderTemplates(templates, ctx, emitter)))
13+
Promise.resolve().then(() => toPromise(this.renderTemplates(templates, ctx, emitter)))
1414
.then(() => emitter.end(), err => emitter.error(err))
1515
return emitter.stream
1616
}

‎src/util/async.ts

+39-57
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,6 @@ export interface Thenable<T> {
77
catch (reject: resolver): Thenable<T>;
88
}
99

10-
function createResolvedThenable<T> (value: T): Thenable<T> {
11-
const ret = {
12-
then: (resolve: resolver) => resolve(value),
13-
catch: () => ret
14-
}
15-
return ret
16-
}
17-
18-
function createRejectedThenable<T> (err: Error): Thenable<T> {
19-
const ret = {
20-
then: (resolve: resolver, reject?: resolver) => {
21-
if (reject) return reject(err)
22-
return ret
23-
},
24-
catch: (reject: resolver) => reject(err)
25-
}
26-
return ret
27-
}
28-
2910
function isThenable<T> (val: any): val is Thenable<T> {
3011
return val && isFunction(val.then)
3112
}
@@ -34,48 +15,49 @@ function isAsyncIterator (val: any): val is IterableIterator<any> {
3415
return val && isFunction(val.next) && isFunction(val.throw) && isFunction(val.return)
3516
}
3617

37-
// convert an async iterator to a thenable (Promise compatible)
38-
export function toThenable<T> (val: IteratorResult<unknown, T> | Thenable<T> | any): Thenable<T> {
39-
if (isThenable(val)) return val
40-
if (isAsyncIterator(val)) return reduce()
41-
return createResolvedThenable(val)
42-
43-
function reduce<T> (prev?: T): Thenable<T> {
44-
let state
18+
// convert an async iterator to a Promise
19+
export async function toPromise<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): Promise<T> {
20+
if (!isAsyncIterator(val)) return val
21+
let value: unknown
22+
let done = false
23+
let next = 'next'
24+
do {
25+
const state = val[next](value)
26+
done = state.done
27+
value = state.value
28+
next = 'next'
4529
try {
46-
state = val.next(prev)
30+
if (isAsyncIterator(value)) value = toPromise(value)
31+
if (isThenable(value)) value = await value
4732
} catch (err) {
48-
return createRejectedThenable(err as Error)
33+
next = 'throw'
34+
value = err
4935
}
50-
51-
if (state.done) return createResolvedThenable(state.value)
52-
return toThenable(state.value!).then(reduce, err => {
53-
let state
36+
} while (!done)
37+
return value as T
38+
}
39+
40+
// convert an async iterator to a value in a synchronous maner
41+
export function toValue<T> (val: Generator<unknown, T, unknown> | T): T {
42+
if (!isAsyncIterator(val)) return val
43+
let value: any
44+
let done = false
45+
let next = 'next'
46+
do {
47+
const state = val[next](value)
48+
done = state.done
49+
value = state.value
50+
next = 'next'
51+
if (isAsyncIterator(value)) {
5452
try {
55-
state = val.throw!(err)
56-
} catch (e) {
57-
return createRejectedThenable(e as Error)
53+
value = toValue(value)
54+
} catch (err) {
55+
next = 'throw'
56+
value = err
5857
}
59-
if (state.done) return createResolvedThenable(state.value)
60-
return reduce(state.value)
61-
})
62-
}
63-
}
64-
65-
export function toPromise<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): Promise<T> {
66-
return Promise.resolve(toThenable(val))
58+
}
59+
} while (!done)
60+
return value
6761
}
6862

69-
// get the value of async iterator in synchronous manner
70-
export function toValue<T> (val: Generator<unknown, T, unknown> | Thenable<T> | T): T {
71-
let ret: T
72-
toThenable(val)
73-
.then((x: any) => {
74-
ret = x
75-
return createResolvedThenable(ret)
76-
})
77-
.catch((err: Error) => {
78-
throw err
79-
})
80-
return ret!
81-
}
63+
export const toThenable = toPromise

‎test/e2e/issues.ts

+10
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,14 @@ describe('Issues', function () {
246246
const html = await engine.parseAndRender(`{% if template contains "product" %}contains{%endif%}`, ctx)
247247
expect(html).to.equal('contains')
248248
})
249+
it('#513 should support large number of templates [async]', async () => {
250+
const engine = new Liquid()
251+
const html = await engine.parseAndRender(`{% for i in (1..10000) %}{{ i }}{% endfor %}`)
252+
expect(html).to.have.lengthOf(38894)
253+
})
254+
it('#513 should support large number of templates [sync]', () => {
255+
const engine = new Liquid()
256+
const html = engine.parseAndRenderSync(`{% for i in (1..10000) %}{{ i }}{% endfor %}`)
257+
expect(html).to.have.lengthOf(38894)
258+
})
249259
})

‎test/integration/liquid/cache.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ describe('LiquidOptions#cache', function () {
126126
extname: '.html',
127127
cache: true
128128
})
129-
try { await engine.renderFile('foo') } catch (err) {}
129+
try {
130+
await engine.renderFile('foo')
131+
} catch (err) {}
130132

131133
mock({ '/root/foo.html': 'foo' })
132-
const y = await engine.renderFile('foo')
133-
expect(y).to.equal('foo')
134+
const html = await engine.renderFile('foo')
135+
expect(html).to.equal('foo')
134136
})
135137
})
136138

‎test/unit/render/expression.ts

+43-43
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Tokenizer } from '../../../src/parser/tokenizer'
22
import { expect } from 'chai'
33
import { Drop } from '../../../src/drop/drop'
44
import { Context } from '../../../src/context/context'
5-
import { toThenable } from '../../../src/util/async'
5+
import { toPromise } from '../../../src/util/async'
66
import { defaultOperators } from '../../../src/render/operator'
77
import { createTrie } from '../../../src/util/operator-trie'
88

@@ -12,7 +12,7 @@ describe('Expression', function () {
1212
const create = (str: string) => new Tokenizer(str, trie).readExpression()
1313

1414
it('should throw when context not defined', done => {
15-
toThenable(create('foo').evaluate(undefined!, false))
15+
toPromise(create('foo').evaluate(undefined!, false))
1616
.then(() => done(new Error('should not resolved')))
1717
.catch(err => {
1818
expect(err.message).to.match(/context not defined/)
@@ -22,19 +22,19 @@ describe('Expression', function () {
2222

2323
describe('single value', function () {
2424
it('should eval literal', async function () {
25-
expect(await toThenable(create('2.4').evaluate(ctx, false))).to.equal(2.4)
26-
expect(await toThenable(create('"foo"').evaluate(ctx, false))).to.equal('foo')
27-
expect(await toThenable(create('false').evaluate(ctx, false))).to.equal(false)
25+
expect(await toPromise(create('2.4').evaluate(ctx, false))).to.equal(2.4)
26+
expect(await toPromise(create('"foo"').evaluate(ctx, false))).to.equal('foo')
27+
expect(await toPromise(create('false').evaluate(ctx, false))).to.equal(false)
2828
})
2929
it('should eval range expression', async function () {
3030
const ctx = new Context({ two: 2 })
31-
expect(await toThenable(create('(2..4)').evaluate(ctx, false))).to.deep.equal([2, 3, 4])
32-
expect(await toThenable(create('(two..4)').evaluate(ctx, false))).to.deep.equal([2, 3, 4])
31+
expect(await toPromise(create('(2..4)').evaluate(ctx, false))).to.deep.equal([2, 3, 4])
32+
expect(await toPromise(create('(two..4)').evaluate(ctx, false))).to.deep.equal([2, 3, 4])
3333
})
3434
it('should eval literal', async function () {
35-
expect(await toThenable(create('2.4').evaluate(ctx, false))).to.equal(2.4)
36-
expect(await toThenable(create('"foo"').evaluate(ctx, false))).to.equal('foo')
37-
expect(await toThenable(create('false').evaluate(ctx, false))).to.equal(false)
35+
expect(await toPromise(create('2.4').evaluate(ctx, false))).to.equal(2.4)
36+
expect(await toPromise(create('"foo"').evaluate(ctx, false))).to.equal('foo')
37+
expect(await toPromise(create('false').evaluate(ctx, false))).to.equal(false)
3838
})
3939

4040
it('should eval property access', async function () {
@@ -43,119 +43,119 @@ describe('Expression', function () {
4343
coo: 'bar',
4444
doo: { foo: 'bar', bar: { foo: 'bar' } }
4545
})
46-
expect(await toThenable(create('foo.bar').evaluate(ctx, false))).to.equal('BAR')
47-
expect(await toThenable(create('foo["bar"]').evaluate(ctx, false))).to.equal('BAR')
48-
expect(await toThenable(create('foo[coo]').evaluate(ctx, false))).to.equal('BAR')
49-
expect(await toThenable(create('foo[doo.foo]').evaluate(ctx, false))).to.equal('BAR')
50-
expect(await toThenable(create('foo[doo["foo"]]').evaluate(ctx, false))).to.equal('BAR')
51-
expect(await toThenable(create('doo[coo].foo').evaluate(ctx, false))).to.equal('bar')
46+
expect(await toPromise(create('foo.bar').evaluate(ctx, false))).to.equal('BAR')
47+
expect(await toPromise(create('foo["bar"]').evaluate(ctx, false))).to.equal('BAR')
48+
expect(await toPromise(create('foo[coo]').evaluate(ctx, false))).to.equal('BAR')
49+
expect(await toPromise(create('foo[doo.foo]').evaluate(ctx, false))).to.equal('BAR')
50+
expect(await toPromise(create('foo[doo["foo"]]').evaluate(ctx, false))).to.equal('BAR')
51+
expect(await toPromise(create('doo[coo].foo').evaluate(ctx, false))).to.equal('bar')
5252
})
5353
})
5454

5555
describe('simple expression', function () {
5656
it('should return false for "1==2"', async () => {
57-
expect(await toThenable(create('1==2').evaluate(ctx, false))).to.equal(false)
57+
expect(await toPromise(create('1==2').evaluate(ctx, false))).to.equal(false)
5858
})
5959
it('should return true for "1<2"', async () => {
60-
expect(await toThenable(create('1<2').evaluate(ctx, false))).to.equal(true)
60+
expect(await toPromise(create('1<2').evaluate(ctx, false))).to.equal(true)
6161
})
6262
it('should return true for "1 < 2"', async () => {
63-
expect(await toThenable(create('1 < 2').evaluate(ctx, false))).to.equal(true)
63+
expect(await toPromise(create('1 < 2').evaluate(ctx, false))).to.equal(true)
6464
})
6565
it('should return true for "1 < 2"', async () => {
66-
expect(await toThenable(create('1 < 2').evaluate(ctx, false))).to.equal(true)
66+
expect(await toPromise(create('1 < 2').evaluate(ctx, false))).to.equal(true)
6767
})
6868
it('should return true for "2 <= 2"', async () => {
69-
expect(await toThenable(create('2 <= 2').evaluate(ctx, false))).to.equal(true)
69+
expect(await toPromise(create('2 <= 2').evaluate(ctx, false))).to.equal(true)
7070
})
7171
it('should return true for "one <= two"', async () => {
7272
const ctx = new Context({ one: 1, two: 2 })
73-
expect(await toThenable(create('one <= two').evaluate(ctx, false))).to.equal(true)
73+
expect(await toPromise(create('one <= two').evaluate(ctx, false))).to.equal(true)
7474
})
7575
it('should return false for "x contains "x""', async () => {
7676
const ctx = new Context({ x: 'XXX' })
77-
expect(await toThenable(create('x contains "x"').evaluate(ctx, false))).to.equal(false)
77+
expect(await toPromise(create('x contains "x"').evaluate(ctx, false))).to.equal(false)
7878
})
7979
it('should return true for "x contains "X""', async () => {
8080
const ctx = new Context({ x: 'XXX' })
81-
expect(await toThenable(create('x contains "X"').evaluate(ctx, false))).to.equal(true)
81+
expect(await toPromise(create('x contains "X"').evaluate(ctx, false))).to.equal(true)
8282
})
8383
it('should return false for "1 contains "x""', async () => {
8484
const ctx = new Context({ x: 'XXX' })
85-
expect(await toThenable(create('1 contains "x"').evaluate(ctx, false))).to.equal(false)
85+
expect(await toPromise(create('1 contains "x"').evaluate(ctx, false))).to.equal(false)
8686
})
8787
it('should return false for "y contains "x""', async () => {
8888
const ctx = new Context({ x: 'XXX' })
89-
expect(await toThenable(create('y contains "x"').evaluate(ctx, false))).to.equal(false)
89+
expect(await toPromise(create('y contains "x"').evaluate(ctx, false))).to.equal(false)
9090
})
9191
it('should return false for "z contains "x""', async () => {
9292
const ctx = new Context({ x: 'XXX' })
93-
expect(await toThenable(create('z contains "x"').evaluate(ctx, false))).to.equal(false)
93+
expect(await toPromise(create('z contains "x"').evaluate(ctx, false))).to.equal(false)
9494
})
9595
it('should return true for "(1..5) contains 3"', async () => {
9696
const ctx = new Context({ x: 'XXX' })
97-
expect(await toThenable(create('(1..5) contains 3').evaluate(ctx, false))).to.equal(true)
97+
expect(await toPromise(create('(1..5) contains 3').evaluate(ctx, false))).to.equal(true)
9898
})
9999
it('should return false for "(1..5) contains 6"', async () => {
100100
const ctx = new Context({ x: 'XXX' })
101-
expect(await toThenable(create('(1..5) contains 6').evaluate(ctx, false))).to.equal(false)
101+
expect(await toPromise(create('(1..5) contains 6').evaluate(ctx, false))).to.equal(false)
102102
})
103103
it('should return true for ""<=" == "<=""', async () => {
104-
expect(await toThenable(create('"<=" == "<="').evaluate(ctx, false))).to.equal(true)
104+
expect(await toPromise(create('"<=" == "<="').evaluate(ctx, false))).to.equal(true)
105105
})
106106
})
107107

108108
it('should allow space in quoted value', async function () {
109109
const ctx = new Context({ space: ' ' })
110-
expect(await toThenable(create('" " == space').evaluate(ctx, false))).to.equal(true)
110+
expect(await toPromise(create('" " == space').evaluate(ctx, false))).to.equal(true)
111111
})
112112

113113
describe('escape', () => {
114114
it('should escape quote', async function () {
115115
const ctx = new Context({ quote: '"' })
116-
expect(await toThenable(create('"\\"" == quote').evaluate(ctx, false))).to.equal(true)
116+
expect(await toPromise(create('"\\"" == quote').evaluate(ctx, false))).to.equal(true)
117117
})
118118
it('should escape square bracket', async function () {
119119
const ctx = new Context({ obj: { ']': 'bracket' } })
120-
expect(await toThenable(create('obj["]"] == "bracket"').evaluate(ctx, false))).to.equal(true)
120+
expect(await toPromise(create('obj["]"] == "bracket"').evaluate(ctx, false))).to.equal(true)
121121
})
122122
})
123123

124124
describe('complex expression', function () {
125125
it('should support value or value', async function () {
126-
expect(await toThenable(create('false or true').evaluate(ctx, false))).to.equal(true)
126+
expect(await toPromise(create('false or true').evaluate(ctx, false))).to.equal(true)
127127
})
128128
it('should support < and contains', async function () {
129-
expect(await toThenable(create('1 < 2 and x contains "x"').evaluate(ctx, false))).to.equal(false)
129+
expect(await toPromise(create('1 < 2 and x contains "x"').evaluate(ctx, false))).to.equal(false)
130130
})
131131
it('should support < or contains', async function () {
132-
expect(await toThenable(create('1 < 2 or x contains "x"').evaluate(ctx, false))).to.equal(true)
132+
expect(await toPromise(create('1 < 2 or x contains "x"').evaluate(ctx, false))).to.equal(true)
133133
})
134134
it('should support Drops for "x contains "x""', async () => {
135135
class TemplateDrop extends Drop {
136136
valueOf () { return 'X' }
137137
}
138138
const ctx = new Context({ x: 'XXX', X: new TemplateDrop() })
139-
expect(await toThenable(create('x contains X').evaluate(ctx, false))).to.equal(true)
139+
expect(await toPromise(create('x contains X').evaluate(ctx, false))).to.equal(true)
140140
})
141141
it('should support value and !=', async function () {
142142
const ctx = new Context({ empty: '' })
143-
expect(await toThenable(create('empty and empty != ""').evaluate(ctx, false))).to.equal(false)
143+
expect(await toPromise(create('empty and empty != ""').evaluate(ctx, false))).to.equal(false)
144144
})
145145
it('should recognize quoted value', async function () {
146-
expect(await toThenable(create('">"').evaluate(ctx, false))).to.equal('>')
146+
expect(await toPromise(create('">"').evaluate(ctx, false))).to.equal('>')
147147
})
148148
it('should evaluate from right to left', async function () {
149-
expect(await toThenable(create('true or false and false').evaluate(ctx, false))).to.equal(true)
150-
expect(await toThenable(create('true and false and false or true').evaluate(ctx, false))).to.equal(false)
149+
expect(await toPromise(create('true or false and false').evaluate(ctx, false))).to.equal(true)
150+
expect(await toPromise(create('true and false and false or true').evaluate(ctx, false))).to.equal(false)
151151
})
152152
it('should recognize property access', async function () {
153153
const ctx = new Context({ obj: { foo: true } })
154-
expect(await toThenable(create('obj["foo"] and true').evaluate(ctx, false))).to.equal(true)
154+
expect(await toPromise(create('obj["foo"] and true').evaluate(ctx, false))).to.equal(true)
155155
})
156156
it('should allow nested property access', async function () {
157157
const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } })
158-
expect(await toThenable(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).to.equal('FOO')
158+
expect(await toPromise(create('obj[keys["what\'s this"]]').evaluate(ctx, false))).to.equal('FOO')
159159
})
160160
})
161161
})

‎test/unit/render/render.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HTMLToken } from '../../../src/tokens/html-token'
44
import { Render } from '../../../src/render/render'
55
import { HTML } from '../../../src/template/html'
66
import { SimpleEmitter } from '../../../src/emitters/simple-emitter'
7-
import { toThenable } from '../../../src/util/async'
7+
import { toPromise } from '../../../src/util/async'
88
import { Tag } from '../../../src/template/tag/tag'
99
import { TagToken } from '../../../src/types'
1010

@@ -18,7 +18,7 @@ describe('render', function () {
1818
it('should render html', async function () {
1919
const scope = new Context()
2020
const token = { getContent: () => '<p>' } as HTMLToken
21-
const html = await toThenable(render.renderTemplates([new HTML(token)], scope, new SimpleEmitter()))
21+
const html = await toPromise(render.renderTemplates([new HTML(token)], scope, new SimpleEmitter()))
2222
return expect(html).to.equal('<p>')
2323
})
2424
})

‎test/unit/template/filter/filter.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as chai from 'chai'
22
import * as sinon from 'sinon'
33
import * as sinonChai from 'sinon-chai'
44
import { Context } from '../../../../src/context/context'
5-
import { toThenable } from '../../../../src/util/async'
5+
import { toPromise } from '../../../../src/util/async'
66
import { NumberToken } from '../../../../src/tokens/number-token'
77
import { QuotedToken } from '../../../../src/tokens/quoted-token'
88
import { IdentifierToken } from '../../../../src/tokens/identifier-token'
@@ -25,46 +25,46 @@ describe('filter', function () {
2525
})
2626

2727
it('should render input if filter not registered', async function () {
28-
expect(await toThenable(filters.create('undefined', []).render('foo', ctx))).to.equal('foo')
28+
expect(await toPromise(filters.create('undefined', []).render('foo', ctx))).to.equal('foo')
2929
})
3030

3131
it('should call filter impl with correct arguments', async function () {
3232
const spy = sinon.spy()
3333
filters.set('foo', spy)
3434
const thirty = new NumberToken(new IdentifierToken('30', 0, 2), undefined)
35-
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
35+
await toPromise(filters.create('foo', [thirty]).render('foo', ctx))
3636
expect(spy).to.have.been.calledWith('foo', 30)
3737
})
3838
it('should call filter impl with correct this', async function () {
3939
const spy = sinon.spy()
4040
filters.set('foo', spy)
4141
const thirty = new NumberToken(new IdentifierToken('33', 0, 2), undefined)
42-
await toThenable(filters.create('foo', [thirty]).render('foo', ctx))
42+
await toPromise(filters.create('foo', [thirty]).render('foo', ctx))
4343
expect(spy).to.have.been.calledOn(sinon.match.has('context', ctx))
4444
expect(spy).to.have.been.calledOn(sinon.match.has('liquid', liquid))
4545
})
4646
it('should render a simple filter', async function () {
4747
filters.set('upcase', x => x.toUpperCase())
48-
expect(await toThenable(filters.create('upcase', []).render('foo', ctx))).to.equal('FOO')
48+
expect(await toPromise(filters.create('upcase', []).render('foo', ctx))).to.equal('FOO')
4949
})
5050

5151
it('should render filters with argument', async function () {
5252
filters.set('add', (a, b) => a + b)
5353
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
54-
expect(await toThenable(filters.create('add', [two]).render(3, ctx))).to.equal(5)
54+
expect(await toPromise(filters.create('add', [two]).render(3, ctx))).to.equal(5)
5555
})
5656

5757
it('should render filters with multiple arguments', async function () {
5858
filters.set('add', (a, b, c) => a + b + c)
5959
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
6060
const c = new QuotedToken('"c"', 0, 3)
61-
expect(await toThenable(filters.create('add', [two, c]).render(3, ctx))).to.equal('5c')
61+
expect(await toPromise(filters.create('add', [two, c]).render(3, ctx))).to.equal('5c')
6262
})
6363

6464
it('should pass Objects/Drops as it is', async function () {
6565
filters.set('name', a => a.constructor.name)
6666
class Foo {}
67-
expect(await toThenable(filters.create('name', []).render(new Foo(), ctx))).to.equal('Foo')
67+
expect(await toPromise(filters.create('name', []).render(new Foo(), ctx))).to.equal('Foo')
6868
})
6969

7070
it('should not throw when filter name illegal', function () {
@@ -76,6 +76,6 @@ describe('filter', function () {
7676
it('should support key value pairs', async function () {
7777
filters.set('add', (a, b) => b[0] + ':' + (a + b[1]))
7878
const two = new NumberToken(new IdentifierToken('2', 0, 1), undefined)
79-
expect(await toThenable((filters.create('add', [['num', two]]).render(3, ctx)))).to.equal('num:5')
79+
expect(await toPromise((filters.create('add', [['num', two]]).render(3, ctx)))).to.equal('num:5')
8080
})
8181
})

‎test/unit/template/hash.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
import * as chai from 'chai'
2-
import { toThenable } from '../../../src/util/async'
2+
import { toPromise } from '../../../src/util/async'
33
import { Hash } from '../../../src/template/tag/hash'
44
import { Context } from '../../../src/context/context'
55

66
const expect = chai.expect
77

88
describe('Hash', function () {
99
it('should parse "reverse"', async function () {
10-
const hash = await toThenable(new Hash('reverse').render(new Context({ foo: 3 })))
10+
const hash = await toPromise(new Hash('reverse').render(new Context({ foo: 3 })))
1111
expect(hash).to.haveOwnProperty('reverse')
1212
expect(hash.reverse).to.be.true
1313
})
1414
it('should parse "num:foo"', async function () {
15-
const hash = await toThenable(new Hash('num:foo').render(new Context({ foo: 3 })))
15+
const hash = await toPromise(new Hash('num:foo').render(new Context({ foo: 3 })))
1616
expect(hash.num).to.equal(3)
1717
})
1818
it('should parse "num:3"', async function () {
19-
const hash = await toThenable(new Hash('num:3').render(new Context()))
19+
const hash = await toPromise(new Hash('num:3').render(new Context()))
2020
expect(hash.num).to.equal(3)
2121
})
2222
it('should parse "num: arr[0]"', async function () {
23-
const hash = await toThenable(new Hash('num:3').render(new Context({ arr: [3] })))
23+
const hash = await toPromise(new Hash('num:3').render(new Context({ arr: [3] })))
2424
expect(hash.num).to.equal(3)
2525
})
2626
it('should parse "num: 2.3"', async function () {
27-
const hash = await toThenable(new Hash('num:2.3').render(new Context()))
27+
const hash = await toPromise(new Hash('num:2.3').render(new Context()))
2828
expect(hash.num).to.equal(2.3)
2929
})
3030
it('should parse "num:bar.coo"', async function () {
3131
const pending = new Hash('num:bar.coo').render(new Context({ bar: { coo: 3 } }))
32-
const hash = await toThenable(pending)
32+
const hash = await toPromise(pending)
3333
expect(hash.num).to.equal(3)
3434
})
3535
it('should parse "num1:2.3 reverse,num2:bar.coo\n num3: arr[0]"', async function () {
3636
const ctx = new Context({ bar: { coo: 3 }, arr: [4] })
37-
const hash = await toThenable(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
37+
const hash = await toPromise(new Hash('num1:2.3 reverse,num2:bar.coo\n num3: arr[0]').render(ctx))
3838
expect(hash).to.deep.equal({
3939
num1: 2.3,
4040
reverse: true,

‎test/unit/template/output.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as chai from 'chai'
2-
import { toThenable } from '../../../src/util/async'
2+
import { toPromise } from '../../../src/util/async'
33
import { Context } from '../../../src/context/context'
44
import { Output } from '../../../src/template/output'
55
import { OutputToken } from '../../../src/tokens/output-token'
@@ -21,25 +21,25 @@ describe('Output', function () {
2121
foo: { obj: { arr: ['a', 2] } }
2222
})
2323
const output = new Output({ content: 'foo' } as OutputToken, liquid)
24-
await toThenable(output.render(scope, emitter))
24+
await toPromise(output.render(scope, emitter))
2525
return expect(emitter.html).to.equal('[object Object]')
2626
})
2727
it('should skip function property', async function () {
2828
const scope = new Context({ obj: { foo: 'foo', bar: (x: any) => x } })
2929
const output = new Output({ content: 'obj' } as OutputToken, liquid)
30-
await toThenable(output.render(scope, emitter))
30+
await toPromise(output.render(scope, emitter))
3131
return expect(emitter.html).to.equal('[object Object]')
3232
})
3333
it('should respect to .toString()', async () => {
3434
const scope = new Context({ obj: { toString: () => 'FOO' } })
3535
const output = new Output({ content: 'obj' } as OutputToken, liquid)
36-
await toThenable(output.render(scope, emitter))
36+
await toPromise(output.render(scope, emitter))
3737
return expect(emitter.html).to.equal('FOO')
3838
})
3939
it('should respect to .toString()', async () => {
4040
const scope = new Context({ obj: { toString: () => 'FOO' } })
4141
const output = new Output({ content: 'obj' } as OutputToken, liquid)
42-
await toThenable(output.render(scope, emitter))
42+
await toPromise(output.render(scope, emitter))
4343
return expect(emitter.html).to.equal('FOO')
4444
})
4545
context('when keepOutputType is enabled', () => {
@@ -62,31 +62,31 @@ describe('Output', function () {
6262
foo: 42
6363
}, { ...defaultOptions, keepOutputType: true })
6464
const output = new Output({ content: 'foo' } as OutputToken, liquid)
65-
await toThenable(output.render(scope, emitter))
65+
await toPromise(output.render(scope, emitter))
6666
return expect(emitter.html).to.equal(42)
6767
})
6868
it('should respect output variable boolean type', async () => {
6969
const scope = new Context({
7070
foo: true
7171
}, { ...defaultOptions, keepOutputType: true })
7272
const output = new Output({ content: 'foo' } as OutputToken, liquid)
73-
await toThenable(output.render(scope, emitter))
73+
await toPromise(output.render(scope, emitter))
7474
return expect(emitter.html).to.equal(true)
7575
})
7676
it('should respect output variable object type', async () => {
7777
const scope = new Context({
7878
foo: 'test'
7979
}, { ...defaultOptions, keepOutputType: true })
8080
const output = new Output({ content: 'foo' } as OutputToken, liquid)
81-
await toThenable(output.render(scope, emitter))
81+
await toPromise(output.render(scope, emitter))
8282
return expect(emitter.html).to.equal('test')
8383
})
8484
it('should respect output variable string type', async () => {
8585
const scope = new Context({
8686
foo: { a: { b: 42 } }
8787
}, { ...defaultOptions, keepOutputType: true })
8888
const output = new Output({ content: 'foo' } as OutputToken, liquid)
89-
await toThenable(output.render(scope, emitter))
89+
await toPromise(output.render(scope, emitter))
9090
return expect(emitter.html).to.deep.equal({ a: { b: 42 } })
9191
})
9292
})

‎test/unit/template/tag.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Context } from '../../../src/context/context'
44
import * as sinon from 'sinon'
55
import * as sinonChai from 'sinon-chai'
66
import { TagToken } from '../../../src/tokens/tag-token'
7-
import { toThenable } from '../../../src/util/async'
7+
import { toPromise } from '../../../src/util/async'
88

99
chai.use(sinonChai)
1010
const expect = chai.expect
@@ -20,7 +20,7 @@ describe('Tag', function () {
2020
args: '',
2121
name: 'foo'
2222
} as TagToken
23-
await toThenable(new Tag(token, [], {
23+
await toPromise(new Tag(token, [], {
2424
tags: {
2525
get: () => ({ render: spy })
2626
}

‎test/unit/template/value.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as chai from 'chai'
22
import { Liquid } from '../../../src/liquid'
33
import { QuotedToken } from '../../../src/tokens/quoted-token'
4-
import { toThenable } from '../../../src/util/async'
4+
import { toPromise } from '../../../src/util/async'
55
import * as sinonChai from 'sinon-chai'
66
import * as sinon from 'sinon'
77
import { Context } from '../../../src/context/context'
@@ -36,7 +36,7 @@ describe('Value', function () {
3636
const scope = new Context({
3737
foo: { bar: 'bar' }
3838
})
39-
await toThenable(tpl.value(scope, false))
39+
await toPromise(tpl.value(scope, false))
4040
expect(date).to.have.been.calledWith('bar', 'b')
4141
expect(time).to.have.been.calledWith('y', 2)
4242
})

‎test/unit/util/async.ts

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { toThenable, toPromise, toValue } from '../../../src/util/async'
1+
import { toPromise, toValue } from '../../../src/util/async'
22
import { expect, use } from 'chai'
33
import * as chaiAsPromised from 'chai-as-promised'
44

@@ -13,47 +13,45 @@ describe('utils/async', () => {
1313
const result = await toPromise(foo())
1414
expect(result).to.equal('foo')
1515
})
16-
})
17-
describe('#toThenable()', function () {
1816
it('should support iterable with single return statement', async () => {
1917
function * foo () {
2018
return 'foo'
2119
}
22-
const result = await toThenable(foo())
20+
const result = await toPromise(foo())
2321
expect(result).to.equal('foo')
2422
})
2523
it('should support promise', async () => {
2624
function foo () {
2725
return Promise.resolve('foo')
2826
}
29-
const result = await toThenable(foo())
27+
const result = await toPromise(foo())
3028
expect(result).to.equal('foo')
3129
})
3230
it('should resolve dependency', async () => {
33-
function * foo () {
31+
function * foo (): Generator<Generator<string>> {
3432
return yield bar()
3533
}
36-
function * bar () {
34+
function * bar (): Generator<string> {
3735
return 'bar'
3836
}
39-
const result = await toThenable(foo())
37+
const result = await toPromise(foo())
4038
expect(result).to.equal('bar')
4139
})
4240
it('should support promise dependency', async () => {
43-
function * foo () {
41+
function * foo (): Generator<Promise<string>> {
4442
return yield Promise.resolve('foo')
4543
}
46-
const result = await toThenable(foo())
44+
const result = await toPromise(foo())
4745
expect(result).to.equal('foo')
4846
})
4947
it('should reject Promise if dependency throws syncly', done => {
50-
function * foo () {
48+
function * foo (): Generator<Generator<never>> {
5149
return yield bar()
5250
}
53-
function * bar (): IterableIterator<any> {
51+
function * bar (): Generator<never> {
5452
throw new Error('bar')
5553
}
56-
toThenable(foo()).catch(err => {
54+
toPromise(foo()).catch(err => {
5755
expect(err.message).to.equal('bar')
5856
done()
5957
return 0 as any
@@ -70,43 +68,43 @@ describe('utils/async', () => {
7068
ret += 'foo'
7169
return ret
7270
}
73-
function * bar (): IterableIterator<any> {
71+
function * bar (): Generator<never> {
7472
throw new Error('bar')
7573
}
76-
const ret = await toThenable(foo())
74+
const ret = await toPromise(foo())
7775
expect(ret).to.equal('barfoo')
7876
})
7977
})
8078
describe('#toValue()', function () {
8179
it('should throw Error if dependency throws syncly', () => {
82-
function * foo () {
80+
function * foo (): Generator<Generator<never>> {
8381
return yield bar()
8482
}
85-
function * bar (): IterableIterator<any> {
83+
function * bar (): Generator<never> {
8684
throw new Error('bar')
8785
}
8886
expect(() => toValue(foo())).to.throw('bar')
8987
})
9088
it('should resume yield after catch', () => {
91-
function * foo () {
89+
function * foo (): Generator<unknown, never, never> {
9290
try {
9391
yield bar()
9492
} catch (e) {}
9593
return yield 'foo'
9694
}
97-
function * bar (): IterableIterator<any> {
95+
function * bar (): Generator<never> {
9896
throw new Error('bar')
9997
}
10098
expect(toValue(foo())).to.equal('foo')
10199
})
102100
it('should resume return after catch', () => {
103-
function * foo () {
101+
function * foo (): Generator<Generator<never>, string> {
104102
try {
105103
yield bar()
106104
} catch (e) {}
107105
return 'foo'
108106
}
109-
function * bar (): IterableIterator<any> {
107+
function * bar (): Generator<never> {
110108
throw new Error('bar')
111109
}
112110
expect(toValue(foo())).to.equal('foo')

0 commit comments

Comments
 (0)
Please sign in to comment.