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: type signature for full declaration #66

Merged
merged 2 commits into from
May 3, 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 @@ -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<Zod.ZodString, Date | undefined, string>",
},
{
"block": "import {
Expand Down Expand Up @@ -140,14 +135,7 @@ exports[`dependencyGraphService initializes 1`] = `
"name": "App",
"path": "/test-project/src/app/index.tsx",
"startLine": 4,
"typeSignature": "function App() {
return (
<div>
<h1>My React App</h1>
<MyNewComponent />
</div>
);
}
"typeSignature": "() => JSX.Element
",
},
{
Expand All @@ -172,9 +160,22 @@ exports[`dependencyGraphService initializes 1`] = `
"name": "MyNewComponent",
"path": "/test-project/src/app/index.tsx",
"startLine": 13,
"typeSignature": "function MyNewComponent() {
return <p>This is my new component</p>;
}
"typeSignature": "() => JSX.Element
",
},
{
"block": "export async function listDirectory(dir: string): Promise<null> {
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<null>
",
},
{
Expand Down Expand Up @@ -312,11 +313,7 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = 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
}",
},
{
Expand All @@ -332,11 +329,7 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = 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
",
},
{
Expand All @@ -355,15 +348,7 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = 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 = () =>
Expand All @@ -378,12 +363,7 @@ type UseQueryResult<TData = unknown, TError = DefaultError> = 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<Zod.ZodString, Date | undefined, string>",
},
]
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
noqcks marked this conversation as resolved.
Show resolved Hide resolved
// 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) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/test-project/src/utils/dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export async function listDirectory(dir: string): Promise<null> {
console.log("Listing directory", dir);
return Promise.resolve(null);
}
Loading