Merge pull request #2914 from continuedev/tomasz/robust-ts-definitions
TypeScript definitions + tests
This commit is contained in:
commit
98d2d0fe3e
6 changed files with 191 additions and 42 deletions
|
|
@ -1,29 +1,8 @@
|
|||
import { TYPESCRIPT_TEST_CASES } from "./testCases/typescript";
|
||||
import { testRootPathContext } from "./testUtils";
|
||||
|
||||
const TEST_CASES = [
|
||||
{
|
||||
nodeType: "function_declaration",
|
||||
fileName: "file1.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 10, character: 24 },
|
||||
definitionPositions: [
|
||||
{ row: 9, column: 34 }, // Person
|
||||
{ row: 9, column: 44 }, // Address
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "method_declaration",
|
||||
fileName: "file1.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 22, character: 30 },
|
||||
definitionPositions: [
|
||||
{ row: 13, column: 29 }, // BaseClass
|
||||
{ row: 13, column: 55 }, // FirstInterface
|
||||
{ row: 13, column: 72 }, // SecondInterface
|
||||
{ row: 21, column: 33 }, // Person
|
||||
{ row: 21, column: 43 }, // Address
|
||||
],
|
||||
},
|
||||
...TYPESCRIPT_TEST_CASES,
|
||||
{
|
||||
nodeType: "function_definition",
|
||||
fileName: "file1.py",
|
||||
|
|
|
|||
|
|
@ -7,10 +7,6 @@ import {
|
|||
// @ts-ignore
|
||||
} from "./types";
|
||||
|
||||
function getAddress(person: Person): Address {
|
||||
return person.address;
|
||||
}
|
||||
|
||||
class Group extends BaseClass implements FirstInterface, SecondInterface {
|
||||
people: Person[];
|
||||
|
||||
|
|
@ -20,6 +16,18 @@ class Group extends BaseClass implements FirstInterface, SecondInterface {
|
|||
}
|
||||
|
||||
getPersonAddress(person: Person): Address {
|
||||
return getAddress(person);
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
import {
|
||||
Address,
|
||||
Person,
|
||||
// @ts-ignore
|
||||
} from "./types";
|
||||
|
||||
function getAddress(person: Person): Address {
|
||||
return person.address;
|
||||
}
|
||||
|
||||
function getFirstAddress(people: Person[]): Address {
|
||||
return people[0].address;
|
||||
}
|
||||
|
||||
function logPerson(person: Person) {
|
||||
console.log(`Name: ${person.name}, Age: ${person.age}`);
|
||||
}
|
||||
|
||||
function getHardcodedAddress(): Address {
|
||||
return { street: "123 Main St", city: "Anytown", zipCode: "12345" };
|
||||
}
|
||||
|
||||
function getAddresses(people: Person[]): Address[] {
|
||||
return people[0].address;
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
export const TYPESCRIPT_TEST_CASES = [
|
||||
{
|
||||
nodeType: "function_declaration with a param and a return type",
|
||||
fileName: "functions.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 7, character: 24 },
|
||||
definitionPositions: [
|
||||
{ row: 6, column: 34 }, // Person
|
||||
{ row: 6, column: 44 }, // Address
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "function_declaration with array param",
|
||||
fileName: "functions.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 11, character: 27 },
|
||||
definitionPositions: [
|
||||
{ row: 10, column: 39 }, // Person
|
||||
{ row: 10, column: 51 }, // Address
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "function_declaration without return type",
|
||||
fileName: "functions.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 15, character: 58 },
|
||||
definitionPositions: [
|
||||
{ row: 14, column: 33 }, // Person
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "function_declaration without params",
|
||||
fileName: "functions.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 19, character: 70 },
|
||||
definitionPositions: [
|
||||
{ row: 18, column: 39 }, // Person
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "method_declaration",
|
||||
fileName: "class.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 18, character: 26 },
|
||||
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
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "method_declaration without arguments",
|
||||
fileName: "class.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 22, character: 54 },
|
||||
definitionPositions: [
|
||||
{ row: 9, column: 29 }, // BaseClass
|
||||
{ row: 9, column: 55 }, // FirstInterface
|
||||
{ row: 9, column: 72 }, // SecondInterface
|
||||
{ row: 21, column: 32 }, // Address
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "method_declaration without return type",
|
||||
fileName: "class.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 26, character: 43 },
|
||||
definitionPositions: [
|
||||
{ row: 9, column: 29 }, // BaseClass
|
||||
{ row: 9, column: 55 }, // FirstInterface
|
||||
{ row: 9, column: 72 }, // SecondInterface
|
||||
{ row: 25, column: 26 }, // Person
|
||||
],
|
||||
},
|
||||
{
|
||||
nodeType: "method_declaration with array type arguments",
|
||||
fileName: "class.ts",
|
||||
language: "TypeScript",
|
||||
cursorPosition: { line: 30, character: 46 },
|
||||
definitionPositions: [
|
||||
{ row: 9, column: 29 }, // BaseClass
|
||||
{ row: 9, column: 55 }, // FirstInterface
|
||||
{ row: 9, column: 72 }, // SecondInterface
|
||||
{ row: 29, column: 26 }, // Person
|
||||
],
|
||||
},
|
||||
];
|
||||
|
|
@ -1,10 +1,34 @@
|
|||
; Pattern for capturing the return type
|
||||
(
|
||||
(function_declaration
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation) @param_type
|
||||
)
|
||||
(function_declaration
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
)
|
||||
|
||||
; Pattern for parameters with direct type_identifier
|
||||
(
|
||||
(function_declaration
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation
|
||||
(type_identifier) @param_type
|
||||
)
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
; Pattern for parameters with one level of nesting
|
||||
(
|
||||
(function_declaration
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation
|
||||
(_
|
||||
(type_identifier) @param_type
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
@ -1,10 +1,34 @@
|
|||
; Pattern for capturing the return type
|
||||
(
|
||||
(method_definition
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation) @param_type
|
||||
)
|
||||
)
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
(method_definition
|
||||
(type_annotation) @return_type
|
||||
)
|
||||
)
|
||||
|
||||
; Pattern for parameters with direct type_identifier
|
||||
(
|
||||
(method_definition
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation
|
||||
(type_identifier) @param_type
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
; Pattern for parameters with one level of nesting
|
||||
(
|
||||
(method_definition
|
||||
(formal_parameters
|
||||
(_
|
||||
(type_annotation
|
||||
(_
|
||||
(type_identifier) @param_type
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue