From 6a0ad102a899f276a06f9acaa651c5086784fdb9 Mon Sep 17 00:00:00 2001 From: Alex de Wergifosse Date: Tue, 15 Dec 2020 09:38:46 +1300 Subject: [PATCH] fix: address refactor comments --- src/builtin/tags/block.ts | 2 +- src/builtin/tags/capture.ts | 3 +-- src/builtin/tags/include.ts | 2 +- src/builtin/tags/layout.ts | 6 +++--- src/builtin/tags/render.ts | 2 +- src/render/emitter.ts | 7 +++++++ src/render/render.ts | 5 ++++- src/template/output.ts | 7 +------ 8 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/builtin/tags/block.ts b/src/builtin/tags/block.ts index 302381fddf..f8ad384bfe 100644 --- a/src/builtin/tags/block.ts +++ b/src/builtin/tags/block.ts @@ -20,7 +20,7 @@ export default { const r = this.liquid.renderer const html = childDefined !== undefined ? childDefined - : yield r.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType)) + : yield r.renderTemplates(this.tpls, ctx) if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) { blocks[this.block] = html diff --git a/src/builtin/tags/capture.ts b/src/builtin/tags/capture.ts index 3febc3a297..3b0aa1fd39 100644 --- a/src/builtin/tags/capture.ts +++ b/src/builtin/tags/capture.ts @@ -1,6 +1,5 @@ import { Tokenizer, assert, Template, Context, TagImplOptions, TagToken, TopLevelToken } from '../../types' import { evalQuotedToken } from '../../render/expression' -import { Emitter } from '../../render/emitter' export default { parse: function (tagToken: TagToken, remainTokens: TopLevelToken[]) { @@ -20,7 +19,7 @@ export default { }, render: function * (ctx: Context) { const r = this.liquid.renderer - const html = yield r.renderTemplates(this.templates, ctx, new Emitter(ctx.opts.keepOutputType)) + const html = yield r.renderTemplates(this.templates, ctx) ctx.bottom()[this.variable] = html } } as TagImplOptions diff --git a/src/builtin/tags/include.ts b/src/builtin/tags/include.ts index 1b67e7b98d..504f4eb83c 100644 --- a/src/builtin/tags/include.ts +++ b/src/builtin/tags/include.ts @@ -26,7 +26,7 @@ export default { const { renderer } = liquid const filepath = ctx.opts.dynamicPartials ? (TypeGuards.isQuotedToken(file) - ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) + ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx) : yield evalToken(file, ctx)) : file.getText() assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) diff --git a/src/builtin/tags/layout.ts b/src/builtin/tags/layout.ts index 374633c4ed..a27cf5a130 100644 --- a/src/builtin/tags/layout.ts +++ b/src/builtin/tags/layout.ts @@ -16,7 +16,7 @@ export default { const { renderer } = liquid const filepath = ctx.opts.dynamicPartials ? (TypeGuards.isQuotedToken(file) - ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) + ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx) : evalToken(this.file, ctx)) : file.getText() assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) @@ -24,12 +24,12 @@ export default { // render the remaining tokens immediately ctx.setRegister('blockMode', BlockMode.STORE) const blocks = ctx.getRegister('blocks') - const html = yield renderer.renderTemplates(this.tpls, ctx, new Emitter(ctx.opts.keepOutputType)) + const html = yield renderer.renderTemplates(this.tpls, ctx) if (blocks[''] === undefined) blocks[''] = html const templates = yield liquid._parseFile(filepath, ctx.opts, ctx.sync) ctx.push(yield hash.render(ctx)) ctx.setRegister('blockMode', BlockMode.OUTPUT) - const partial = yield renderer.renderTemplates(templates, ctx, new Emitter(ctx.opts.keepOutputType)) + const partial = yield renderer.renderTemplates(templates, ctx) ctx.pop() emitter.write(partial) } diff --git a/src/builtin/tags/render.ts b/src/builtin/tags/render.ts index 70e3a7b442..48d0c7f50e 100644 --- a/src/builtin/tags/render.ts +++ b/src/builtin/tags/render.ts @@ -44,7 +44,7 @@ export default { const { renderer } = liquid const filepath = ctx.opts.dynamicPartials ? (TypeGuards.isQuotedToken(file) - ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx, new Emitter(ctx.opts.keepOutputType)) + ? yield renderer.renderTemplates(liquid.parse(evalQuotedToken(file)), ctx) : evalToken(file, ctx)) : file.getText() assert(filepath, () => `illegal filename "${file.getText()}":"${filepath}"`) diff --git a/src/render/emitter.ts b/src/render/emitter.ts index 676fbc50fa..127c8faed0 100644 --- a/src/render/emitter.ts +++ b/src/render/emitter.ts @@ -1,3 +1,5 @@ +import { stringify, toValue } from '../util/underscore' + export class Emitter { public html: any = ''; public break = false; @@ -9,6 +11,11 @@ export class Emitter { } public write (html: any) { + if (this.keepOutputType === true) { + html = toValue(html) + } else { + html = stringify(toValue(html)) + } // This will only preserve the type if the value is isolated. // I.E: // {{ my-port }} -> 42 diff --git a/src/render/render.ts b/src/render/render.ts index 800765e24b..3fa32cd992 100644 --- a/src/render/render.ts +++ b/src/render/render.ts @@ -4,7 +4,10 @@ import { Template } from '../template/template' import { Emitter } from './emitter' export class Render { - public * renderTemplates (templates: Template[], ctx: Context, emitter: Emitter): IterableIterator { + public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator { + if (!emitter) { + emitter = new Emitter(ctx.opts.keepOutputType) + } for (const tpl of templates) { try { const html = yield tpl.render(ctx, emitter) diff --git a/src/template/output.ts b/src/template/output.ts index db102db8e1..d95fcdee5a 100644 --- a/src/template/output.ts +++ b/src/template/output.ts @@ -1,6 +1,5 @@ import { Value } from './value' import { FilterMap } from './filter/filter-map' -import { stringify, toValue } from '../util/underscore' import { TemplateImpl } from '../template/template-impl' import { Template } from '../template/template' import { Context } from '../context/context' @@ -16,10 +15,6 @@ export class Output extends TemplateImpl implements Template { } public * render (ctx: Context, emitter: Emitter) { const val = yield this.value.value(ctx) - if (ctx.opts.keepOutputType) { - emitter.write(toValue(val)) - } else { - emitter.write(stringify(toValue(val))) - } + emitter.write(val) } }