improv: handle generics in class definitons

This commit is contained in:
tomasz-io 2024-11-14 16:26:24 -08:00
parent 62a4e434f1
commit 698db54ee0
3 changed files with 94 additions and 6 deletions

View file

@ -1,3 +1,9 @@
// @ts-nocheck
class Group extends BaseClass {}
class Group implements FirstInterface {}
class Group extends BaseClass implements FirstInterface, SecondInterface {}
class Group extends BaseClass<User> implements FirstInterface<User> {}

View file

@ -216,8 +216,64 @@ const CLASS_METHODS = [
},
];
const CLASS_DEFINITIONS = [
{
nodeType: "class_declaration with base class",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 2, character: 31 },
definitionPositions: [
{ row: 2, column: 29 }, // BaseClass
],
},
{
nodeType: "class_declaration with interface",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 4, character: 39 },
definitionPositions: [
{ row: 4, column: 37 }, // FirstInterface
],
},
{
nodeType: "class_declaration with base class and multiple interfaces",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 6, character: 74 },
definitionPositions: [
{ row: 6, column: 29 }, // BaseClass
{ row: 6, column: 55 }, // FirstInterface
{ row: 6, column: 72 }, // SecondInterface
],
},
{
nodeType: "class_declaration with base class and multiple interfaces",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 6, character: 74 },
definitionPositions: [
{ row: 6, column: 29 }, // BaseClass
{ row: 6, column: 55 }, // FirstInterface
{ row: 6, column: 72 }, // SecondInterface
],
},
{
nodeType: "class_declaration with generic base class and generic interface",
fileName: "classes.ts",
language: "TypeScript",
cursorPosition: { line: 8, character: 69 },
definitionPositions: [
{ row: 8, column: 29 }, // BaseClass
{ row: 8, column: 61 }, // FirstInterface
{ row: 8, column: 34 }, // User
{ row: 8, column: 66 }, // User
],
},
];
export const TYPESCRIPT_TEST_CASES = [
...FUNCTIONS,
...ARROW_FUNCTIONS,
...CLASS_METHODS,
...CLASS_DEFINITIONS,
];

View file

@ -1,13 +1,21 @@
(
(class_declaration
(class_heritage
(extends_clause
(identifier) @base-class
)
; Match a class declaration that extends a base class
; Returns:
; @base-class: The identifier of the base class being extended
; @generic_argument: The type identifier of the generic argument, if present
(class_declaration
(class_heritage
(extends_clause
(identifier) @base-class
(type_arguments
(type_identifier) @generic_argument
)?
)
)
)
; Match a class declaration that implements an interface
; Returns:
; @interface: The type identifier of the interface being implemented
(
(class_declaration
(class_heritage
@ -16,4 +24,22 @@
)
)
)
)
; Match a class declaration that implements a generic interface
; Returns:
; @interface: The type identifier of the generic interface being implemented
; @generic_argument: The type identifier of the generic argument
(
class_declaration
(class_heritage
(implements_clause
(generic_type
(type_identifier) @interface
(type_arguments
(type_identifier) @generic_argument
)
)
)
)
)