Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/diagnosticProviders/ruff.ts
13395 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 * as fs from 'fs';
8
import * as path from 'path';
9
import { TestingCacheSalts } from '../../base/salts';
10
import { CacheScope } from '../../base/simulationContext';
11
import { ITestDiagnostic } from './diagnosticsProvider';
12
import { ensurePythonVEnv } from './python';
13
import { LintingDiagnosticsProvider } from './utils';
14
15
/**
16
* Class which finds python rule tooling diagnostic erors
17
*/
18
export class RuffDiagnosticsProvider extends LintingDiagnosticsProvider {
19
20
override readonly id = 'ruff';
21
override readonly cacheSalt = TestingCacheSalts.ruffCacheSalt;
22
override readonly cacheScope = CacheScope.Ruff;
23
24
private get ruffConfig(): string {
25
// pyproject.toml
26
return `
27
[tool.ruff]
28
select = ["ALL"]
29
30
[tool.ruff.lint]
31
preview = true
32
33
[tool.ruff.format]
34
preview = true
35
`;
36
37
}
38
39
protected override async fetchCommand(temporaryDirectory: string, filePath: string) {
40
const ruffConfigFile = path.join(temporaryDirectory, 'pyproject.toml');
41
await fs.promises.writeFile(ruffConfigFile, this.ruffConfig, 'utf8');
42
const virtualEnvironment = ensurePythonVEnv();
43
if (!virtualEnvironment) {
44
throw new Error('No virtual environment found');
45
}
46
47
return {
48
command: virtualEnvironment.pythonInterpreter,
49
arguments: ['-m', 'ruff', 'check', filePath, '--config', ruffConfigFile, '--output-format', 'json']
50
};
51
}
52
53
protected override processDiagnostics(fileName: string, stdoutResult: any): ITestDiagnostic[] {
54
assert(Array.isArray(stdoutResult));
55
if (stdoutResult.length === 0) {
56
return [];
57
}
58
const sanitizeLineOrColumn = (lineOrColumn: any) => typeof lineOrColumn !== 'number' || Number.isNaN(lineOrColumn) ? 0 : Math.max(0, lineOrColumn - 1);
59
const diagnostics = [];
60
const messages = stdoutResult;
61
assert(Array.isArray(messages));
62
for (const message of messages) {
63
const messageText = message.message;
64
assert(typeof messageText === 'string');
65
diagnostics.push({
66
file: fileName,
67
startLine: sanitizeLineOrColumn(message.location.row),
68
startCharacter: sanitizeLineOrColumn(message.location.column),
69
endLine: sanitizeLineOrColumn(message.end_location.row),
70
endCharacter: sanitizeLineOrColumn(message.end_location.column),
71
message: messageText,
72
code: message.ruleId,
73
relatedInformation: undefined,
74
source: 'Ruff'
75
});
76
}
77
return diagnostics;
78
}
79
}
80
81