Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from CLIEngine to the new ESLint class. #22756

Merged
merged 2 commits into from Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 21 additions & 13 deletions scripts/eslint/index.js
Expand Up @@ -8,16 +8,16 @@
'use strict';

const minimatch = require('minimatch');
const CLIEngine = require('eslint').CLIEngine;
const {ESLint} = require('eslint');
const listChangedFiles = require('../shared/listChangedFiles');

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

let changedFiles = null;

function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
const cli = new CLIEngine(options);
const formatter = cli.getFormatter();
async function runESLintOnFilesWithOptions(filePatterns, onlyChanged, options) {
const eslint = new ESLint(options);
const formatter = await eslint.loadFormatter();

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

if (options != null && options.fix === true) {
CLIEngine.outputFixes(report);
await ESLint.outputFixes(results);
}

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

const ignoredMessageCount = report.results.length - messages.length;
const errorCount = results.reduce(
(count, result) => count + result.errorCount,
0
);
const warningCount = results.reduce(
(count, result) => count + result.warningCount,
0
);
Comment on lines +48 to +55
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new class no longer returns a global report with errorCount and warningCount, so I calculated them manually.

const ignoredMessageCount = results.length - messages.length;
return {
output: formatter(messages),
errorCount: report.errorCount,
warningCount: report.warningCount - ignoredMessageCount,
output: formatter.format(messages),
errorCount: errorCount,
warningCount: warningCount - ignoredMessageCount,
};
}

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

function runESLint({onlyChanged, ...options}) {
async function runESLint({onlyChanged, ...options}) {
if (typeof onlyChanged !== 'boolean') {
throw new Error('Pass options.onlyChanged as a boolean.');
}
const {errorCount, warningCount, output} = runESLintOnFilesWithOptions(
const {errorCount, warningCount, output} = await runESLintOnFilesWithOptions(
allPaths,
onlyChanged,
options
Expand Down
28 changes: 17 additions & 11 deletions scripts/tasks/eslint.js
Expand Up @@ -10,16 +10,22 @@
const minimist = require('minimist');
const runESLint = require('../eslint');

console.log('Linting all files...');
// https://circleci.com/docs/2.0/env-vars/#circleci-environment-variable-descriptions
if (!process.env.CI) {
console.log('Hint: run `yarn linc` to only lint changed files.');
}
async function main() {
console.log('Linting all files...');
// https://circleci.com/docs/2.0/env-vars/#circleci-environment-variable-descriptions
if (!process.env.CI) {
console.log('Hint: run `yarn linc` to only lint changed files.');
}

// eslint-disable-next-line no-unused-vars
const {_, ...cliOptions} = minimist(process.argv.slice(2));
Comment on lines +20 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add destructuring to prevent passing _, which minimist generates by default. Otherwise, ESlint returns this error:

yarn linc
yarn run v1.22.17
$ node ./scripts/tasks/linc.js
Linting changed files...
Lint passed for changed files.
(node:84712) UnhandledPromiseRejectionWarning: Error: Invalid Options:
- Unknown options: _
    at processOptions (/Users/XXX/projects/react/node_modules/eslint/lib/eslint/eslint.js:273:15)
    at new ESLint (/Users/XXX/projects/react/node_modules/eslint/lib/eslint/eslint.js:416:34)


const cliOptions = minimist(process.argv.slice(2));
if (runESLint({onlyChanged: false, ...cliOptions})) {
console.log('Lint passed.');
} else {
console.log('Lint failed.');
process.exit(1);
if (await runESLint({onlyChanged: false, ...cliOptions})) {
console.log('Lint passed.');
} else {
console.log('Lint failed.');
process.exit(1);
}
}

main();
20 changes: 13 additions & 7 deletions scripts/tasks/linc.js
Expand Up @@ -10,12 +10,18 @@
const minimist = require('minimist');
const runESLint = require('../eslint');

console.log('Linting changed files...');
async function main() {
console.log('Linting changed files...');

const cliOptions = minimist(process.argv.slice(2));
if (runESLint({onlyChanged: true, ...cliOptions})) {
console.log('Lint passed for changed files.');
} else {
console.log('Lint failed for changed files.');
process.exit(1);
// eslint-disable-next-line no-unused-vars
const {_, ...cliOptions} = minimist(process.argv.slice(2));
Comment on lines +16 to +17
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


if (await runESLint({onlyChanged: true, ...cliOptions})) {
console.log('Lint passed for changed files.');
} else {
console.log('Lint failed for changed files.');
process.exit(1);
}
}

main();