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

Add dependencies for import types #767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also fix microsoft/DefinitelyTyped-tools#44? Probably not, but the additional tree walking surely makes some difference.

}
}
}
Expand Down