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