chore: use ESLint API for linting (#26705)

* chore: use ESLint API for linting

* chore: fix typo

* chore: fix import
This commit is contained in:
David Sanders 2020-11-30 17:47:29 -08:00 committed by GitHub
parent cf970f31a3
commit 61f339d3aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
const { GitProcess } = require('dugite'); const { GitProcess } = require('dugite');
const childProcess = require('child_process'); const childProcess = require('child_process');
const { ESLint } = require('eslint');
const fs = require('fs'); const fs = require('fs');
const klaw = require('klaw'); const klaw = require('klaw');
const minimist = require('minimist'); const minimist = require('minimist');
@ -96,39 +97,24 @@ const LINTERS = [{
roots: ['build', 'default_app', 'lib', 'npm', 'script', 'spec', 'spec-main'], roots: ['build', 'default_app', 'lib', 'npm', 'script', 'spec', 'spec-main'],
ignoreRoots: ['spec/node_modules', 'spec-main/node_modules'], ignoreRoots: ['spec/node_modules', 'spec-main/node_modules'],
test: filename => filename.endsWith('.js') || filename.endsWith('.ts'), test: filename => filename.endsWith('.js') || filename.endsWith('.ts'),
run: (opts, filenames) => { run: async (opts, filenames) => {
const cmd = path.join(SOURCE_ROOT, 'node_modules', '.bin', 'eslint'); const eslint = new ESLint({
const args = ['--cache', '--ext', '.js,.ts']; cache: true,
if (opts.fix) args.unshift('--fix'); extensions: ['.js', '.ts'],
// Windows has a max command line length of 2047 characters, so we can't provide fix: opts.fix
// all of the filenames without going over that. To work around it, run eslint });
// multiple times and chunk the filenames so that each run is under that limit. const formatter = await eslint.loadFormatter();
// Use a much higher limit on other platforms which will effectively be a no-op. let successCount = 0;
const MAX_FILENAME_ARGS_LENGTH = IS_WINDOWS ? 1900 : 100 * 1024; for (const result of await eslint.lintFiles(filenames)) {
const cmdOpts = { stdio: 'inherit', shell: IS_WINDOWS, cwd: SOURCE_ROOT }; successCount += result.errorCount === 0 ? 1 : 0;
if (IS_WINDOWS) { if (result.errorCount !== 0 || result.warningCount !== 0) {
// When running with shell spaces in filenames are problematic console.log(formatter.format([result]));
filenames = filenames.map(filename => `"${filename}"`); } else if (opts.verbose) {
console.log(`${result.filePath}: no errors or warnings`);
} }
const chunkedFilenames = filenames.reduce((chunkedFilenames, filename) => {
const currentChunk = chunkedFilenames[chunkedFilenames.length - 1];
const currentChunkLength = currentChunk.reduce((totalLength, _filename) => totalLength + _filename.length, 0);
if (currentChunkLength + filename.length > MAX_FILENAME_ARGS_LENGTH) {
chunkedFilenames.push([filename]);
} else {
currentChunk.push(filename);
} }
return chunkedFilenames; if (successCount !== filenames.length) {
}, [[]]); console.error('Linting had errors');
const allOk = chunkedFilenames.map(filenames => {
const result = childProcess.spawnSync(cmd, [...args, ...filenames], cmdOpts);
if (result.error) {
console.error(result.error);
process.exit(result.status || 1);
}
return result.status === 0;
}).every(x => x);
if (!allOk) {
process.exit(1); process.exit(1);
} }
} }
@ -324,7 +310,7 @@ async function main () {
const filenames = await findFiles(opts, linter); const filenames = await findFiles(opts, linter);
if (filenames.length) { if (filenames.length) {
if (opts.verbose) { console.log(`linting ${filenames.length} ${linter.key} ${filenames.length === 1 ? 'file' : 'files'}`); } if (opts.verbose) { console.log(`linting ${filenames.length} ${linter.key} ${filenames.length === 1 ? 'file' : 'files'}`); }
linter.run(opts, filenames); await linter.run(opts, filenames);
} }
} }
} }