Path: blob/main/extensions/copilot/test/inline/slashDoc.py.stest.ts
13388 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { guessIndentation } from '../../src/extension/prompt/node/indentationGuesser';678export function validateDocstringFormat(fileContents: string, targetLineString: string): void {9const lines = fileContents.split('\n');10const targetLineIndex = lines.findIndex(line => line.includes(targetLineString));1112if (targetLineIndex === -1) {13throw new Error('Target line not found in the file contents.');14}1516if (targetLineIndex === lines.length - 1) {17throw new Error('Target line is the last line of the file. No space for a docstring.');18}1920const indentation = guessIndentation(lines, 4, true);21const tabSize = indentation.tabSize;22const insertSpaces = indentation.insertSpaces;2324const targetLine = lines[targetLineIndex];25const targetIndentation = targetLine.match(/^\s*/)?.[0] || '';2627// Check the next line for a docstring start28const nextLine = lines[targetLineIndex + 1];29const docstringStart = nextLine.trim().match(/^('''|""")/);30if (!docstringStart) {31throw new Error('No docstring found after the target line.');32}3334const docstringIndentation = nextLine.match(/^\s*/)?.[0] || '';3536// Calculate the expected indentation37let expectedIndentation: string;38if (insertSpaces) {39expectedIndentation = targetIndentation + ' '.repeat(tabSize);40} else {41expectedIndentation = targetIndentation + '\t';42}4344// The docstring should have the expected indentation45if (docstringIndentation !== expectedIndentation) {46throw new Error(`Incorrect docstring indentation. Expected: '${expectedIndentation.replace(/ /g, '·')}', but got: '${docstringIndentation.replace(/ /g, '·')}'`);47}48}495051