Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/inline/slashDoc.util.ts
13388 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
import * as assert from 'assert';
6
7
export function assertDocLines(fileContents: string | string[], line: string, lineAssertion = (line: string) => { }) {
8
9
const fileLines = (Array.isArray(fileContents)) ? fileContents : fileContents.split('\n');
10
11
const lineNum = fileLines.findIndex(s => s.startsWith(line));
12
assert.ok(lineNum >= 0, 'missing or unterminated doc comment');
13
14
assert.strictEqual(fileLines[lineNum - 1].trim(), '*/', 'has closing */');
15
16
for (let i = lineNum - 2; i >= 0; --i) {
17
const line = fileLines[i];
18
if (line.trimStart().startsWith('/**')) {
19
return;
20
}
21
assert.ok(line.trimStart().startsWith('*'), 'has middle *');
22
lineAssertion(line);
23
}
24
25
}
26
27
/**
28
* Golang & Ruby use inline comments for doc comments, ie golang uses `//` both for implementation & doc comments
29
*/
30
export function assertDocLinesForInlineComments(fileContents: string | string[], line: string, docCommentPrefix: string) {
31
32
const fileLines = (Array.isArray(fileContents)) ? fileContents : fileContents.split('\n');
33
34
const lineNum = fileLines.lastIndexOf(line);
35
36
if (lineNum === -1) { throw new Error(`given line cannot be found: either original line was changed or test was incorrectly created`); }
37
38
const indentation = fileLines[lineNum].match(/^\s+/)?.[0] ?? '';
39
const expectedPrefix = `${indentation}${docCommentPrefix}`;
40
41
let hadDoc = false;
42
for (let i = lineNum - 1; i >= 0; --i) {
43
const line = fileLines[i];
44
if (line.startsWith(expectedPrefix)) {
45
hadDoc = true;
46
} else {
47
break;
48
}
49
}
50
assert.ok(hadDoc, 'did not see comments');
51
}
52
53