Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/parser/test/node/parser.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 assert from 'assert';
7
import { afterAll, expect, suite, test } from 'vitest';
8
import { _getDocumentableNodeIfOnIdentifier } from '../../node/docGenParsing';
9
import { _getCoarseParentScope, } from '../../node/parserImpl';
10
import { _dispose, _parse } from '../../node/parserWithCaching';
11
import { WASMLanguage } from '../../node/treeSitterLanguages';
12
import { allKnownQueries } from '../../node/treeSitterQueries';
13
import { insertRangeMarkers } from './markers';
14
15
suite('getDocumentableNodeIfOnIdentifier', () => {
16
// TODO@ulugbekna: rewrite all tests using insertRangeMarkers for better visualization
17
18
test('should return undefined for range not containing an identifier', async () => {
19
const result = await _getDocumentableNodeIfOnIdentifier(
20
WASMLanguage.TypeScript,
21
'const x = 1;',
22
{
23
startIndex: 0,
24
endIndex: 0,
25
}
26
);
27
expect(result).toBeUndefined();
28
});
29
30
test('should return object for range containing an identifier not in a definition or declaration', async () => {
31
const result = await _getDocumentableNodeIfOnIdentifier(
32
WASMLanguage.TypeScript,
33
'const x = 1;',
34
{
35
startIndex: 6,
36
endIndex: 7,
37
}
38
);
39
expect(result).toMatchInlineSnapshot(`
40
{
41
"identifier": "x",
42
"nodeRange": {
43
"endIndex": 11,
44
"startIndex": 6,
45
},
46
}
47
`);
48
expect(
49
insertRangeMarkers('const x = 1;', [result?.nodeRange!])
50
).toMatchInlineSnapshot(
51
`"const <>x = 1</>;"`
52
);
53
});
54
55
test('should return the identifier and node range for a range containing an identifier in a definition', async () => {
56
const result = await _getDocumentableNodeIfOnIdentifier(
57
WASMLanguage.TypeScript,
58
'function foo() {}',
59
{ startIndex: 9, endIndex: 12 }
60
);
61
expect(result).toMatchInlineSnapshot(`
62
{
63
"identifier": "foo",
64
"nodeRange": {
65
"endIndex": 17,
66
"startIndex": 0,
67
},
68
}
69
`);
70
expect(
71
insertRangeMarkers('function foo() {}', [result?.nodeRange!])
72
).toMatchInlineSnapshot(`"<>function foo() {}</>"`);
73
});
74
75
test('should return the identifier and node range for a range containing an identifier in a declaration', async () => {
76
const result = await _getDocumentableNodeIfOnIdentifier(
77
WASMLanguage.TypeScript,
78
'const x = 1;',
79
{
80
startIndex: 6,
81
endIndex: 7,
82
}
83
);
84
expect(result).toMatchInlineSnapshot(`
85
{
86
"identifier": "x",
87
"nodeRange": {
88
"endIndex": 11,
89
"startIndex": 6,
90
},
91
}
92
`);
93
});
94
95
test('should return the identifier and node range for a range containing an identifier in a var spec', async () => {
96
const result = await _getDocumentableNodeIfOnIdentifier(
97
WASMLanguage.TypeScript,
98
'var x: number;',
99
{ startIndex: 4, endIndex: 5 }
100
);
101
expect(result).toMatchInlineSnapshot(`
102
{
103
"identifier": "x",
104
"nodeRange": {
105
"endIndex": 13,
106
"startIndex": 4,
107
},
108
}
109
`);
110
});
111
});
112
113
suite('getParentScope', () => {
114
test('Finding parent node in TypeScript', async () => {
115
const result = await _getCoarseParentScope(
116
WASMLanguage.TypeScript,
117
[
118
'interface IFar {',
119
' bar(): void;',
120
' foo(): void;',
121
'}'
122
].join('\n'),
123
{ startPosition: { row: 1, column: 2 }, endPosition: { row: 1, column: 5 } }
124
);
125
expect(result).toStrictEqual({
126
startPosition: { row: 0, column: 0 },
127
endPosition: { row: 3, column: 1 },
128
});
129
});
130
131
test('Finding parent node in Python', async () => {
132
const result = await _getCoarseParentScope(
133
WASMLanguage.Python,
134
[
135
'# some comment',
136
'class Room:',
137
' length = 0.0',
138
' breadth = 0.0',
139
' def Area(abc):',
140
' print("The area is " + length*breadth)',
141
'',
142
'# some other comment',
143
].join('\n'),
144
{
145
startPosition: { row: 5, column: 5 },
146
endPosition: { row: 5, column: 5 },
147
}
148
);
149
expect(result).toStrictEqual({
150
startPosition: { row: 4, column: 3 },
151
endPosition: { row: 5, column: 44 },
152
});
153
});
154
155
test('Finding parent node in Java', async () => {
156
const result = await _getCoarseParentScope(
157
WASMLanguage.Java,
158
[
159
'/*** comment',
160
'',
161
' comment ***/ ',
162
'public class Main {',
163
' public static void main(String[] args) {',
164
' System.out.println("Hello World");',
165
' }',
166
'}',
167
].join('\n'),
168
{
169
startPosition: { row: 5, column: 5 },
170
endPosition: { row: 5, column: 5 },
171
}
172
);
173
expect(result).toStrictEqual({
174
startPosition: { row: 4, column: 1 },
175
endPosition: { row: 6, column: 2 },
176
});
177
});
178
});
179
180
181
suite('All Tree Sitter Queries are valid', () => {
182
183
afterAll(() => _dispose());
184
185
for (const language in allKnownQueries) {
186
generateTest(language as WASMLanguage);
187
}
188
189
function generateTest(language: WASMLanguage) {
190
test(`Valid queries for ${language}`, async () => {
191
const queries = allKnownQueries[language];
192
const parseTreeRef = await _parse(language, '');
193
try {
194
const lang = parseTreeRef.tree.getLanguage();
195
for (const query of queries) {
196
try {
197
lang.query(query);
198
} catch (err) {
199
assert.fail(`Query failed for ${query}: ${err}`);
200
}
201
}
202
} finally {
203
parseTreeRef.dispose();
204
}
205
});
206
}
207
});
208
209