Skip to content

Commit

Permalink
Add dependencies for import types
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Aug 27, 2020
1 parent f65467b commit 456c691
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions packages/definitions-parser/src/lib/module-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,35 +275,45 @@ function findReferencedFiles(src: ts.SourceFile, packageName: string, subDirecto
* All strings referenced in `import` statements.
* Does *not* include <reference> directives.
*/
function* imports({ statements }: ts.SourceFile | ts.ModuleBlock): Iterable<string> {
function imports({ statements }: ts.SourceFile | ts.ModuleBlock): Iterable<string> {
const result: string[] = [];
for (const node of statements) {
recur(node);
}
return result;

function recur(node: ts.Node) {
switch (node.kind) {
case ts.SyntaxKind.ImportDeclaration:
case ts.SyntaxKind.ExportDeclaration: {
const { moduleSpecifier } = node as ts.ImportDeclaration | ts.ExportDeclaration;
if (moduleSpecifier && moduleSpecifier.kind === ts.SyntaxKind.StringLiteral) {
yield (moduleSpecifier as ts.StringLiteral).text;
result.push((moduleSpecifier as ts.StringLiteral).text);
}
break;
}

case ts.SyntaxKind.ImportEqualsDeclaration: {
const { moduleReference } = node as ts.ImportEqualsDeclaration;
if (moduleReference.kind === ts.SyntaxKind.ExternalModuleReference) {
yield parseRequire(moduleReference);
result.push(parseRequire(moduleReference));
}
break;
}

case ts.SyntaxKind.ModuleDeclaration: {
const { name, body } = node as ts.ModuleDeclaration;
if (name.kind === ts.SyntaxKind.StringLiteral && body) {
yield* imports(body as ts.ModuleBlock);
case ts.SyntaxKind.ImportType: {
const { argument } = node as ts.ImportTypeNode;
if (argument.kind === ts.SyntaxKind.LiteralType) {
const { literal } = argument as ts.LiteralTypeNode;
if (literal.kind === ts.SyntaxKind.StringLiteral) {
result.push(literal.text);
}
}
break;
}

default:
ts.forEachChild(node, recur);
}
}
}
Expand Down

0 comments on commit 456c691

Please sign in to comment.