mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: expose FilterToken to filter this, #762
This commit is contained in:
@@ -80,9 +80,7 @@ export class Tokenizer {
|
|||||||
readFilter (): FilterToken | null {
|
readFilter (): FilterToken | null {
|
||||||
this.skipBlank()
|
this.skipBlank()
|
||||||
if (this.end()) return null
|
if (this.end()) return null
|
||||||
this.assert(this.peek() === '|', `expected "|" before filter`)
|
this.assert(this.read() === '|', `expected "|" before filter`)
|
||||||
this.p++
|
|
||||||
const begin = this.p
|
|
||||||
const name = this.readIdentifier()
|
const name = this.readIdentifier()
|
||||||
if (!name.size()) {
|
if (!name.size()) {
|
||||||
this.assert(this.end(), `expected filter name`)
|
this.assert(this.end(), `expected filter name`)
|
||||||
@@ -103,7 +101,7 @@ export class Tokenizer {
|
|||||||
} else {
|
} else {
|
||||||
throw this.error('expected ":" after filter name')
|
throw this.error('expected ":" after filter name')
|
||||||
}
|
}
|
||||||
return new FilterToken(name.getText(), args, this.input, begin, this.p, this.file)
|
return new FilterToken(name.getText(), args, this.input, name.begin, this.p, this.file)
|
||||||
}
|
}
|
||||||
|
|
||||||
readFilterArg (): FilterArg | undefined {
|
readFilterArg (): FilterArg | undefined {
|
||||||
@@ -298,7 +296,9 @@ export class Tokenizer {
|
|||||||
end () {
|
end () {
|
||||||
return this.p >= this.N
|
return this.p >= this.N
|
||||||
}
|
}
|
||||||
|
read () {
|
||||||
|
return this.input[this.p++]
|
||||||
|
}
|
||||||
readTo (end: string): number {
|
readTo (end: string): number {
|
||||||
while (this.p < this.N) {
|
while (this.p < this.N) {
|
||||||
++this.p
|
++this.p
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import type { Context } from '../context'
|
import type { Context } from '../context'
|
||||||
import type { Liquid } from '../liquid'
|
import type { Liquid } from '../liquid'
|
||||||
|
import type { FilterToken } from '../tokens'
|
||||||
|
|
||||||
export interface FilterImpl {
|
export interface FilterImpl {
|
||||||
context: Context;
|
context: Context;
|
||||||
|
token: FilterToken;
|
||||||
liquid: Liquid;
|
liquid: Liquid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,14 +7,14 @@ describe('filter', function () {
|
|||||||
const ctx = new Context({ thirty: 30 })
|
const ctx = new Context({ thirty: 30 })
|
||||||
const liquid = { testVersion: '1.0' } as any
|
const liquid = { testVersion: '1.0' } as any
|
||||||
it('should not change input if filter not registered', async function () {
|
it('should not change input if filter not registered', async function () {
|
||||||
const filter = new Filter('foo', undefined as any, [], liquid)
|
const filter = new Filter({ name: 'foo', args: [] } as any, undefined as any, liquid)
|
||||||
expect(await toPromise(filter.render('value', ctx))).toBe('value')
|
expect(await toPromise(filter.render('value', ctx))).toBe('value')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should call filter impl with correct arguments', async function () {
|
it('should call filter impl with correct arguments', async function () {
|
||||||
const spy = jest.fn()
|
const spy = jest.fn()
|
||||||
const thirty = new NumberToken('30', 0, 2, undefined)
|
const thirty = new NumberToken('30', 0, 2, undefined)
|
||||||
const filter = new Filter('foo', spy, [thirty], liquid)
|
const filter = new Filter({ name: 'foo', args: [thirty] } as any, spy, liquid)
|
||||||
await toPromise(filter.render('foo', ctx))
|
await toPromise(filter.render('foo', ctx))
|
||||||
expect(spy).toHaveBeenCalledWith('foo', 30)
|
expect(spy).toHaveBeenCalledWith('foo', 30)
|
||||||
})
|
})
|
||||||
@@ -24,37 +24,37 @@ describe('filter', function () {
|
|||||||
return `${this.liquid.testVersion}: ${val + diff}`
|
return `${this.liquid.testVersion}: ${val + diff}`
|
||||||
})
|
})
|
||||||
const ten = new NumberToken('10', 0, 2, undefined)
|
const ten = new NumberToken('10', 0, 2, undefined)
|
||||||
const filter = new Filter('add', spy, [ten], liquid)
|
const filter = new Filter({ name: 'add', args: [ten] } as any, spy, liquid)
|
||||||
const val = await toPromise(filter.render('thirty', ctx))
|
const val = await toPromise(filter.render('thirty', ctx))
|
||||||
expect(val).toEqual('1.0: 40')
|
expect(val).toEqual('1.0: 40')
|
||||||
})
|
})
|
||||||
it('should render a simple filter', async function () {
|
it('should render a simple filter', async function () {
|
||||||
expect(await toPromise(new Filter('upcase', (x: string) => x.toUpperCase(), [], liquid).render('foo', ctx))).toBe('FOO')
|
expect(await toPromise(new Filter({ name: 'upcase', args: [] } as any, (x: string) => x.toUpperCase(), liquid).render('foo', ctx))).toBe('FOO')
|
||||||
})
|
})
|
||||||
it('should reject promise when filter throws', async function () {
|
it('should reject promise when filter throws', async function () {
|
||||||
const filter = new Filter('foo', function * () { throw new Error('intended') }, [], liquid)
|
const filter = new Filter({ name: 'foo', args: [] } as any, function * () { throw new Error('intended') }, liquid)
|
||||||
expect(toPromise(filter.render('foo', ctx))).rejects.toMatchObject({
|
expect(toPromise(filter.render('foo', ctx))).rejects.toMatchObject({
|
||||||
message: 'intended'
|
message: 'intended'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
it('should render filters with argument', async function () {
|
it('should render filters with argument', async function () {
|
||||||
const two = new NumberToken('2', 0, 1, undefined)
|
const two = new NumberToken('2', 0, 1, undefined)
|
||||||
expect(await toPromise(new Filter('add', (a: number, b: number) => a + b, [two], liquid).render(3, ctx))).toBe(5)
|
expect(await toPromise(new Filter({ name: 'add', args: [two] } as any, (a: number, b: number) => a + b, liquid).render(3, ctx))).toBe(5)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should render filters with multiple arguments', async function () {
|
it('should render filters with multiple arguments', async function () {
|
||||||
const two = new NumberToken('2', 0, 1, undefined)
|
const two = new NumberToken('2', 0, 1, undefined)
|
||||||
const c = new QuotedToken('"c"', 0, 3)
|
const c = new QuotedToken('"c"', 0, 3)
|
||||||
expect(await toPromise(new Filter('add', (a: number, b: number, c: number) => a + b + c, [two, c], liquid).render(3, ctx))).toBe('5c')
|
expect(await toPromise(new Filter({ name: 'add', args: [two, c] } as any, (a: number, b: number, c: number) => a + b + c, liquid).render(3, ctx))).toBe('5c')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should pass Objects/Drops as it is', async function () {
|
it('should pass Objects/Drops as it is', async function () {
|
||||||
class Foo {}
|
class Foo {}
|
||||||
expect(await toPromise(new Filter('name', (a: any) => a.constructor.name, [], liquid).render(new Foo(), ctx))).toBe('Foo')
|
expect(await toPromise(new Filter({ name: 'name', args: [] } as any, (a: any) => a.constructor.name, liquid).render(new Foo(), ctx))).toBe('Foo')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support key value pairs', async function () {
|
it('should support key value pairs', async function () {
|
||||||
const two = new NumberToken('2', 0, 1, undefined)
|
const two = new NumberToken('2', 0, 1, undefined)
|
||||||
expect(await toPromise(new Filter('add', (a: number, b: number[]) => b[0] + ':' + (a + b[1]), [['num', two]], liquid).render(3, ctx))).toBe('num:5')
|
expect(await toPromise(new Filter({ name: 'add', args: [['num', two]] } as any, (a: number, b: number[]) => b[0] + ':' + (a + b[1]), liquid).render(3, ctx))).toBe('num:5')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { identify, isFunction } from '../util/underscore'
|
|||||||
import { FilterHandler, FilterImplOptions } from './filter-impl-options'
|
import { FilterHandler, FilterImplOptions } from './filter-impl-options'
|
||||||
import { FilterArg, isKeyValuePair } from '../parser/filter-arg'
|
import { FilterArg, isKeyValuePair } from '../parser/filter-arg'
|
||||||
import { Liquid } from '../liquid'
|
import { Liquid } from '../liquid'
|
||||||
|
import { FilterToken } from '../tokens'
|
||||||
|
|
||||||
export class Filter {
|
export class Filter {
|
||||||
public name: string
|
public name: string
|
||||||
@@ -11,14 +12,16 @@ export class Filter {
|
|||||||
public readonly raw: boolean
|
public readonly raw: boolean
|
||||||
private handler: FilterHandler
|
private handler: FilterHandler
|
||||||
private liquid: Liquid
|
private liquid: Liquid
|
||||||
|
private token: FilterToken
|
||||||
|
|
||||||
public constructor (name: string, options: FilterImplOptions | undefined, args: FilterArg[], liquid: Liquid) {
|
public constructor (token: FilterToken, options: FilterImplOptions | undefined, liquid: Liquid) {
|
||||||
this.name = name
|
this.token = token
|
||||||
|
this.name = token.name
|
||||||
this.handler = isFunction(options)
|
this.handler = isFunction(options)
|
||||||
? options
|
? options
|
||||||
: (isFunction(options?.handler) ? options!.handler : identify)
|
: (isFunction(options?.handler) ? options!.handler : identify)
|
||||||
this.raw = !isFunction(options) && !!options?.raw
|
this.raw = !isFunction(options) && !!options?.raw
|
||||||
this.args = args
|
this.args = token.args
|
||||||
this.liquid = liquid
|
this.liquid = liquid
|
||||||
}
|
}
|
||||||
public * render (value: any, context: Context): IterableIterator<unknown> {
|
public * render (value: any, context: Context): IterableIterator<unknown> {
|
||||||
@@ -27,6 +30,6 @@ export class Filter {
|
|||||||
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
|
||||||
else argv.push(yield evalToken(arg, context))
|
else argv.push(yield evalToken(arg, context))
|
||||||
}
|
}
|
||||||
return yield this.handler.apply({ context, liquid: this.liquid }, [value, ...argv])
|
return yield this.handler.apply({ context, token: this.token, liquid: this.liquid }, [value, ...argv])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { OutputToken } from '../tokens/output-token'
|
|||||||
import { Tokenizer } from '../parser'
|
import { Tokenizer } from '../parser'
|
||||||
import { Liquid } from '../liquid'
|
import { Liquid } from '../liquid'
|
||||||
import { Filter } from './filter'
|
import { Filter } from './filter'
|
||||||
|
import { FilterToken } from '../tokens'
|
||||||
|
|
||||||
export class Output extends TemplateImpl<OutputToken> implements Template {
|
export class Output extends TemplateImpl<OutputToken> implements Template {
|
||||||
value: Value
|
value: Value
|
||||||
@@ -16,7 +17,8 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
|
|||||||
const filters = this.value.filters
|
const filters = this.value.filters
|
||||||
const outputEscape = liquid.options.outputEscape
|
const outputEscape = liquid.options.outputEscape
|
||||||
if (!filters[filters.length - 1]?.raw && outputEscape) {
|
if (!filters[filters.length - 1]?.raw && outputEscape) {
|
||||||
filters.push(new Filter(toString.call(outputEscape), outputEscape, [], liquid))
|
const token = new FilterToken(toString.call(outputEscape), [], '', 0, 0)
|
||||||
|
filters.push(new Filter(token, outputEscape, liquid))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public * render (ctx: Context, emitter: Emitter): IterableIterator<unknown> {
|
public * render (ctx: Context, emitter: Emitter): IterableIterator<unknown> {
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ export class Value {
|
|||||||
? new Tokenizer(input, liquid.options.operators).readFilteredValue()
|
? new Tokenizer(input, liquid.options.operators).readFilteredValue()
|
||||||
: input
|
: input
|
||||||
this.initial = token.initial
|
this.initial = token.initial
|
||||||
this.filters = token.filters.map(({ name, args }) => new Filter(name, this.getFilter(liquid, name), args, liquid))
|
this.filters = token.filters.map(token => new Filter(token, this.getFilter(liquid, token.name), liquid))
|
||||||
}
|
}
|
||||||
public * value (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
|
public * value (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
|
||||||
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
|
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
|
||||||
|
|||||||
@@ -515,4 +515,13 @@ describe('Issues', function () {
|
|||||||
expect(LiquidError.is(err) && err.originalError).toHaveProperty('message', 'intended')
|
expect(LiquidError.is(err) && err.originalError).toHaveProperty('message', 'intended')
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
it('getting current line number and template name from a filter #762', () => {
|
||||||
|
const engine = new Liquid()
|
||||||
|
engine.registerFilter('pos', function (val: string) {
|
||||||
|
const [line, col] = this.token.getPosition()
|
||||||
|
return `[${line},${col}] ${val}`
|
||||||
|
})
|
||||||
|
const result = engine.parseAndRenderSync(`\n{{ "foo" | pos }}`)
|
||||||
|
expect(result).toEqual('\n[2,12] foo')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user