getESLint.js
3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loadESLint = loadESLint;
exports.loadESLintThreaded = loadESLintThreaded;
exports.default = getESLint;
var _os = _interopRequireDefault(require("os"));
var _jestWorker = _interopRequireDefault(require("jest-worker"));
var _options = require("./options");
var _utils = require("./utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/** @type {{[key: string]: any}} */
const cache = {};
/** @typedef {import('eslint').ESLint} ESLint */
/** @typedef {import('eslint').ESLint.LintResult} LintResult */
/** @typedef {import('./options').Options} Options */
/** @typedef {() => Promise<void>} AsyncTask */
/** @typedef {(files: string|string[]) => Promise<LintResult[]>} LintTask */
/** @typedef {JestWorker & {lintFiles: LintTask}} Worker */
/** @typedef {{threads: number, ESLint: ESLint, eslint: ESLint, lintFiles: LintTask, cleanup: AsyncTask}} Linter */
/**
* @param {Options} options
* @returns {Linter}
*/
function loadESLint(options) {
const {
eslintPath
} = options;
const {
ESLint
} = require(eslintPath || 'eslint'); // Filter out loader options before passing the options to ESLint.
const eslint = new ESLint((0, _options.getESLintOptions)(options));
return {
threads: 1,
ESLint,
eslint,
lintFiles: async files => {
const results = await eslint.lintFiles(files); // istanbul ignore else
if (options.fix) {
await ESLint.outputFixes(results);
}
return results;
},
// no-op for non-threaded
cleanup: async () => {}
};
}
/**
* @param {number} poolSize
* @param {Options} options
* @returns {Linter}
*/
function loadESLintThreaded(poolSize, options) {
const key = getCacheKey(options);
const {
eslintPath = 'eslint'
} = options;
const source = require.resolve('./worker');
const workerOptions = {
enableWorkerThreads: true,
numWorkers: poolSize,
setupArgs: [{
eslintPath,
eslintOptions: (0, _options.getESLintOptions)(options)
}]
};
const local = loadESLint(options);
/** @type {Worker?} */
// prettier-ignore
let worker =
/** @type {Worker} */
new _jestWorker.default(source, workerOptions);
/** @type {Linter} */
const context = { ...local,
threads: poolSize,
lintFiles: async (files) => worker && (await worker.lintFiles(files)) ||
/* istanbul ignore next */
[],
cleanup: async () => {
cache[key] = local;
context.lintFiles = files => local.lintFiles(files);
if (worker) {
worker.end();
worker = null;
}
}
};
return context;
}
/**
* @param {Options} options
* @returns {Linter}
*/
function getESLint({
threads,
...options
}) {
const max = typeof threads !== 'number' ? threads ? _os.default.cpus().length - 1 : 1 :
/* istanbul ignore next */
threads;
const key = getCacheKey({
threads,
...options
});
if (!cache[key]) {
cache[key] = max > 1 ? loadESLintThreaded(max, options) : loadESLint(options);
}
return cache[key];
}
/**
* @param {Options} options
* @returns {string}
*/
function getCacheKey(options) {
return JSON.stringify(options, _utils.jsonStringifyReplacerSortKeys);
}