feat: detectRuleIndex を最後のマッチに変更する (#25)

This commit is contained in:
nrslib 2026-02-01 03:15:45 +00:00
parent 68e09e04af
commit ed1fe99c3b
2 changed files with 12 additions and 2 deletions

View File

@ -66,4 +66,13 @@ describe('detectRuleIndex', () => {
expect(detectRuleIndex('[AI_REVIEW:1]', 'ai_review')).toBe(0);
expect(detectRuleIndex('[SECURITY_FIX:2]', 'security_fix')).toBe(1);
});
it('should detect last occurrence when multiple tags exist', () => {
const content = 'Previous: [AI_REVIEW:1]\n\nActual result:\n[AI_REVIEW:2]';
expect(detectRuleIndex(content, 'ai_review')).toBe(1);
});
it('should detect last match with multiple occurrences', () => {
expect(detectRuleIndex('[PLAN:1] then [PLAN:2] finally [PLAN:3]', 'plan')).toBe(2);
});
});

View File

@ -43,8 +43,9 @@ export interface ClaudeCallOptions {
*/
export function detectRuleIndex(content: string, stepName: string): number {
const tag = stepName.toUpperCase();
const regex = new RegExp(`\\[${tag}:(\\d+)\\]`, 'i');
const match = content.match(regex);
const regex = new RegExp(`\\[${tag}:(\\d+)\\]`, 'gi');
const matches = [...content.matchAll(regex)];
const match = matches.at(-1);
if (match?.[1]) {
const index = Number.parseInt(match[1], 10) - 1;
return index >= 0 ? index : -1;