fix bad re.lastIndex usage

This commit is contained in:
benceruleanlu
2025-04-30 15:45:23 -04:00
parent d366a1e8ef
commit 9272179bce
2 changed files with 14 additions and 2 deletions

View File

@@ -10,14 +10,19 @@ import { reactive } from 'vue'
*/ */
function tokenize(expr: string): { t: string }[] { function tokenize(expr: string): { t: string }[] {
const tokens: { t: string }[] = [] const tokens: { t: string }[] = []
let pos = 0
const re = const re =
/\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|==|!=|&&|\|\||[A-Za-z0-9_.]+|!|\(|\))\s*/g /\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|==|!=|&&|\|\||[A-Za-z0-9_.]+|!|\(|\))\s*/g
let m: RegExpExecArray | null let m: RegExpExecArray | null
while ((m = re.exec(expr))) { while ((m = re.exec(expr))) {
if (m.index !== pos) {
throw new Error(`Invalid character in expression at pos ${pos}`)
}
tokens.push({ t: m[1] }) tokens.push({ t: m[1] })
pos = re.lastIndex
} }
if (re.lastIndex !== expr.length) { if (pos !== expr.length) {
throw new Error(`Invalid character in expression at pos ${re.lastIndex}`) throw new Error(`Invalid character in expression at pos ${pos}`)
} }
return tokens return tokens
} }

View File

@@ -11,6 +11,13 @@ describe('evalAst via evaluateCondition', () => {
store = useContextKeyStore() store = useContextKeyStore()
}) })
it('evaluates logical OR correctly', () => {
store.setContextKey('a', true)
store.setContextKey('b', false)
const result = store.evaluateCondition('a || b')
expect(result).toBe(true)
})
it('evaluates logical AND and NOT correctly', () => { it('evaluates logical AND and NOT correctly', () => {
store.setContextKey('a', true) store.setContextKey('a', true)
store.setContextKey('b', false) store.setContextKey('b', false)