|
| 1 | +// Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Use V8's code cache to speed up instantiation time. |
| 6 | +await import('v8-compile-cache'); |
| 7 | + |
| 8 | +import path from 'path'; |
| 9 | +import {ESLint} from 'eslint'; |
| 10 | +import { fileURLToPath } from 'url'; |
| 11 | + |
| 12 | +import yargs from 'yargs'; |
| 13 | + |
| 14 | +// False positive from the ESLint rule: crbug.com/1319352 |
| 15 | +// eslint-disable-next-line rulesdir/es_modules_import |
| 16 | +import {hideBin} from 'yargs/helpers'; |
| 17 | + |
| 18 | +const flags = yargs(hideBin(process.argv)).option('fix', { |
| 19 | + type: 'boolean', |
| 20 | + default: true, |
| 21 | + describe: 'If to run ESLint in fix mode', |
| 22 | +}).parse(); |
| 23 | + |
| 24 | +const shouldFix = flags.fix === true; |
| 25 | + |
| 26 | +if(!shouldFix) { |
| 27 | + console.log('[ESLint]: fix is disabled; no errors will be autofixed.'); |
| 28 | +} |
| 29 | + |
| 30 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 31 | + |
| 32 | +const ROOT_DIRECTORY = path.join(__dirname, '..', '..'); |
| 33 | +const FRONT_END_DIRECTORY = path.join(ROOT_DIRECTORY, 'front_end'); |
| 34 | +const INSPECTOR_OVERLAY_DIRECTORY = path.join(ROOT_DIRECTORY, 'inspector_overlay'); |
| 35 | +const TEST_DIRECTORY = path.join(ROOT_DIRECTORY, 'test'); |
| 36 | +const SCRIPTS_DIRECTORY = path.join(ROOT_DIRECTORY, 'scripts'); |
| 37 | + |
| 38 | +const DEFAULT_DIRECTORIES_TO_LINT = |
| 39 | + [FRONT_END_DIRECTORY, INSPECTOR_OVERLAY_DIRECTORY, TEST_DIRECTORY, SCRIPTS_DIRECTORY]; |
| 40 | + |
| 41 | +const eslintignorePath = path.join(ROOT_DIRECTORY, '.eslintignore'); |
| 42 | + |
| 43 | +// Yargs gathers up any non-flag arguments into the `_` property. |
| 44 | +// npm run check-lint-js foo => flags._ === ['foo'] |
| 45 | +let directoriesOrFilesToLint = flags._; |
| 46 | + |
| 47 | +if (directoriesOrFilesToLint.length === 0) { |
| 48 | + directoriesOrFilesToLint = DEFAULT_DIRECTORIES_TO_LINT; |
| 49 | +} |
| 50 | + |
| 51 | +const cli = new ESLint({ |
| 52 | + extensions: ['.js', '.ts'], |
| 53 | + ignorePath: eslintignorePath, |
| 54 | + fix: shouldFix, |
| 55 | +}); |
| 56 | + |
| 57 | +// We filter out certain files in the `.eslintignore`. However, ESLint produces warnings |
| 58 | +// when you include a particular file that is ignored. This means that if you edit a file |
| 59 | +// that is directly ignored in the `.eslintignore`, ESLint would report a failure. |
| 60 | +// This was originally reported in https://github.com/eslint/eslint/issues/9977 |
| 61 | +// The suggested workaround is to use the CLIEngine to pre-emptively filter out these |
| 62 | +// problematic paths. |
| 63 | +const filteredFilesToLint = []; |
| 64 | +for (const path of directoriesOrFilesToLint) { |
| 65 | + if (!(await cli.isPathIgnored(path))) { |
| 66 | + filteredFilesToLint.push(path); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +const results = await cli.lintFiles(filteredFilesToLint); |
| 71 | + |
| 72 | +// Write fixes to the filesystem |
| 73 | +if (shouldFix) { |
| 74 | + await ESLint.outputFixes(results); |
| 75 | +} |
| 76 | + |
| 77 | +const formatter = await cli.loadFormatter('stylish'); |
| 78 | +console.log(formatter.format(results)); |
| 79 | + |
| 80 | +const hasProblems = results.find(report => report.errorCount + report.warningCount > 0); |
| 81 | +process.exit(hasProblems ? 1 : 0); |
0 commit comments