mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
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:
+1
-2
@@ -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 { Drop } from './drop'
|
||||
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 { Value, Hash, Template, FilterImplOptions, Tag, Filter } from './template'
|
||||
export { Token, TopLevelToken, TagToken, ValueToken } from './tokens'
|
||||
|
||||
@@ -20,14 +20,14 @@ export class Expression {
|
||||
const result = yield evalOperatorToken(ctx.opts.operators, token, l, r, ctx)
|
||||
operands.push(result)
|
||||
} 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]
|
||||
}
|
||||
}
|
||||
|
||||
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 (isRangeToken(token)) return yield evalRangeToken(token, ctx)
|
||||
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> {
|
||||
const props: string[] = []
|
||||
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 {
|
||||
return yield ctx._get([token.propertyName, ...props])
|
||||
@@ -68,8 +68,8 @@ function evalLiteralToken (token: LiteralToken) {
|
||||
}
|
||||
|
||||
function * evalRangeToken (token: RangeToken, ctx: Context) {
|
||||
const low: number = yield _evalToken(token.lhs, ctx)
|
||||
const high: number = yield _evalToken(token.rhs, ctx)
|
||||
const low: number = yield evalToken(token.lhs, ctx)
|
||||
const high: number = yield evalToken(token.rhs, ctx)
|
||||
return range(+low, +high + 1)
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -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 {
|
||||
private cond: Value
|
||||
@@ -39,7 +39,7 @@ export default class extends Tag {
|
||||
const r = this.liquid.renderer
|
||||
const cond = toValue(yield this.cond.value(ctx, ctx.opts.lenientIf))
|
||||
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) {
|
||||
yield r.renderTemplates(branch.templates, ctx, emitter)
|
||||
return
|
||||
|
||||
+3
-3
@@ -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 {
|
||||
private candidates: ValueToken[] = []
|
||||
@@ -25,7 +25,7 @@ export default class extends Tag {
|
||||
}
|
||||
|
||||
* 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 groups = ctx.getRegister('cycle')
|
||||
let idx = groups[fingerprint]
|
||||
@@ -37,6 +37,6 @@ export default class extends Tag {
|
||||
const candidate = this.candidates[idx]
|
||||
idx = (idx + 1) % this.candidates.length
|
||||
groups[fingerprint] = idx
|
||||
return yield _evalToken(candidate, ctx)
|
||||
return yield evalToken(candidate, ctx)
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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 { ForloopDrop } from '../drop/forloop-drop'
|
||||
|
||||
@@ -43,7 +43,7 @@ export default class extends Tag {
|
||||
}
|
||||
* render (ctx: Context, emitter: Emitter): Generator<unknown, void | string, Template[]> {
|
||||
const r = this.liquid.renderer
|
||||
let collection = toEnumerable(yield _evalToken(this.collection, ctx))
|
||||
let collection = toEnumerable(yield evalToken(this.collection, ctx))
|
||||
|
||||
if (!collection.length) {
|
||||
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
|
||||
|
||||
+2
-2
@@ -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 { parseFilePath, renderFilePath } from './render'
|
||||
|
||||
@@ -33,7 +33,7 @@ export default class extends Tag {
|
||||
ctx.setRegister('blocks', {})
|
||||
ctx.setRegister('blockMode', BlockMode.OUTPUT)
|
||||
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[]
|
||||
ctx.push(ctx.opts.jekyllInclude ? { include: scope } : scope)
|
||||
yield renderer.renderTemplates(templates, ctx, emitter)
|
||||
|
||||
+4
-4
@@ -1,7 +1,7 @@
|
||||
import { __assign } from 'tslib'
|
||||
import { ForloopDrop } from '../drop'
|
||||
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
|
||||
|
||||
@@ -57,12 +57,12 @@ export default class extends Tag {
|
||||
__assign(scope, yield hash.render(ctx))
|
||||
if (this['with']) {
|
||||
const { value, alias } = this['with']
|
||||
scope[alias || filepath] = yield _evalToken(value, ctx)
|
||||
scope[alias || filepath] = yield evalToken(value, ctx)
|
||||
}
|
||||
|
||||
if (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)
|
||||
for (const item of collection) {
|
||||
scope[alias] = item
|
||||
@@ -109,5 +109,5 @@ function optimize (templates: Template[]): string | Template[] {
|
||||
export function * renderFilePath (file: ParsedFileName, ctx: Context, liquid: Liquid): IterableIterator<unknown> {
|
||||
if (typeof file === 'string') return file
|
||||
if (Array.isArray(file)) return liquid.renderer.renderTemplates(file, ctx)
|
||||
return yield _evalToken(file, ctx)
|
||||
return yield evalToken(file, ctx)
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
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 { Tokenizer } from '../parser/tokenizer'
|
||||
|
||||
@@ -39,7 +39,7 @@ export default class extends Tag {
|
||||
}
|
||||
|
||||
* 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 offset = hash.offset || 0
|
||||
const limit = (hash.limit === undefined) ? collection.length : hash.limit
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { _evalToken } from '../render'
|
||||
import { evalToken } from '../render'
|
||||
import { Context } from '../context'
|
||||
import { identify } from '../util/underscore'
|
||||
import { FilterImplOptions } from './filter-impl-options'
|
||||
@@ -20,8 +20,8 @@ export class Filter {
|
||||
public * render (value: any, context: Context): IterableIterator<unknown> {
|
||||
const argv: any[] = []
|
||||
for (const arg of this.args as FilterArg[]) {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield _evalToken(arg[1], context)])
|
||||
else argv.push(yield _evalToken(arg, context))
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||
else argv.push(yield evalToken(arg, context))
|
||||
}
|
||||
return this.impl.apply({ context, liquid: this.liquid }, [value, ...argv])
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { _evalToken } from '../render/expression'
|
||||
import { evalToken } from '../render/expression'
|
||||
import { Context } from '../context/context'
|
||||
import { Tokenizer } from '../parser/tokenizer'
|
||||
import { Token } from '../tokens/token'
|
||||
@@ -24,7 +24,7 @@ export class Hash {
|
||||
* render (ctx: Context): Generator<unknown, Record<string, any>, unknown> {
|
||||
const 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user