mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-03-30 13:43:26 +08:00
fix: grepFile global regex lastIndex bug, add 12 tests
Fix grepFile() silently skipping matches when called with /g flag regex. The global flag makes .test() stateful, causing alternating match/miss on consecutive matching lines. Strip g flag since per-line testing doesn't need global state. Add first-ever tests for evaluate-session.js (5 tests: short session, long session, missing transcript, malformed stdin, env var fallback) and suggest-compact.js (5 tests: counter increment, threshold trigger, periodic suggestions, below-threshold silence, invalid threshold).
This commit is contained in:
@@ -454,7 +454,15 @@ function grepFile(filePath, pattern) {
|
||||
|
||||
let regex;
|
||||
try {
|
||||
regex = pattern instanceof RegExp ? pattern : new RegExp(pattern);
|
||||
if (pattern instanceof RegExp) {
|
||||
// Always create a new RegExp without the 'g' flag to prevent lastIndex
|
||||
// state issues when using .test() in a loop (g flag makes .test() stateful,
|
||||
// causing alternating match/miss on consecutive matching lines)
|
||||
const flags = pattern.flags.replace('g', '');
|
||||
regex = new RegExp(pattern.source, flags);
|
||||
} else {
|
||||
regex = new RegExp(pattern);
|
||||
}
|
||||
} catch {
|
||||
return []; // Invalid regex pattern
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user