| 136 | } |
| 137 | |
| 138 | function findCommandNode(node: Node, parent: Node | null): Node | null { |
| 139 | const { type, children } = node |
| 140 | |
| 141 | if (COMMAND_TYPES.has(type)) return node |
| 142 | |
| 143 | // Variable assignment followed by command |
| 144 | if (type === 'variable_assignment' && parent) { |
| 145 | return ( |
| 146 | parent.children.find( |
| 147 | c => COMMAND_TYPES.has(c.type) && c.startIndex > node.startIndex, |
| 148 | ) ?? null |
| 149 | ) |
| 150 | } |
| 151 | |
| 152 | // Pipeline: recurse into first child (which may be a redirected_statement) |
| 153 | if (type === 'pipeline') { |
| 154 | for (const child of children) { |
| 155 | const result = findCommandNode(child, node) |
| 156 | if (result) return result |
| 157 | } |
| 158 | return null |
| 159 | } |
| 160 | |
| 161 | // Redirected statement: find the command inside |
| 162 | if (type === 'redirected_statement') { |
| 163 | return children.find(c => COMMAND_TYPES.has(c.type)) ?? null |
| 164 | } |
| 165 | |
| 166 | // Recursive search |
| 167 | for (const child of children) { |
| 168 | const result = findCommandNode(child, node) |
| 169 | if (result) return result |
| 170 | } |
| 171 | |
| 172 | return null |
| 173 | } |
| 174 | |
| 175 | function extractEnvVars(commandNode: Node | null): string[] { |
| 176 | if (!commandNode || commandNode.type !== 'command') return [] |