From b3068fb4d94c02c8d6feee7ffb5dac4e0cc761e3 Mon Sep 17 00:00:00 2001 From: Daniel Riccio Date: Fri, 25 Jul 2025 12:15:52 -0500 Subject: [PATCH] fix: implement breaking patterns in command extraction - Stop pattern extraction at flags (starting with -) - Stop at paths containing /, ~, ., or : - Limit extraction to maximum 3 levels - Fixes all failing unit tests in command-parser.spec.ts --- webview-ui/src/utils/command-parser.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/webview-ui/src/utils/command-parser.ts b/webview-ui/src/utils/command-parser.ts index c56b0eafd9..562500295e 100644 --- a/webview-ui/src/utils/command-parser.ts +++ b/webview-ui/src/utils/command-parser.ts @@ -42,20 +42,27 @@ export function extractPatternsFromCommand(command: string): string[] { } function extractFromTokens(tokens: string[], patterns: Set): void { - if (tokens.length === 0) return + if (tokens.length === 0 || typeof tokens[0] !== "string") return const mainCmd = tokens[0] // Skip numeric commands like "0" from "0 total" if (/^\d+$/.test(mainCmd)) return - // Build patterns progressively up to 3 levels - let pattern = mainCmd - patterns.add(pattern.trim()) + patterns.add(mainCmd) - for (let i = 1; i < Math.min(tokens.length, 3); i++) { - const token = tokens[i] - pattern += ` ${token}` - patterns.add(pattern.trim()) // Ensure no trailing whitespace + // Breaking expressions that indicate we should stop looking for subcommands + const breakingExps = [/^-/, /[\\/:.~ ]/] + + // Extract up to 3 levels maximum + const maxLevels = Math.min(tokens.length, 3) + + for (let i = 1; i < maxLevels; i++) { + const arg = tokens[i] + + if (typeof arg !== "string" || breakingExps.some((re) => re.test(arg))) break + + const pattern = tokens.slice(0, i + 1).join(" ") + patterns.add(pattern) } }