forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.mjs
124 lines (108 loc) · 3.21 KB
/
lint.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env node
import process from 'node:process';
import {parseArgs} from 'node:util';
// 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: {
fix = false,
},
positionals: patterns,
} = parseArgs({
options: {
fix: {
type: 'boolean',
},
},
allowPositionals: true,
});
const configs = [
// TODO: Use `eslintPluginUnicorn.configs.all` instead when we change preset to flat config
{
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: eslintPluginUnicorn.configs.all.rules,
},
{
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',
// Buggy
'unicorn/custom-error-definition': 'off',
'unicorn/consistent-function-scoping': 'off',
// Annoying
'unicorn/no-keyword-prefix': 'off',
'unicorn/no-unsafe-regex': 'off',
// Not ready yet
'unicorn/prefer-string-replace-all': 'off',
'unicorn/prefer-at': '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 -- <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.');
}
} else {
console.log(summary);
console.log();
console.log('All tests have passed.');
}
process.exit(errorCount);
}
await run();