Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
Add dependencies for import types
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Apr 21, 2020
1 parent dd69e3b commit 78f7ca2
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/lib/module-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,35 +265,43 @@ 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> {
for (const node of statements) {
function imports(sourceFile: ts.SourceFile): Iterable<string> {
const result: string[] = [];
ts.forEachChild(sourceFile, recur);
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 78f7ca2

Please sign in to comment.