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..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 { @@ -140,14 +135,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 +160,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 +313,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 +329,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 ", }, { @@ -355,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 = () => @@ -378,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 665a773..cd771ef 100644 --- a/packages/bumpgen-core/src/services/language/typescript/signatures.ts +++ b/packages/bumpgen-core/src/services/language/typescript/signatures.ts @@ -102,18 +102,57 @@ 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) => { - 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) => { - 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); +}