Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/diagnosticProviders/eslint.ts
13394 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 { LintingDiagnosticsProvider } from './utils';
13
14
/**
15
* Class which finds eslint diagnostic erors
16
*/
17
export class EslintDiagnosticsProvider extends LintingDiagnosticsProvider {
18
19
override readonly id = 'eslint';
20
override readonly cacheSalt = TestingCacheSalts.eslintCacheSalt;
21
override readonly cacheScope = CacheScope.ESLint;
22
23
private get eslintConfig(): any {
24
return {
25
'parser': '@typescript-eslint/parser',
26
'plugins': ['@typescript-eslint'],
27
'extends': [],
28
'parserOptions': {
29
'warnOnUnsupportedTypeScriptVersion': false,
30
'sourceType': 'module',
31
'ecmaVersion': 'latest',
32
'ecmaFeatures': { 'jsx': true, 'experimentalObjectRestSpread': true },
33
},
34
'ignorePatterns': ['!+'],
35
'rules': {
36
'constructor-super': 'error',
37
'for-direction': 'error',
38
'getter-return': 'error',
39
'no-async-promise-executor': 'error',
40
'no-class-assign': 'error',
41
'no-compare-neg-zero': 'error',
42
'no-cond-assign': 'error',
43
'no-const-assign': 'error',
44
'no-constant-condition': 'error',
45
'no-control-regex': 'error',
46
'no-dupe-args': 'error',
47
'no-empty-pattern': 'error',
48
'no-ex-assign': 'error',
49
'no-invalid-regexp': 'error',
50
'no-new-symbol': 'error',
51
'no-obj-calls': 'error',
52
'no-prototype-builtins': 'error',
53
'no-self-assign': 'error',
54
'no-setter-return': 'error',
55
'no-unreachable': 'error',
56
'no-unreachable-loop': 'error',
57
'no-unsafe-finally': 'error',
58
'no-unsafe-negation': 'error',
59
'no-unsafe-optional-chaining': 'error',
60
'use-isnan': 'error',
61
'indent': 'off'
62
},
63
};
64
}
65
66
protected override async fetchCommand(temporaryDirectory: string, filePath: string) {
67
const eslintConfigFile = path.join(temporaryDirectory, '.eslintrc.json');
68
await fs.promises.writeFile(eslintConfigFile, JSON.stringify(this.eslintConfig));
69
return {
70
command: 'npx',
71
arguments: ['eslint', '--no-eslintrc', '--config', eslintConfigFile, '--no-ignore', '-f', 'json', filePath]
72
};
73
}
74
75
protected override processDiagnostics(fileName: string, stdoutResult: any): ITestDiagnostic[] {
76
assert(Array.isArray(stdoutResult));
77
if (stdoutResult.length === 0) {
78
return [];
79
}
80
const sanitizeLineOrColumn = (lineOrColumn: any) => typeof lineOrColumn !== 'number' || Number.isNaN(lineOrColumn) ? 0 : Math.max(0, lineOrColumn - 1);
81
const diagnostics = [];
82
const messages = stdoutResult[0].messages;
83
assert(Array.isArray(messages));
84
for (const message of messages) {
85
const messageText = message.message;
86
assert(typeof messageText === 'string');
87
diagnostics.push({
88
file: fileName,
89
startLine: sanitizeLineOrColumn(message.line),
90
startCharacter: sanitizeLineOrColumn(message.column),
91
endLine: sanitizeLineOrColumn(message.endLine),
92
endCharacter: sanitizeLineOrColumn(message.endColumn),
93
message: messageText,
94
code: message.ruleId,
95
relatedInformation: undefined,
96
source: 'eslint'
97
});
98
}
99
return diagnostics;
100
}
101
}
102
103