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

fix: handling export declarations #80

Merged
merged 2 commits into from
May 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ exports[`dependencyGraphService initializes 1`] = `
"startLine": 12,
"typeSignature": "any",
},
{
"block": "export * as zodUtils from "./utils";",
"edits": [],
"endLine": 7,
"id": "0fe7d446ed9f696d18685f95c58433a275c4475b",
"kind": "ExportDeclaration",
"name": "zodUtils",
"path": "/test-project/src/index.ts",
"startLine": 7,
"typeSignature": "any",
},
{
"block": "date().parse("19th September 2023");",
"edits": [],
Expand Down Expand Up @@ -306,12 +317,12 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = UseBaseQueryResult
}
}",
"edits": [],
"endLine": 26,
"endLine": 29,
"id": "8e0051ad7c49716420e0140ade5add29840c0b96",
"kind": "ClassDeclaration",
"name": "NodeRelations",
"path": "/test-project/src/utils/index.ts",
"startLine": 14,
"startLine": 17,
"typeSignature": "class NodeRelations {
constructor (hasCycle: boolean) => NodeRelations NodeRelations.addEdge: () => string
}",
Expand All @@ -323,12 +334,12 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = UseBaseQueryResult
return text;
}",
"edits": [],
"endLine": 38,
"endLine": 41,
"id": "d377594088a1eac0cb38d0b30cce7d28f03f4011",
"kind": "FunctionDeclaration",
"name": "myFunction",
"path": "/test-project/src/utils/index.ts",
"startLine": 34,
"startLine": 37,
"typeSignature": "(text: string) => string
",
},
Expand All @@ -342,12 +353,12 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = UseBaseQueryResult
});
};",
"edits": [],
"endLine": 12,
"endLine": 15,
"id": "e467e428b74115c75603dd6f843a91c3adcb5ee1",
"kind": "VariableDeclaration",
"name": "useQueryFn",
"path": "/test-project/src/utils/index.ts",
"startLine": 5,
"startLine": 8,
"typeSignature": "() => void",
},
{
Expand All @@ -357,14 +368,36 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = UseBaseQueryResult
return isNaN(new Date(date).getTime()) ? undefined : new Date(date);
});",
"edits": [],
"endLine": 32,
"endLine": 35,
"id": "87b9ecaf3b92371fe3d4b50777f320bfc95b0d82",
"kind": "VariableDeclaration",
"name": "date",
"path": "/test-project/src/utils/index.ts",
"startLine": 28,
"startLine": 31,
"typeSignature": "() => Zod.ZodEffects<Zod.ZodString, Date | undefined, string>",
},
{
"block": "export * from "zod";",
"edits": [],
"endLine": 4,
"id": "fa3c9ab36ce27ac1d7e793c372758dfcca7bf676",
"kind": "ExportDeclaration",
"name": "export * from "zod";",
"path": "/test-project/src/utils/index.ts",
"startLine": 4,
"typeSignature": "",
},
{
"block": "export * from "@tanstack/react-query";",
"edits": [],
"endLine": 5,
"id": "0050f9ec7e413a7bff568fbc2b39d5d9268bd096",
"kind": "ExportDeclaration",
"name": "export * from "@tanstack/react-query";",
"path": "/test-project/src/utils/index.ts",
"startLine": 5,
"typeSignature": "",
},
]
`;

Expand Down
22 changes: 20 additions & 2 deletions packages/bumpgen-core/src/services/language/typescript/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createHash } from "crypto";
import type {
ClassDeclaration,
ExportAssignment,
ExportDeclaration,
ExpressionStatement,
FunctionDeclaration,
Identifier,
Expand All @@ -23,6 +24,7 @@ import type {
import { isImportNode } from "./signatures";

type TopLevelTypes =
| ExportDeclaration
| ModuleDeclaration
| InterfaceDeclaration
| ClassDeclaration
Expand All @@ -34,6 +36,7 @@ type TopLevelTypes =

const isTopLevelType = (node: Node): node is TopLevelTypes => {
return (
node.getKind() === SyntaxKind.ExportDeclaration ||
node.getKind() === SyntaxKind.ModuleDeclaration ||
node.getKind() === SyntaxKind.InterfaceDeclaration ||
node.getKind() === SyntaxKind.ClassDeclaration ||
Expand Down Expand Up @@ -267,15 +270,24 @@ const getReferenceNodes = (node: TopLevelTypes) => {

const createTopLevelNode = (n: TopLevelTypes) => {
const kind = makeKind(n.getKind());
const idName = n

const identifierName = n
.getFirstDescendantByKind(SyntaxKind.Identifier)
?.getSymbol()
?.getName();
const nodeName = "getName" in n ? n.getName() : n.getSymbol()?.getName();
const name = nodeName ?? idName;
let name = nodeName ?? identifierName;
if (!name) {
return;
}

// handling the case of 'export * from "x"' where the name is __export
// for all exports of this type, but we need to make sure ids are unique
// in the graph
if (n.getKind() === SyntaxKind.ExportDeclaration && name === "__export") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

might eventually split this out into its own branching logic per node type if it gets more complicated, starting to smell

even this line is smelly

const nodeName = "getName" in n ? n.getName() : n.getSymbol()?.getName();

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should fix this when we revamp the ID approach (map nodes by similarity)

name = n.getText();
}

const path = n.getSourceFile().getFilePath();
const surroundingBlock = getSurroundingBlock(n);

Expand Down Expand Up @@ -370,6 +382,12 @@ export const processSourceFile = (sourceFile: SourceFile) => {
collectedEdges.push(...edges);
});

sourceFile.getExportDeclarations().forEach((exportDeclaration) => {
const { nodes, edges } = processTopLevelItem(exportDeclaration);
collectedNodes.push(...nodes);
collectedEdges.push(...edges);
});

sourceFile.getExportAssignments().forEach((exportAssignment) => {
const { nodes, edges } = processTopLevelItem(exportAssignment);
collectedNodes.push(...nodes);
Expand Down
3 changes: 3 additions & 0 deletions packages/test-project/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useQuery } from "@tanstack/react-query";
import { z } from "zod";

export * from "zod";
export * from "@tanstack/react-query";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const useQueryFn = () => {
useQuery({
Expand Down
Loading