refactor: return value of Tag#render is no longer used

BREAKING CHANGE: Tag#render now returns void, use emitter argument
to write rendered html.
This commit is contained in:
harttle
2019-08-26 10:15:43 -05:00
committed by Jun Yang
parent 76019e9e18
commit 8028f82499
8 changed files with 26 additions and 38 deletions
+4 -9
View File
@@ -1,10 +1,5 @@
import BlockMode from '../../context/block-mode'
import { TagToken } from '../../parser/tag-token'
import { Token } from '../../parser/token'
import { ITemplate } from '../../template/itemplate'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { ParseStream } from '../../parser/parse-stream'
import { ParseStream, TagToken, Token, ITemplate, Context, ITagImplOptions, Emitter, Hash } from '../../types'
export default {
parse: function (token: TagToken, remainTokens: Token[]) {
@@ -19,7 +14,7 @@ export default {
})
stream.start()
},
render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const blocks = ctx.getRegister('blocks')
const childDefined = blocks[this.block]
const html = childDefined !== undefined
@@ -28,8 +23,8 @@ export default {
if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
return ''
return
}
return html
emitter.write(html)
}
} as ITagImplOptions
+3 -7
View File
@@ -1,9 +1,6 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { Expression } from '../../render/expression'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { Emitter, Expression, TagToken, Context, ITagImplOptions, Hash } from '../../types'
const groupRE = new RegExp(`^(?:(${rValue.source})\\s*:\\s*)?(.*)$`)
const candidatesRE = new RegExp(rValue.source, 'g')
@@ -24,7 +21,7 @@ export default {
assert(this.candidates.length, `empty candidates: ${tagToken.raw}`)
},
render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const group = this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
@@ -37,7 +34,6 @@ export default {
const candidate = this.candidates[idx]
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx
return new Expression(candidate).value(ctx)
emitter.write(new Expression(candidate).value(ctx))
}
} as ITagImplOptions
+4 -6
View File
@@ -1,9 +1,7 @@
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 { isNumber } from '../../util/underscore'
import { Emitter, TagToken, Context, ITagImplOptions, Hash } from '../../types'
import { isNumber, stringify } from '../../util/underscore'
export default {
parse: function (token: TagToken) {
@@ -11,11 +9,11 @@ export default {
assert(match, `illegal identifier ${token.args}`)
this.variable = match[0]
},
render: function (context: Context) {
render: function (context: Context, hash: Hash, emitter: Emitter) {
const scope = context.environments
if (!isNumber(scope[this.variable])) {
scope[this.variable] = 0
}
return --scope[this.variable]
emitter.write(stringify(--scope[this.variable]))
}
} as ITagImplOptions
+2 -1
View File
@@ -47,7 +47,8 @@ export default {
}
}
if (!isArray(collection) || !collection.length) {
return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
return
}
const offset = hash.offset || 0
+5 -5
View File
@@ -1,21 +1,21 @@
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import { isNumber } from '../../util/underscore'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { isNumber, stringify } from '../../util/underscore'
import { Emitter, TagToken, Context, ITagImplOptions, Hash } from '../../types'
export default {
parse: function (token) {
parse: function (token: TagToken) {
const match = token.args.match(identifier)
assert(match, `illegal identifier ${token.args}`)
this.variable = match![0]
},
render: function (context) {
render: function (context: Context, hash: Hash, emitter: Emitter) {
const scope = context.environments
if (!isNumber(scope[this.variable])) {
scope[this.variable] = 0
}
const val = scope[this.variable]
scope[this.variable]++
return val
emitter.write(stringify(val))
}
} as ITagImplOptions
+3 -4
View File
@@ -1,8 +1,7 @@
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { Expression, TagToken, Token, Context, ITagImplOptions } from '../../types'
import { Emitter, Hash, Expression, TagToken, Token, Context, ITagImplOptions } from '../../types'
import BlockMode from '../../context/block-mode'
import { Hash } from '../../template/tag/hash'
const staticFileRE = /\S+/
@@ -20,7 +19,7 @@ export default {
this.tpls = this.liquid.parser.parse(remainTokens)
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const layout = ctx.opts.dynamicPartials
? await (new Expression(this.layout).value(ctx))
: this.staticLayout
@@ -38,6 +37,6 @@ export default {
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = await this.liquid.renderer.renderTemplates(templates, ctx)
ctx.pop()
return partial
emitter.write(partial)
}
} as ITagImplOptions
+3 -3
View File
@@ -1,4 +1,4 @@
import { TagToken, Token, ITagImplOptions } from '../../types'
import { TagToken, Token, ITagImplOptions, Context, Emitter, Hash } from '../../types'
export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
@@ -15,7 +15,7 @@ export default {
})
stream.start()
},
render: function () {
return this.tokens.map((token: Token) => token.raw).join('')
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.write(this.tokens.map((token: Token) => token.raw).join(''))
}
} as ITagImplOptions
+2 -3
View File
@@ -1,4 +1,4 @@
import { stringify, isFunction } from '../../util/underscore'
import { isFunction } from '../../util/underscore'
import { assert } from '../../util/assert'
import { Liquid } from '../../liquid'
import { Template } from '../../template/template'
@@ -26,8 +26,7 @@ export class Tag extends Template<TagToken> implements ITemplate {
public async render (ctx: Context, emitter: Emitter) {
const hash = await Hash.create(this.token.args, ctx)
const impl = this.impl
const html = isFunction(impl.render) ? stringify(await impl.render(ctx, hash, emitter)) : ''
html && emitter.write(html)
if (isFunction(impl.render)) await impl.render(ctx, hash, emitter)
}
public static register (name: string, tag: ITagImplOptions) {
Tag.impls[name] = tag