Skip to content

Commit

Permalink
style: update code style and types
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 22, 2023
1 parent 9bbfa3c commit 59f30fa
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/parser/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './tokenizer'
export * from './parser'
export * from './parse-stream'
export * from './parse-string-literal'
export * from './token-kind'
6 changes: 3 additions & 3 deletions src/parser/tokenizer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ describe('Tokenizer', function () {
const token: NumberToken = new Tokenizer('123').readValueOrThrow() as any
expect(token).toBeInstanceOf(NumberToken)
expect(token.getText()).toBe('123')
expect(token.number).toBe(123)
expect(token.value).toBe(123)
})
it('should read negative number', () => {
const token: NumberToken = new Tokenizer('-123').readValueOrThrow() as any
expect(token).toBeInstanceOf(NumberToken)
expect(token.getText()).toBe('-123')
expect(token.number).toBe(-123)
expect(token.value).toBe(-123)
})
it('should read float number', () => {
const token: NumberToken = new Tokenizer('1.23').readValueOrThrow() as any
expect(token).toBeInstanceOf(NumberToken)
expect(token.getText()).toBe('1.23')
expect(token.number).toBe(1.23)
expect(token.value).toBe(1.23)
})
it('should treat 1.2.3 as property read', () => {
const token: PropertyAccessToken = new Tokenizer('1.2.3').readValueOrThrow() as any
Expand Down
6 changes: 3 additions & 3 deletions src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FilteredValueToken, TagToken, HTMLToken, HashToken, QuotedToken, LiquidTagToken, OutputToken, ValueToken, Token, RangeToken, FilterToken, TopLevelToken, PropertyAccessToken, OperatorToken, LiteralToken, IdentifierToken, NumberToken } from '../tokens'
import { OperatorHandler } from '../render/operator'
import { TrieNode, isQuotedToken, isWordToken, Trie, createTrie, ellipsis, literalValues, TokenizationError, TYPES, QUOTE, BLANK, IDENTIFIER, NUMBER, SIGN } from '../util'
import { TrieNode, LiteralValue, Trie, createTrie, ellipsis, literalValues, TokenizationError, TYPES, QUOTE, BLANK, IDENTIFIER, NUMBER, SIGN } from '../util'
import { Operators, Expression } from '../render'
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
import { FilterArg } from './filter-arg'
Expand All @@ -11,7 +11,7 @@ export class Tokenizer {
N: number
private rawBeginAt = -1
private opTrie: Trie<OperatorHandler>
private literalTrie: Trie<typeof literalValues[keyof typeof literalValues]>
private literalTrie: Trie<LiteralValue>

constructor (
public input: string,
Expand Down Expand Up @@ -306,7 +306,7 @@ export class Tokenizer {
this.skipBlank()
const begin = this.p
const variable = this.readLiteral() || this.readQuoted() || this.readRange() || this.readNumber()
const props: (QuotedToken | IdentifierToken)[] = []
const props: (ValueToken | IdentifierToken)[] = []
while (true) {
if (this.peek() === '[') {
this.p++
Expand Down
6 changes: 5 additions & 1 deletion src/render/expression.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Tokenizer } from '../parser'
import { Drop } from '../drop'
import { QuotedToken } from '../tokens'
import { Context } from '../context'
import { toPromise, toValueSync } from '../util'
import { evalQuotedToken } from './expression'

describe('Expression', function () {
const ctx = new Context({})
Expand Down Expand Up @@ -32,7 +34,9 @@ describe('Expression', function () {
expect(await toPromise(create('"foo"').evaluate(ctx, false))).toBe('foo')
expect(await toPromise(create('false').evaluate(ctx, false))).toBe(false)
})

it('should support evalQuotedToken()', async function () {
expect(evalQuotedToken(new QuotedToken('"foo"', 0, 5))).toBe('foo')
})
it('should eval property access', async function () {
const ctx = new Context({
foo: { bar: 'BAR' },
Expand Down
15 changes: 7 additions & 8 deletions src/render/expression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RangeToken, OperatorToken, Token, LiteralToken, NumberToken, PropertyAccessToken, QuotedToken, OperatorType, operatorTypes } from '../tokens'
import { QuotedToken, RangeToken, OperatorToken, Token, LiteralToken, PropertyAccessToken, OperatorType, operatorTypes } from '../tokens'
import { isQuotedToken, isWordToken, isNumberToken, isLiteralToken, isRangeToken, isPropertyAccessToken, UndefinedVariableError, range, isOperatorToken, literalValues, assert } from '../util'
import { parseStringLiteral } from '../parser'
import type { Context } from '../context'
import type { UnaryOperatorHandler } from '../render'

Expand Down Expand Up @@ -39,9 +38,9 @@ export function * evalToken (token: Token | undefined, ctx: Context, lenient = f
if (isPropertyAccessToken(token)) return yield evalPropertyAccessToken(token, ctx, lenient)
if (isRangeToken(token)) return yield evalRangeToken(token, ctx)
if (isLiteralToken(token)) return evalLiteralToken(token)
if (isNumberToken(token)) return token.number
if (isNumberToken(token)) return token.value
if (isQuotedToken(token)) return token.value
if (isWordToken(token)) return token.content
if (isQuotedToken(token)) return evalQuotedToken(token)
}

function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, lenient: boolean): IterableIterator<unknown> {
Expand All @@ -62,14 +61,14 @@ function * evalPropertyAccessToken (token: PropertyAccessToken, ctx: Context, le
}
}

export function evalQuotedToken (token: QuotedToken) {
return parseStringLiteral(token.getText())
}

function evalLiteralToken (token: LiteralToken) {
return literalValues[token.literal]
}

export function evalQuotedToken (token: QuotedToken) {
return token.value
}

function * evalRangeToken (token: RangeToken, ctx: Context) {
const low: number = yield evalToken(token.lhs, ctx)
const high: number = yield evalToken(token.rhs, ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseStringLiteral } from './parse-string-literal'
import { parseStringLiteral } from './string'

describe('parseStringLiteral()', function () {
it('should parse octal escape', () => {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/template/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context } from '../context'
import { toPromise } from '../util'
import { IdentifierToken, NumberToken, QuotedToken } from '../tokens'
import { NumberToken, QuotedToken } from '../tokens'
import { Filter } from './filter'

describe('filter', function () {
Expand Down
3 changes: 3 additions & 0 deletions src/tokens/literal-token.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Token } from './token'
import { TokenKind } from '../parser'
import { literalValues, LiteralValue } from '../util'

export class LiteralToken extends Token {
public value: LiteralValue
public literal: string
public constructor (
public input: string,
Expand All @@ -11,5 +13,6 @@ export class LiteralToken extends Token {
) {
super(TokenKind.Literal, input, begin, end, file)
this.literal = this.getText()
this.value = literalValues[this.literal]
}
}
4 changes: 2 additions & 2 deletions src/tokens/number-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Token } from './token'
import { TokenKind } from '../parser'

export class NumberToken extends Token {
public number: number
public value: number
constructor (
public input: string,
public begin: number,
public end: number,
public file?: string
) {
super(TokenKind.Number, input, begin, end, file)
this.number = Number(this.getText())
this.value = Number(this.getText())
}
}
6 changes: 4 additions & 2 deletions src/tokens/property-access-token.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Token } from './token'
import { LiteralToken } from './literal-token'
import { ValueToken } from './value-token'
import { IdentifierToken } from './identifier-token'
import { NumberToken } from './number-token'
import { RangeToken } from './range-token'
import { QuotedToken } from './quoted-token'
import { TokenKind } from '../parser'

export class PropertyAccessToken extends Token {
constructor (
public variable: QuotedToken | RangeToken | LiteralToken | undefined,
public props: ValueToken[],
public variable: QuotedToken | RangeToken | LiteralToken | NumberToken | undefined,
public props: (ValueToken | IdentifierToken)[],
input: string,
begin: number,
end: number,
Expand Down
3 changes: 3 additions & 0 deletions src/tokens/quoted-token.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Token } from './token'
import { TokenKind } from '../parser'
import { parseStringLiteral } from '../render/string'

export class QuotedToken extends Token {
public readonly value: string
constructor (
public input: string,
public begin: number,
public end: number,
public file?: string
) {
super(TokenKind.Quoted, input, begin, end, file)
this.value = parseStringLiteral(this.getText())
}
}
3 changes: 2 additions & 1 deletion src/tokens/value-token.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RangeToken } from './range-token'
import { LiteralToken } from './literal-token'
import { NumberToken } from './number-token'
import { QuotedToken } from './quoted-token'
import { PropertyAccessToken } from './property-access-token'

export type ValueToken = RangeToken | LiteralToken | QuotedToken | PropertyAccessToken
export type ValueToken = RangeToken | LiteralToken | QuotedToken | PropertyAccessToken | NumberToken
3 changes: 3 additions & 0 deletions src/util/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export const literalValues = {
'empty': new EmptyDrop(),
'blank': new BlankDrop()
}

export type LiteralKey = keyof typeof literalValues
export type LiteralValue = typeof literalValues[LiteralKey]

0 comments on commit 59f30fa

Please sign in to comment.