(rootNode: unknown)
| 446 | * Extract dangerous pattern information from the AST. |
| 447 | */ |
| 448 | export function extractDangerousPatterns(rootNode: unknown): DangerousPatterns { |
| 449 | const n = rootNode as TreeSitterNode |
| 450 | let hasCommandSubstitution = false |
| 451 | let hasProcessSubstitution = false |
| 452 | let hasParameterExpansion = false |
| 453 | let hasHeredoc = false |
| 454 | let hasComment = false |
| 455 | |
| 456 | function walk(node: TreeSitterNode): void { |
| 457 | switch (node.type) { |
| 458 | case 'command_substitution': |
| 459 | hasCommandSubstitution = true |
| 460 | break |
| 461 | case 'process_substitution': |
| 462 | hasProcessSubstitution = true |
| 463 | break |
| 464 | case 'expansion': |
| 465 | hasParameterExpansion = true |
| 466 | break |
| 467 | case 'heredoc_redirect': |
| 468 | hasHeredoc = true |
| 469 | break |
| 470 | case 'comment': |
| 471 | hasComment = true |
| 472 | break |
| 473 | } |
| 474 | |
| 475 | for (const child of node.children) { |
| 476 | if (child) walk(child) |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | walk(n) |
| 481 | |
| 482 | return { |
| 483 | hasCommandSubstitution, |
| 484 | hasProcessSubstitution, |
| 485 | hasParameterExpansion, |
| 486 | hasHeredoc, |
| 487 | hasComment, |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Perform complete tree-sitter analysis of a command. |
no test coverage detected