Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependencies for import types #95

Merged
merged 3 commits into from
Sep 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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: 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): 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);
}
}
jablko marked this conversation as resolved.
Show resolved Hide resolved
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