chore: allow passing more roots to lint.js (#40571)

This commit is contained in:
Cheng Zhao 2023-11-27 09:26:33 +09:00 коммит произвёл GitHub
Родитель 79e714a825
Коммит 58a21a3cd9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 31 добавлений и 7 удалений

Просмотреть файл

@ -95,7 +95,7 @@ function isObjCHeader (filename) {
} }
const LINTERS = [{ const LINTERS = [{
key: 'c++', key: 'cpp',
roots: ['shell'], roots: ['shell'],
test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)), test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)),
run: (opts, filenames) => { run: (opts, filenames) => {
@ -136,7 +136,9 @@ const LINTERS = [{
cache: !process.env.CI, cache: !process.env.CI,
cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`, cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`,
extensions: ['.js', '.ts'], extensions: ['.js', '.ts'],
fix: opts.fix fix: opts.fix,
overrideConfigFile: path.join(ELECTRON_ROOT, '.eslintrc.json'),
resolvePluginsRelativeTo: ELECTRON_ROOT
}); });
const formatter = await eslint.loadFormatter(); const formatter = await eslint.loadFormatter();
let successCount = 0; let successCount = 0;
@ -363,18 +365,39 @@ const LINTERS = [{
function parseCommandLine () { function parseCommandLine () {
let help; let help;
const langs = ['cpp', 'objc', 'javascript', 'python', 'gn', 'patches', 'markdown'];
const langRoots = langs.map(lang => lang + '-roots');
const langIgnoreRoots = langs.map(lang => lang + '-ignore-roots');
const opts = minimist(process.argv.slice(2), { const opts = minimist(process.argv.slice(2), {
boolean: ['c++', 'objc', 'javascript', 'python', 'gn', 'patches', 'markdown', 'help', 'changed', 'fix', 'verbose', 'only'], boolean: [...langs, 'help', 'changed', 'fix', 'verbose', 'only'],
alias: { 'c++': ['cc', 'cpp', 'cxx'], javascript: ['js', 'es'], python: 'py', markdown: 'md', changed: 'c', help: 'h', verbose: 'v' }, alias: { cpp: ['c++', 'cc', 'cxx'], javascript: ['js', 'es'], python: 'py', markdown: 'md', changed: 'c', help: 'h', verbose: 'v' },
string: [...langRoots, ...langIgnoreRoots],
unknown: () => { help = true; } unknown: () => { help = true; }
}); });
if (help || opts.help) { if (help || opts.help) {
console.log('Usage: script/lint.js [--cc] [--js] [--py] [--md] [-c|--changed] [-h|--help] [-v|--verbose] [--fix] [--only -- file1 file2]'); const langFlags = langs.map(lang => `[--${lang}]`).join(' ');
console.log(`Usage: script/lint.js ${langFlags} [-c|--changed] [-h|--help] [-v|--verbose] [--fix] [--only -- file1 file2]`);
process.exit(0); process.exit(0);
} }
return opts; return opts;
} }
function populateLinterWithArgs (linter, opts) {
const extraRoots = opts[`${linter.key}-roots`];
if (extraRoots) {
linter.roots.push(...extraRoots.split(','));
}
const extraIgnoreRoots = opts[`${linter.key}-ignore-roots`];
if (extraIgnoreRoots) {
const list = extraIgnoreRoots.split(',');
if (linter.ignoreRoots) {
linter.ignoreRoots.push(...list);
} else {
linter.ignoreRoots = list;
}
}
}
async function findChangedFiles (top) { async function findChangedFiles (top) {
const result = await GitProcess.exec(['diff', '--name-only', '--cached'], top); const result = await GitProcess.exec(['diff', '--name-only', '--cached'], top);
if (result.exitCode !== 0) { if (result.exitCode !== 0) {
@ -432,13 +455,14 @@ async function main () {
const opts = parseCommandLine(); const opts = parseCommandLine();
// no mode specified? run 'em all // no mode specified? run 'em all
if (!opts['c++'] && !opts.javascript && !opts.objc && !opts.python && !opts.gn && !opts.patches && !opts.markdown) { if (!opts.cpp && !opts.javascript && !opts.objc && !opts.python && !opts.gn && !opts.patches && !opts.markdown) {
opts['c++'] = opts.javascript = opts.objc = opts.python = opts.gn = opts.patches = opts.markdown = true; opts.cpp = opts.javascript = opts.objc = opts.python = opts.gn = opts.patches = opts.markdown = true;
} }
const linters = LINTERS.filter(x => opts[x.key]); const linters = LINTERS.filter(x => opts[x.key]);
for (const linter of linters) { for (const linter of linters) {
populateLinterWithArgs(linter, opts);
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'}`); }