Block "type" and its label values. Returns null if the block is malformed.
(block: SyntaxNode, source: string)
| 49 | |
| 50 | /** Block "type" and its label values. Returns null if the block is malformed. */ |
| 51 | function readBlockHeader(block: SyntaxNode, source: string): { type: string; labels: string[] } | null { |
| 52 | const named = block.namedChildren.filter((c): c is SyntaxNode => c !== null); |
| 53 | const first = named[0]; |
| 54 | if (!first || first.type !== 'identifier') return null; |
| 55 | const type = getNodeText(first, source); |
| 56 | const labels: string[] = []; |
| 57 | for (let i = 1; i < named.length; i++) { |
| 58 | const child = named[i]; |
| 59 | if (!child) continue; |
| 60 | if (child.type === 'string_lit') { |
| 61 | labels.push(stringLitValue(child, source)); |
| 62 | } else if (child.type === 'identifier') { |
| 63 | // HCL allows unquoted identifier labels (rare in Terraform but legal). |
| 64 | labels.push(getNodeText(child, source)); |
| 65 | } else { |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | return { type, labels }; |
| 70 | } |
| 71 | |
| 72 | /** Find the `body` child of a block (it's after the labels and block_start). */ |
| 73 | function getBlockBody(block: SyntaxNode): SyntaxNode | null { |
no test coverage detected