improv: expand typescript queries, simplify tests

This commit is contained in:
tomasz-io 2024-11-14 11:38:33 -08:00
parent 2f089eb16f
commit c9d44a17ee
10 changed files with 336 additions and 161 deletions

View file

@ -39,6 +39,7 @@ export class RootPathContextService {
}
private static TYPES_TO_USE = new Set([
"arrow_function",
"program",
"function_declaration",
"function_definition",
@ -74,9 +75,9 @@ export class RootPathContextService {
this.importDefinitionsService.get(filepath);
break;
default:
// const type = node.type;
// debugger;
// console.log(getSyntaxTreeString(node));
const type = node.type;
debugger;
console.log(getSyntaxTreeString(node));
query = await getQueryForFile(
filepath,

View file

@ -3,61 +3,61 @@ import { testRootPathContext } from "./testUtils";
const TEST_CASES = [
...TYPESCRIPT_TEST_CASES,
{
nodeType: "function_definition",
fileName: "file1.py",
language: "Python",
cursorPosition: { line: 4, character: 25 },
definitionPositions: [
{ row: 3, column: 30 }, // Person
{ row: 3, column: 42 }, // Address
],
},
{
nodeType: "function_definition (inside a class)",
fileName: "file1.py",
language: "Python",
cursorPosition: { line: 12, character: 33 },
definitionPositions: [
{ row: 6, column: 21 }, // BaseClass
{ row: 6, column: 33 }, // Collection
{ row: 11, column: 47 }, // Person
{ row: 11, column: 59 }, // Address
],
},
{
nodeType: "function_definition",
fileName: "file1.php",
language: "PHP",
cursorPosition: { line: 12, character: 32 },
definitionPositions: [
{ row: 10, column: 26 }, // Person
{ row: 10, column: 44 }, // Address
],
},
{
nodeType: "method_declaration",
fileName: "file1.php",
language: "PHP",
cursorPosition: { line: 26, character: 35 },
definitionPositions: [
{ row: 15, column: 29 }, // BaseClass
{ row: 15, column: 55 }, // FirstInterface
{ row: 15, column: 72 }, // SecondInterface
{ row: 25, column: 43 }, // Person
{ row: 25, column: 61 }, // Address
],
},
{
nodeType: "function_declaration",
fileName: "file1.go",
language: "Go",
cursorPosition: { line: 7, character: 21 },
definitionPositions: [
{ row: 6, column: 33 }, // models.User
{ row: 6, column: 50 }, // models.Address
],
},
// {
// nodeType: "function_definition",
// fileName: "file1.py",
// language: "Python",
// cursorPosition: { line: 4, character: 25 },
// definitionPositions: [
// { row: 3, column: 30 }, // Person
// { row: 3, column: 42 }, // Address
// ],
// },
// {
// nodeType: "function_definition (inside a class)",
// fileName: "file1.py",
// language: "Python",
// cursorPosition: { line: 12, character: 33 },
// definitionPositions: [
// { row: 6, column: 21 }, // BaseClass
// { row: 6, column: 33 }, // Collection
// { row: 11, column: 47 }, // Person
// { row: 11, column: 59 }, // Address
// ],
// },
// {
// nodeType: "function_definition",
// fileName: "file1.php",
// language: "PHP",
// cursorPosition: { line: 12, character: 32 },
// definitionPositions: [
// { row: 10, column: 26 }, // Person
// { row: 10, column: 44 }, // Address
// ],
// },
// {
// nodeType: "method_declaration",
// fileName: "file1.php",
// language: "PHP",
// cursorPosition: { line: 26, character: 35 },
// definitionPositions: [
// { row: 15, column: 29 }, // BaseClass
// { row: 15, column: 55 }, // FirstInterface
// { row: 15, column: 72 }, // SecondInterface
// { row: 25, column: 43 }, // Person
// { row: 25, column: 61 }, // Address
// ],
// },
// {
// nodeType: "function_declaration",
// fileName: "file1.go",
// language: "Go",
// cursorPosition: { line: 7, character: 21 },
// definitionPositions: [
// { row: 6, column: 33 }, // models.User
// { row: 6, column: 50 }, // models.Address
// ],
// },
];
describe("RootPathContextService", () => {

View file

@ -0,0 +1,21 @@
// @ts-nocheck
const getAddress = (person: Person): Address => {
// TODO
};
const logPerson = (person: Person) => {
// TODO
};
const getHardcodedAddress = (): Address => {
// TODO
};
const getAddresses = (people: Person[]): Address[] => {
// TODO
};
const logPersonWithAddres = (person: Person<Address>): Person<Address> => {
// TODO
};

View file

@ -1,37 +0,0 @@
import {
Address,
Person,
BaseClass,
FirstInterface,
SecondInterface,
// @ts-ignore
} from "./types";
class Group extends BaseClass implements FirstInterface, SecondInterface {
people: Person[];
constructor(people: Person[]) {
super();
this.people = people;
}
getPersonAddress(person: Person): Address {
return person.address;
}
getHardcodedAddress(): Address {
return { street: "123 Main St", city: "Anytown" };
}
addPerson(person: Person) {
this.people = [...this.people, person];
}
addPeople(people: Person[]) {
this.people = [...this.people, ...people];
}
getAddresses(people: Person[]): Address[] {
return people.map((person) => person.address);
}
}

View file

@ -0,0 +1,27 @@
// @ts-nocheck
class Group {
getPersonAddress(person: Person): Address {
// TODO
}
getHardcodedAddress(): Address {
// TODO
}
addPerson(person: Person) {
// TODO
}
addPeople(people: Person[]) {
// TODO
}
getAddresses(people: Person[]): Address[] {
// TODO
}
logPersonWithAddress(person: Person<Address>): Person<Address> {
// TODO
}
}

View file

@ -0,0 +1,3 @@
// @ts-nocheck
class Group extends BaseClass implements FirstInterface, SecondInterface {}

View file

@ -1,32 +1,25 @@
import {
Address,
Person,
// @ts-ignore
} from "./types";
// @ts-nocheck
function getAddress(person: Person): Address {
return person.address;
// TODO
}
function getFirstAddress(people: Person[]): Address {
return people[0].address;
// TODO
}
function logPerson(person: Person) {
console.log(`Name: ${person.name}, Age: ${person.age}`);
// TODO
}
function getHardcodedAddress(): Address {
return { street: "123 Main St", city: "Anytown", zipCode: "12345" };
// TODO
}
function getAddresses(people: Person[]): Address[] {
return people[0].address;
// TODO
}
function logPersonWithAddress(person: Person<Address>): Person<Address> {
console.log(
`Name: ${person.name}, Age: ${person.age}, Address: ${person.address.street}`,
);
return person;
// TODO
}

View file

@ -1,50 +1,50 @@
export const TYPESCRIPT_TEST_CASES = [
const FUNCTIONS = [
{
nodeType: "function_declaration with a param and a return type",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 7, character: 24 },
cursorPosition: { line: 3, character: 9 },
definitionPositions: [
{ row: 6, column: 34 }, // Person
{ row: 6, column: 44 }, // Address
{ row: 2, column: 34 }, // Person
{ row: 2, column: 44 }, // Address
],
},
{
nodeType: "function_declaration with array param",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 11, character: 27 },
cursorPosition: { line: 7, character: 9 },
definitionPositions: [
{ row: 10, column: 39 }, // Person
{ row: 10, column: 51 }, // Address
{ row: 6, column: 39 }, // Person
{ row: 6, column: 51 }, // Address
],
},
{
nodeType: "function_declaration without return type",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 15, character: 58 },
cursorPosition: { line: 11, character: 9 },
definitionPositions: [
{ row: 14, column: 33 }, // Person
{ row: 10, column: 33 }, // Person
],
},
{
nodeType: "function_declaration without params",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 19, character: 70 },
cursorPosition: { line: 15, character: 9 },
definitionPositions: [
{ row: 18, column: 39 }, // Person
{ row: 14, column: 39 }, // Person
],
},
{
nodeType: "function_declaration with array params and array return type",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 23, character: 27 },
cursorPosition: { line: 19, character: 9 },
definitionPositions: [
{ row: 22, column: 36 }, // Person
{ row: 22, column: 48 }, // Address
{ row: 18, column: 36 }, // Person
{ row: 18, column: 48 }, // Address
],
},
{
@ -52,75 +52,135 @@ export const TYPESCRIPT_TEST_CASES = [
"function_declaration with generic params and generic return type",
fileName: "functions.ts",
language: "TypeScript",
cursorPosition: { line: 27, character: 14 },
cursorPosition: { line: 23, character: 9 },
definitionPositions: [
{ row: 26, column: 44 }, // Person
{ row: 26, column: 52 }, // Address
{ row: 26, column: 62 }, // Person
{ row: 26, column: 70 }, // Address
{ row: 22, column: 44 }, // Person
{ row: 22, column: 52 }, // Address
{ row: 22, column: 62 }, // Person
{ row: 22, column: 70 }, // Address
],
},
];
const ARROW_FUNCTIONS = [
{
nodeType: "arrow_function with a param and a return type",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 3, character: 9 },
definitionPositions: [
{ row: 2, column: 34 }, // Person
{ row: 2, column: 44 }, // Address
],
},
{
nodeType: "method_declaration with a param and a return type",
fileName: "class.ts",
nodeType: "arrow_function without return type",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 18, character: 26 },
cursorPosition: { line: 7, character: 9 },
definitionPositions: [
{ row: 9, column: 29 }, // BaseClass
{ row: 9, column: 55 }, // FirstInterface
{ row: 9, column: 72 }, // SecondInterface
{ row: 17, column: 33 }, // Person
{ row: 17, column: 43 }, // Address
{ row: 6, column: 33 }, // Person
],
},
{
nodeType: "arrow_function without params",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 11, character: 9 },
definitionPositions: [
{ row: 10, column: 39 }, // Person
],
},
{
nodeType: "arrow_function with array params and array return type",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 15, character: 9 },
definitionPositions: [
{ row: 14, column: 36 }, // Person
{ row: 14, column: 48 }, // Address
],
},
{
nodeType: "arrow_function with generic params and generic return type",
fileName: "arrowFunctions.ts",
language: "TypeScript",
cursorPosition: { line: 19, character: 9 },
definitionPositions: [
{ row: 18, column: 43 }, // Person
{ row: 18, column: 51 }, // Address
{ row: 18, column: 61 }, // Person
{ row: 18, column: 69 }, // Address
],
},
];
const CLASS_METHODS = [
{
nodeType: "method_declaration with a param and a return type",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 4, character: 11 },
definitionPositions: [
{ row: 3, column: 33 }, // Person
{ row: 3, column: 43 }, // Address
],
},
{
nodeType: "method_declaration without arguments",
fileName: "class.ts",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 22, character: 54 },
cursorPosition: { line: 8, character: 11 },
definitionPositions: [
{ row: 9, column: 29 }, // BaseClass
{ row: 9, column: 55 }, // FirstInterface
{ row: 9, column: 72 }, // SecondInterface
{ row: 21, column: 32 }, // Address
{ row: 7, column: 32 }, // Address
],
},
{
nodeType: "method_declaration without return type",
fileName: "class.ts",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 26, character: 43 },
cursorPosition: { line: 12, character: 11 },
definitionPositions: [
{ row: 9, column: 29 }, // BaseClass
{ row: 9, column: 55 }, // FirstInterface
{ row: 9, column: 72 }, // SecondInterface
{ row: 25, column: 26 }, // Person
{ row: 11, column: 26 }, // Person
],
},
{
nodeType: "method_declaration with array type arguments",
fileName: "class.ts",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 30, character: 46 },
cursorPosition: { line: 16, character: 11 },
definitionPositions: [
{ row: 9, column: 29 }, // BaseClass
{ row: 9, column: 55 }, // FirstInterface
{ row: 9, column: 72 }, // SecondInterface
{ row: 29, column: 26 }, // Person
{ row: 15, column: 26 }, // Person
],
},
{
nodeType:
"method_declaration with array type arguments and array type return",
fileName: "class.ts",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 34, character: 50 },
cursorPosition: { line: 20, character: 11 },
definitionPositions: [
{ row: 9, column: 29 }, // BaseClass
{ row: 9, column: 55 }, // FirstInterface
{ row: 9, column: 72 }, // SecondInterface
{ row: 33, column: 29 }, // Person
{ row: 33, column: 41 }, // Address
{ row: 19, column: 29 }, // Person
{ row: 19, column: 41 }, // Address
],
},
{
nodeType:
"method_declaration with with generic params and generic return type",
fileName: "classMethods.ts",
language: "TypeScript",
cursorPosition: { line: 24, character: 11 },
definitionPositions: [
{ row: 23, column: 37 }, // Person
{ row: 23, column: 45 }, // Address
{ row: 23, column: 55 }, // Person
{ row: 23, column: 63 }, // Address
],
},
];
export const TYPESCRIPT_TEST_CASES = [
...FUNCTIONS,
...ARROW_FUNCTIONS,
...CLASS_METHODS,
];

View file

@ -0,0 +1,77 @@
; Pattern for return type with direct type_identifier
(
(arrow_function
(type_annotation
(type_identifier) @return_type
)
)
)
; Pattern for return type with one level of nesting
(
(arrow_function
(type_annotation
(_
(type_identifier) @param_type
)
)
)
)
; Pattern for return type with two levels of nesting
(
(arrow_function
(type_annotation
(_
(_
(type_identifier) @return_type
)
)
)
)
)
; Pattern for parameters with direct type_identifier
(
(arrow_function
(formal_parameters
(_
(type_annotation
(type_identifier) @param_type
)
)
)
)
)
; Pattern for parameters with one level of nesting
(
(arrow_function
(formal_parameters
(_
(type_annotation
(_
(type_identifier) @param_type
)
)
)
)
)
)
; Pattern for parameters with two levels of nesting
(
(arrow_function
(formal_parameters
(_
(type_annotation
(_
(_
(type_identifier) @param_type
)
)
)
)
)
)
)

View file

@ -18,6 +18,19 @@
)
)
; Pattern for return type with two levels of nesting
(
(method_definition
(type_annotation
(_
(_
(type_identifier) @param_type
)
)
)
)
)
; Pattern for parameters with direct type_identifier
(
(method_definition
@ -45,3 +58,20 @@
)
)
)
; Pattern for parameters with two levels of nesting
(
(method_definition
(formal_parameters
(_
(type_annotation
(_
(_
(type_identifier) @param_type
)
)
)
)
)
)
)