Skip to content

Commit 4e1a30a

Browse files
committed
refactor: _evalToken renamed to evalToken
BREAKING CHANGE: `evalToken` now returns a generator (LiquidJS async), which is different from `evalToken` in previous LiquidJS versions.
1 parent 9299268 commit 4e1a30a

File tree

10 files changed

+26
-27
lines changed

10 files changed

+26
-27
lines changed

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ export * as TypeGuards from './util/type-guards'
44
export { toValue, TimezoneDate, createTrie, Trie, toPromise, toValueSync, assert, ParseError, TokenizationError, AssertionError } from './util'
55
export { Drop } from './drop'
66
export { Emitter } from './emitters'
7-
// TODO change to _evalToken
8-
export { defaultOperators, Operators, _evalToken, evalQuotedToken, Expression, isFalsy, isTruthy } from './render'
7+
export { defaultOperators, Operators, evalToken, evalQuotedToken, Expression, isFalsy, isTruthy } from './render'
98
export { Context, Scope } from './context'
109
export { Value, Hash, Template, FilterImplOptions, Tag, Filter } from './template'
1110
export { Token, TopLevelToken, TagToken, ValueToken } from './tokens'

src/render/expression.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ export class Expression {
2020
const result = yield evalOperatorToken(ctx.opts.operators, token, l, r, ctx)
2121
operands.push(result)
2222
} else {
23-
operands.push(yield _evalToken(token, ctx, lenient && this.postfix.length === 1))
23+
operands.push(yield evalToken(token, ctx, lenient && this.postfix.length === 1))
2424
}
2525
}
2626
return operands[0]
2727
}
2828
}
2929

30-
export function * _evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> {
30+
export function * evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> {
3131
if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient)
3232
if (isRangeToken(token)) return yield evalRangeToken(token, ctx)
3333
if (isLiteralToken(token)) return evalLiteralToken(token)
@@ -39,7 +39,7 @@ export function * _evalToken (token: Token | undefined, ctx: Context, lenient =
3939
function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {
4040
const props: string[] = []
4141
for (const prop of token.props) {
42-
props.push((yield _evalToken(prop, ctx, false)) as unknown as string)
42+
props.push((yield evalToken(prop, ctx, false)) as unknown as string)
4343
}
4444
try {
4545
return yield ctx._get([token.propertyName, ...props])
@@ -68,8 +68,8 @@ function evalLiteralToken (token: LiteralToken) {
6868
}
6969

7070
function * evalRangeToken (token: RangeToken, ctx: Context) {
71-
const low: number = yield _evalToken(token.lhs, ctx)
72-
const high: number = yield _evalToken(token.rhs, ctx)
71+
const low: number = yield evalToken(token.lhs, ctx)
72+
const high: number = yield evalToken(token.rhs, ctx)
7373
return range(+low, +high + 1)
7474
}
7575

src/tags/case.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ValueToken, Liquid, Tokenizer, toValue, _evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..'
1+
import { ValueToken, Liquid, Tokenizer, toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..'
22

33
export default class extends Tag {
44
private cond: Value
@@ -39,7 +39,7 @@ export default class extends Tag {
3939
const r = this.liquid.renderer
4040
const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf))
4141
for (const branch of this.cases) {
42-
const val = yield _evalToken(branch.val, ctx, ctx.opts.lenientIf)
42+
const val = yield evalToken(branch.val, ctx, ctx.opts.lenientIf)
4343
if (val === cond) {
4444
yield r.renderTemplates(branch.templates, ctx, emitter)
4545
return

src/tags/cycle.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Tokenizer, assert, TopLevelToken, Liquid, ValueToken, _evalToken, Emitter, TagToken, Context, Tag } from '..'
1+
import { Tokenizer, assert, TopLevelToken, Liquid, ValueToken, evalToken, Emitter, TagToken, Context, Tag } from '..'
22

33
export default class extends Tag {
44
private candidates: ValueToken[] = []
@@ -25,7 +25,7 @@ export default class extends Tag {
2525
}
2626

2727
* render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, unknown> {
28-
const group = (yield _evalToken(this.group, ctx)) as ValueToken
28+
const group = (yield evalToken(this.group, ctx)) as ValueToken
2929
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
3030
const groups = ctx.getRegister('cycle')
3131
let idx = groups[fingerprint]
@@ -37,6 +37,6 @@ export default class extends Tag {
3737
const candidate = this.candidates[idx]
3838
idx = (idx + 1) % this.candidates.length
3939
groups[fingerprint] = idx
40-
return yield _evalToken(candidate, ctx)
40+
return yield evalToken(candidate, ctx)
4141
}
4242
}

src/tags/for.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hash, ValueToken, Liquid, Tag, Tokenizer, _evalToken, Emitter, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
1+
import { Hash, ValueToken, Liquid, Tag, Tokenizer, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
22
import { toEnumerable } from '../util/collection'
33
import { ForloopDrop } from '../drop/forloop-drop'
44

@@ -43,7 +43,7 @@ export default class extends Tag {
4343
}
4444
* render (ctx: Context, emitter: Emitter): Generator<unknown, void | string, Template[]> {
4545
const r = this.liquid.renderer
46-
let collection = toEnumerable(yield _evalToken(this.collection, ctx))
46+
let collection = toEnumerable(yield evalToken(this.collection, ctx))
4747

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

src/tags/include.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Template, ValueToken, TopLevelToken, Liquid, Tag, assert, Tokenizer, _evalToken, Hash, Emitter, TagToken, Context } from '..'
1+
import { Template, ValueToken, TopLevelToken, Liquid, Tag, assert, Tokenizer, evalToken, Hash, Emitter, TagToken, Context } from '..'
22
import { BlockMode, Scope } from '../context'
33
import { parseFilePath, renderFilePath } from './render'
44

@@ -33,7 +33,7 @@ export default class extends Tag {
3333
ctx.setRegister('blocks', {})
3434
ctx.setRegister('blockMode', BlockMode.OUTPUT)
3535
const scope = (yield hash.render(ctx)) as Scope
36-
if (withVar) scope[filepath] = yield _evalToken(withVar, ctx)
36+
if (withVar) scope[filepath] = yield evalToken(withVar, ctx)
3737
const templates = (yield liquid._parsePartialFile(filepath, ctx.sync, this['currentFile'])) as Template[]
3838
ctx.push(ctx.opts.jekyllInclude ? { include: scope } : scope)
3939
yield renderer.renderTemplates(templates, ctx, emitter)

src/tags/render.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { __assign } from 'tslib'
22
import { ForloopDrop } from '../drop'
33
import { toEnumerable } from '../util'
4-
import { TopLevelToken, assert, Liquid, Token, Template, evalQuotedToken, TypeGuards, Tokenizer, _evalToken, Hash, Emitter, TagToken, Context, Tag } from '..'
4+
import { TopLevelToken, assert, Liquid, Token, Template, evalQuotedToken, TypeGuards, Tokenizer, evalToken, Hash, Emitter, TagToken, Context, Tag } from '..'
55

66
export type ParsedFileName = Template[] | Token | string | undefined
77

@@ -57,12 +57,12 @@ export default class extends Tag {
5757
__assign(scope, yield hash.render(ctx))
5858
if (this['with']) {
5959
const { value, alias } = this['with']
60-
scope[alias || filepath] = yield _evalToken(value, ctx)
60+
scope[alias || filepath] = yield evalToken(value, ctx)
6161
}
6262

6363
if (this['for']) {
6464
const { value, alias } = this['for']
65-
const collection = toEnumerable(yield _evalToken(value, ctx))
65+
const collection = toEnumerable(yield evalToken(value, ctx))
6666
scope['forloop'] = new ForloopDrop(collection.length, value.getText(), alias)
6767
for (const item of collection) {
6868
scope[alias] = item
@@ -109,5 +109,5 @@ function optimize (templates: Template[]): string | Template[] {
109109
export function * renderFilePath (file: ParsedFileName, ctx: Context, liquid: Liquid): IterableIterator<unknown> {
110110
if (typeof file === 'string') return file
111111
if (Array.isArray(file)) return liquid.renderer.renderTemplates(file, ctx)
112-
return yield _evalToken(file, ctx)
112+
return yield evalToken(file, ctx)
113113
}

src/tags/tablerow.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { toEnumerable } from '../util/collection'
2-
import { ValueToken, Liquid, Tag, _evalToken, Emitter, Hash, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
2+
import { ValueToken, Liquid, Tag, evalToken, Emitter, Hash, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
33
import { TablerowloopDrop } from '../drop/tablerowloop-drop'
44
import { Tokenizer } from '../parser/tokenizer'
55

@@ -39,7 +39,7 @@ export default class extends Tag {
3939
}
4040

4141
* render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
42-
let collection = toEnumerable(yield _evalToken(this.collection, ctx))
42+
let collection = toEnumerable(yield evalToken(this.collection, ctx))
4343
const hash = (yield this.hash.render(ctx)) as Record<string, any>
4444
const offset = hash.offset || 0
4545
const limit = (hash.limit === undefined) ? collection.length : hash.limit

src/template/filter.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _evalToken } from '../render'
1+
import { evalToken } from '../render'
22
import { Context } from '../context'
33
import { identify } from '../util/underscore'
44
import { FilterImplOptions } from './filter-impl-options'
@@ -20,8 +20,8 @@ export class Filter {
2020
public * render (value: any, context: Context): IterableIterator<unknown> {
2121
const argv: any[] = []
2222
for (const arg of this.args as FilterArg[]) {
23-
if (isKeyValuePair(arg)) argv.push([arg[0], yield _evalToken(arg[1], context)])
24-
else argv.push(yield _evalToken(arg, context))
23+
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
24+
else argv.push(yield evalToken(arg, context))
2525
}
2626
return this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
2727
}

src/template/hash.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { _evalToken } from '../render/expression'
1+
import { evalToken } from '../render/expression'
22
import { Context } from '../context/context'
33
import { Tokenizer } from '../parser/tokenizer'
44
import { Token } from '../tokens/token'
@@ -24,7 +24,7 @@ export class Hash {
2424
* render (ctx: Context): Generator<unknown, Record<string, any>, unknown> {
2525
const hash = {}
2626
for (const key of Object.keys(this.hash)) {
27-
hash[key] = this.hash[key] === undefined ? true : yield _evalToken(this.hash[key], ctx)
27+
hash[key] = this.hash[key] === undefined ? true : yield evalToken(this.hash[key], ctx)
2828
}
2929
return hash
3030
}

0 commit comments

Comments
 (0)