Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref: remove await/async from internal calls #166

Merged
merged 1 commit into from
Oct 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"no-dupe-class-members": "off",
"@typescript-eslint/indent": ["error", 2],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-object-literal-type-assertion": "off",
"@typescript-eslint/no-use-before-define": "off",
Expand Down
6 changes: 2 additions & 4 deletions src/builtin/tags/assign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export default {
this.key = match[1]
this.value = match[2]
},
render: async function (ctx: Context) {
ctx.front()[this.key] = ctx.sync
? this.liquid.evalValueSync(this.value, ctx)
: await this.liquid.evalValue(this.value, ctx)
render: function * (ctx: Context) {
ctx.front()[this.key] = yield this.liquid._evalValue(this.value, ctx)
}
} as ITagImplOptions
7 changes: 2 additions & 5 deletions src/builtin/tags/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@ export default {
})
stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const blocks = ctx.getRegister('blocks')
const childDefined = blocks[this.block]
const r = this.liquid.renderer
const html = childDefined !== undefined
? childDefined
: (ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
)
: yield r.renderTemplates(this.tpls, ctx)

if (ctx.getRegister('blockMode', BlockMode.OUTPUT) === BlockMode.STORE) {
blocks[this.block] = html
Expand Down
2 changes: 1 addition & 1 deletion src/builtin/tags/break.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Emitter, Context, Hash } from '../../types'

export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.break = true
}
}
6 changes: 2 additions & 4 deletions src/builtin/tags/capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ export default {
})
stream.start()
},
render: async function (ctx: Context) {
render: function * (ctx: Context) {
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.templates, ctx)
: await r.renderTemplates(this.templates, ctx)
const html = yield r.renderTemplates(this.templates, ctx)
ctx.front()[this.variable] = html
}
} as ITagImplOptions
24 changes: 5 additions & 19 deletions src/builtin/tags/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,17 @@ export default {
stream.start()
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = await new Expression(branch.val).value(ctx)
const cond = await new Expression(this.cond).value(ctx)
const val = yield new Expression(branch.val).value(ctx)
const cond = yield new Expression(this.cond).value(ctx)
if (val === cond) {
await r.renderTemplates(branch.templates, ctx, emitter)
yield r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},

renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
for (let i = 0; i < this.cases.length; i++) {
const branch = this.cases[i]
const val = new Expression(branch.val).valueSync(ctx)
const cond = new Expression(this.cond).valueSync(ctx)
if (val === cond) {
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
2 changes: 1 addition & 1 deletion src/builtin/tags/continue.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Emitter, Context, Hash } from '../../types'

export default {
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function (ctx: Context, hash: Hash, emitter: Emitter) {
emitter.continue = true
}
}
10 changes: 3 additions & 7 deletions src/builtin/tags/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ export default {
assert(this.candidates.length, `empty candidates: ${tagToken.raw}`)
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const group = ctx.sync
? this.group.valueSync(ctx)
: await this.group.value(ctx)
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const group = yield this.group.value(ctx)
const fingerprint = `cycle:${group}:` + this.candidates.join(',')
const groups = ctx.getRegister('cycle')
let idx = groups[fingerprint]
Expand All @@ -36,9 +34,7 @@ export default {
const candidate = this.candidates[idx]
idx = (idx + 1) % this.candidates.length
groups[fingerprint] = idx
const html = ctx.sync
? new Expression(candidate).valueSync(ctx)
: await new Expression(candidate).value(ctx)
const html = yield new Expression(candidate).value(ctx)
emitter.write(html)
}
} as ITagImplOptions
14 changes: 4 additions & 10 deletions src/builtin/tags/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ export default {

stream.start()
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx)
: await new Expression(this.collection).value(ctx)
let collection = yield new Expression(this.collection).value(ctx)

if (!isArray(collection)) {
if (isString(collection) && collection.length > 0) {
Expand All @@ -50,9 +48,7 @@ export default {
}
}
if (!isArray(collection) || !collection.length) {
ctx.sync
? r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
: await r.renderTemplates(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
return
}

Expand All @@ -66,9 +62,7 @@ export default {
ctx.push(scope)
for (const item of collection) {
scope[this.variable] = item
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
yield r.renderTemplates(this.templates, ctx, emitter)
if (emitter.break) {
emitter.break = false
break
Expand Down
21 changes: 4 additions & 17 deletions src/builtin/tags/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,16 @@ export default {
stream.start()
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer

for (const branch of this.branches) {
const cond = await new Expression(branch.cond).value(ctx)
const cond = yield new Expression(branch.cond).value(ctx)
if (isTruthy(cond)) {
await r.renderTemplates(branch.templates, ctx, emitter)
yield r.renderTemplates(branch.templates, ctx, emitter)
return
}
}
await r.renderTemplates(this.elseTemplates, ctx, emitter)
},

renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer

for (const branch of this.branches) {
const cond = new Expression(branch.cond).valueSync(ctx)
if (isTruthy(cond)) {
r.renderTemplatesSync(branch.templates, ctx, emitter)
return
}
}
r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
yield r.renderTemplates(this.elseTemplates, ctx, emitter)
}
} as ITagImplOptions
42 changes: 6 additions & 36 deletions src/builtin/tags/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ export default {
match = withRE.exec(token.args)
if (match) this.with = match[1]
},
renderSync: function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = this.liquid.parseAndRenderSync(template, ctx.getAll(), ctx.opts)
filepath = yield this.liquid._parseAndRender(template, ctx.getAll(), ctx.opts, ctx.sync)
} else {
filepath = new Expression(this.value).valueSync(ctx)
filepath = yield new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
Expand All @@ -37,41 +37,11 @@ export default {
ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = new Expression(this.with).evaluateSync(ctx)
hash[filepath] = yield new Expression(this.with).evaluate(ctx)
}
const templates = this.liquid.parseFileSync(filepath, ctx.opts)
const templates = yield this.liquid._parseFile(filepath, ctx.opts, ctx.sync)
ctx.push(hash)
this.liquid.renderer.renderTemplatesSync(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let filepath
if (ctx.opts.dynamicPartials) {
if (quotedLine.exec(this.value)) {
const template = this.value.slice(1, -1)
filepath = await this.liquid.parseAndRender(template, ctx.getAll(), ctx.opts)
} else {
filepath = await new Expression(this.value).value(ctx)
}
} else {
filepath = this.staticValue
}
assert(filepath, `cannot include with empty filename`)

const originBlocks = ctx.getRegister('blocks')
const originBlockMode = ctx.getRegister('blockMode')

ctx.setRegister('blocks', {})
ctx.setRegister('blockMode', BlockMode.OUTPUT)
if (this.with) {
hash[filepath] = await new Expression(this.with).evaluate(ctx)
}
const templates = await this.liquid.parseFile(filepath, ctx.opts)
ctx.push(hash)
await this.liquid.renderer.renderTemplates(templates, ctx, emitter)
yield this.liquid.renderer.renderTemplates(templates, ctx, emitter)
ctx.pop()
ctx.setRegister('blocks', originBlocks)
ctx.setRegister('blockMode', originBlockMode)
Expand Down
19 changes: 5 additions & 14 deletions src/builtin/tags/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,24 @@ export default {

this.tpls = this.liquid.parser.parse(remainTokens)
},
render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const layout = ctx.opts.dynamicPartials
? (ctx.sync
? new Expression(this.layout).valueSync(ctx)
: await new Expression(this.layout).value(ctx)
)
? yield new Expression(this.layout).value(ctx)
: this.staticLayout
assert(layout, `cannot apply layout with empty filename`)

// render the remaining tokens immediately
ctx.setRegister('blockMode', BlockMode.STORE)
const blocks = ctx.getRegister('blocks')
const r = this.liquid.renderer
const html = ctx.sync
? r.renderTemplatesSync(this.tpls, ctx)
: await r.renderTemplates(this.tpls, ctx)
const html = yield r.renderTemplates(this.tpls, ctx)
if (blocks[''] === undefined) {
blocks[''] = html
}
const templates = ctx.sync
? this.liquid.parseFileSync(layout, ctx.opts)
: await this.liquid.parseFile(layout, ctx.opts)
const templates = yield this.liquid._parseFile(layout, ctx.opts, ctx.sync)
ctx.push(hash)
ctx.setRegister('blockMode', BlockMode.OUTPUT)
const partial = ctx.sync
? r.renderTemplatesSync(templates, ctx)
: await r.renderTemplates(templates, ctx)
const partial = yield r.renderTemplates(templates, ctx)
ctx.pop()
emitter.write(partial)
}
Expand Down
6 changes: 3 additions & 3 deletions src/builtin/tags/raw.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TagToken, Token, ITagImplOptions, Context } from '../../types'
import { Hash, Emitter, TagToken, Token, ITagImplOptions, Context } from '../../types'

export default {
parse: function (tagToken: TagToken, remainTokens: Token[]) {
Expand All @@ -15,7 +15,7 @@ export default {
})
stream.start()
},
render: function (ctx: Context) {
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
10 changes: 3 additions & 7 deletions src/builtin/tags/tablerow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ export default {
stream.start()
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = ctx.sync
? new Expression(this.collection).valueSync(ctx) || []
: await new Expression(this.collection).value(ctx) || []
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
let collection = (yield new Expression(this.collection).value(ctx)) || []
const offset = hash.offset || 0
const limit = (hash.limit === undefined) ? collection.length : hash.limit

Expand All @@ -50,9 +48,7 @@ export default {
emitter.write(`<tr class="row${tablerowloop.row()}">`)
}
emitter.write(`<td class="col${tablerowloop.col()}">`)
ctx.sync
? r.renderTemplatesSync(this.templates, ctx, emitter)
: await r.renderTemplates(this.templates, ctx, emitter)
yield r.renderTemplates(this.templates, ctx, emitter)
emitter.write('</td>')
}
if (collection.length) emitter.write('</tr>')
Expand Down
14 changes: 3 additions & 11 deletions src/builtin/tags/unless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,10 @@ export default {
stream.start()
},

renderSync: async function (ctx: Context, hash: Hash, emitter: Emitter) {
render: function * (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = new Expression(this.cond).valueSync(ctx)
isFalsy(cond)
? r.renderTemplatesSync(this.templates, ctx, emitter)
: r.renderTemplatesSync(this.elseTemplates, ctx, emitter)
},

render: async function (ctx: Context, hash: Hash, emitter: Emitter) {
const r = this.liquid.renderer
const cond = await new Expression(this.cond).value(ctx)
await (isFalsy(cond)
const cond = yield new Expression(this.cond).value(ctx)
yield (isFalsy(cond)
? r.renderTemplates(this.templates, ctx, emitter)
: r.renderTemplates(this.elseTemplates, ctx, emitter))
}
Expand Down
Loading