-
Notifications
You must be signed in to change notification settings - Fork 218
feat: add ReturnType parser #2317
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
Open
rockerBOO
wants to merge
7
commits into
vega:next
Choose a base branch
from
rockerBOO:returntype-parser
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1bc0449
feat: add initial ReturnType parser
rockerBOO f379eac
feat: add ReturnType<SomeType['method']> support
rockerBOO af0482e
fix: add implicit return type test
rockerBOO 1008735
feat: add more complex function/method parsing tests. abstract common…
rockerBOO 7c06489
chore: formatting
rockerBOO e188df5
fix: removed extra try/catch
rockerBOO a266867
fix: remove ObjectType
rockerBOO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import ts from "typescript"; | ||
| import type { Context, NodeParser } from "../NodeParser.js"; | ||
| import type { SubNodeParser } from "../SubNodeParser.js"; | ||
| import type { BaseType } from "../Type/BaseType.js"; | ||
| import { UnknownNodeError } from "../Error/Errors.js"; | ||
|
|
||
| export class ReturnTypeNodeParser implements SubNodeParser { | ||
| constructor( | ||
| private readonly childNodeParser: NodeParser, | ||
| private readonly checker: ts.TypeChecker, | ||
| ) {} | ||
|
|
||
| supportsNode(node: ts.Node): boolean { | ||
| if (!ts.isTypeReferenceNode(node)) { | ||
| return false; | ||
| } | ||
|
|
||
| const typeName = ts.isIdentifier(node.typeName) ? node.typeName.text : node.typeName.getText(); | ||
| return typeName === "ReturnType" && node.typeArguments?.length === 1; | ||
| } | ||
|
|
||
| createType(node: ts.TypeReferenceNode, context: Context): BaseType { | ||
| if (!node.typeArguments || node.typeArguments.length !== 1) { | ||
| throw new UnknownNodeError(node); | ||
| } | ||
|
|
||
| const typeArg = node.typeArguments[0]; | ||
|
|
||
| // Handle different types of type arguments | ||
| if (ts.isTypeQueryNode(typeArg)) { | ||
| // Case: ReturnType<typeof functionName> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove all the unnecessary comments. We don't need comments explaining what happens but instead should only have comments that explain why we do something the way we do. |
||
| // Get the symbol for the identifier | ||
| const symbol = this.checker.getSymbolAtLocation(typeArg.exprName); | ||
| if (!symbol) { | ||
| throw new UnknownNodeError(node); | ||
| } | ||
|
|
||
| // Get the declarations of the symbol | ||
| const declarations = symbol.getDeclarations() || []; | ||
|
|
||
| // Try multiple methods to extract return type | ||
| for (const decl of declarations) { | ||
| let returnTypeNode: ts.TypeNode | undefined; | ||
|
|
||
| // If declaration is a function/method with explicit return type | ||
| if ( | ||
| (ts.isFunctionDeclaration(decl) || | ||
| ts.isMethodDeclaration(decl) || | ||
| ts.isArrowFunction(decl) || | ||
| ts.isFunctionExpression(decl)) && | ||
| decl.type | ||
| ) { | ||
| returnTypeNode = decl.type; | ||
| } | ||
| // If declaration is a variable with function type annotation | ||
| else if (ts.isVariableDeclaration(decl) && decl.type && ts.isFunctionTypeNode(decl.type)) { | ||
| returnTypeNode = decl.type.type; | ||
| } | ||
|
|
||
| // If we found a return type node, process it | ||
| if (returnTypeNode) { | ||
| const baseType = this.childNodeParser.createType(returnTypeNode, context); | ||
| return baseType; | ||
| } | ||
| } | ||
|
|
||
| // Fallback to type checking method | ||
| const type = this.checker.getTypeOfSymbolAtLocation(symbol, typeArg); | ||
| const result = extractReturnTypeFromSignatures(type, this.checker, this.childNodeParser, context); | ||
| if (result) { | ||
| return result; | ||
| } | ||
| } else { | ||
| // Case: ReturnType<SomeType["methodName"]> or other complex types | ||
| // Get the type directly from TypeScript's type system | ||
| const argType = this.checker.getTypeAtLocation(typeArg); | ||
|
|
||
| // If it's a function type, get its return type | ||
| const result = extractReturnTypeFromSignatures(argType, this.checker, this.childNodeParser, context); | ||
| if (result) { | ||
| return result; | ||
| } | ||
|
|
||
| // Final fallback: try to get type directly | ||
| const type = this.checker.getTypeAtLocation(typeArg); | ||
| const typeNode = this.checker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.NoTruncation); | ||
|
|
||
| if (typeNode) { | ||
| return this.childNodeParser.createType(typeNode, context); | ||
| } | ||
| } | ||
|
|
||
| throw new UnknownNodeError(node); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper function to extract return type from call signatures | ||
| */ | ||
| function extractReturnTypeFromSignatures( | ||
| type: ts.Type, | ||
| checker: ts.TypeChecker, | ||
| childNodeParser: NodeParser, | ||
| context: Context, | ||
| ): BaseType | null { | ||
| const signatures = type.getCallSignatures(); | ||
| if (signatures.length > 0) { | ||
| const returnType = signatures[0].getReturnType(); | ||
| const returnTypeNode = checker.typeToTypeNode(returnType, undefined, ts.NodeBuilderFlags.NoTruncation); | ||
|
|
||
| if (returnTypeNode) { | ||
| return childNodeParser.createType(returnTypeNode, context); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Simulated Redux Toolkit scenario | ||
| export interface TestState { | ||
| counter: number; | ||
| name: string; | ||
| } | ||
|
|
||
| export function createTestStore() { | ||
| return { | ||
| getState: () => ({ counter: 0, name: "test" }) as TestState, | ||
| dispatch: (action: any) => {}, | ||
| }; | ||
| } | ||
|
|
||
| export type TestAppStore = ReturnType<typeof createTestStore>; | ||
| export type TestAppState = ReturnType<TestAppStore["getState"]>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| { | ||
| "$ref": "#/definitions/TestAppState", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "definitions": { | ||
| "TestAppState": { | ||
| "$ref": "#/definitions/TestState" | ||
| }, | ||
| "TestState": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "counter": { | ||
| "type": "number" | ||
| }, | ||
| "name": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "counter", | ||
| "name" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Test cases to demonstrate ReturnType parsing with various function types | ||
|
|
||
| // Implicit return type | ||
| export function implicitReturn() { | ||
| return { message: "Hello", count: 42 }; | ||
| } | ||
|
|
||
| // Arrow function with implicit return | ||
| export const arrowImplicitReturn = () => ({ | ||
| nested: { | ||
| value: "test", | ||
| count: 123, | ||
| }, | ||
| }); | ||
|
|
||
| // Function expression with implicit return | ||
| export const functionExprImplicitReturn = function () { | ||
| return { | ||
| dynamic: true, | ||
| payload: { id: 456, name: "example" }, | ||
| }; | ||
| }; | ||
|
|
||
| // Complex nested return type with explicit annotation | ||
| export function complexNestedReturn(): { | ||
| meta: { | ||
| version: number; | ||
| type: string; | ||
| }; | ||
| data: string[]; | ||
| } { | ||
| return { | ||
| meta: { version: 1, type: "test" }, | ||
| data: ["item1", "item2"], | ||
| }; | ||
| } | ||
|
|
||
| // Combined type that tests all function return types | ||
| export type FunctionReturnTypes = { | ||
| implicit: ReturnType<typeof implicitReturn>; | ||
| arrow: ReturnType<typeof arrowImplicitReturn>; | ||
| functionExpr: ReturnType<typeof functionExprImplicitReturn>; | ||
| complex: ReturnType<typeof complexNestedReturn>; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| { | ||
| "$ref": "#/definitions/FunctionReturnTypes", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "definitions": { | ||
| "FunctionReturnTypes": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "arrow": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "nested": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "count": { | ||
| "type": "number" | ||
| }, | ||
| "value": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "value", | ||
| "count" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "nested" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "complex": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "data": { | ||
| "items": { | ||
| "type": "string" | ||
| }, | ||
| "type": "array" | ||
| }, | ||
| "meta": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "type": { | ||
| "type": "string" | ||
| }, | ||
| "version": { | ||
| "type": "number" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "version", | ||
| "type" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "meta", | ||
| "data" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "functionExpr": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "dynamic": { | ||
| "type": "boolean" | ||
| }, | ||
| "payload": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "id": { | ||
| "type": "number" | ||
| }, | ||
| "name": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "id", | ||
| "name" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "dynamic", | ||
| "payload" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "implicit": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "count": { | ||
| "type": "number" | ||
| }, | ||
| "message": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "message", | ||
| "count" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "implicit", | ||
| "arrow", | ||
| "functionExpr", | ||
| "complex" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| export function implicitReturn() { | ||
| return { message: "Hello", count: 42 }; | ||
| } | ||
|
|
||
| export type ImplicitReturnType = ReturnType<typeof implicitReturn>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "$ref": "#/definitions/ImplicitReturnType", | ||
| "$schema": "http://json-schema.org/draft-07/schema#", | ||
| "definitions": { | ||
| "ImplicitReturnType": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "count": { | ||
| "type": "number" | ||
| }, | ||
| "message": { | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "message", | ||
| "count" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getText might throw in sourceless cenarios, where node.typeName.pos === -1