diff options
| author | Dax Raad <[email protected]> | 2025-05-27 02:26:53 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-27 02:26:53 -0400 |
| commit | 98b5390a22e2dc07c94727e19aad19d8b71d5a4d (patch) | |
| tree | 06c96e319af72ac8bda2f645b060662e27f24d93 /js/src/tool/grep.ts | |
| parent | c040baae118787cd0573e5b674a2a225f36d898c (diff) | |
| download | opencode-98b5390a22e2dc07c94727e19aad19d8b71d5a4d.tar.gz opencode-98b5390a22e2dc07c94727e19aad19d8b71d5a4d.zip | |
Refactor grep tool output generation and fix ls directory traversal bug
🤖 Generated with opencode
Co-Authored-By: opencode <[email protected]>
Diffstat (limited to 'js/src/tool/grep.ts')
| -rw-r--r-- | js/src/tool/grep.ts | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/js/src/tool/grep.ts b/js/src/tool/grep.ts index b67a4790e..21f242244 100644 --- a/js/src/tool/grep.ts +++ b/js/src/tool/grep.ts @@ -81,7 +81,7 @@ function globToRegex(glob: string): string { regexPattern = regexPattern.replaceAll("?", "."); // Handle {a,b,c} patterns - regexPattern = regexPattern.replace(/\{([^}]+)\}/g, (match, inner) => { + regexPattern = regexPattern.replace(/\{([^}]+)\}/g, (_, inner) => { return "(" + inner.replace(/,/g, "|") + ")"; }); @@ -301,36 +301,36 @@ export const grep = Tool.define({ 100, ); - let output: string; if (matches.length === 0) { - output = "No files found"; - } else { - const lines = [`Found ${matches.length} matches`]; - - let currentFile = ""; - for (const match of matches) { - if (currentFile !== match.path) { - if (currentFile !== "") { - lines.push(""); - } - currentFile = match.path; - lines.push(`${match.path}:`); - } - if (match.lineNum > 0) { - lines.push(` Line ${match.lineNum}: ${match.lineText}`); - } else { - lines.push(` ${match.path}`); + return { + metadata: { matches: 0, truncated }, + output: "No files found" + }; + } + + const lines = [`Found ${matches.length} matches`]; + + let currentFile = ""; + for (const match of matches) { + if (currentFile !== match.path) { + if (currentFile !== "") { + lines.push(""); } + currentFile = match.path; + lines.push(`${match.path}:`); } - - if (truncated) { - lines.push(""); - lines.push( - "(Results are truncated. Consider using a more specific path or pattern.)", - ); + if (match.lineNum > 0) { + lines.push(` Line ${match.lineNum}: ${match.lineText}`); + } else { + lines.push(` ${match.path}`); } + } - output = lines.join("\n"); + if (truncated) { + lines.push(""); + lines.push( + "(Results are truncated. Consider using a more specific path or pattern.)", + ); } return { @@ -338,7 +338,7 @@ export const grep = Tool.define({ matches: matches.length, truncated, }, - output, + output: lines.join("\n"), }; }, }); |
