Skip to content

Commit

Permalink
fix: break/continue omitting output before them, #123
Browse files Browse the repository at this point in the history
BREAKING CHANGE: remove default export, now should be used like import
{Liquid} from 'liquidjs'
  • Loading branch information
harttle committed Aug 26, 2019
1 parent 854e12b commit ae45c46
Show file tree
Hide file tree
Showing 99 changed files with 373 additions and 407 deletions.
8 changes: 3 additions & 5 deletions benchmark/demo.ts
@@ -1,7 +1,5 @@
import * as Benchmark from 'benchmark'
import Liquid from '../src/liquid'
import TagToken from '../src/parser/tag-token'
import Context from '../src/context/context'
import { Context, TagToken, Liquid } from '../src/liquid'

const engine = new Liquid({
root: __dirname,
Expand Down Expand Up @@ -34,13 +32,13 @@ const template = `
</ul>
`

export default function () {
export function demo () {
console.log('--- demo ---')
return new Promise(resolve => {
new Benchmark.Suite('demo')
.add('demo', {
defer: true,
fn: (d: any) => engine.parseAndRender(template, ctx).then(x => d.resolve(x))
fn: (d: any) => engine.parseAndRender(template, ctx).then((x: any) => d.resolve(x))
})
.on('cycle', (event: any) => console.log(String(event.target)))
.on('complete', resolve)
Expand Down
8 changes: 4 additions & 4 deletions benchmark/index.ts
@@ -1,7 +1,7 @@
import output from './output'
import tag from './tag'
import demo from './demo'
import layout from './layout'
import { output } from './output'
import { tag } from './tag'
import { demo } from './demo'
import { layout } from './layout'

async function main () {
await output()
Expand Down
6 changes: 3 additions & 3 deletions benchmark/layout.ts
Expand Up @@ -17,17 +17,17 @@ const template = `
{% block body %}a small body{% endblock %}
`

export default function () {
export function layout () {
console.log('--- layout ---')
return new Promise(resolve => {
new Benchmark.Suite('layout')
.add('cache=false', {
defer: true,
fn: (d: any) => engine.parseAndRender(template, {}).then(x => d.resolve(x))
fn: (d: any) => engine.parseAndRender(template, {}).then((x: any) => d.resolve(x))
})
.add('cache=true', {
defer: true,
fn: (d: any) => cachingEngine.parseAndRender(template, {}).then(x => d.resolve(x))
fn: (d: any) => cachingEngine.parseAndRender(template, {}).then((x: any) => d.resolve(x))
})
.on('cycle', (event: any) => console.log(String(event.target)))
.on('complete', resolve)
Expand Down
4 changes: 2 additions & 2 deletions benchmark/output.ts
Expand Up @@ -3,7 +3,7 @@ import Liquid from '../src/liquid'

const liquid = new Liquid()

export default function () {
export function output () {
console.log('--- output ---')
return new Promise(resolve => {
new Benchmark.Suite('output')
Expand All @@ -21,6 +21,6 @@ export default function () {
function test (str: string) {
return {
defer: true,
fn: (d: any) => liquid.parseAndRender(str).then(x => d.resolve(x))
fn: (d: any) => liquid.parseAndRender(str).then((x: any) => d.resolve(x))
}
}
4 changes: 2 additions & 2 deletions benchmark/tag.ts
Expand Up @@ -3,7 +3,7 @@ import Liquid from '../src/liquid'

const liquid = new Liquid()

export default function () {
export function tag () {
console.log('--- tag ---')
return new Promise(resolve => {
new Benchmark.Suite('tag')
Expand All @@ -25,6 +25,6 @@ export default function () {
function test (str: string) {
return {
defer: true,
fn: (d: any) => liquid.parseAndRender(str).then(x => d.resolve(x))
fn: (d: any) => liquid.parseAndRender(str).then((x: any) => d.resolve(x))
}
}
3 changes: 2 additions & 1 deletion demo/browser/index.html
Expand Up @@ -8,7 +8,8 @@

<body>
<script>
var engine = new window.Liquid({
var Liquid = window.liquidjs.Liquid
var engine = new Liquid({
extname: '.html',
cache: true
});
Expand Down
2 changes: 1 addition & 1 deletion demo/express/app.js
@@ -1,5 +1,5 @@
const express = require('express')
const Liquid = require('../..')
const { Liquid } = require('liquidjs')

const app = express()
const engine = new Liquid({
Expand Down
2 changes: 1 addition & 1 deletion demo/nodejs/index.js
@@ -1,4 +1,4 @@
const Liquid = require('liquidjs')
const { Liquid } = require('liquidjs')

const engine = new Liquid({
root: __dirname,
Expand Down
8 changes: 4 additions & 4 deletions demo/typescript/index.ts
@@ -1,17 +1,17 @@
import Liquid from 'liquidjs'
import { Liquid, TagToken, Hash, Context } from 'liquidjs'

const engine = new Liquid({
root: __dirname,
extname: '.liquid'
})

engine.registerTag('header', {
parse: function (token) {
parse: function (token: TagToken) {
const [key, val] = token.args.split(':')
this[key] = val
},
render: function (scope, hash) {
const title = this.liquid.evalValue(this['content'], scope)
render: function (context: Context, hash: Hash) {
const title = this.liquid.evalValue(this['content'], context)
return `<h1>${title}</h1>`
}
})
Expand Down
5 changes: 2 additions & 3 deletions rollup.config.ts
Expand Up @@ -18,7 +18,6 @@ const input = './src/liquid.ts'
const cjs = {
output: [{
file: 'dist/liquid.common.js',
name: 'Liquid',
format: 'cjs',
sourcemap,
banner
Expand All @@ -41,7 +40,7 @@ const cjs = {
const umd = {
output: [{
file: 'dist/liquid.js',
name: 'Liquid',
name: 'liquidjs',
format: 'umd',
sourcemap,
banner
Expand Down Expand Up @@ -70,7 +69,7 @@ const umd = {
const min = {
output: [{
file: 'dist/liquid.min.js',
name: 'Liquid',
name: 'liquidjs',
format: 'umd',
sourcemap
}],
Expand Down
8 changes: 4 additions & 4 deletions src/builtin/tags/assign.ts
@@ -1,8 +1,8 @@
import assert from '../../util/assert'
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 { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'

const re = new RegExp(`(${identifier.source})\\s*=([^]*)`)

Expand Down
12 changes: 6 additions & 6 deletions src/builtin/tags/block.ts
@@ -1,10 +1,10 @@
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 { 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'

export default {
parse: function (token: TagToken, remainTokens: Token[]) {
Expand Down
9 changes: 3 additions & 6 deletions src/builtin/tags/capture.ts
@@ -1,9 +1,6 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { ITemplate, Context, ITagImplOptions, TagToken, Token } from '../../types'

const re = new RegExp(`(${identifier.source})`)

Expand All @@ -17,7 +14,7 @@ export default {

const stream = this.liquid.parser.parseStream(remainTokens)
stream.on('tag:endcapture', () => stream.stop())
.on('template', (tpl) => this.templates.push(tpl))
.on('template', (tpl: ITemplate) => this.templates.push(tpl))
.on('end', () => {
throw new Error(`tag ${tagToken.raw} not closed`)
})
Expand Down
14 changes: 5 additions & 9 deletions src/builtin/tags/case.ts
@@ -1,10 +1,5 @@
import { Hash, Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { evalExp } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand All @@ -30,15 +25,16 @@ export default {
stream.start()
},

render: async function (ctx: Context) {
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)
if (val === cond) {
return this.liquid.renderer.renderTemplates(branch.templates, ctx)
this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
}
}
return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
6 changes: 3 additions & 3 deletions src/builtin/tags/comment.ts
@@ -1,6 +1,6 @@
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Token } from '../../parser/token'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand Down
8 changes: 4 additions & 4 deletions src/builtin/tags/cycle.ts
@@ -1,9 +1,9 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { value as rValue } from '../../parser/lexical'
import { evalValue } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'

const groupRE = new RegExp(`^(?:(${rValue.source})\\s*:\\s*)?(.*)$`)
const candidatesRE = new RegExp(rValue.source, 'g')
Expand Down
8 changes: 4 additions & 4 deletions src/builtin/tags/decrement.ts
@@ -1,8 +1,8 @@
import assert from '../../util/assert'
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 { TagToken } from '../../parser/tag-token'
import { Context } from '../../context/context'
import { ITagImplOptions } from '../../template/tag/itag-impl-options'
import { isNumber } from '../../util/underscore'

export default {
Expand Down
23 changes: 7 additions & 16 deletions src/builtin/tags/for.ts
@@ -1,15 +1,10 @@
import { Emitter, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'
import { isString, isObject, isArray } from '../../util/underscore'
import { parseExp } from '../../render/syntax'
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { identifier, value, hash } from '../../parser/lexical'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { ForloopDrop } from '../../drop/forloop-drop'
import { Hash } from '../../template/tag/hash'

const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
Expand Down Expand Up @@ -41,7 +36,7 @@ export default {

stream.start()
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = await parseExp(this.collection, ctx)

if (!isArray(collection)) {
Expand All @@ -63,20 +58,16 @@ export default {

const context = { forloop: new ForloopDrop(collection.length) }
ctx.push(context)
let html = ''
for (const item of collection) {
context[this.variable] = item
try {
html += await this.liquid.renderer.renderTemplates(this.templates, ctx)
await this.liquid.renderer.renderTemplates(this.templates, ctx, emitter)
} catch (e) {
if (e.name === 'RenderBreakError') {
html += e.resolvedHTML
if (e.message === 'break') break
} else throw e
if (e.name !== 'RenderBreakError') throw e
if (e.message === 'break') break
}
context.forloop.next()
}
ctx.pop()
return html
}
} as ITagImplOptions
15 changes: 5 additions & 10 deletions src/builtin/tags/if.ts
@@ -1,10 +1,4 @@
import { evalExp, isTruthy } from '../../render/syntax'
import TagToken from '../../parser/tag-token'
import Token from '../../parser/token'
import Context from '../../context/context'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { Hash, Emitter, evalExp, isTruthy, TagToken, Token, Context, ITemplate, ITagImplOptions, ParseStream } from '../../types'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand Down Expand Up @@ -33,13 +27,14 @@ export default {
stream.start()
},

render: async function (ctx: Context) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
for (const branch of this.branches) {
const cond = await evalExp(branch.cond, ctx)
if (isTruthy(cond)) {
return this.liquid.renderer.renderTemplates(branch.templates, ctx)
await this.liquid.renderer.renderTemplates(branch.templates, ctx, emitter)
return
}
}
return this.liquid.renderer.renderTemplates(this.elseTemplates, ctx)
await this.liquid.renderer.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
12 changes: 4 additions & 8 deletions src/builtin/tags/include.ts
@@ -1,11 +1,8 @@
import assert from '../../util/assert'
import { assert } from '../../util/assert'
import { 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'
import TagToken from '../../parser/tag-token'
import Context from '../../context/context'
import Hash from '../../template/tag/hash'
import ITagImplOptions from '../../template/tag/itag-impl-options'

const staticFileRE = /[^\s,]+/
const withRE = new RegExp(`with\\s+(${value.source})`)
Expand All @@ -21,7 +18,7 @@ export default {
match = withRE.exec(token.args)
if (match) this.with = match[1]
},
render: async function (ctx: Context, hash: Hash) {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
Expand All @@ -45,10 +42,9 @@ export default {
}
const templates = await this.liquid.getTemplate(filepath, ctx.opts)
ctx.push(hash)
const html = await this.liquid.renderer.renderTemplates(templates, ctx)
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
return html
}
} as ITagImplOptions

0 comments on commit ae45c46

Please sign in to comment.