mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
style: introduce @typescript-eslint/recommended
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import Context from '../../context/context'
|
||||
|
||||
export interface FilterImpl {
|
||||
context: Context
|
||||
context: Context;
|
||||
}
|
||||
|
||||
@@ -5,15 +5,15 @@ import { FilterImplOptions } from './filter-impl-options'
|
||||
|
||||
type KeyValuePair = [string?, string?]
|
||||
type FilterArg = string|KeyValuePair
|
||||
export type FilterArgs = Array<FilterArg>
|
||||
export type FilterArgs = FilterArg[]
|
||||
|
||||
export class Filter {
|
||||
name: string
|
||||
impl: FilterImplOptions
|
||||
args: FilterArgs
|
||||
private name: string
|
||||
private impl: FilterImplOptions
|
||||
private args: FilterArgs
|
||||
private static impls: {[key: string]: FilterImplOptions} = {}
|
||||
|
||||
constructor (name: string, args: FilterArgs, strictFilters: boolean) {
|
||||
public constructor (name: string, args: FilterArgs, strictFilters: boolean) {
|
||||
const impl = Filter.impls[name]
|
||||
if (!impl && strictFilters) throw new TypeError(`undefined filter: ${name}`)
|
||||
|
||||
@@ -21,7 +21,7 @@ export class Filter {
|
||||
this.impl = impl || (x => x)
|
||||
this.args = args
|
||||
}
|
||||
async render (value: any, context: Context) {
|
||||
public async render (value: any, context: Context) {
|
||||
const argv: any[] = []
|
||||
for (const arg of this.args) {
|
||||
if (isKeyValuePair(arg)) argv.push([arg[0], await parseValue(arg[1], context)])
|
||||
@@ -29,10 +29,10 @@ export class Filter {
|
||||
}
|
||||
return this.impl.apply({ context }, [value, ...argv])
|
||||
}
|
||||
static register (name: string, filter: FilterImplOptions) {
|
||||
public static register (name: string, filter: FilterImplOptions) {
|
||||
Filter.impls[name] = filter
|
||||
}
|
||||
static clear () {
|
||||
public static clear () {
|
||||
Filter.impls = {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ import ITemplate from '../template/itemplate'
|
||||
import HTMLToken from '../parser/html-token'
|
||||
|
||||
export default class extends Template<HTMLToken> implements ITemplate {
|
||||
str: string
|
||||
constructor (token: HTMLToken) {
|
||||
private str: string
|
||||
public constructor (token: HTMLToken) {
|
||||
super(token)
|
||||
this.str = token.value
|
||||
}
|
||||
async render (): Promise<string> {
|
||||
public async render (): Promise<string> {
|
||||
return this.str
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ import Context from '../context/context'
|
||||
import OutputToken from '../parser/output-token'
|
||||
|
||||
export default class Output extends Template<OutputToken> implements ITemplate {
|
||||
value: Value
|
||||
constructor (token: OutputToken, strictFilters: boolean) {
|
||||
private value: Value
|
||||
public constructor (token: OutputToken, strictFilters: boolean) {
|
||||
super(token)
|
||||
this.value = new Value(token.value, strictFilters)
|
||||
}
|
||||
async render (ctx: Context): Promise<string> {
|
||||
public async render (ctx: Context): Promise<string> {
|
||||
const val = await this.value.value(ctx)
|
||||
return stringify(toValue(val))
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import Context from '../../context/context'
|
||||
*/
|
||||
export default class Hash {
|
||||
[key: string]: any
|
||||
static async create (markup: string, ctx: Context) {
|
||||
public static async create (markup: string, ctx: Context) {
|
||||
const instance = new Hash()
|
||||
let match
|
||||
hashCapture.lastIndex = 0
|
||||
|
||||
@@ -5,6 +5,6 @@ import Hash from '../../template/tag/hash'
|
||||
import ITagImpl from './itag-impl'
|
||||
|
||||
export default interface ITagImplOptions {
|
||||
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Array<Token>) => void
|
||||
render?: (this: ITagImpl, ctx: Context, hash: Hash) => any | Promise<any>
|
||||
parse?: (this: ITagImpl, token: TagToken, remainingTokens: Token[]) => void;
|
||||
render?: (this: ITagImpl, ctx: Context, hash: Hash) => any | Promise<any>;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,6 @@ import Liquid from '../../liquid'
|
||||
import ITagImplOptions from './itag-impl-options'
|
||||
|
||||
export default interface ITagImpl extends ITagImplOptions {
|
||||
liquid: Liquid,
|
||||
[key: string]: any
|
||||
liquid: Liquid;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
@@ -11,11 +11,11 @@ import TagToken from '../../parser/tag-token'
|
||||
import Token from '../../parser/token'
|
||||
|
||||
export default class Tag extends Template<TagToken> implements ITemplate {
|
||||
name: string
|
||||
public name: string
|
||||
private impl: ITagImpl
|
||||
static impls: { [key: string]: ITagImplOptions } = {}
|
||||
private static impls: { [key: string]: ITagImplOptions } = {}
|
||||
|
||||
constructor (token: TagToken, tokens: Token[], liquid: Liquid) {
|
||||
public constructor (token: TagToken, tokens: Token[], liquid: Liquid) {
|
||||
super(token)
|
||||
this.name = token.name
|
||||
|
||||
@@ -28,15 +28,15 @@ export default class Tag extends Template<TagToken> implements ITemplate {
|
||||
this.impl.parse(token, tokens)
|
||||
}
|
||||
}
|
||||
async render (ctx: Context) {
|
||||
public async render (ctx: Context) {
|
||||
const hash = await Hash.create(this.token.args, ctx)
|
||||
const impl = this.impl
|
||||
return isFunction(impl.render) ? stringify(await impl.render(ctx, hash)) : ''
|
||||
}
|
||||
static register (name: string, tag: ITagImplOptions) {
|
||||
public static register (name: string, tag: ITagImplOptions) {
|
||||
Tag.impls[name] = tag
|
||||
}
|
||||
static clear () {
|
||||
public static clear () {
|
||||
Tag.impls = {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export default abstract class Template<T> {
|
||||
token: T;
|
||||
constructor (token: T) {
|
||||
public token: T;
|
||||
public constructor (token: T) {
|
||||
this.token = token
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import Context from '../context/context'
|
||||
export default class Value {
|
||||
private strictFilters: boolean
|
||||
private initial: string
|
||||
private filters: Array<Filter> = []
|
||||
private filters: Filter[] = []
|
||||
|
||||
/**
|
||||
* @param str value string, like: "i have a dream | truncate: 3
|
||||
*/
|
||||
constructor (str: string, strictFilters: boolean) {
|
||||
public constructor (str: string, strictFilters: boolean) {
|
||||
const tokens = Value.tokenize(str)
|
||||
this.strictFilters = strictFilters
|
||||
this.initial = tokens[0]
|
||||
@@ -35,7 +35,7 @@ export default class Value {
|
||||
for (let i = begin + 1; i < end + 1; i++) {
|
||||
if (i === end || tokens[i] === ',') {
|
||||
if (argName || argValue) {
|
||||
args.push(argName ? [argName, argValue] : <string>argValue)
|
||||
args.push(argName ? [argName, argValue] : argValue as string)
|
||||
}
|
||||
argValue = argName = undefined
|
||||
} else if (tokens[i] === ':') {
|
||||
@@ -47,14 +47,14 @@ export default class Value {
|
||||
}
|
||||
this.filters.push(new Filter(name, args, this.strictFilters))
|
||||
}
|
||||
async value (ctx: Context) {
|
||||
public async value (ctx: Context) {
|
||||
let val = await parseExp(this.initial, ctx)
|
||||
for (const filter of this.filters) {
|
||||
val = await filter.render(val, ctx)
|
||||
}
|
||||
return val
|
||||
}
|
||||
static tokenize (str: string): Array<'|' | ',' | ':' | string> {
|
||||
public static tokenize (str: string): ('|' | ',' | ':' | string)[] {
|
||||
const tokens = []
|
||||
let i = 0
|
||||
while (i < str.length) {
|
||||
|
||||
Reference in New Issue
Block a user