(name: string, rootNode: ts.Node)
| 93 | * Walk the AST tree from the `rootNode` looking for a declaration that has the given `name`. |
| 94 | */ |
| 95 | export function walkForDeclarations(name: string, rootNode: ts.Node): DeclarationNode[] { |
| 96 | const chosenDecls: DeclarationNode[] = []; |
| 97 | rootNode.forEachChild((node) => { |
| 98 | if (ts.isVariableStatement(node)) { |
| 99 | node.declarationList.declarations.forEach((decl) => { |
| 100 | if (bindingNameEquals(decl.name, name)) { |
| 101 | chosenDecls.push(decl); |
| 102 | if (decl.initializer) { |
| 103 | chosenDecls.push(...walkForDeclarations(name, decl.initializer)); |
| 104 | } |
| 105 | } else { |
| 106 | chosenDecls.push(...walkForDeclarations(name, node)); |
| 107 | } |
| 108 | }); |
| 109 | } else if (isNamedDeclaration(node)) { |
| 110 | if (node.name !== undefined && node.name.text === name) { |
| 111 | chosenDecls.push(node); |
| 112 | } |
| 113 | chosenDecls.push(...walkForDeclarations(name, node)); |
| 114 | } else if ( |
| 115 | ts.isImportDeclaration(node) && |
| 116 | node.importClause !== undefined && |
| 117 | node.importClause.name !== undefined && |
| 118 | node.importClause.name.text === name |
| 119 | ) { |
| 120 | chosenDecls.push(node.importClause); |
| 121 | } else { |
| 122 | chosenDecls.push(...walkForDeclarations(name, node)); |
| 123 | } |
| 124 | }); |
| 125 | return chosenDecls; |
| 126 | } |
| 127 | |
| 128 | export function isNamedDeclaration(node: ts.Node): node is ts.Declaration & {name: ts.Identifier} { |
| 129 | const namedNode = node as {name?: ts.Identifier}; |
no test coverage detected
searching dependent graphs…