mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
perf: introduce AST to avoid reparse
This commit is contained in:
@@ -10,7 +10,7 @@ export class FilterMap {
|
||||
|
||||
get (name: string) {
|
||||
const impl = this.impls[name]
|
||||
assert(impl || !this.strictFilters, `undefined filter: ${name}`)
|
||||
assert(impl || !this.strictFilters, () => `undefined filter: ${name}`)
|
||||
return impl
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Expression } from '../../render/expression'
|
||||
import { evalToken } from '../../render/expression'
|
||||
import { Context } from '../../context/context'
|
||||
import { identify } from '../../util/underscore'
|
||||
import { FilterImplOptions } from './filter-impl-options'
|
||||
@@ -16,9 +16,9 @@ export class Filter {
|
||||
}
|
||||
public * render (value: any, context: Context) {
|
||||
const argv: any[] = []
|
||||
for (const arg of this.args) {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield new Expression(arg[1]).evaluate(context)])
|
||||
else argv.push(yield new Expression(arg).evaluate(context))
|
||||
for (const arg of this.args as FilterArg[]) {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||
else argv.push(yield evalToken(arg, context))
|
||||
}
|
||||
return this.impl.apply({ context }, [value, ...argv])
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { TemplateImpl } from '../template/template-impl'
|
||||
import { Template } from '../template/template'
|
||||
import { HTMLToken } from '../parser/html-token'
|
||||
import { HTMLToken } from '../tokens/html-token'
|
||||
import { Context } from '../context/context'
|
||||
import { Emitter } from '../render/emitter'
|
||||
|
||||
@@ -8,7 +8,7 @@ export class HTML extends TemplateImpl<HTMLToken> implements Template {
|
||||
private str: string
|
||||
public constructor (token: HTMLToken) {
|
||||
super(token)
|
||||
this.str = token.content
|
||||
this.str = token.getContent()
|
||||
}
|
||||
public * render (ctx: Context, emitter: Emitter): IterableIterator<void> {
|
||||
emitter.write(this.str)
|
||||
|
||||
@@ -5,7 +5,7 @@ import { TemplateImpl } from '../template/template-impl'
|
||||
import { Template } from '../template/template'
|
||||
import { Context } from '../context/context'
|
||||
import { Emitter } from '../render/emitter'
|
||||
import { OutputToken } from '../parser/output-token'
|
||||
import { OutputToken } from '../tokens/output-token'
|
||||
|
||||
export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||
private value: Value
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Expression } from '../../render/expression'
|
||||
import { evalToken } from '../../render/expression'
|
||||
import { Context } from '../../context/context'
|
||||
import { Tokenizer } from '../../parser/tokenizer'
|
||||
|
||||
@@ -11,17 +11,17 @@ import { Tokenizer } from '../../parser/tokenizer'
|
||||
* hash['reversed'] === undefined
|
||||
*/
|
||||
export class Hash {
|
||||
[key: string]: any
|
||||
hash: { [key: string]: any } = {}
|
||||
constructor (markup: string) {
|
||||
const tokenizer = new Tokenizer(markup)
|
||||
for (const [name, value] of tokenizer.readHashes()) {
|
||||
this[name] = value
|
||||
for (const hash of tokenizer.readHashes()) {
|
||||
this.hash[hash.name.content] = hash.value
|
||||
}
|
||||
}
|
||||
* render (ctx: Context) {
|
||||
const hash = {}
|
||||
for (const key of Object.keys(this)) {
|
||||
hash[key] = yield new Expression(this[key]).evaluate(ctx)
|
||||
for (const key of Object.keys(this.hash)) {
|
||||
hash[key] = evalToken(this.hash[key], ctx)
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Context } from '../../context/context'
|
||||
import { TagToken } from '../../parser/tag-token'
|
||||
import { Token } from '../../parser/token'
|
||||
import { TagToken } from '../../tokens/tag-token'
|
||||
import { TopLevelToken } from '../../tokens/toplevel-token'
|
||||
import { TagImpl } from './tag-impl'
|
||||
import { Hash } from '../../template/tag/hash'
|
||||
import { Emitter } from '../../render/emitter'
|
||||
|
||||
export interface TagImplOptions {
|
||||
parse?: (this: TagImpl, token: TagToken, remainingTokens: Token[]) => void;
|
||||
parse?: (this: TagImpl, token: TagToken, remainingTokens: TopLevelToken[]) => void;
|
||||
render: (this: TagImpl, ctx: Context, emitter: Emitter, hash: Hash) => any;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ export class TagMap {
|
||||
|
||||
get (name: string) {
|
||||
const impl = this.impls[name]
|
||||
assert(impl, `tag "${name}" not found`)
|
||||
assert(impl, () => `tag "${name}" not found`)
|
||||
return impl
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { isFunction } from '../../util/underscore'
|
||||
import { Liquid } from '../../liquid'
|
||||
import { TemplateImpl } from '../../template/template-impl'
|
||||
import { Emitter, Hash, Context, TagImplOptions, TagToken, Template, Token } from '../../types'
|
||||
import { Emitter, Hash, Context, TagImplOptions, TagToken, Template, TopLevelToken } from '../../types'
|
||||
import { TagImpl } from './tag-impl'
|
||||
|
||||
export class Tag extends TemplateImpl<TagToken> implements Template {
|
||||
@@ -9,7 +9,7 @@ export class Tag extends TemplateImpl<TagToken> implements Template {
|
||||
private impl: TagImpl
|
||||
private static impls: { [key: string]: TagImplOptions } = {}
|
||||
|
||||
public constructor (token: TagToken, tokens: Token[], liquid: Liquid) {
|
||||
public constructor (token: TagToken, tokens: TopLevelToken[], liquid: Liquid) {
|
||||
super(token)
|
||||
this.name = token.name
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Context } from '../context/context'
|
||||
import { Token } from '../parser/token'
|
||||
import { Token } from '../tokens/token'
|
||||
import { Emitter } from '../render/emitter'
|
||||
|
||||
export interface Template {
|
||||
|
||||
@@ -1,23 +1,24 @@
|
||||
import { Expression } from '../render/expression'
|
||||
import { evalToken } from '../render/expression'
|
||||
import { Tokenizer } from '../parser/tokenizer'
|
||||
import { FilterMap } from '../template/filter/filter-map'
|
||||
import { Filter } from './filter/filter'
|
||||
import { Context } from '../context/context'
|
||||
import { ValueToken } from '../tokens/value-token'
|
||||
|
||||
export class Value {
|
||||
public readonly filters: Filter[] = []
|
||||
public readonly initial: string
|
||||
public readonly initial?: ValueToken
|
||||
|
||||
/**
|
||||
* @param str the value to be valuated, eg.: "foobar" | truncate: 3
|
||||
*/
|
||||
public constructor (str: string, private readonly filterMap: FilterMap) {
|
||||
const tokenizer = new Tokenizer(str)
|
||||
this.initial = tokenizer.readValue().toString()
|
||||
this.filters = tokenizer.readFilterTokens().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
||||
this.initial = tokenizer.readValue()
|
||||
this.filters = tokenizer.readFilters().map(({ name, args }) => new Filter(name, this.filterMap.get(name), args))
|
||||
}
|
||||
public * value (ctx: Context) {
|
||||
let val = yield new Expression(this.initial).evaluate(ctx)
|
||||
let val = yield evalToken(this.initial, ctx)
|
||||
for (const filter of this.filters) {
|
||||
val = yield filter.render(val, ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user