Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/getNodeToDocument.ts.spec.ts
13405 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { outdent } from 'outdent';
7
import { afterAll, expect, suite, test } from 'vitest';
8
import {
9
_dispose,
10
_getNodeToDocument
11
} from '../../node/parserImpl';
12
import { WASMLanguage } from '../../node/treeSitterLanguages';
13
import { srcWithAnnotatedNodeToDoc } from './getNodeToDocument.util';
14
15
16
suite('getNodeToDocument - typescript', () => {
17
18
afterAll(() => _dispose());
19
20
async function run(annotatedSrc: string) {
21
return srcWithAnnotatedNodeToDoc(
22
WASMLanguage.TypeScript,
23
annotatedSrc,
24
);
25
}
26
27
test('should return root node for invalid range', async () => {
28
const result = await _getNodeToDocument(
29
WASMLanguage.TypeScript,
30
'const a = 1;',
31
{
32
startIndex: 100,
33
endIndex: 200,
34
}
35
);
36
expect(result).toMatchInlineSnapshot(`
37
{
38
"nodeIdentifier": undefined,
39
"nodeSelectionBy": "expanding",
40
"nodeToDocument": {
41
"endIndex": 12,
42
"startIndex": 0,
43
"type": "program",
44
},
45
}
46
`);
47
});
48
49
test('should return root node for empty source', async () => {
50
51
const result = await run('<<>>');
52
53
expect(result).toMatchInlineSnapshot(`"<PROGRAM></PROGRAM>"`);
54
});
55
56
test('should return node position for a variable declaration', async () => {
57
const result = await run(
58
'<<const>> a = 1;',
59
);
60
expect(result).toMatchInlineSnapshot(`"<LEXICAL_DECLARATION>const <IDENT>a</IDENT> = 1;</LEXICAL_DECLARATION>"`);
61
});
62
63
test('should return node position for a function declaration', async () => {
64
const result = await srcWithAnnotatedNodeToDoc(
65
WASMLanguage.TypeScript,
66
'<<function>> add(a: number, b: number): number { return a + b; }',
67
);
68
expect(result).toMatchInlineSnapshot(`"<FUNCTION_DECLARATION>function <IDENT>add</IDENT>(a: number, b: number): number { return a + b; }</FUNCTION_DECLARATION>"`);
69
});
70
71
test('should return node position for a class declaration', async () => {
72
const result = await run(
73
'<<class>> MyClass { constructor() {} }',
74
);
75
expect(result).toMatchInlineSnapshot(`"<CLASS_DECLARATION>class <IDENT>MyClass</IDENT> { constructor() {} }</CLASS_DECLARATION>"`);
76
});
77
78
test('should return the whole program', async () => {
79
const result = await run(
80
outdent`
81
/**
82
* This is a comment
83
*/
84
<<const>> foo = 1;
85
`,
86
);
87
expect(result).toMatchInlineSnapshot(`
88
"/**
89
* This is a comment
90
*/
91
<LEXICAL_DECLARATION>const <IDENT>foo</IDENT> = 1;</LEXICAL_DECLARATION>"
92
`);
93
});
94
95
test('should return the whole program - function', async () => {
96
const result = await run(
97
outdent`
98
/**
99
* This is a comment
100
*/
101
<<function>> add(a: number, b: number): number {
102
return a + b;
103
}
104
`,
105
);
106
expect(result).toMatchInlineSnapshot(`
107
"/**
108
* This is a comment
109
*/
110
<FUNCTION_DECLARATION>function <IDENT>add</IDENT>(a: number, b: number): number {
111
return a + b;
112
}</FUNCTION_DECLARATION>"
113
`);
114
});
115
116
test('should return the whole program - class', async () => {
117
const result = await run(
118
outdent`
119
/**
120
* This is a comment
121
*/
122
<<class>> MyClass {
123
constructor() {}
124
}
125
`,
126
);
127
expect(result).toMatchInlineSnapshot(`
128
"/**
129
* This is a comment
130
*/
131
<CLASS_DECLARATION>class <IDENT>MyClass</IDENT> {
132
constructor() {}
133
}</CLASS_DECLARATION>"
134
`);
135
});
136
});
137
138