Skip to content

Commit

Permalink
feat: promise support for drops, working on #65
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 10, 2019
1 parent ad0930f commit 4a8088d
Show file tree
Hide file tree
Showing 25 changed files with 260 additions and 250 deletions.
5 changes: 2 additions & 3 deletions src/builtin/tags/assign.ts
Expand Up @@ -14,10 +14,9 @@ export default {
this.key = match[1]
this.value = match[2]
},
render: function (scope: Scope) {
render: async function (scope: Scope) {
const ctx = new AssignScope()
ctx[this.key] = this.liquid.evalValue(this.value, scope)
ctx[this.key] = await this.liquid.evalValue(this.value, scope)
scope.push(ctx)
return Promise.resolve('')
}
} as ITagImplOptions
6 changes: 3 additions & 3 deletions src/builtin/tags/case.ts
Expand Up @@ -30,11 +30,11 @@ export default {
stream.start()
},

render: function (scope: Scope) {
render: async function (scope: Scope) {
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = evalExp(branch.val, scope)
const cond = evalExp(this.cond, scope)
const val = await evalExp(branch.val, scope)
const cond = await evalExp(this.cond, scope)
if (val === cond) {
return this.liquid.renderer.renderTemplates(branch.templates, scope)
}
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/cycle.ts
Expand Up @@ -24,8 +24,8 @@ export default <ITagImplOptions>{
assert(this.candidates.length, `empty candidates: ${tagToken.raw}`)
},

render: function (scope: Scope) {
const group = evalValue(this.group, scope)
render: async function (scope: Scope) {
const group = await evalValue(this.group, scope)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = scope.groups
let idx = groups[fingerprint]
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/for.ts
Expand Up @@ -42,7 +42,7 @@ export default <ITagImplOptions>{
stream.start()
},
render: async function (scope: Scope, hash: Hash) {
let collection = evalExp(this.collection, scope)
let collection = await evalExp(this.collection, scope)

if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/if.ts
Expand Up @@ -33,9 +33,9 @@ export default {
stream.start()
},

render: function (scope: Scope) {
render: async function (scope: Scope) {
for (const branch of this.branches) {
const cond = evalExp(branch.cond, scope)
const cond = await evalExp(branch.cond, scope)
if (isTruthy(cond)) {
return this.liquid.renderer.renderTemplates(branch.templates, scope)
}
Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/include.ts
Expand Up @@ -34,7 +34,7 @@ export default <ITagImplOptions>{
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, scope.getAll(), scope.opts)
} else {
filepath = evalValue(this.value, scope)
filepath = await evalValue(this.value, scope)
}
} else {
filepath = this.staticValue
Expand All @@ -47,7 +47,7 @@ export default <ITagImplOptions>{
scope.blocks = {}
scope.blockMode = BlockMode.OUTPUT
if (this.with) {
hash[filepath] = evalValue(this.with, scope)
hash[filepath] = await evalValue(this.with, scope)
}
const templates = await this.liquid.getTemplate(filepath, scope.opts)
scope.push(hash)
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/layout.ts
Expand Up @@ -26,7 +26,7 @@ export default {
},
render: async function (scope: Scope, hash: Hash) {
const layout = scope.opts.dynamicPartials
? evalValue(this.layout, scope)
? await evalValue(this.layout, scope)
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)

Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/tablerow.ts
Expand Up @@ -36,7 +36,7 @@ export default {
},

render: async function (scope: Scope, hash: Hash) {
let collection = evalExp(this.collection, scope) || []
let collection = await evalExp(this.collection, scope) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit

Expand Down
4 changes: 2 additions & 2 deletions src/builtin/tags/unless.ts
Expand Up @@ -25,8 +25,8 @@ export default {
stream.start()
},

render: function (scope: Scope) {
const cond = evalExp(this.cond, scope)
render: async function (scope: Scope) {
const cond = await evalExp(this.cond, scope)
return isFalsy(cond)
? this.liquid.renderer.renderTemplates(this.templates, scope)
: this.liquid.renderer.renderTemplates(this.elseTemplates, scope)
Expand Down
2 changes: 1 addition & 1 deletion src/drop/drop.ts
Expand Up @@ -3,7 +3,7 @@ export abstract class Drop {
return undefined
}

liquidMethodMissing (key: string): string | undefined {
liquidMethodMissing (key: string): Promise<string | undefined> | string | undefined {
return undefined
}
}
22 changes: 11 additions & 11 deletions src/render/syntax.ts
Expand Up @@ -48,36 +48,36 @@ const binaryOperators: {[key: string]: (lhs: any, rhs: any) => boolean} = {
'or': (l: any, r: any) => isTruthy(l) || isTruthy(r)
}

export function parseExp (exp: string, scope: Scope): any {
export async function parseExp (exp: string, scope: Scope): Promise<any> {
assert(scope, 'unable to parseExp: scope undefined')
const operatorREs = lexical.operators
let match
for (let i = 0; i < operatorREs.length; i++) {
const operatorRE = operatorREs[i]
const expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`)
if ((match = exp.match(expRE))) {
const l = parseExp(match[1], scope)
const l = await parseExp(match[1], scope)
const op = binaryOperators[match[2].trim()]
const r = parseExp(match[3], scope)
const r = await parseExp(match[3], scope)
return op(l, r)
}
}

if ((match = exp.match(lexical.rangeLine))) {
const low = evalValue(match[1], scope)
const high = evalValue(match[2], scope)
return range(low, high + 1)
const low = await evalValue(match[1], scope)
const high = await evalValue(match[2], scope)
return range(+low, +high + 1)
}

return parseValue(exp, scope)
}

export function evalExp (str: string, scope: Scope): any {
const value = parseExp(str, scope)
export async function evalExp (str: string, scope: Scope): Promise<any> {
const value = await parseExp(str, scope)
return value instanceof Drop ? value.valueOf() : value
}

function parseValue (str: string | undefined, scope: Scope): any {
async function parseValue (str: string | undefined, scope: Scope): Promise<any> {
if (!str) return null
str = str.trim()

Expand All @@ -91,8 +91,8 @@ function parseValue (str: string | undefined, scope: Scope): any {
return scope.get(str)
}

export function evalValue (str: string | undefined, scope: Scope): any {
const value = parseValue(str, scope)
export async function evalValue (str: string | undefined, scope: Scope) {
const value = await parseValue(str, scope)
return value instanceof Drop ? value.valueOf() : value
}

Expand Down
10 changes: 10 additions & 0 deletions src/scope/context.ts
@@ -0,0 +1,10 @@
import { Drop } from '../drop/drop'

type PlainObject = {
[key: string]: any
liquid_method_missing?: (key: string) => any // eslint-disable-line
to_liquid?: () => any // eslint-disable-line
toLiquid?: () => any // eslint-disable-line
}

export type Context = PlainObject | Drop
38 changes: 16 additions & 22 deletions src/scope/scope.ts
Expand Up @@ -4,13 +4,7 @@ import { __assign } from 'tslib'
import assert from '../util/assert'
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
import BlockMode from './block-mode'

export type Context = {
[key: string]: any
liquid_method_missing?: (key: string) => any // eslint-disable-line
to_liquid?: () => any // eslint-disable-line
toLiquid?: () => any // eslint-disable-line
}
import { Context } from './context'

export default class Scope {
opts: NormalizedFullOptions
Expand All @@ -25,19 +19,19 @@ export default class Scope {
getAll () {
return this.contexts.reduce((ctx, val) => __assign(ctx, val), {})
}
get (path: string): any {
const paths = this.propertyAccessSeq(path)
const scope = this.findContextFor(paths[0]) || _.last(this.contexts)
return paths.reduce((value, key) => {
const val = this.readProperty(value, key)
if (_.isNil(val) && this.opts.strictVariables) {
throw new TypeError(`undefined variable: ${key}`)
async get (path: string) {
const paths = await this.propertyAccessSeq(path)
let ctx = this.findContextFor(paths[0]) || _.last(this.contexts)
for (let path of paths) {
ctx = this.readProperty(ctx, path)
if (_.isNil(ctx) && this.opts.strictVariables) {
throw new TypeError(`undefined variable: ${path}`)
}
return val
}, scope)
}
return ctx
}
set (path: string, v: any): void {
const paths = this.propertyAccessSeq(path)
async set (path: string, v: any) {
const paths = await this.propertyAccessSeq(path)
let scope = this.findContextFor(paths[0]) || _.last(this.contexts)
paths.some((key, i) => {
if (!_.isObject(scope)) {
Expand Down Expand Up @@ -99,7 +93,7 @@ export default class Scope {
* accessSeq("foo['b]r']") // ['foo', 'b]r']
* accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar'
*/
propertyAccessSeq (str: string) {
async propertyAccessSeq (str: string) {
str = String(str)
const seq: string[] = []
let name = ''
Expand All @@ -122,7 +116,7 @@ export default class Scope {
assert(j !== -1, `unbalanced []: ${str}`)
name = str.slice(i + 1, j)
if (!/^[+-]?\d+$/.test(name)) { // foo[bar] vs. foo[1]
name = String(this.get(name))
name = String(await this.get(name))
}
push()
i = j + 1
Expand Down Expand Up @@ -152,9 +146,9 @@ export default class Scope {
}

function readSize (obj: Context) {
if (!_.isNil(obj.size)) return obj.size
if (!_.isNil(obj['size'])) return obj['size']
if (_.isArray(obj) || _.isString(obj)) return obj.length
return obj.size
return obj['size']
}

function matchRightBracket (str: string, begin: number) {
Expand Down
10 changes: 7 additions & 3 deletions src/template/filter/filter.ts
Expand Up @@ -19,9 +19,13 @@ export class Filter {
this.impl = impl || (x => x)
this.args = args
}
render (value: any, scope: Scope): any {
const args = this.args.map(arg => isArray(arg) ? [arg[0], evalValue(arg[1], scope)] : evalValue(arg, scope))
return this.impl.apply(null, [value, ...args])
async render (value: any, scope: Scope) {
const argv: any[] = []
for(let arg of this.args) {
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
else argv.push(await evalValue(arg, scope))
}
return this.impl.apply(null, [value, ...argv])
}
static register (name: string, filter: FilterImpl) {
Filter.impls[name] = filter
Expand Down
6 changes: 4 additions & 2 deletions src/template/tag/hash.ts
Expand Up @@ -10,13 +10,15 @@ import Scope from '../../scope/scope'
*/
export default class Hash {
[key: string]: any
constructor (markup: string, scope: Scope) {
static async create (markup: string, scope: Scope) {
const instance = new Hash()
let match
hashCapture.lastIndex = 0
while ((match = hashCapture.exec(markup))) {
const k = match[1]
const v = match[2]
this[k] = evalValue(v, scope)
instance[k] = await evalValue(v, scope)
}
return instance
}
}
2 changes: 1 addition & 1 deletion src/template/tag/tag.ts
Expand Up @@ -28,7 +28,7 @@ export default class Tag extends Template<TagToken> implements ITemplate {
}
}
async render (scope: Scope) {
const hash = new Hash(this.token.args, scope)
const hash = await Hash.create(this.token.args, scope)
const impl = this.impl
if (typeof impl.render !== 'function') {
return ''
Expand Down
10 changes: 6 additions & 4 deletions src/template/value.ts
Expand Up @@ -47,10 +47,12 @@ export default class Value {
}
this.filters.push(new Filter(name, args, this.strictFilters))
}
value (scope: Scope) {
return this.filters.reduce(
(prev, filter) => filter.render(prev, scope),
evalExp(this.initial, scope))
async value (scope: Scope) {
let val = await evalExp(this.initial, scope)
for (let filter of this.filters) {
val = await filter.render(val, scope)
}
return val
}
static tokenize (str: string): Array<'|' | ',' | ':' | string> {
const tokens = []
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/eval-value.ts
Expand Up @@ -5,7 +5,7 @@ describe('.evalValue()', function () {
var engine: Liquid
beforeEach(() => { engine = new Liquid() })

it('should throw when scope undefined', function () {
expect(() => engine.evalValue('{{"foo"}}', null as any)).to.throw(/scope undefined/)
it('should throw when scope undefined', async function () {
return expect(engine.evalValue('{{"foo"}}', null as any)).to.be.rejectedWith(/scope undefined/)
})
})
4 changes: 2 additions & 2 deletions test/integration/builtin/filters/array.ts
Expand Up @@ -33,9 +33,9 @@ describe('filters/array', function () {
' | split: ", " %}{{ my_array | size }}',
'4')
})
it('should also be used with dot notation - string',
it('should be respected with <string>.size notation',
() => test('{% assign my_string = "Ground control to Major Tom." %}{{ my_string.size }}', '28'))
it('should also be used with dot notation - array',
it('should be respected with <array>.size notation',
() => test('{% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}{{ my_array.size }}', '4'))
})
describe('slice', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/builtin/tags/for.ts
@@ -1,7 +1,7 @@
import Liquid from '../../../../src/liquid'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { Context } from '../../../../src/scope/scope'
import { Context } from '../../../../src/scope/context'

use(chaiAsPromised)

Expand Down
25 changes: 23 additions & 2 deletions test/integration/drop/drop.ts
Expand Up @@ -8,17 +8,26 @@ describe('drop/drop', function () {
class CustomDrop extends Liquid.Types.Drop {
name: string = 'NAME'
getName () {
return 'GETNAME'
return 'GET NAME'
}
}
class CustomDropWithMethodMissing extends CustomDrop {
liquidMethodMissing (key: string) {
return key.toUpperCase()
}
}
class PromiseDrop extends Liquid.Types.Drop {
name = Promise.resolve('NAME')
async getName () {
return 'GET NAME'
}
async liquidMethodMissing (key: string) {
return key.toUpperCase()
}
}
it('should call corresponding method', async function () {
const html = await liquid.parseAndRender(`{{obj.getName}}`, { obj: new CustomDrop() })
expect(html).to.equal('GETNAME')
expect(html).to.equal('GET NAME')
})
it('should read corresponding property', async function () {
const html = await liquid.parseAndRender(`{{obj.name}}`, { obj: new CustomDrop() })
Expand All @@ -32,4 +41,16 @@ describe('drop/drop', function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new CustomDropWithMethodMissing() })
expect(html).to.equal('FOO')
})
it('should call corresponding promise method', async function () {
const html = await liquid.parseAndRender(`{{obj.getName}}`, { obj: new PromiseDrop() })
expect(html).to.equal('GET NAME')
})
it('should read corresponding promise property', async function () {
const html = await liquid.parseAndRender(`{{obj.name}}`, { obj: new PromiseDrop() })
expect(html).to.equal('NAME')
})
it('should support promise returned by liquidMethodMissing', async function () {
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new PromiseDrop() })
expect(html).to.equal('FOO')
})
})

0 comments on commit 4a8088d

Please sign in to comment.