Skip to content

Commit

Permalink
refactor: _evalToken renamed to evalToken
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `evalToken` now returns a generator (LiquidJS async), which is different from `evalToken` in previous LiquidJS versions.
  • Loading branch information
harttle committed Nov 27, 2022
1 parent 9299268 commit 4e1a30a
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 27 deletions.
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 5 additions & 5 deletions src/render/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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])
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/tags/case.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/tags/cycle.ts
Original file line number Diff line number Diff line change
@@ -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[] = []
Expand All @@ -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]
Expand All @@ -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)
}
}
4 changes: 2 additions & 2 deletions src/tags/for.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/tags/include.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/tags/render.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions src/tags/tablerow.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/template/filter.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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])
}
Expand Down
4 changes: 2 additions & 2 deletions src/template/hash.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
}
Expand Down

0 comments on commit 4e1a30a

Please sign in to comment.