refactor: _evalToken renamed to evalToken

BREAKING CHANGE: `evalToken` now returns a generator (LiquidJS async), which is different from `evalToken` in previous LiquidJS versions.
This commit is contained in:
Jun Yang
2022-11-27 14:04:02 +08:00
parent 92992689cd
commit 4e1a30a20c
10 changed files with 26 additions and 27 deletions
+1 -2
View File
@@ -4,8 +4,7 @@ export * as TypeGuards from './util/type-guards'
export { toValue, TimezoneDate, createTrie, Trie, toPromise, toValueSync, assert, ParseError, TokenizationError, AssertionError } from './util' export { toValue, TimezoneDate, createTrie, Trie, toPromise, toValueSync, assert, ParseError, TokenizationError, AssertionError } from './util'
export { Drop } from './drop' export { Drop } from './drop'
export { Emitter } from './emitters' export { Emitter } from './emitters'
// TODO change to _evalToken export { defaultOperators, Operators, evalToken, evalQuotedToken, Expression, isFalsy, isTruthy } from './render'
export { defaultOperators, Operators, _evalToken, evalQuotedToken, Expression, isFalsy, isTruthy } from './render'
export { Context, Scope } from './context' export { Context, Scope } from './context'
export { Value, Hash, Template, FilterImplOptions, Tag, Filter } from './template' export { Value, Hash, Template, FilterImplOptions, Tag, Filter } from './template'
export { Token, TopLevelToken, TagToken, ValueToken } from './tokens' export { Token, TopLevelToken, TagToken, ValueToken } from './tokens'
+5 -5
View File
@@ -20,14 +20,14 @@ export class Expression {
const result = yield evalOperatorToken(ctx.opts.operators, token, l, r, ctx) const result = yield evalOperatorToken(ctx.opts.operators, token, l, r, ctx)
operands.push(result) operands.push(result)
} else { } else {
operands.push(yield _evalToken(token, ctx, lenient && this.postfix.length === 1)) operands.push(yield evalToken(token, ctx, lenient && this.postfix.length === 1))
} }
} }
return operands[0] return operands[0]
} }
} }
export function * _evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> { export function * evalToken (token: Token | undefined, ctx: Context, lenient = false): IterableIterator<unknown> {
if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient) if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient)
if (isRangeToken(token)) return yield evalRangeToken(token, ctx) if (isRangeToken(token)) return yield evalRangeToken(token, ctx)
if (isLiteralToken(token)) return evalLiteralToken(token) if (isLiteralToken(token)) return evalLiteralToken(token)
@@ -39,7 +39,7 @@ export function * _evalToken (token: Token | undefined, ctx: Context, lenient =
function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> { function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {
const props: string[] = [] const props: string[] = []
for (const prop of token.props) { for (const prop of token.props) {
props.push((yield _evalToken(prop, ctx, false)) as unknown as string) props.push((yield evalToken(prop, ctx, false)) as unknown as string)
} }
try { try {
return yield ctx._get([token.propertyName, ...props]) return yield ctx._get([token.propertyName, ...props])
@@ -68,8 +68,8 @@ function evalLiteralToken (token: LiteralToken) {
} }
function * evalRangeToken (token: RangeToken, ctx: Context) { function * evalRangeToken (token: RangeToken, ctx: Context) {
const low: number = yield _evalToken(token.lhs, ctx) const low: number = yield evalToken(token.lhs, ctx)
const high: number = yield _evalToken(token.rhs, ctx) const high: number = yield evalToken(token.rhs, ctx)
return range(+low, +high + 1) return range(+low, +high + 1)
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { ValueToken, Liquid, Tokenizer, toValue, _evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..' import { ValueToken, Liquid, Tokenizer, toValue, evalToken, Value, Emitter, TagToken, TopLevelToken, Context, Template, Tag, ParseStream } from '..'
export default class extends Tag { export default class extends Tag {
private cond: Value private cond: Value
@@ -39,7 +39,7 @@ export default class extends Tag {
const r = this.liquid.renderer const r = this.liquid.renderer
const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf)) const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf))
for (const branch of this.cases) { for (const branch of this.cases) {
const val = yield _evalToken(branch.val, ctx, ctx.opts.lenientIf) const val = yield evalToken(branch.val, ctx, ctx.opts.lenientIf)
if (val === cond) { if (val === cond) {
yield r.renderTemplates(branch.templates, ctx, emitter) yield r.renderTemplates(branch.templates, ctx, emitter)
return return
+3 -3
View File
@@ -1,4 +1,4 @@
import { Tokenizer, assert, TopLevelToken, Liquid, ValueToken, _evalToken, Emitter, TagToken, Context, Tag } from '..' import { Tokenizer, assert, TopLevelToken, Liquid, ValueToken, evalToken, Emitter, TagToken, Context, Tag } from '..'
export default class extends Tag { export default class extends Tag {
private candidates: ValueToken[] = [] private candidates: ValueToken[] = []
@@ -25,7 +25,7 @@ export default class extends Tag {
} }
* render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, unknown> { * render (ctx: Context, emitter: Emitter): Generator<unknown, unknown, unknown> {
const group = (yield _evalToken(this.group, ctx)) as ValueToken const group = (yield evalToken(this.group, ctx)) as ValueToken
const fingerprint = `cycle:${group}:` + this.candidates.join(',') const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle') const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint] let idx = groups[fingerprint]
@@ -37,6 +37,6 @@ export default class extends Tag {
const candidate = this.candidates[idx] const candidate = this.candidates[idx]
idx = (idx + 1) % this.candidates.length idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx groups[fingerprint] = idx
return yield _evalToken(candidate, ctx) return yield evalToken(candidate, ctx)
} }
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { Hash, ValueToken, Liquid, Tag, Tokenizer, _evalToken, Emitter, TagToken, TopLevelToken, Context, Template, ParseStream } from '..' import { Hash, ValueToken, Liquid, Tag, Tokenizer, evalToken, Emitter, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
import { toEnumerable } from '../util/collection' import { toEnumerable } from '../util/collection'
import { ForloopDrop } from '../drop/forloop-drop' import { ForloopDrop } from '../drop/forloop-drop'
@@ -43,7 +43,7 @@ export default class extends Tag {
} }
* render (ctx: Context, emitter: Emitter): Generator<unknown, void | string, Template[]> { * render (ctx: Context, emitter: Emitter): Generator<unknown, void | string, Template[]> {
const r = this.liquid.renderer const r = this.liquid.renderer
let collection = toEnumerable(yield _evalToken(this.collection, ctx)) let collection = toEnumerable(yield evalToken(this.collection, ctx))
if (!collection.length) { if (!collection.length) {
yield r.renderTemplates(this.elseTemplates, ctx, emitter) yield r.renderTemplates(this.elseTemplates, ctx, emitter)
+2 -2
View File
@@ -1,4 +1,4 @@
import { Template, ValueToken, TopLevelToken, Liquid, Tag, assert, Tokenizer, _evalToken, Hash, Emitter, TagToken, Context } from '..' import { Template, ValueToken, TopLevelToken, Liquid, Tag, assert, Tokenizer, evalToken, Hash, Emitter, TagToken, Context } from '..'
import { BlockMode, Scope } from '../context' import { BlockMode, Scope } from '../context'
import { parseFilePath, renderFilePath } from './render' import { parseFilePath, renderFilePath } from './render'
@@ -33,7 +33,7 @@ export default class extends Tag {
ctx.setRegister('blocks', {}) ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT) ctx.setRegister('blockMode', BlockMode.OUTPUT)
const scope = (yield hash.render(ctx)) as Scope const scope = (yield hash.render(ctx)) as Scope
if (withVar) scope[filepath] = yield _evalToken(withVar, ctx) if (withVar) scope[filepath] = yield evalToken(withVar, ctx)
const templates = (yield liquid._parsePartialFile(filepath, ctx.sync, this['currentFile'])) as Template[] const templates = (yield liquid._parsePartialFile(filepath, ctx.sync, this['currentFile'])) as Template[]
ctx.push(ctx.opts.jekyllInclude ? { include: scope } : scope) ctx.push(ctx.opts.jekyllInclude ? { include: scope } : scope)
yield renderer.renderTemplates(templates, ctx, emitter) yield renderer.renderTemplates(templates, ctx, emitter)
+4 -4
View File
@@ -1,7 +1,7 @@
import { __assign } from 'tslib' import { __assign } from 'tslib'
import { ForloopDrop } from '../drop' import { ForloopDrop } from '../drop'
import { toEnumerable } from '../util' import { toEnumerable } from '../util'
import { TopLevelToken, assert, Liquid, Token, Template, evalQuotedToken, TypeGuards, Tokenizer, _evalToken, Hash, Emitter, TagToken, Context, Tag } from '..' import { TopLevelToken, assert, Liquid, Token, Template, evalQuotedToken, TypeGuards, Tokenizer, evalToken, Hash, Emitter, TagToken, Context, Tag } from '..'
export type ParsedFileName = Template[] | Token | string | undefined export type ParsedFileName = Template[] | Token | string | undefined
@@ -57,12 +57,12 @@ export default class extends Tag {
__assign(scope, yield hash.render(ctx)) __assign(scope, yield hash.render(ctx))
if (this['with']) { if (this['with']) {
const { value, alias } = this['with'] const { value, alias } = this['with']
scope[alias || filepath] = yield _evalToken(value, ctx) scope[alias || filepath] = yield evalToken(value, ctx)
} }
if (this['for']) { if (this['for']) {
const { value, alias } = this['for'] const { value, alias } = this['for']
const collection = toEnumerable(yield _evalToken(value, ctx)) const collection = toEnumerable(yield evalToken(value, ctx))
scope['forloop'] = new ForloopDrop(collection.length, value.getText(), alias) scope['forloop'] = new ForloopDrop(collection.length, value.getText(), alias)
for (const item of collection) { for (const item of collection) {
scope[alias] = item scope[alias] = item
@@ -109,5 +109,5 @@ function optimize (templates: Template[]): string | Template[] {
export function * renderFilePath (file: ParsedFileName, ctx: Context, liquid: Liquid): IterableIterator<unknown> { export function * renderFilePath (file: ParsedFileName, ctx: Context, liquid: Liquid): IterableIterator<unknown> {
if (typeof file === 'string') return file if (typeof file === 'string') return file
if (Array.isArray(file)) return liquid.renderer.renderTemplates(file, ctx) if (Array.isArray(file)) return liquid.renderer.renderTemplates(file, ctx)
return yield _evalToken(file, ctx) return yield evalToken(file, ctx)
} }
+2 -2
View File
@@ -1,5 +1,5 @@
import { toEnumerable } from '../util/collection' import { toEnumerable } from '../util/collection'
import { ValueToken, Liquid, Tag, _evalToken, Emitter, Hash, TagToken, TopLevelToken, Context, Template, ParseStream } from '..' import { ValueToken, Liquid, Tag, evalToken, Emitter, Hash, TagToken, TopLevelToken, Context, Template, ParseStream } from '..'
import { TablerowloopDrop } from '../drop/tablerowloop-drop' import { TablerowloopDrop } from '../drop/tablerowloop-drop'
import { Tokenizer } from '../parser/tokenizer' import { Tokenizer } from '../parser/tokenizer'
@@ -39,7 +39,7 @@ export default class extends Tag {
} }
* render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> { * render (ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
let collection = toEnumerable(yield _evalToken(this.collection, ctx)) let collection = toEnumerable(yield evalToken(this.collection, ctx))
const hash = (yield this.hash.render(ctx)) as Record<string, any> const hash = (yield this.hash.render(ctx)) as Record<string, any>
const offset = hash.offset || 0 const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit const limit = (hash.limit === undefined) ? collection.length : hash.limit
+3 -3
View File
@@ -1,4 +1,4 @@
import { _evalToken } from '../render' import { evalToken } from '../render'
import { Context } from '../context' import { Context } from '../context'
import { identify } from '../util/underscore' import { identify } from '../util/underscore'
import { FilterImplOptions } from './filter-impl-options' import { FilterImplOptions } from './filter-impl-options'
@@ -20,8 +20,8 @@ export class Filter {
public * render (value: any, context: Context): IterableIterator<unknown> { public * render (value: any, context: Context): IterableIterator<unknown> {
const argv: any[] = [] const argv: any[] = []
for (const arg of this.args as FilterArg[]) { for (const arg of this.args as FilterArg[]) {
if (isKeyValuePair(arg)) argv.push([arg[0], yield _evalToken(arg[1], context)]) if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
else argv.push(yield _evalToken(arg, context)) else argv.push(yield evalToken(arg, context))
} }
return this.impl.apply({ context, liquid: this.liquid }, [value, ...argv]) return this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { _evalToken } from '../render/expression' import { evalToken } from '../render/expression'
import { Context } from '../context/context' import { Context } from '../context/context'
import { Tokenizer } from '../parser/tokenizer' import { Tokenizer } from '../parser/tokenizer'
import { Token } from '../tokens/token' import { Token } from '../tokens/token'
@@ -24,7 +24,7 @@ export class Hash {
* render (ctx: Context): Generator<unknown, Record<string, any>, unknown> { * render (ctx: Context): Generator<unknown, Record<string, any>, unknown> {
const hash = {} const hash = {}
for (const key of Object.keys(this.hash)) { for (const key of Object.keys(this.hash)) {
hash[key] = this.hash[key] === undefined ? true : yield _evalToken(this.hash[key], ctx) hash[key] = this.hash[key] === undefined ? true : yield evalToken(this.hash[key], ctx)
} }
return hash return hash
} }