Skip to content

Commit 542968e

Browse files
committed
refactor: remove unused lexical regexps
1 parent 3b7351d commit 542968e

File tree

9 files changed

+30
-232
lines changed

9 files changed

+30
-232
lines changed

src/liquid-options.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export interface LiquidOptions {
3333
greedy?: boolean
3434
}
3535

36-
export interface NormalizedOptions extends LiquidOptions {
36+
interface NormalizedOptions extends LiquidOptions {
3737
root?: string[]
3838
}
3939

src/parser/lexical.ts

-43
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const quoted = new RegExp(`${singleQuoted.source}|${doubleQuoted.source}`
55
export const quoteBalanced = new RegExp(`(?:${quoted.source}|[^'"])*`)
66

77
// basic types
8-
export const integer = /-?\d+/
98
export const number = /[+-]?(?:\d+\.?\d*|\.?\d+)/
109
export const bool = /true|false/
1110

@@ -28,59 +27,17 @@ export const hashCapture = new RegExp(`(${identifier.source})\\s*:\\s*(${value.s
2827

2928
// full match
3029
export const tagLine = new RegExp(`^\\s*(${identifier.source})\\s*([\\s\\S]*?)\\s*$`)
31-
export const literalLine = new RegExp(`^${literal.source}$`, 'i')
32-
export const variableLine = new RegExp(`^${variable.source}$`)
3330
export const numberLine = new RegExp(`^${number.source}$`)
3431
export const boolLine = new RegExp(`^${bool.source}$`, 'i')
3532
export const quotedLine = new RegExp(`^${quoted.source}$`)
3633
export const rangeLine = new RegExp(`^${rangeCapture.source}$`)
37-
export const integerLine = new RegExp(`^${integer.source}$`)
38-
39-
// filter related
40-
export const valueDeclaration = new RegExp(`(?:${identifier.source}\\s*:\\s*)?${value.source}`)
41-
export const valueList = new RegExp(`${valueDeclaration.source}(\\s*,\\s*${valueDeclaration.source})*`)
42-
export const filter = new RegExp(`${identifier.source}(?:\\s*:\\s*${valueList.source})?`, 'g')
43-
export const filterCapture = new RegExp(`(${identifier.source})(?:\\s*:\\s*(${valueList.source}))?`)
44-
export const filterLine = new RegExp(`^${filterCapture.source}$`)
4534

4635
export const operators = [
4736
/\s+or\s+/,
4837
/\s+and\s+/,
4938
/==|!=|<=|>=|<|>|\s+contains\s+/
5039
]
5140

52-
export function isInteger (str: string) {
53-
return integerLine.test(str)
54-
}
55-
56-
export function isLiteral (str: string) {
57-
return literalLine.test(str)
58-
}
59-
6041
export function isRange (str: string) {
6142
return rangeLine.test(str)
6243
}
63-
64-
export function isVariable (str: string) {
65-
return variableLine.test(str)
66-
}
67-
68-
export function matchValue (str: string) {
69-
return value.exec(str)
70-
}
71-
72-
export function parseLiteral (str: string) {
73-
let res = str.match(numberLine)
74-
if (res) {
75-
return Number(str)
76-
}
77-
res = str.match(boolLine)
78-
if (res) {
79-
return str.toLowerCase() === 'true'
80-
}
81-
res = str.match(quotedLine)
82-
if (res) {
83-
return str.slice(1, -1)
84-
}
85-
throw new TypeError(`cannot parse '${str}' as literal`)
86-
}

src/render/syntax.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as lexical from '../parser/lexical'
22
import assert from '../util/assert'
33
import Scope from 'src/scope/scope'
4-
import { range } from 'src/util/underscore'
4+
import { range, last } from 'src/util/underscore'
55

66
const operators = {
77
'==': (l: any, r: any) => l === r,
@@ -46,15 +46,12 @@ export function evalExp (exp: string, scope: Scope): any {
4646
export function evalValue (str: string, scope: Scope) {
4747
if (!str) return null
4848
str = str.trim()
49-
if (!str) return undefined
5049

51-
if (lexical.isLiteral(str)) {
52-
return lexical.parseLiteral(str)
53-
}
54-
if (lexical.isVariable(str)) {
55-
return scope.get(str)
56-
}
57-
throw new TypeError(`cannot eval '${str}' as value`)
50+
if (str === 'true') return true
51+
if (str === 'false') return false
52+
if (!isNaN(Number(str))) return Number(str)
53+
if ((str[0] === '"' || str[0] === "'") && str[0] === last(str)) return str.slice(1, -1)
54+
return scope.get(str)
5855
}
5956

6057
export function isTruthy (val: any): boolean {

src/scope/scope.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as _ from '../util/underscore'
22
import { __assign } from 'tslib'
3-
import * as lexical from '../parser/lexical'
43
import assert from '../util/assert'
54
import { NormalizedFullOptions, applyDefault } from '../liquid-options'
65
import BlockMode from './block-mode'
@@ -115,7 +114,7 @@ export default class Scope {
115114
j = matchRightBracket(str, i + 1)
116115
assert(j !== -1, `unbalanced []: ${str}`)
117116
name = str.slice(i + 1, j)
118-
if (!lexical.isInteger(name)) { // foo[bar] vs. foo[1]
117+
if (!/^[+-]?\d+$/.test(name)) { // foo[bar] vs. foo[1]
119118
name = String(this.get(name))
120119
}
121120
push()

src/template/tag/block-tag.ts

-45
This file was deleted.

test/stub/render.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Liquid from 'src/liquid'
22
import { expect } from 'chai'
33

4-
export const liquid = new Liquid()
4+
const liquid = new Liquid()
55

66
export const ctx = {
77
date: new Date(),

test/unit/parser/lexical.ts

-119
Original file line numberDiff line numberDiff line change
@@ -4,128 +4,9 @@ const expect = chai.expect
44
const lexical = require('src/parser/lexical')
55

66
describe('lexical', function () {
7-
it('should test filter syntax', function () {
8-
expect(lexical.filterLine.test('abs')).to.equal(true)
9-
expect(lexical.filterLine.test('plus:1')).to.equal(true)
10-
expect(lexical.filterLine.test('replace: "a", b')).to.equal(true)
11-
expect(lexical.filterLine.test('foo: a, "b"')).to.equal(true)
12-
expect(lexical.filterLine.test('abs | another')).to.equal(false)
13-
expect(lexical.filterLine.test('join: "," | another')).to.equal(false)
14-
expect(lexical.filterLine.test('obj_test: k1: "v1", k2: "v2"')).to.equal(true)
15-
})
16-
17-
it('should test boolean literal', function () {
18-
expect(lexical.isLiteral('true')).to.equal(true)
19-
expect(lexical.isLiteral('TrUE')).to.equal(true)
20-
expect(lexical.isLiteral('false')).to.equal(true)
21-
})
22-
23-
it('should test number literal', function () {
24-
expect(lexical.isLiteral('2.3')).to.equal(true)
25-
expect(lexical.isLiteral('.3')).to.equal(true)
26-
expect(lexical.isLiteral('-3.')).to.equal(true)
27-
expect(lexical.isLiteral('23')).to.equal(true)
28-
})
29-
307
it('should test range literal', function () {
318
expect(lexical.isRange('(12..32)')).to.equal(true)
329
expect(lexical.isRange('(12..foo)')).to.equal(true)
3310
expect(lexical.isRange('(foo.bar..foo)')).to.equal(true)
3411
})
35-
36-
it('should test string literal', function () {
37-
expect(lexical.isLiteral('""')).to.equal(true)
38-
expect(lexical.isLiteral('"a\'b"')).to.equal(true)
39-
expect(lexical.isLiteral("''")).to.equal(true)
40-
expect(lexical.isLiteral("'a bcd'")).to.equal(true)
41-
})
42-
43-
describe('.isVariable()', function () {
44-
it('should return true for foo', function () {
45-
expect(lexical.isVariable('foo')).to.equal(true)
46-
})
47-
it('should return true for.bar.foo', function () {
48-
expect(lexical.isVariable('foo.bar.foo')).to.equal(true)
49-
})
50-
it('should return true for foo[0].b', function () {
51-
expect(lexical.isVariable('foo[0].b')).to.equal(true)
52-
})
53-
it('should return true for 0a', function () {
54-
expect(lexical.isVariable('0a')).to.equal(true)
55-
})
56-
it('should return true for foo[a.b]', function () {
57-
expect(lexical.isVariable('foo[a.b]')).to.equal(true)
58-
})
59-
it('should return true for foo[a.b]', function () {
60-
expect(lexical.isVariable("foo['a[0]']")).to.equal(true)
61-
})
62-
it('should return true for "var-1"', function () {
63-
expect(lexical.isVariable('var-1')).to.equal(true)
64-
})
65-
it('should return true for "-var"', function () {
66-
expect(lexical.isVariable('-var')).to.equal(true)
67-
})
68-
it('should return true for "var-"', function () {
69-
expect(lexical.isVariable('var-')).to.equal(true)
70-
})
71-
it('should return true for "3-4"', function () {
72-
expect(lexical.isVariable('3-4')).to.equal(true)
73-
})
74-
})
75-
76-
it('should test none literal', function () {
77-
expect(lexical.isLiteral('2a')).to.equal(false)
78-
expect(lexical.isLiteral('"x')).to.equal(false)
79-
expect(lexical.isLiteral('a2')).to.equal(false)
80-
})
81-
82-
it('should test none variable', function () {
83-
expect(lexical.isVariable('a.')).to.equal(false)
84-
expect(lexical.isVariable('.b')).to.equal(false)
85-
expect(lexical.isVariable('.')).to.equal(false)
86-
expect(lexical.isVariable('[0][12].bar[0]')).to.equal(false)
87-
})
88-
89-
describe('.parseLiteral()', function () {
90-
it('should parse boolean literal', function () {
91-
expect(lexical.parseLiteral('true')).to.equal(true)
92-
expect(lexical.parseLiteral('TrUE')).to.equal(true)
93-
expect(lexical.parseLiteral('false')).to.equal(false)
94-
})
95-
96-
it('should parse number literal', function () {
97-
expect(lexical.parseLiteral('2.3')).to.equal(2.3)
98-
expect(lexical.parseLiteral('.32')).to.equal(0.32)
99-
expect(lexical.parseLiteral('-23.')).to.equal(-23)
100-
expect(lexical.parseLiteral('23')).to.equal(23)
101-
})
102-
103-
it('should parse string literal', function () {
104-
expect(lexical.parseLiteral('"ab\'c"')).to.equal("ab'c")
105-
})
106-
107-
it('should throw if non-literal', function () {
108-
const fn = () => lexical.parseLiteral('a')
109-
expect(fn).to.throw("cannot parse 'a' as literal")
110-
})
111-
})
112-
113-
describe('.matchValue()', function () {
114-
it('should match -5-5', function () {
115-
const match = lexical.matchValue('-5-5')
116-
expect(match && match[0]).to.equal('-5-5')
117-
})
118-
it('should match 4-3', function () {
119-
const match = lexical.matchValue('4-3')
120-
expect(match && match[0]).to.equal('4-3')
121-
})
122-
it('should match 4-3', function () {
123-
const match = lexical.matchValue('4-3')
124-
expect(match && match[0]).to.equal('4-3')
125-
})
126-
it('should match var-1', function () {
127-
const match = lexical.matchValue('var-1')
128-
expect(match && match[0]).to.equal('var-1')
129-
})
130-
})
13112
})

test/unit/render/syntax.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,26 @@ describe('expression', function () {
1818
})
1919

2020
describe('.evalValue()', function () {
21-
it('should eval literals', function () {
22-
expect(evalValue('2.3', scope)).to.equal(2.3)
23-
expect(evalValue('"foo"', scope)).to.equal('foo')
21+
it('should eval boolean literal', function () {
22+
expect(evalValue('true', scope)).to.equal(true)
23+
expect(evalValue('TrUE', scope)).to.equal(undefined)
24+
expect(evalValue('false', scope)).to.equal(false)
2425
})
25-
26-
it('should eval variables', function () {
26+
it('should eval number literal', function () {
27+
expect(evalValue('2.3', scope)).to.equal(2.3)
28+
expect(evalValue('.32', scope)).to.equal(0.32)
29+
expect(evalValue('-23.', scope)).to.equal(-23)
2730
expect(evalValue('23', scope)).to.equal(23)
31+
})
32+
it('should eval literal', function () {
33+
expect(evalValue('"ab\'c"', scope)).to.equal("ab'c")
34+
expect(evalValue("'ab\"c'", scope)).to.equal('ab"c')
35+
})
36+
it('should eval scope variables', function () {
2837
expect(evalValue('one', scope)).to.equal(1)
2938
expect(evalValue('has_value?', scope)).to.equal(true)
3039
expect(evalValue('x', scope)).to.equal('XXX')
3140
})
32-
33-
it('should throw if not valid', function () {
34-
const fn = () => evalValue('===', scope)
35-
expect(fn).to.throw("cannot eval '===' as value")
36-
})
3741
})
3842

3943
describe('.isTruthy()', function () {

test/unit/template/value.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,19 @@ describe('Value', function () {
2424
expect(tpl.filters.length).to.equal(1)
2525
expect(tpl.filters[0].args).to.eql([])
2626
})
27-
27+
it('should parse "foo | add: "foo" bar, 3"', function () {
28+
const tpl: any = new Value('foo | add: "foo" bar, 3', false)
29+
expect(tpl.initial).to.equal('foo')
30+
expect(tpl.filters.length).to.equal(1)
31+
expect(tpl.filters[0].name).to.eql('add')
32+
expect(tpl.filters[0].args).to.eql(['"foo"', '3'])
33+
})
2834
it('should parse "foo | add: 3, false"', function () {
2935
const tpl: any = new Value('foo | add: 3, "foo"', false)
3036
expect(tpl.initial).to.equal('foo')
3137
expect(tpl.filters.length).to.equal(1)
3238
expect(tpl.filters[0].args).to.eql(['3', '"foo"'])
3339
})
34-
3540
it('should parse "foo | add: "|", 3', function () {
3641
const tpl: any = new Value('foo | add: "|", 3', false)
3742
expect(tpl.initial).to.equal('foo')

0 commit comments

Comments
 (0)