chore: lint code blocks in docs with ESLint (#42113)

This commit is contained in:
David Sanders 2025-05-29 12:45:26 -07:00 committed by GitHub
commit 0d70389ccb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 308 additions and 34 deletions

View file

@ -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);
}