(sourceRootDir: string, node?: ts.Node)
| 103 | * Calculate the node's namespace according to the file it is defined. |
| 104 | */ |
| 105 | export function getNamespaceFromNode(sourceRootDir: string, node?: ts.Node): string | undefined { |
| 106 | if (!node) |
| 107 | return; |
| 108 | const sourceFile = node.getSourceFile(); |
| 109 | if (sourceFile.isDeclarationFile) { |
| 110 | const {fileName} = sourceFile; |
| 111 | if (fileName.includes('/node_modules/@types/node/') || |
| 112 | fileName.endsWith('/node_modules/typescript/lib/lib.dom.d.ts')) |
| 113 | return 'compilets::nodejs'; |
| 114 | if (fileName.match(/node_modules\/typescript\/lib\/lib\.es.*\.d\.ts$/)) |
| 115 | return 'compilets'; |
| 116 | throw new Error(`Can not get namespace for declaration file "${fileName}"`); |
| 117 | } |
| 118 | const fileName = path.relative(sourceRootDir, sourceFile.fileName); |
| 119 | return getNamespaceFromFileName(fileName); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Return the computed namespace from relative fileName. |
no test coverage detected