From f3ca6a08a825966f6b240e765e263a88445171b5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 17 Jul 2024 02:16:31 +0800 Subject: [PATCH] Switch `run-rules-on-codebase` script to use ESLint CLI directly (#2402) --- eslint.dogfooding.config.mjs | 47 +++++++++++ package.json | 2 +- test/run-rules-on-codebase/lint.mjs | 120 ---------------------------- 3 files changed, 48 insertions(+), 121 deletions(-) create mode 100644 eslint.dogfooding.config.mjs delete mode 100644 test/run-rules-on-codebase/lint.mjs diff --git a/eslint.dogfooding.config.mjs b/eslint.dogfooding.config.mjs new file mode 100644 index 0000000000..5973533ec2 --- /dev/null +++ b/eslint.dogfooding.config.mjs @@ -0,0 +1,47 @@ +/* Run all unicorn rules on codebase */ +/* +! If you're making a new rule, you can ignore this before review. +*/ + +import eslintPluginUnicorn from './index.js'; + +const config = [ + eslintPluginUnicorn.configs['flat/all'], + { + linterOptions: { + reportUnusedDisableDirectives: false, + }, + }, + { + ignores: [ + 'coverage', + 'test/integration/fixtures', + 'test/integration/fixtures-local', + 'rules/utils/lodash.js', + ], + }, + { + rules: { + // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255 + 'unicorn/consistent-destructuring': 'off', + // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2341 + 'unicorn/escape-case': 'off', + 'unicorn/no-hex-escape': 'off', + // Buggy + 'unicorn/custom-error-definition': 'off', + 'unicorn/consistent-function-scoping': 'off', + // Annoying + 'unicorn/no-keyword-prefix': 'off', + }, + }, + { + files: [ + '**/*.js', + ], + rules: { + 'unicorn/prefer-module': 'off', + }, + }, +]; + +export default config; diff --git a/package.json b/package.json index e04d29ff57..d02bc549c3 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "lint:js": "xo", "lint:markdown": "markdownlint \"**/*.md\"", "lint:package-json": "npmPkgJsonLint .", - "run-rules-on-codebase": "node ./test/run-rules-on-codebase/lint.mjs", + "run-rules-on-codebase": "eslint --config=./eslint.dogfooding.config.mjs", "smoke": "eslint-remote-tester --config ./test/smoke/eslint-remote-tester.config.mjs", "test": "npm-run-all --continue-on-error lint test:*", "test:js": "c8 ava" diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs deleted file mode 100644 index bf72648400..0000000000 --- a/test/run-rules-on-codebase/lint.mjs +++ /dev/null @@ -1,120 +0,0 @@ -#!/usr/bin/env node -import process from 'node:process'; -import {parseArgs} from 'node:util'; -import eslintExperimentalApis from 'eslint/use-at-your-own-risk'; -import chalk from 'chalk'; -import {outdent} from 'outdent'; -import eslintPluginUnicorn from '../../index.js'; - -const {FlatESLint} = eslintExperimentalApis; - -const { - values: { - fix = false, - }, - positionals: patterns, -} = parseArgs({ - options: { - fix: { - type: 'boolean', - }, - }, - allowPositionals: true, -}); - -const configs = [ - eslintPluginUnicorn.configs['flat/all'], - { - linterOptions: { - reportUnusedDisableDirectives: false, - }, - }, - { - ignores: [ - 'coverage', - 'test/integration/fixtures', - 'test/integration/fixtures-local', - 'rules/utils/lodash.js', - ], - }, - { - rules: { - // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255 - 'unicorn/consistent-destructuring': 'off', - // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/2341 - 'unicorn/escape-case': 'off', - 'unicorn/no-hex-escape': 'off', - // Buggy - 'unicorn/custom-error-definition': 'off', - 'unicorn/consistent-function-scoping': 'off', - // Annoying - 'unicorn/no-keyword-prefix': 'off', - }, - }, - { - files: [ - '**/*.js', - ], - rules: { - 'unicorn/prefer-module': 'off', - }, - }, -]; - -const sum = (collection, fieldName) => - collection.reduce((total, {[fieldName]: value}) => total + value, 0); - -async function run() { - const eslint = new FlatESLint({ - overrideConfigFile: true, - overrideConfig: configs, - fix, - }); - - const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); - - if (fix) { - await FlatESLint.outputFixes(results); - } - - const errorCount = sum(results, 'errorCount'); - const warningCount = sum(results, 'warningCount'); - const fixableErrorCount = sum(results, 'fixableErrorCount'); - const fixableWarningCount = sum(results, 'fixableWarningCount'); - - const hasFixable = fixableErrorCount || fixableWarningCount; - const summary = outdent` - ${results.length} files linted: - - error: ${chalk.gray(errorCount)} - - warning: ${chalk.gray(warningCount)} - - fixable error: ${chalk.gray(fixableErrorCount)} - - fixable warning: ${chalk.gray(fixableWarningCount)} - `; - - if (errorCount || warningCount) { - console.log('*! If you\'re making a new rule, you can ignore this before review. !*'); - - console.log(); - console.log(summary); - - const {format} = await eslint.loadFormatter(); - console.log(); - console.log(format(results)); - - console.log(); - console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run run-rules-on-codebase -- \` to check again.`); - - if (hasFixable) { - console.log(); - console.log('You may also want run `npm run run-rules-on-codebase -- --fix` to fix fixable problems.'); - } - } else { - console.log(summary); - console.log(); - console.log('All tests have passed.'); - } - - process.exit(errorCount); -} - -await run();