From 502a29a85e243be38cbdccd45d1309ba7501b277 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:04:21 +0800 Subject: [PATCH 01/10] Run `run-rules-on-codebase` with `FlatESLint` --- test/run-rules-on-codebase/lint.mjs | 100 ++++++++++++++++------------ 1 file changed, 58 insertions(+), 42 deletions(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 974363ef66..192ec8e048 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -1,37 +1,26 @@ #!/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'; +import eslintExperimentalApis from 'eslint/use-at-your-own-risk'; +import chalk from 'chalk'; +import {outdent} from 'outdent'; +import eslintPluginUnicorn from '../../index.js'; -const { - values: { - fix = false, - }, - positionals: patterns, -} = parseArgs({ - options: { - fix: { - type: 'boolean', - }, - }, - allowPositionals: true, -}); +const {FlatESLint} = eslintExperimentalApis; -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', + 'test/integration/fixtures/**', + 'test/integration/fixtures-local/**', ], rules: { // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255 @@ -46,24 +35,42 @@ 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 { + values: { + fix = false, + }, + positionals: patterns, +} = parseArgs({ + options: { + fix: { + type: 'boolean', + }, + }, + allowPositionals: true, }); 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); @@ -77,19 +84,28 @@ async function run() { const hasFixable = fixableErrorCount || fixableWarningCount; if (errorCount || warningCount) { + console.log('*! If you\'re making a new rule, you can ignore this before review. !*'); + + console.log(); + console.log(outdent` + ${results.length} files linted: + - error: ${chalk.gray(errorCount)} + - warning: ${chalk.gray(warningCount)} + - fixable error: ${chalk.gray(fixableErrorCount)} + - fixable warning: ${chalk.gray(fixableWarningCount)} + `); + 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.`); + 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.'); + console.log('You may also want run `npm run run-rules-on-codebase -- --fix` to fix fixable problems.'); } - - console.log(); - console.log('* If you\'re making a new rule, you can fix this later. *'); } else { console.log('All tests have passed.'); } From 2c53afc5f70fe35dc0802e7f11ec46ef4fc6ac82 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:06:44 +0800 Subject: [PATCH 02/10] Fix --- rules/no-unnecessary-await.js | 8 ++++++-- test/run-rules-on-codebase/lint.mjs | 19 +++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/rules/no-unnecessary-await.js b/rules/no-unnecessary-await.js index cf06620366..94e6789e34 100644 --- a/rules/no-unnecessary-await.js +++ b/rules/no-unnecessary-await.js @@ -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 } diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 192ec8e048..895c2d2bf0 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -73,7 +73,7 @@ async function run() { const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); if (fix) { - await ESLint.outputFixes(results); + await FlatESLint.outputFixes(results); } const errorCount = sum(results, 'errorCount'); @@ -82,18 +82,19 @@ 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(outdent` - ${results.length} files linted: - - error: ${chalk.gray(errorCount)} - - warning: ${chalk.gray(warningCount)} - - fixable error: ${chalk.gray(fixableErrorCount)} - - fixable warning: ${chalk.gray(fixableWarningCount)} - `); + console.log(summary); const {format} = await eslint.loadFormatter(); console.log(); @@ -107,6 +108,8 @@ async function run() { 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.'); } From 91ee4529dd25db3969fd19d414b76f3245bc0e44 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:10:00 +0800 Subject: [PATCH 03/10] Reduce diff --- test/run-rules-on-codebase/lint.mjs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 895c2d2bf0..f28c59ca9a 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -19,9 +19,11 @@ const configs = [ { ignores: [ 'coverage', - 'test/integration/fixtures/**', - 'test/integration/fixtures-local/**', + 'test/integration/fixtures', + 'test/integration/fixtures-local', ], + }, + { rules: { // https://github.com/sindresorhus/eslint-plugin-unicorn/issues/1109#issuecomment-782689255 'unicorn/consistent-destructuring': 'off', From 5e6b0cab4aa47b3d6b0d3a6435c9bf65709d942c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:10:45 +0800 Subject: [PATCH 04/10] Reduce diff --- test/run-rules-on-codebase/lint.mjs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index f28c59ca9a..5f53325251 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -8,6 +8,20 @@ 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 { @@ -48,20 +62,6 @@ const configs = [ }, ]; -const { - values: { - fix = false, - }, - positionals: patterns, -} = parseArgs({ - options: { - fix: { - type: 'boolean', - }, - }, - allowPositionals: true, -}); - const sum = (collection, fieldName) => collection.reduce((total, {[fieldName]: value}) => total + value, 0); From 651f9506b97be304cc61e0bb9dd2145f823c3065 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:38:42 +0800 Subject: [PATCH 05/10] Linting --- test/run-rules-on-codebase/lint.mjs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 5f53325251..45a31bf07c 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -1,6 +1,7 @@ #!/usr/bin/env node import process from 'node:process'; import {parseArgs} from 'node:util'; +// eslint-disable-next-line n/file-extension-in-import -- false positive import eslintExperimentalApis from 'eslint/use-at-your-own-risk'; import chalk from 'chalk'; import {outdent} from 'outdent'; From b73f0659522825bfef1615581b6f6013ece70a7e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:39:53 +0800 Subject: [PATCH 06/10] Add issue link --- test/run-rules-on-codebase/lint.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 45a31bf07c..d2f6298bc9 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import process from 'node:process'; import {parseArgs} from 'node:util'; -// eslint-disable-next-line n/file-extension-in-import -- false positive +// 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'; From 845b87fa101cc06e90b1638ffe5ab657204dccc3 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:48:18 +0800 Subject: [PATCH 07/10] Ignore unreleated errors --- test/run-rules-on-codebase/lint.mjs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index d2f6298bc9..42d6ea80c5 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -73,7 +73,10 @@ async function run() { fix, }); - const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); + let results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); + + // Ignore errors not caused by this plugin + results = results.map(file => file.messages.filter(message => message.ruleId && !message.ruleId.startsWith('unicorn/'))); if (fix) { await FlatESLint.outputFixes(results); From 4cb22225465dd1331c853652a988fe30ff6a4f06 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 14:51:10 +0800 Subject: [PATCH 08/10] Fix --- test/run-rules-on-codebase/lint.mjs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 42d6ea80c5..3adbf28f2a 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -76,7 +76,9 @@ async function run() { let results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); // Ignore errors not caused by this plugin - results = results.map(file => file.messages.filter(message => message.ruleId && !message.ruleId.startsWith('unicorn/'))); + for (const result of results) { + result.messages = result.messages.filter(message => !message.ruleId || message.ruleId.startsWith('unicorn/')) + } if (fix) { await FlatESLint.outputFixes(results); From b9c5dddb9361cdf9b7ffb29bf68568e321b971a5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 15:00:51 +0800 Subject: [PATCH 09/10] Fix --- test/run-rules-on-codebase/lint.mjs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 3adbf28f2a..56a12296d1 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -36,6 +36,8 @@ const configs = [ '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', ], }, { @@ -75,16 +77,11 @@ async function run() { let results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); - // Ignore errors not caused by this plugin - for (const result of results) { - result.messages = result.messages.filter(message => !message.ruleId || message.ruleId.startsWith('unicorn/')) - } - if (fix) { await FlatESLint.outputFixes(results); } - const errorCount = sum(results, 'errorCount'); + let errorCount = sum(results, 'errorCount'); const warningCount = sum(results, 'warningCount'); const fixableErrorCount = sum(results, 'fixableErrorCount'); const fixableWarningCount = sum(results, 'fixableWarningCount'); From 53873ae4da36a217e1d33bc3f8dfb77e0c300f40 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 21 Sep 2022 15:21:35 +0800 Subject: [PATCH 10/10] Linting --- test/run-rules-on-codebase/lint.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 56a12296d1..250db662d3 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -75,13 +75,13 @@ async function run() { fix, }); - let results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); + const results = await eslint.lintFiles(patterns.length === 0 ? ['.'] : patterns); if (fix) { await FlatESLint.outputFixes(results); } - let errorCount = sum(results, 'errorCount'); + const errorCount = sum(results, 'errorCount'); const warningCount = sum(results, 'warningCount'); const fixableErrorCount = sum(results, 'fixableErrorCount'); const fixableWarningCount = sum(results, 'fixableWarningCount');