refactor: rewrite expression evaluation, fix #130

This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 538610e169
commit 76019e9e18
31 changed files with 407 additions and 246 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
import { isArray, last } from '../../util/underscore'
import { isTruthy } from '../../render/syntax'
import { isTruthy } from '../../render/boolean'
export default {
'join': (v: any[], arg: string) => v.join(arg === undefined ? ' ' : arg),
+1 -1
View File
@@ -1,4 +1,4 @@
import { isFalsy } from '../../render/syntax'
import { isFalsy } from '../../render/boolean'
import { toValue } from '../../util/underscore'
export default {
+1 -3
View File
@@ -1,8 +1,6 @@
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { ITagImplOptions, TagToken, Context } from '../../types'
const re = new RegExp(`(${identifier.source})\\s*=([^]*)`)
+3 -4
View File
@@ -1,5 +1,4 @@
import { Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { evalExp } from '../../render/syntax'
import { Expression, Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -28,8 +27,8 @@ export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = await evalExp(branch.val, ctx)
const cond = await evalExp(this.cond, ctx)
const val = new Expression(branch.val).value(ctx)
const cond = new Expression(this.cond).value(ctx)
if (val === cond) {
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
+4 -4
View File
@@ -1,6 +1,6 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import { Expression } from '../../render/expression'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
@@ -13,7 +13,7 @@ export default {
let match: RegExpExecArray | null = groupRE.exec(tagToken.args) as RegExpExecArray
assert(match, `illegal tag: ${tagToken.raw}`)
this.group = match[1] || ''
this.group = new Expression(match[1])
const candidates = match[2]
this.candidates = []
@@ -25,7 +25,7 @@ export default {
},
render: async function (ctx: Context) {
const group = await evalValue(this.group, ctx)
const group = this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]
@@ -38,6 +38,6 @@ export default {
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx
return evalValue(candidate, ctx)
return new Expression(candidate).value(ctx)
}
} as ITagImplOptions
+2 -2
View File
@@ -1,6 +1,6 @@
import { Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { isString, isObject, isArray } from '../../util/underscore'
import { parseExp } from '../../render/syntax'
import { Expression } from '../../render/expression'
import { assert } from '../../util/assert'
import { identifier, value, hash } from '../../parser/lexical'
import { ForloopDrop } from '../../drop/forloop-drop'
@@ -37,7 +37,7 @@ export default {
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await parseExp(this.collection, ctx)
let collection = new Expression(this.collection).value(ctx)
if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
+2 -2
View File
@@ -1,4 +1,4 @@
import { Hash, Emitter, evalExp, isTruthy, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { Hash, Emitter, isTruthy, Expression, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -29,7 +29,7 @@ export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (const branch of this.branches) {
const cond = await evalExp(branch.cond, ctx)
const cond = new Expression(branch.cond).value(ctx)
if (isTruthy(cond)) {
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
+3 -4
View File
@@ -1,7 +1,6 @@
import { assert } from '../../util/assert'
import { Hash, Emitter, TagToken, Context, ITagImplOptions } from '../../types'
import { Expression, Hash, Emitter, TagToken, Context, ITagImplOptions } from '../../types'
import { value, quotedLine } from '../../parser/lexical'
import { evalValue, parseValue } from '../../render/syntax'
import BlockMode from '../../context/block-mode'
const staticFileRE = /[^\s,]+/
@@ -25,7 +24,7 @@ export default {
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
} else {
filepath = await evalValue(this.value, ctx)
filepath = new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
@@ -38,7 +37,7 @@ export default {
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = await parseValue(this.with, ctx)
hash[filepath] = new Expression(this.with).evaluate(ctx)
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
+2 -3
View File
@@ -1,7 +1,6 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import { TagToken, Token, Context, ITagImplOptions } from '../../types'
import { Expression, TagToken, Token, Context, ITagImplOptions } from '../../types'
import BlockMode from '../../context/block-mode'
import { Hash } from '../../template/tag/hash'
@@ -23,7 +22,7 @@ export default {
},
render: async function (ctx: Context, hash: Hash) {
const layout = ctx.opts.dynamicPartials
? await evalValue(this.layout, ctx)
? await (new Expression(this.layout).value(ctx))
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)
+2 -2
View File
@@ -1,5 +1,5 @@
import { assert } from '../../util/assert'
import { evalExp, Emitter, Hash, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { Expression, Emitter, Hash, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { identifier, value, hash } from '../../parser/lexical'
import { TablerowloopDrop } from '../../drop/tablerowloop-drop'
@@ -29,7 +29,7 @@ export default {
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await evalExp(this.collection, ctx) || []
let collection = new Expression(this.collection).value(ctx) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit
+2 -2
View File
@@ -1,4 +1,4 @@
import { Emitter, evalExp, isFalsy, ParseStream, Context, ITagImplOptions, Token, Hash, TagToken } from '../../types'
import { Emitter, Expression, isFalsy, ParseStream, Context, ITagImplOptions, Token, Hash, TagToken } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -21,7 +21,7 @@ export default {
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const cond = await evalExp(this.cond, ctx)
const cond = new Expression(this.cond).value(ctx)
isFalsy(cond)
? await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
: await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)