Skip to content

Commit d705888

Browse files
committed
feat: expose FilterToken to filter this, #762
1 parent 59da3fa commit d705888

File tree

7 files changed

+36
-20
lines changed

7 files changed

+36
-20
lines changed

src/parser/tokenizer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ export class Tokenizer {
8080
readFilter (): FilterToken | null {
8181
this.skipBlank()
8282
if (this.end()) return null
83-
this.assert(this.peek() === '|', `expected "|" before filter`)
84-
this.p++
85-
const begin = this.p
83+
this.assert(this.read() === '|', `expected "|" before filter`)
8684
const name = this.readIdentifier()
8785
if (!name.size()) {
8886
this.assert(this.end(), `expected filter name`)
@@ -103,7 +101,7 @@ export class Tokenizer {
103101
} else {
104102
throw this.error('expected ":" after filter name')
105103
}
106-
return new FilterToken(name.getText(), args, this.input, begin, this.p, this.file)
104+
return new FilterToken(name.getText(), args, this.input, name.begin, this.p, this.file)
107105
}
108106

109107
readFilterArg (): FilterArg | undefined {
@@ -298,7 +296,9 @@ export class Tokenizer {
298296
end () {
299297
return this.p >= this.N
300298
}
301-
299+
read () {
300+
return this.input[this.p++]
301+
}
302302
readTo (end: string): number {
303303
while (this.p < this.N) {
304304
++this.p

src/template/filter-impl-options.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Context } from '../context'
22
import type { Liquid } from '../liquid'
3+
import type { FilterToken } from '../tokens'
34

45
export interface FilterImpl {
56
context: Context;
7+
token: FilterToken;
68
liquid: Liquid;
79
}
810

src/template/filter.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ describe('filter', function () {
77
const ctx = new Context({ thirty: 30 })
88
const liquid = { testVersion: '1.0' } as any
99
it('should not change input if filter not registered', async function () {
10-
const filter = new Filter('foo', undefined as any, [], liquid)
10+
const filter = new Filter({ name: 'foo', args: [] } as any, undefined as any, liquid)
1111
expect(await toPromise(filter.render('value', ctx))).toBe('value')
1212
})
1313

1414
it('should call filter impl with correct arguments', async function () {
1515
const spy = jest.fn()
1616
const thirty = new NumberToken('30', 0, 2, undefined)
17-
const filter = new Filter('foo', spy, [thirty], liquid)
17+
const filter = new Filter({ name: 'foo', args: [thirty] } as any, spy, liquid)
1818
await toPromise(filter.render('foo', ctx))
1919
expect(spy).toHaveBeenCalledWith('foo', 30)
2020
})
@@ -24,37 +24,37 @@ describe('filter', function () {
2424
return `${this.liquid.testVersion}: ${val + diff}`
2525
})
2626
const ten = new NumberToken('10', 0, 2, undefined)
27-
const filter = new Filter('add', spy, [ten], liquid)
27+
const filter = new Filter({ name: 'add', args: [ten] } as any, spy, liquid)
2828
const val = await toPromise(filter.render('thirty', ctx))
2929
expect(val).toEqual('1.0: 40')
3030
})
3131
it('should render a simple filter', async function () {
32-
expect(await toPromise(new Filter('upcase', (x: string) => x.toUpperCase(), [], liquid).render('foo', ctx))).toBe('FOO')
32+
expect(await toPromise(new Filter({ name: 'upcase', args: [] } as any, (x: string) => x.toUpperCase(), liquid).render('foo', ctx))).toBe('FOO')
3333
})
3434
it('should reject promise when filter throws', async function () {
35-
const filter = new Filter('foo', function * () { throw new Error('intended') }, [], liquid)
35+
const filter = new Filter({ name: 'foo', args: [] } as any, function * () { throw new Error('intended') }, liquid)
3636
expect(toPromise(filter.render('foo', ctx))).rejects.toMatchObject({
3737
message: 'intended'
3838
})
3939
})
4040
it('should render filters with argument', async function () {
4141
const two = new NumberToken('2', 0, 1, undefined)
42-
expect(await toPromise(new Filter('add', (a: number, b: number) => a + b, [two], liquid).render(3, ctx))).toBe(5)
42+
expect(await toPromise(new Filter({ name: 'add', args: [two] } as any, (a: number, b: number) => a + b, liquid).render(3, ctx))).toBe(5)
4343
})
4444

4545
it('should render filters with multiple arguments', async function () {
4646
const two = new NumberToken('2', 0, 1, undefined)
4747
const c = new QuotedToken('"c"', 0, 3)
48-
expect(await toPromise(new Filter('add', (a: number, b: number, c: number) => a + b + c, [two, c], liquid).render(3, ctx))).toBe('5c')
48+
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')
4949
})
5050

5151
it('should pass Objects/Drops as it is', async function () {
5252
class Foo {}
53-
expect(await toPromise(new Filter('name', (a: any) => a.constructor.name, [], liquid).render(new Foo(), ctx))).toBe('Foo')
53+
expect(await toPromise(new Filter({ name: 'name', args: [] } as any, (a: any) => a.constructor.name, liquid).render(new Foo(), ctx))).toBe('Foo')
5454
})
5555

5656
it('should support key value pairs', async function () {
5757
const two = new NumberToken('2', 0, 1, undefined)
58-
expect(await toPromise(new Filter('add', (a: number, b: number[]) => b[0] + ':' + (a + b[1]), [['num', two]], liquid).render(3, ctx))).toBe('num:5')
58+
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')
5959
})
6060
})

src/template/filter.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ import { identify, isFunction } from '../util/underscore'
44
import { FilterHandler, FilterImplOptions } from './filter-impl-options'
55
import { FilterArg, isKeyValuePair } from '../parser/filter-arg'
66
import { Liquid } from '../liquid'
7+
import { FilterToken } from '../tokens'
78

89
export class Filter {
910
public name: string
1011
public args: FilterArg[]
1112
public readonly raw: boolean
1213
private handler: FilterHandler
1314
private liquid: Liquid
15+
private token: FilterToken
1416

15-
public constructor (name: string, options: FilterImplOptions | undefined, args: FilterArg[], liquid: Liquid) {
16-
this.name = name
17+
public constructor (token: FilterToken, options: FilterImplOptions | undefined, liquid: Liquid) {
18+
this.token = token
19+
this.name = token.name
1720
this.handler = isFunction(options)
1821
? options
1922
: (isFunction(options?.handler) ? options!.handler : identify)
2023
this.raw = !isFunction(options) && !!options?.raw
21-
this.args = args
24+
this.args = token.args
2225
this.liquid = liquid
2326
}
2427
public * render (value: any, context: Context): IterableIterator<unknown> {
@@ -27,6 +30,6 @@ export class Filter {
2730
if (isKeyValuePair(arg)) argv.push([arg[0], yield evalToken(arg[1], context)])
2831
else argv.push(yield evalToken(arg, context))
2932
}
30-
return yield this.handler.apply({ context, liquid: this.liquid }, [value, ...argv])
33+
return yield this.handler.apply({ context, token: this.token, liquid: this.liquid }, [value, ...argv])
3134
}
3235
}

src/template/output.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OutputToken } from '../tokens/output-token'
66
import { Tokenizer } from '../parser'
77
import { Liquid } from '../liquid'
88
import { Filter } from './filter'
9+
import { FilterToken } from '../tokens'
910

1011
export class Output extends TemplateImpl<OutputToken> implements Template {
1112
value: Value
@@ -16,7 +17,8 @@ export class Output extends TemplateImpl<OutputToken> implements Template {
1617
const filters = this.value.filters
1718
const outputEscape = liquid.options.outputEscape
1819
if (!filters[filters.length - 1]?.raw && outputEscape) {
19-
filters.push(new Filter(toString.call(outputEscape), outputEscape, [], liquid))
20+
const token = new FilterToken(toString.call(outputEscape), [], '', 0, 0)
21+
filters.push(new Filter(token, outputEscape, liquid))
2022
}
2123
}
2224
public * render (ctx: Context, emitter: Emitter): IterableIterator<unknown> {

src/template/value.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class Value {
1818
? new Tokenizer(input, liquid.options.operators).readFilteredValue()
1919
: input
2020
this.initial = token.initial
21-
this.filters = token.filters.map(({ name, args }) => new Filter(name, this.getFilter(liquid, name), args, liquid))
21+
this.filters = token.filters.map(token => new Filter(token, this.getFilter(liquid, token.name), liquid))
2222
}
2323
public * value (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
2424
lenient = lenient || (ctx.opts.lenientIf && this.filters.length > 0 && this.filters[0].name === 'default')

test/e2e/issues.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,13 @@ describe('Issues', function () {
515515
expect(LiquidError.is(err) && err.originalError).toHaveProperty('message', 'intended')
516516
}
517517
})
518+
it('getting current line number and template name from a filter #762', () => {
519+
const engine = new Liquid()
520+
engine.registerFilter('pos', function (val: string) {
521+
const [line, col] = this.token.getPosition()
522+
return `[${line},${col}] ${val}`
523+
})
524+
const result = engine.parseAndRenderSync(`\n{{ "foo" | pos }}`)
525+
expect(result).toEqual('\n[2,12] foo')
526+
})
518527
})

0 commit comments

Comments
 (0)