Skip to content

Commit

Permalink
feat: expose FilterToken to filter this, #762
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 16, 2024
1 parent 59da3fa commit d705888
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ export class Tokenizer {
readFilter (): FilterToken | null {
this.skipBlank()
if (this.end()) return null
this.assert(this.peek() === '|', `expected "|" before filter`)
this.p++
const begin = this.p
this.assert(this.read() === '|', `expected "|" before filter`)
const name = this.readIdentifier()
if (!name.size()) {
this.assert(this.end(), `expected filter name`)
Expand All @@ -103,7 +101,7 @@ export class Tokenizer {
} else {
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 {
Expand Down Expand Up @@ -298,7 +296,9 @@ export class Tokenizer {
end () {
return this.p >= this.N
}

read () {
return this.input[this.p++]
}
readTo (end: string): number {
while (this.p < this.N) {
++this.p
Expand Down
2 changes: 2 additions & 0 deletions src/template/filter-impl-options.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { Context } from '../context'
import type { Liquid } from '../liquid'
import type { FilterToken } from '../tokens'

export interface FilterImpl {
context: Context;
token: FilterToken;
liquid: Liquid;
}

Expand Down
18 changes: 9 additions & 9 deletions src/template/filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ describe('filter', function () {
const ctx = new Context({ thirty: 30 })
const liquid = { testVersion: '1.0' } as any
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')
})

it('should call filter impl with correct arguments', async function () {
const spy = jest.fn()
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))
expect(spy).toHaveBeenCalledWith('foo', 30)
})
Expand All @@ -24,37 +24,37 @@ describe('filter', function () {
return `${this.liquid.testVersion}: ${val + diff}`
})
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))
expect(val).toEqual('1.0: 40')
})
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 () {
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({
message: 'intended'
})
})
it('should render filters with argument', async function () {
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 () {
const two = new NumberToken('2', 0, 1, undefined)
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 () {
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 () {
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')
})
})
11 changes: 7 additions & 4 deletions src/template/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import { identify, isFunction } from '../util/underscore'
import { FilterHandler, FilterImplOptions } from './filter-impl-options'
import { FilterArg, isKeyValuePair } from '../parser/filter-arg'
import { Liquid } from '../liquid'
import { FilterToken } from '../tokens'

export class Filter {
public name: string
public args: FilterArg[]
public readonly raw: boolean
private handler: FilterHandler
private liquid: Liquid
private token: FilterToken

public constructor (name: string, options: FilterImplOptions | undefined, args: FilterArg[], liquid: Liquid) {
this.name = name
public constructor (token: FilterToken, options: FilterImplOptions | undefined, liquid: Liquid) {
this.token = token
this.name = token.name
this.handler = isFunction(options)
? options
: (isFunction(options?.handler) ? options!.handler : identify)
this.raw = !isFunction(options) && !!options?.raw
this.args = args
this.args = token.args
this.liquid = liquid
}
public * render (value: any, context: Context): IterableIterator<unknown> {
Expand All @@ -27,6 +30,6 @@ export class Filter {
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], 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])
}
}
4 changes: 3 additions & 1 deletion src/template/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { OutputToken } from '../tokens/output-token'
import { Tokenizer } from '../parser'
import { Liquid } from '../liquid'
import { Filter } from './filter'
import { FilterToken } from '../tokens'

export class Output extends TemplateImpl<OutputToken> implements Template {
value: Value
Expand All @@ -16,7 +17,8 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
const filters = this.value.filters
const outputEscape = liquid.options.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> {
Expand Down
2 changes: 1 addition & 1 deletion src/template/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Value {
? new Tokenizer(input, liquid.options.operators).readFilteredValue()
: input
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> {
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')
Expand Down
9 changes: 9 additions & 0 deletions test/e2e/issues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,4 +515,13 @@ describe('Issues', function () {
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')
})
})

0 comments on commit d705888

Please sign in to comment.