Skip to content

Commit be13fe8

Browse files
authored
Use FlatESLint in run-rules-on-codebase script (#1914)
1 parent bc5dbd8 commit be13fe8

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

rules/no-unnecessary-await.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ function notPromise(node) {
2525
case 'Literal':
2626
case 'TemplateLiteral':
2727
case 'UnaryExpression':
28-
case 'UpdateExpression':
28+
case 'UpdateExpression': {
2929
return true;
30-
case 'SequenceExpression':
30+
}
31+
32+
case 'SequenceExpression': {
3133
return notPromise(node.expressions[node.expressions.length - 1]);
34+
}
35+
3236
// No default
3337
}
3438

test/run-rules-on-codebase/lint.mjs

+53-29
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/usr/bin/env node
22
import process from 'node:process';
33
import {parseArgs} from 'node:util';
4-
import {ESLint} from 'eslint';
5-
import unicorn from '../../index.js';
6-
import allConfig from '../../configs/all.js';
4+
// eslint-disable-next-line n/file-extension-in-import -- https://github.com/eslint-community/eslint-plugin-n/issues/50
5+
import eslintExperimentalApis from 'eslint/use-at-your-own-risk';
6+
import chalk from 'chalk';
7+
import {outdent} from 'outdent';
8+
import eslintPluginUnicorn from '../../index.js';
9+
10+
const {FlatESLint} = eslintExperimentalApis;
711

812
const {
913
values: {
@@ -19,20 +23,24 @@ const {
1923
allowPositionals: true,
2024
});
2125

22-
const eslint = new ESLint({
23-
baseConfig: allConfig,
24-
useEslintrc: false,
25-
extensions: ['.js', '.mjs'],
26-
plugins: {
27-
unicorn,
26+
const configs = [
27+
// TODO: Use `eslintPluginUnicorn.configs.all` instead when we change preset to flat config
28+
{
29+
plugins: {
30+
unicorn: eslintPluginUnicorn,
31+
},
32+
rules: eslintPluginUnicorn.configs.all.rules,
2833
},
29-
fix,
30-
overrideConfig: {
31-
ignorePatterns: [
34+
{
35+
ignores: [
3236
'coverage',
3337
'test/integration/fixtures',
3438
'test/integration/fixtures-local',
39+
// Ignore this file self temporarily, disabling `n/file-extension-in-import` comment cause error
40+
'test/run-rules-on-codebase/lint.mjs',
3541
],
42+
},
43+
{
3644
rules: {
3745
// https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255
3846
'unicorn/consistent-destructuring': 'off',
@@ -46,27 +54,31 @@ const eslint = new ESLint({
4654
'unicorn/prefer-string-replace-all': 'off',
4755
'unicorn/prefer-at': 'off',
4856
},
49-
overrides: [
50-
{
51-
files: [
52-
'**/*.js',
53-
],
54-
rules: {
55-
'unicorn/prefer-module': 'off',
56-
},
57-
},
57+
},
58+
{
59+
files: [
60+
'**/*.js',
5861
],
62+
rules: {
63+
'unicorn/prefer-module': 'off',
64+
},
5965
},
60-
});
66+
];
6167

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

6571
async function run() {
66-
const results = await eslint.lintFiles(patterns);
72+
const eslint = new FlatESLint({
73+
overrideConfigFile: true,
74+
overrideConfig: configs,
75+
fix,
76+
});
77+
78+
const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns);
6779

6880
if (fix) {
69-
await ESLint.outputFixes(results);
81+
await FlatESLint.outputFixes(results);
7082
}
7183

7284
const errorCount = sum(results, 'errorCount');
@@ -75,22 +87,34 @@ async function run() {
7587
const fixableWarningCount = sum(results, 'fixableWarningCount');
7688

7789
const hasFixable = fixableErrorCount || fixableWarningCount;
90+
const summary = outdent`
91+
${results.length} files linted:
92+
- error: ${chalk.gray(errorCount)}
93+
- warning: ${chalk.gray(warningCount)}
94+
- fixable error: ${chalk.gray(fixableErrorCount)}
95+
- fixable warning: ${chalk.gray(fixableWarningCount)}
96+
`;
7897

7998
if (errorCount || warningCount) {
99+
console.log('*! If you\'re making a new rule, you can ignore this before review. !*');
100+
101+
console.log();
102+
console.log(summary);
103+
80104
const {format} = await eslint.loadFormatter();
105+
console.log();
81106
console.log(format(results));
82107

83108
console.log();
84-
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.`);
109+
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.`);
85110

86111
if (hasFixable) {
87112
console.log();
88-
console.log('You may also want run `npm run run-rules-on-codebase <file> --fix` to fix fixable problems.');
113+
console.log('You may also want run `npm run run-rules-on-codebase -- <file> --fix` to fix fixable problems.');
89114
}
90-
91-
console.log();
92-
console.log('* If you\'re making a new rule, you can fix this later. *');
93115
} else {
116+
console.log(summary);
117+
console.log();
94118
console.log('All tests have passed.');
95119
}
96120

0 commit comments

Comments
 (0)