mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
chore(TypeScript): fix linting and generate .d.ts
This commit is contained in:
@@ -13,21 +13,21 @@ export default class Filter {
|
||||
args: string[]
|
||||
private static impls: {[key: string]: impl} = {}
|
||||
|
||||
constructor (str: string, strict_filters: boolean = false) {
|
||||
let match = lexical.filterLine.exec(str)
|
||||
constructor (str: string, strictFilters: boolean = false) {
|
||||
const match = lexical.filterLine.exec(str)
|
||||
assert(match, 'illegal filter: ' + str)
|
||||
|
||||
const name = match[1]
|
||||
const argList = match[2] || ''
|
||||
const impl = Filter.impls[name]
|
||||
if (!impl && strict_filters) throw new TypeError(`undefined filter: ${name}`)
|
||||
if (!impl && strictFilters) throw new TypeError(`undefined filter: ${name}`)
|
||||
|
||||
this.name = name
|
||||
this.impl = impl || (x => x)
|
||||
this.args = this.parseArgs(argList)
|
||||
}
|
||||
parseArgs (argList: string): string[] {
|
||||
let match, args = []
|
||||
let match; const args = []
|
||||
while ((match = valueRE.exec(argList.trim()))) {
|
||||
const v = match[0]
|
||||
const re = new RegExp(`${v}\\s*:`, 'g')
|
||||
@@ -42,7 +42,7 @@ export default class Filter {
|
||||
args.unshift(value)
|
||||
return this.impl.apply(null, args)
|
||||
}
|
||||
static register(name, filter) {
|
||||
static register (name, filter) {
|
||||
Filter.impls[name] = filter
|
||||
}
|
||||
static clear () {
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
import Template from 'src/template/template'
|
||||
import Scope from 'src/scope/scope'
|
||||
import ITemplate from 'src/template/itemplate'
|
||||
import Token from 'src/parser/token'
|
||||
|
||||
export default class extends Template implements ITemplate {
|
||||
str: string
|
||||
constructor(token: Token) {
|
||||
constructor (token: Token) {
|
||||
super(token)
|
||||
this.str = token.value
|
||||
}
|
||||
async render(scope: Scope): Promise<string> {
|
||||
async render (): Promise<string> {
|
||||
return this.str
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ import { stringify } from 'src/util/underscore'
|
||||
import Template from 'src/template/template'
|
||||
import ITemplate from 'src/template/itemplate'
|
||||
import Scope from 'src/scope/scope'
|
||||
import OutputToken from 'src/parser/output-token'
|
||||
|
||||
export default class Output extends Template implements ITemplate {
|
||||
value: Value
|
||||
constructor(token, strict_filters?) {
|
||||
constructor (token: OutputToken, strictFilters?: boolean) {
|
||||
super(token)
|
||||
this.value = new Value(token.value, strict_filters)
|
||||
this.value = new Value(token.value, strictFilters)
|
||||
}
|
||||
async render(scope: Scope) {
|
||||
async render (scope: Scope): Promise<string> {
|
||||
const html = await this.value.value(scope)
|
||||
return stringify(html)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { evalValue } from 'src/render/syntax'
|
||||
*/
|
||||
export default class Hash {
|
||||
[key: string]: any
|
||||
constructor(markup, scope) {
|
||||
constructor (markup, scope) {
|
||||
let match
|
||||
hashCapture.lastIndex = 0
|
||||
while ((match = hashCapture.exec(markup))) {
|
||||
|
||||
@@ -6,5 +6,5 @@ import ITagImpl from './itag-impl'
|
||||
|
||||
export default interface ITagImplOptions {
|
||||
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Array<Token>) => void
|
||||
render?: (this: ITagImpl, scope: Scope, hash: Hash) => string | Promise<string>
|
||||
render?: (this: ITagImpl, scope: Scope, hash: Hash) => any | Promise<any>
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { create } from 'src/util/underscore'
|
||||
import { stringify } from 'src/util/underscore'
|
||||
import { create, stringify } from 'src/util/underscore'
|
||||
import assert from 'src/util/assert'
|
||||
import Scope from 'src/scope/scope'
|
||||
import ITagImpl from './itag-impl'
|
||||
import ITagImplOptions from './itag-impl-options'
|
||||
import Liquid from 'src/liquid'
|
||||
@@ -8,26 +8,27 @@ import Hash from './hash'
|
||||
import Template from 'src/template/template'
|
||||
import ITemplate from 'src/template/itemplate'
|
||||
import TagToken from 'src/parser/tag-token'
|
||||
import Token from 'src/parser/token'
|
||||
|
||||
export default class Tag extends Template implements ITemplate {
|
||||
name: string
|
||||
token: TagToken
|
||||
private impl: ITagImpl
|
||||
static impls: object = {}
|
||||
static impls: { [key: string]: ITagImplOptions } = {}
|
||||
|
||||
constructor (token, tokens, liquid: Liquid) {
|
||||
constructor (token: TagToken, tokens: Token[], liquid: Liquid) {
|
||||
super(token)
|
||||
this.name = token.name
|
||||
|
||||
const impl = Tag.impls[token.name]
|
||||
assert(impl, `tag ${token.name} not found`)
|
||||
this.impl = create(impl)
|
||||
this.impl = create<ITagImplOptions, ITagImpl>(impl)
|
||||
this.impl.liquid = liquid
|
||||
if (this.impl.parse) {
|
||||
this.impl.parse(token, tokens)
|
||||
}
|
||||
}
|
||||
async render (scope) {
|
||||
async render (scope: Scope) {
|
||||
const hash = new Hash(this.token.args, scope)
|
||||
const impl = this.impl
|
||||
if (typeof impl.render !== 'function') {
|
||||
|
||||
@@ -2,7 +2,7 @@ import Token from 'src/parser/token'
|
||||
|
||||
export default class Template {
|
||||
token: Token;
|
||||
constructor(token) {
|
||||
this.token = token;
|
||||
constructor (token) {
|
||||
this.token = token
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import Scope from 'src/scope/scope'
|
||||
export default class {
|
||||
initial: any
|
||||
filters: Array<any>
|
||||
constructor(str: string, strict_filters?: boolean) {
|
||||
constructor (str: string, strictFilters?: boolean) {
|
||||
let match = lexical.matchValue(str)
|
||||
assert(match, `illegal value string: ${str}`)
|
||||
|
||||
@@ -20,9 +20,9 @@ export default class {
|
||||
}
|
||||
|
||||
this.initial = initial
|
||||
this.filters = filters.map(str => new Filter(str, strict_filters))
|
||||
this.filters = filters.map(str => new Filter(str, strictFilters))
|
||||
}
|
||||
value(scope: Scope) {
|
||||
value (scope: Scope) {
|
||||
return this.filters.reduce(
|
||||
(prev, filter) => filter.render(prev, scope),
|
||||
evalExp(this.initial, scope))
|
||||
|
||||
Reference in New Issue
Block a user