| 14 | * @returns List of import specifiers in the source file. |
| 15 | */ |
| 16 | export function getModuleReferences( |
| 17 | initialNode: ts.SourceFile, |
| 18 | ignoreTypeOnlyChecks: boolean, |
| 19 | ): string[] { |
| 20 | const references: string[] = []; |
| 21 | const visitNode = (node: ts.Node) => { |
| 22 | if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) { |
| 23 | // When ignoreTypeOnlyChecks are enabled, if the declaration is found to be type only, it is skipped. |
| 24 | if ( |
| 25 | ignoreTypeOnlyChecks && |
| 26 | ((ts.isImportDeclaration(node) && node.importClause?.isTypeOnly) || |
| 27 | (ts.isExportDeclaration(node) && node.isTypeOnly)) |
| 28 | ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if (node.moduleSpecifier !== undefined && ts.isStringLiteral(node.moduleSpecifier)) { |
| 33 | references.push(node.moduleSpecifier.text); |
| 34 | } |
| 35 | } |
| 36 | ts.forEachChild(node, visitNode); |
| 37 | }; |
| 38 | |
| 39 | ts.forEachChild(initialNode, visitNode); |
| 40 | |
| 41 | return references; |
| 42 | } |