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 }[] {
const tokens: { t: string }[] = []
let pos = 0
const re =
/\s*("(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|==|!=|&&|\|\||[A-Za-z0-9_.]+|!|\(|\))\s*/g
let m: RegExpExecArray | null
while ((m = re.exec(expr))) {
if (m.index !== pos) {
throw new Error(`Invalid character in expression at pos ${pos}`)
}
tokens.push({ t: m[1] })
pos = re.lastIndex
}
if (re.lastIndex !== expr.length) {
throw new Error(`Invalid character in expression at pos ${re.lastIndex}`)
if (pos !== expr.length) {
throw new Error(`Invalid character in expression at pos ${pos}`)
}
return tokens
}

View File

@@ -11,6 +11,13 @@ describe('evalAst via evaluateCondition', () => {
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', () => {
store.setContextKey('a', true)
store.setContextKey('b', false)