@samitouri / QOS-React / commits / b32b67706f

Migrate from CLIEngine to the new ESLint class. (#22756)

* Migrate from CLIEngine to the new ESLint class. * fix output property

Esteban committed Nov 18, 2021 at 22:12 UTC b32b67706f1214b730e4203981e5aefe04d473e5
3 files changed +51 -31
scripts/eslint/index.js
+21 -13
@@ -8,16 +8,16 @@
8 'use strict';
9
10 const minimatch = require('minimatch');
11 -const CLIEngine = require('eslint').CLIEngine;
11 +const {ESLint} = require('eslint');
12 const listChangedFiles = require('../shared/listChangedFiles');
13
14 const allPaths = ['**/*.js'];
15
16 let changedFiles = null;
17
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();
21
22 if (onlyChanged && changedFiles === null) {
23 // Calculate lazily.
@@ -26,15 +26,15 @@ function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
26 const finalFilePatterns = onlyChanged
27 ? intersect(changedFiles, filePatterns)
28 : filePatterns;
29 - const report = cli.executeOnFiles(finalFilePatterns);
29 + const results = await eslint.lintFiles(finalFilePatterns);
30
31 if (options != null && options.fix === true) {
32 - CLIEngine.outputFixes(report);
32 + await ESLint.outputFixes(results);
33 }
34
35 // When using `ignorePattern`, eslint will show `File ignored...` warnings for any ignores.
36 // 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 => {
38 if (!onlyChanged) {
39 // Don't suppress the message on a full run.
40 // We only expect it to happen for "only changed" runs.
@@ -45,11 +45,19 @@ function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
45 return !(item.messages[0] && item.messages[0].message === ignoreMessage);
46 });
47
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;
57 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,
61 };
62 }
63
@@ -64,11 +72,11 @@ function intersect(files, patterns) {
72 return [...new Set(intersection)];
73 }
74
67 -function runESLint({onlyChanged, ...options}) {
75 +async function runESLint({onlyChanged, ...options}) {
76 if (typeof onlyChanged !== 'boolean') {
77 throw new Error('Pass options.onlyChanged as a boolean.');
78 }
71 - const {errorCount, warningCount, output} = runESLintOnFilesWithOptions(
79 + const {errorCount, warningCount, output} = await runESLintOnFilesWithOptions(
80 allPaths,
81 onlyChanged,
82 options
scripts/tasks/eslint.js
+17 -11
@@ -10,16 +10,22 @@
10 const minimist = require('minimist');
11 const runESLint = require('../eslint');
12
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));
22
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 + }
29 }
30 +
31 +main();
scripts/tasks/linc.js
+13 -7
@@ -10,12 +10,18 @@
10 const minimist = require('minimist');
11 const runESLint = require('../eslint');
12
13 -console.log('Linting changed files...');
13 +async function main() {
14 + console.log('Linting changed files...');
15
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 + }
25 }
26 +
27 +main();