Skip to content

Commit b32b677

Browse files
authored
Migrate from CLIEngine to the new ESLint class. (#22756)
* Migrate from CLIEngine to the new ESLint class. * fix output property
1 parent 8edeb78 commit b32b677

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

scripts/eslint/index.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
'use strict';
99

1010
const minimatch = require('minimatch');
11-
const CLIEngine = require('eslint').CLIEngine;
11+
const {ESLint} = require('eslint');
1212
const listChangedFiles = require('../shared/listChangedFiles');
1313

1414
const allPaths = ['**/*.js'];
1515

1616
let changedFiles = null;
1717

18-
function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
19-
const cli = new CLIEngine(options);
20-
const formatter = cli.getFormatter();
18+
async function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
19+
const eslint = new ESLint(options);
20+
const formatter = await eslint.loadFormatter();
2121

2222
if (onlyChanged && changedFiles === null) {
2323
// Calculate lazily.
@@ -26,15 +26,15 @@ function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
2626
const finalFilePatterns = onlyChanged
2727
? intersect(changedFiles, filePatterns)
2828
: filePatterns;
29-
const report = cli.executeOnFiles(finalFilePatterns);
29+
const results = await eslint.lintFiles(finalFilePatterns);
3030

3131
if (options != null && options.fix === true) {
32-
CLIEngine.outputFixes(report);
32+
await ESLint.outputFixes(results);
3333
}
3434

3535
// When using `ignorePattern`, eslint will show `File ignored...` warnings for any ignores.
3636
// We don't care because we *expect* some passed files will be ignores if `ignorePattern` is used.
37-
const messages = report.results.filter(item => {
37+
const messages = results.filter(item => {
3838
if (!onlyChanged) {
3939
// Don't suppress the message on a full run.
4040
// We only expect it to happen for "only changed" runs.
@@ -45,11 +45,19 @@ function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
4545
return !(item.messages[0] && item.messages[0].message === ignoreMessage);
4646
});
4747

48-
const ignoredMessageCount = report.results.length - messages.length;
48+
const errorCount = results.reduce(
49+
(count, result) => count + result.errorCount,
50+
0
51+
);
52+
const warningCount = results.reduce(
53+
(count, result) => count + result.warningCount,
54+
0
55+
);
56+
const ignoredMessageCount = results.length - messages.length;
4957
return {
50-
output: formatter(messages),
51-
errorCount: report.errorCount,
52-
warningCount: report.warningCount - ignoredMessageCount,
58+
output: formatter.format(messages),
59+
errorCount: errorCount,
60+
warningCount: warningCount - ignoredMessageCount,
5361
};
5462
}
5563

@@ -64,11 +72,11 @@ function intersect(files, patterns) {
6472
return [...new Set(intersection)];
6573
}
6674

67-
function runESLint({onlyChanged, ...options}) {
75+
async function runESLint({onlyChanged, ...options}) {
6876
if (typeof onlyChanged !== 'boolean') {
6977
throw new Error('Pass options.onlyChanged as a boolean.');
7078
}
71-
const {errorCount, warningCount, output} = runESLintOnFilesWithOptions(
79+
const {errorCount, warningCount, output} = await runESLintOnFilesWithOptions(
7280
allPaths,
7381
onlyChanged,
7482
options

scripts/tasks/eslint.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,22 @@
1010
const minimist = require('minimist');
1111
const runESLint = require('../eslint');
1212

13-
console.log('Linting all files...');
14-
// https://circleci.com/docs/2.0/env-vars/#circleci-environment-variable-descriptions
15-
if (!process.env.CI) {
16-
console.log('Hint: run `yarn linc` to only lint changed files.');
17-
}
13+
async function main() {
14+
console.log('Linting all files...');
15+
// https://circleci.com/docs/2.0/env-vars/#circleci-environment-variable-descriptions
16+
if (!process.env.CI) {
17+
console.log('Hint: run `yarn linc` to only lint changed files.');
18+
}
19+
20+
// eslint-disable-next-line no-unused-vars
21+
const {_, ...cliOptions} = minimist(process.argv.slice(2));
1822

19-
const cliOptions = minimist(process.argv.slice(2));
20-
if (runESLint({onlyChanged: false, ...cliOptions})) {
21-
console.log('Lint passed.');
22-
} else {
23-
console.log('Lint failed.');
24-
process.exit(1);
23+
if (await runESLint({onlyChanged: false, ...cliOptions})) {
24+
console.log('Lint passed.');
25+
} else {
26+
console.log('Lint failed.');
27+
process.exit(1);
28+
}
2529
}
30+
31+
main();

scripts/tasks/linc.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
const minimist = require('minimist');
1111
const runESLint = require('../eslint');
1212

13-
console.log('Linting changed files...');
13+
async function main() {
14+
console.log('Linting changed files...');
1415

15-
const cliOptions = minimist(process.argv.slice(2));
16-
if (runESLint({onlyChanged: true, ...cliOptions})) {
17-
console.log('Lint passed for changed files.');
18-
} else {
19-
console.log('Lint failed for changed files.');
20-
process.exit(1);
16+
// eslint-disable-next-line no-unused-vars
17+
const {_, ...cliOptions} = minimist(process.argv.slice(2));
18+
19+
if (await runESLint({onlyChanged: true, ...cliOptions})) {
20+
console.log('Lint passed for changed files.');
21+
} else {
22+
console.log('Lint failed for changed files.');
23+
process.exit(1);
24+
}
2125
}
26+
27+
main();

0 commit comments

Comments
 (0)