mirror of
https://github.com/Jeuners/ECC.git
synced 2026-09-09 15:02:30 +02: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:
parent
02120fbf5f
commit
6f95dbe7ba
3 changed files with 192 additions and 1 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue