Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow passing more roots to lint.js #40627

Merged
merged 1 commit into from Nov 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 31 additions & 7 deletions script/lint.js
Expand Up @@ -94,7 +94,7 @@ function isObjCHeader (filename) {
}

const LINTERS = [{
key: 'c++',
key: 'cpp',
roots: ['shell'],
test: filename => filename.endsWith('.cc') || (filename.endsWith('.h') && !isObjCHeader(filename)),
run: (opts, filenames) => {
Expand Down Expand Up @@ -135,7 +135,9 @@ const LINTERS = [{
cache: !process.env.CI,
cacheLocation: `node_modules/.eslintcache.${crypto.createHash('md5').update(fs.readFileSync(__filename)).digest('hex')}`,
extensions: ['.js', '.ts'],
fix: opts.fix
fix: opts.fix,
overrideConfigFile: path.join(ELECTRON_ROOT, '.eslintrc.json'),
resolvePluginsRelativeTo: ELECTRON_ROOT
});
const formatter = await eslint.loadFormatter();
let successCount = 0;
Expand Down Expand Up @@ -269,18 +271,39 @@ const LINTERS = [{

function parseCommandLine () {
let help;
const langs = ['cpp', 'objc', 'javascript', 'python', 'gn', 'patches'];
const langRoots = langs.map(lang => lang + '-roots');
const langIgnoreRoots = langs.map(lang => lang + '-ignore-roots');
const opts = minimist(process.argv.slice(2), {
boolean: ['c++', 'objc', 'javascript', 'python', 'gn', 'patches', 'help', 'changed', 'fix', 'verbose', 'only'],
alias: { 'c++': ['cc', 'cpp', 'cxx'], javascript: ['js', 'es'], python: 'py', changed: 'c', help: 'h', verbose: 'v' },
boolean: [...langs, 'help', 'changed', 'fix', 'verbose', 'only'],
alias: { cpp: ['c++', 'cc', 'cxx'], javascript: ['js', 'es'], python: 'py', changed: 'c', help: 'h', verbose: 'v' },
string: [...langRoots, ...langIgnoreRoots],
unknown: () => { help = true; }
});
if (help || opts.help) {
console.log('Usage: script/lint.js [--cc] [--js] [--py] [-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);
}
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) {
const result = await GitProcess.exec(['diff', '--name-only', '--cached'], top);
if (result.exitCode !== 0) {
Expand Down Expand Up @@ -338,13 +361,14 @@ async function main () {
const opts = parseCommandLine();

// no mode specified? run 'em all
if (!opts['c++'] && !opts.javascript && !opts.objc && !opts.python && !opts.gn && !opts.patches) {
opts['c++'] = opts.javascript = opts.objc = opts.python = opts.gn = opts.patches = true;
if (!opts.cpp && !opts.javascript && !opts.objc && !opts.python && !opts.gn && !opts.patches) {
opts.cpp = opts.javascript = opts.objc = opts.python = opts.gn = opts.patches = true;
}

const linters = LINTERS.filter(x => opts[x.key]);

for (const linter of linters) {
populateLinterWithArgs(linter, opts);
const filenames = await findFiles(opts, linter);
if (filenames.length) {
if (opts.verbose) { console.log(`linting ${filenames.length} ${linter.key} ${filenames.length === 1 ? 'file' : 'files'}`); }
Expand Down