fix: correct quote-handling alignment in extractBashCommands

Replace quoted strings with same-length spaces in stripQuotedStrings so
separator indices in the stripped string map correctly to the original.
Add test coverage for quoted separators and isBashTool.
This commit is contained in:
Rafael Calleja 2026-04-14 07:50:53 +02:00
parent b75c2663b4
commit 45ce697eea
2 changed files with 16 additions and 2 deletions

View file

@ -8,7 +8,7 @@ const BASH_TOOLS = new Set(['Bash', 'BashTool', 'PowerShellTool'])
* command boundaries.
*/
function stripQuotedStrings(command: string): string {
return command.replace(/"[^"]*"|'[^']*'/g, '""')
return command.replace(/"[^"]*"|'[^']*'/g, match => ' '.repeat(match.length))
}
export function extractBashCommands(command: string): string[] {

View file

@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { extractBashCommands } from '../src/bash-utils.js'
import { extractBashCommands, isBashTool } from '../src/bash-utils.js'
describe('extractBashCommands', () => {
it('extracts single command', () => {
@ -49,4 +49,18 @@ describe('extractBashCommands', () => {
it('handles command with quotes containing separators', () => {
expect(extractBashCommands('echo "hello && world"')).toEqual(['echo'])
})
it('handles quoted separators followed by real separator', () => {
expect(extractBashCommands('echo "hello && world" && git status')).toEqual(['echo', 'git'])
})
it('handles single-quoted separators', () => {
expect(extractBashCommands("echo 'hello && world'")).toEqual(['echo'])
})
})
describe('isBashTool', () => {
it('recognizes Bash', () => { expect(isBashTool('Bash')).toBe(true) })
it('recognizes BashTool', () => { expect(isBashTool('BashTool')).toBe(true) })
it('rejects unknown tools', () => { expect(isBashTool('Read')).toBe(false) })
})