From dc20606596c4817505f3a803557fff8bd58140b4 Mon Sep 17 00:00:00 2001 From: Benji Visser Date: Fri, 3 May 2024 16:18:44 -0400 Subject: [PATCH 1/2] type signature for full declaration Signed-off-by: Benji Visser --- .../__snapshots__/index.test.ts.snap | 40 +++++++++---------- .../language/typescript/signatures.ts | 34 ++++++++++++++-- packages/test-project/src/utils/dir.ts | 4 ++ 3 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 packages/test-project/src/utils/dir.ts diff --git a/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap b/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap index 1fc06d0..e3dedc7 100644 --- a/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap +++ b/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap @@ -140,14 +140,7 @@ exports[`dependencyGraphService initializes 1`] = ` "name": "App", "path": "/test-project/src/app/index.tsx", "startLine": 4, - "typeSignature": "function App() { - return ( -
-

My React App

- -
- ); -} + "typeSignature": "() => JSX.Element ", }, { @@ -172,9 +165,22 @@ exports[`dependencyGraphService initializes 1`] = ` "name": "MyNewComponent", "path": "/test-project/src/app/index.tsx", "startLine": 13, - "typeSignature": "function MyNewComponent() { - return

This is my new component

; -} + "typeSignature": "() => JSX.Element +", + }, + { + "block": "export async function listDirectory(dir: string): Promise { + console.log("Listing directory", dir); + return Promise.resolve(null); +}", + "edits": [], + "endLine": 4, + "id": "2c69e417f3abb8fe23eec60a5a0241f706e58cec", + "kind": "FunctionDeclaration", + "name": "listDirectory", + "path": "/test-project/src/utils/dir.ts", + "startLine": 1, + "typeSignature": "(dir: string) => Promise ", }, { @@ -312,11 +318,7 @@ type UseQueryResult = UseBaseQueryResult "path": "/test-project/src/utils/index.ts", "startLine": 14, "typeSignature": "class NodeRelations { - constructor (hasCycle: boolean) => NodeRelations NodeRelations.addEdge: addEdge() { - console.log("edge"); - z.string(); - return ""; - } + constructor (hasCycle: boolean) => NodeRelations NodeRelations.addEdge: () => string }", }, { @@ -332,11 +334,7 @@ type UseQueryResult = UseBaseQueryResult "name": "myFunction", "path": "/test-project/src/utils/index.ts", "startLine": 34, - "typeSignature": "export function myFunction(text: string) { - z.string(); - console.log(text); - return text; -} + "typeSignature": "(text: string) => string ", }, { diff --git a/packages/bumpgen-core/src/services/language/typescript/signatures.ts b/packages/bumpgen-core/src/services/language/typescript/signatures.ts index 665a773..5646b82 100644 --- a/packages/bumpgen-core/src/services/language/typescript/signatures.ts +++ b/packages/bumpgen-core/src/services/language/typescript/signatures.ts @@ -102,8 +102,23 @@ const indexSignatureDeclarationSignature = ( }; const functionDeclarationSignature = (node: FunctionDeclaration) => { - const typeDef = node.getText(); - return enrichWithTypeReferences(typeDef, node); + // check if the node is a type decalaration or the full implementation + if (node.getDescendantsOfKind(SyntaxKind.Block).length >= 1) { + // ref: https://github.com/dsherret/ts-morph/issues/907 + const params = node + .getParameters() + .map((parameter) => parameter.getText()) + .join(", "); + const returnType = node.getReturnType().getText( + node, + // https://github.com/dsherret/ts-morph/issues/453#issuecomment-667578386 + TypeFormatFlags.UseAliasDefinedOutsideCurrentScope, + ); + return enrichWithTypeReferences(`(${params}) => ${returnType}`, node); + } else { + const typeDef = node.getText(); + return enrichWithTypeReferences(typeDef, node); + } }; const variableDeclarationSignature = (node: VariableDeclaration) => { @@ -112,8 +127,19 @@ const variableDeclarationSignature = (node: VariableDeclaration) => { }; const methodDeclarationSignature = (node: MethodDeclaration) => { - const typeDef = node.getText(); - return enrichWithTypeReferences(typeDef, node); + // check if the node is a type decalaration or the full implementation + if (node.getDescendantsOfKind(SyntaxKind.Block).length >= 1) { + // ref: https://github.com/dsherret/ts-morph/issues/907 + const params = node + .getParameters() + .map((parameter) => parameter.getText()) + .join(", "); + const returnType = node.getReturnType().getText(); + return enrichWithTypeReferences(`(${params}) => ${returnType}`, node); + } else { + const typeDef = node.getText(); + return enrichWithTypeReferences(typeDef, node); + } }; const heritageClauseSignature = (node: HeritageClause) => { diff --git a/packages/test-project/src/utils/dir.ts b/packages/test-project/src/utils/dir.ts new file mode 100644 index 0000000..3609a18 --- /dev/null +++ b/packages/test-project/src/utils/dir.ts @@ -0,0 +1,4 @@ +export async function listDirectory(dir: string): Promise { + console.log("Listing directory", dir); + return Promise.resolve(null); +} From 19a0959d1a363a7570b32ce542a1072c5a6c1768 Mon Sep 17 00:00:00 2001 From: Benji Visser Date: Fri, 3 May 2024 16:34:51 -0400 Subject: [PATCH 2/2] update declaration signature for variables Signed-off-by: Benji Visser --- .../__snapshots__/index.test.ts.snap | 24 +++---------------- .../language/typescript/signatures.ts | 17 +++++++++++-- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap b/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap index e3dedc7..1ff8efa 100644 --- a/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap +++ b/packages/bumpgen-core/src/services/language/typescript/__snapshots__/index.test.ts.snap @@ -16,12 +16,7 @@ exports[`dependencyGraphService initializes 1`] = ` "name": "date", "path": "/test-project/src/index.ts", "startLine": 1, - "typeSignature": "date = () => - z.string().transform((v) => { - const date = v.replace(/(\\\\d+)(st|nd|rd|th)/, "$1"); - return isNaN(new Date(date).getTime()) ? undefined : new Date(date); - }) -", + "typeSignature": "() => Zod.ZodEffects", }, { "block": "import { @@ -353,15 +348,7 @@ type UseQueryResult = UseBaseQueryResult "name": "useQueryFn", "path": "/test-project/src/utils/index.ts", "startLine": 5, - "typeSignature": "useQueryFn = () => { - useQuery({ - queryKey: ["key"], - queryFn: () => { - return "data"; - }, - }); -} -", + "typeSignature": "() => void", }, { "block": "export const date = () => @@ -376,12 +363,7 @@ type UseQueryResult = UseBaseQueryResult "name": "date", "path": "/test-project/src/utils/index.ts", "startLine": 28, - "typeSignature": "date = () => - z.string().transform((v) => { - const date = v.replace(/(\\\\d+)(st|nd|rd|th)/, "$1"); - return isNaN(new Date(date).getTime()) ? undefined : new Date(date); - }) -", + "typeSignature": "() => Zod.ZodEffects", }, ] `; diff --git a/packages/bumpgen-core/src/services/language/typescript/signatures.ts b/packages/bumpgen-core/src/services/language/typescript/signatures.ts index 5646b82..cd771ef 100644 --- a/packages/bumpgen-core/src/services/language/typescript/signatures.ts +++ b/packages/bumpgen-core/src/services/language/typescript/signatures.ts @@ -122,8 +122,21 @@ const functionDeclarationSignature = (node: FunctionDeclaration) => { }; const variableDeclarationSignature = (node: VariableDeclaration) => { - const typeDef = node.getText(); - return enrichWithTypeReferences(typeDef, node); + // check if the node is a type decalaration or the full implementation + if (node.getDescendantsOfKind(SyntaxKind.Block).length >= 1) { + // ref: https://github.com/dsherret/ts-morph/issues/907 + return node + .getType() + .getText( + undefined, + TypeFormatFlags.UseFullyQualifiedType | + TypeFormatFlags.InTypeAlias | + TypeFormatFlags.NoTruncation, + ); + } else { + const typeDef = node.getText(); + return enrichWithTypeReferences(typeDef, node); + } }; const methodDeclarationSignature = (node: MethodDeclaration) => {