* Detects when a `#` comment contains quote characters that would desync * downstream quote trackers (like extractQuotedContent). * * In bash, everything after an unquoted `#` on a line is a comment — quote * characters inside the comment are literal text, not quote toggles. But our * quote-tra
( context: ValidationContext, )
| 1988 | * approve manually). |
| 1989 | */ |
| 1990 | function validateCommentQuoteDesync( |
| 1991 | context: ValidationContext, |
| 1992 | ): PermissionResult { |
| 1993 | // Tree-sitter path: tree-sitter correctly identifies comment nodes and |
| 1994 | // quoted content. The desync concern is about regex quote tracking being |
| 1995 | // confused by quote characters inside comments. When tree-sitter provides |
| 1996 | // the quote context, this desync cannot happen — the AST is authoritative |
| 1997 | // regardless of whether the command contains a comment. |
| 1998 | if (context.treeSitter) { |
| 1999 | return { |
| 2000 | behavior: 'passthrough', |
| 2001 | message: 'Tree-sitter quote context is authoritative', |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | const { originalCommand } = context |
| 2006 | |
| 2007 | // Track quote state character-by-character using the same (correct) logic |
| 2008 | // as extractQuotedContent: single quotes don't toggle inside double quotes. |
| 2009 | // When we encounter an unquoted `#`, check if the rest of the line (until |
| 2010 | // newline) contains any quote characters. |
| 2011 | let inSingleQuote = false |
| 2012 | let inDoubleQuote = false |
| 2013 | let escaped = false |
| 2014 | |
| 2015 | for (let i = 0; i < originalCommand.length; i++) { |
| 2016 | const char = originalCommand[i] |
| 2017 | |
| 2018 | if (escaped) { |
| 2019 | escaped = false |
| 2020 | continue |
| 2021 | } |
| 2022 | |
| 2023 | if (inSingleQuote) { |
| 2024 | if (char === "'") inSingleQuote = false |
| 2025 | continue |
| 2026 | } |
| 2027 | |
| 2028 | if (char === '\\') { |
| 2029 | escaped = true |
| 2030 | continue |
| 2031 | } |
| 2032 | |
| 2033 | if (inDoubleQuote) { |
| 2034 | if (char === '"') inDoubleQuote = false |
| 2035 | // Single quotes inside double quotes are literal — no toggle |
| 2036 | continue |
| 2037 | } |
| 2038 | |
| 2039 | if (char === "'") { |
| 2040 | inSingleQuote = true |
| 2041 | continue |
| 2042 | } |
| 2043 | |
| 2044 | if (char === '"') { |
| 2045 | inDoubleQuote = true |
| 2046 | continue |
| 2047 | } |
nothing calls this directly
no test coverage detected