Skip to content

Commit

Permalink
Use FlatESLint in run-rules-on-codebase script (#1914)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 21, 2022
1 parent bc5dbd8 commit be13fe8
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 31 deletions.
8 changes: 6 additions & 2 deletions rules/no-unnecessary-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ function notPromise(node) {
case 'Literal':
case 'TemplateLiteral':
case 'UnaryExpression':
case 'UpdateExpression':
case 'UpdateExpression': {
return true;
case 'SequenceExpression':
}

case 'SequenceExpression': {
return notPromise(node.expressions[node.expressions.length - 1]);
}

// No default
}

Expand Down
82 changes: 53 additions & 29 deletions test/run-rules-on-codebase/lint.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env node
import process from 'node:process';
import {parseArgs} from 'node:util';
import {ESLint} from 'eslint';
import unicorn from '../../index.js';
import allConfig from '../../configs/all.js';
// eslint-disable-next-line n/file-extension-in-import -- https://github.com/eslint-community/eslint-plugin-n/issues/50
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: {
Expand All @@ -19,20 +23,24 @@ const {
allowPositionals: true,
});

const eslint = new ESLint({
baseConfig: allConfig,
useEslintrc: false,
extensions: ['.js', '.mjs'],
plugins: {
unicorn,
const configs = [
// TODO: Use `eslintPluginUnicorn.configs.all` instead when we change preset to flat config
{
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: eslintPluginUnicorn.configs.all.rules,
},
fix,
overrideConfig: {
ignorePatterns: [
{
ignores: [
'coverage',
'test/integration/fixtures',
'test/integration/fixtures-local',
// Ignore this file self temporarily, disabling `n/file-extension-in-import` comment cause error
'test/run-rules-on-codebase/lint.mjs',
],
},
{
rules: {
// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255
'unicorn/consistent-destructuring': 'off',
Expand All @@ -46,27 +54,31 @@ const eslint = new ESLint({
'unicorn/prefer-string-replace-all': 'off',
'unicorn/prefer-at': 'off',
},
overrides: [
{
files: [
'**/*.js',
],
rules: {
'unicorn/prefer-module': 'off',
},
},
},
{
files: [
'**/*.js',
],
rules: {
'unicorn/prefer-module': 'off',
},
},
});
];

const sum = (collection, fieldName) =>
collection.reduce((total, {[fieldName]: value}) => total + value, 0);

async function run() {
const results = await eslint.lintFiles(patterns);
const eslint = new FlatESLint({
overrideConfigFile: true,
overrideConfig: configs,
fix,
});

const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns);

if (fix) {
await ESLint.outputFixes(results);
await FlatESLint.outputFixes(results);
}

const errorCount = sum(results, 'errorCount');
Expand All @@ -75,22 +87,34 @@ async function run() {
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 <file>\` to check again.`);
console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run run-rules-on-codebase -- <file>\` to check again.`);

if (hasFixable) {
console.log();
console.log('You may also want run `npm run run-rules-on-codebase <file> --fix` to fix fixable problems.');
console.log('You may also want run `npm run run-rules-on-codebase -- <file> --fix` to fix fixable problems.');
}

console.log();
console.log('* If you\'re making a new rule, you can fix this later. *');
} else {
console.log(summary);
console.log();
console.log('All tests have passed.');
}

Expand Down

0 comments on commit be13fe8

Please sign in to comment.