Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/gulp-eslint.ts
4770 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 { ESLint } from 'eslint';
7
import fancyLog from 'fancy-log';
8
import { relative } from 'path';
9
import { Transform, type TransformOptions } from 'stream';
10
11
interface ESLintResults extends Array<ESLint.LintResult> {
12
errorCount: number;
13
warningCount: number;
14
}
15
16
interface EslintAction {
17
(results: ESLintResults): void;
18
}
19
20
export default function eslint(action: EslintAction) {
21
const linter = new ESLint({});
22
const formatter = linter.loadFormatter('compact');
23
24
const results: ESLintResults = Object.assign([], { errorCount: 0, warningCount: 0 });
25
26
return createTransform(
27
async (file, _enc, cb) => {
28
const filePath = relative(process.cwd(), file.path);
29
30
if (file.isNull()) {
31
cb(null, file);
32
return;
33
}
34
35
if (file.isStream()) {
36
cb(new Error('vinyl files with Stream contents are not supported'));
37
return;
38
}
39
40
try {
41
// TODO: Should this be checked?
42
if (await linter.isPathIgnored(filePath)) {
43
cb(null, file);
44
return;
45
}
46
47
const result = (await linter.lintText(file.contents.toString(), { filePath }))[0];
48
results.push(result);
49
results.errorCount += result.errorCount;
50
results.warningCount += result.warningCount;
51
52
const message = (await formatter).format([result]);
53
if (message) {
54
fancyLog(message);
55
}
56
cb(null, file);
57
} catch (error) {
58
cb(error);
59
}
60
},
61
(done) => {
62
try {
63
action(results);
64
done();
65
} catch (error) {
66
done(error);
67
}
68
});
69
}
70
71
function createTransform(
72
transform: TransformOptions['transform'],
73
flush: TransformOptions['flush']
74
): Transform {
75
return new Transform({
76
objectMode: true,
77
transform,
78
flush
79
});
80
}
81
82