chore: lint code blocks in docs with ESLint (#42113)
This commit is contained in:
parent
4e61f5b26f
commit
0d70389ccb
51 changed files with 308 additions and 34 deletions
|
@ -73,6 +73,24 @@ function spawnAndCheckExitCode (cmd, args, opts) {
|
|||
}
|
||||
}
|
||||
|
||||
async function runEslint (eslint, filenames, { fix, verbose }) {
|
||||
const formatter = await eslint.loadFormatter();
|
||||
let successCount = 0;
|
||||
const results = await eslint.lintFiles(filenames);
|
||||
for (const result of results) {
|
||||
successCount += result.errorCount === 0 ? 1 : 0;
|
||||
if (verbose && result.errorCount === 0 && result.warningCount === 0) {
|
||||
console.log(`${result.filePath}: no errors or warnings`);
|
||||
}
|
||||
}
|
||||
console.log(formatter.format(results));
|
||||
if (fix) {
|
||||
await ESLint.outputFixes(results);
|
||||
}
|
||||
|
||||
return successCount === filenames.length;
|
||||
}
|
||||
|
||||
function cpplint (args) {
|
||||
args.unshift(`--root=${SOURCE_ROOT}`);
|
||||
const cmd = IS_WINDOWS ? 'cpplint.bat' : 'cpplint.py';
|
||||
|
@ -146,20 +164,8 @@ const LINTERS = [{
|
|||
fix: opts.fix,
|
||||
resolvePluginsRelativeTo: ELECTRON_ROOT
|
||||
});
|
||||
const formatter = await eslint.loadFormatter();
|
||||
let successCount = 0;
|
||||
const results = await eslint.lintFiles(filenames);
|
||||
for (const result of results) {
|
||||
successCount += result.errorCount === 0 ? 1 : 0;
|
||||
if (opts.verbose && result.errorCount === 0 && result.warningCount === 0) {
|
||||
console.log(`${result.filePath}: no errors or warnings`);
|
||||
}
|
||||
}
|
||||
console.log(formatter.format(results));
|
||||
if (opts.fix) {
|
||||
await ESLint.outputFixes(results);
|
||||
}
|
||||
if (successCount !== filenames.length) {
|
||||
const clean = await runEslint(eslint, filenames, { fix: opts.fix, verbose: opts.verbose });
|
||||
if (!clean) {
|
||||
console.error('Linting had errors');
|
||||
process.exit(1);
|
||||
}
|
||||
|
@ -363,6 +369,26 @@ const LINTERS = [{
|
|||
}
|
||||
}
|
||||
|
||||
const eslint = new ESLint({
|
||||
// Do not use the lint cache on CI builds
|
||||
cache: !process.env.CI,
|
||||
cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`,
|
||||
fix: opts.fix,
|
||||
overrideConfigFile: path.join(ELECTRON_ROOT, 'docs', '.eslintrc.json'),
|
||||
resolvePluginsRelativeTo: ELECTRON_ROOT
|
||||
});
|
||||
const clean = await runEslint(
|
||||
eslint,
|
||||
docs.filter(
|
||||
// TODO(dsanders11): Once we move to newer ESLint and the flat config,
|
||||
// switch to using `ignorePatterns` and `warnIgnore: false` instead of
|
||||
// explicitly filtering out this file that we don't want to lint
|
||||
(filename) => !filename.endsWith('docs/breaking-changes.md')
|
||||
),
|
||||
{ fix: opts.fix, verbose: opts.verbose }
|
||||
);
|
||||
errors ||= !clean;
|
||||
|
||||
if (errors) {
|
||||
process.exit(1);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue