Skip to content

Commit fd5db57

Browse files
author
Eric Wheeler
committed
feat: add generic type declaration support to tree-sitter TypeScript parser
- Added support for parsing generic types with constraints - Added test case for Dictionary<K extends string | number, V> pattern - Enhanced TypeScript queries to capture generic type definitions Signed-off-by: Eric Wheeler <[email protected]>
1 parent 3b2f61b commit fd5db57

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/services/tree-sitter/__tests__/parseSourceCodeDefinitions.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,49 @@ it("should parse namespace declarations", async function () {
544544
expect(result).toContain("isValidPhone")
545545
})
546546

547+
it("should parse generic type declarations with constraints", async function () {
548+
const genericTypeContent = `
549+
/**
550+
* Dictionary interface with constrained key types
551+
*/
552+
interface Dictionary<K extends string | number, V> {
553+
/**
554+
* Gets a value by its key
555+
* @param key - The key to look up
556+
* @returns The value associated with the key, or undefined
557+
*/
558+
get(key: K): V | undefined;
559+
560+
/**
561+
* Sets a value for a key
562+
* @param key - The key to set
563+
* @param value - The value to associate with the key
564+
*/
565+
set(key: K, value: V): void;
566+
567+
/**
568+
* Checks if the dictionary contains a key
569+
* @param key - The key to check
570+
*/
571+
has(key: K): boolean;
572+
}
573+
574+
/**
575+
* Type alias with constrained generic parameters
576+
*/
577+
type KeyValuePair<K extends string | number, V> = {
578+
key: K;
579+
value: V;
580+
}
581+
`
582+
mockedFs.readFile.mockResolvedValue(Buffer.from(genericTypeContent))
583+
584+
const result = await testParseSourceCodeDefinitions("/test/generic-type.tsx", genericTypeContent)
585+
expect(result).toBeDefined()
586+
expect(result).toContain("interface Dictionary<K extends string | number, V>")
587+
expect(result).toContain("type KeyValuePair<K extends string | number, V>")
588+
})
589+
547590
describe("parseSourceCodeDefinitions", () => {
548591
const testFilePath = "/test/TemperatureControl.tsx"
549592

src/services/tree-sitter/queries/typescript.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,14 @@ export default `
7878
; Namespace declarations
7979
(internal_module
8080
name: (identifier) @name.definition.namespace) @definition.namespace
81+
82+
; Interface declarations with generic type parameters and constraints
83+
(interface_declaration
84+
name: (type_identifier) @name.definition.interface
85+
type_parameters: (type_parameters)?) @definition.interface
86+
87+
; Type alias declarations with generic type parameters and constraints
88+
(type_alias_declaration
89+
name: (type_identifier) @name.definition.type
90+
type_parameters: (type_parameters)?) @definition.type
8191
`

0 commit comments

Comments
 (0)