Skip to content
Merged
Show file tree
Hide file tree
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
21 changes: 14 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,42 @@ 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): 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 (ts.isLiteralTypeNode(argument) && ts.isStringLiteral(argument.literal)) {
result.push(argument.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.

this seems pretty reasonable; I bet performance is (1) a lot worse (2) not noticeably worse.

We should keep an eye on large packages just in case.

}
}
}
Expand Down
13 changes: 13 additions & 0 deletions packages/definitions-parser/test/module-info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ testo({
expect(Array.from(types.keys())).toEqual(["merges.d.ts"]);
expect(Array.from(tests.keys())).toEqual(["globby-tests.ts", "test/other-tests.ts"]);
},
allReferencedFilesIncludesTypesImports() {
const pkg = new Dir(undefined);
pkg.set(
"index.d.ts",
`type T = import("./types");
`
);
pkg.set("types.d.ts", "");
const memFS = new InMemoryFS(pkg, "types/mock");
const { types, tests } = allReferencedFiles(["index.d.ts"], memFS, "mock", "types/mock");
expect(Array.from(types.keys())).toEqual(["index.d.ts", "types.d.ts"]);
expect(Array.from(tests.keys())).toEqual([]);
},
getModuleInfoWorksWithOtherFiles() {
const { types } = getBoringReferences();
// written as if it were from OTHER_FILES.txt
Expand Down