diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a941bb0134..c7513eb405 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,9 +10,9 @@ jobs: fail-fast: false matrix: node-version: + - 18 - 16 - 14 - - 12 os: - ubuntu-latest - windows-latest @@ -20,8 +20,8 @@ jobs: - os: ubuntu-latest node-version: 16 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }} - run: npm install @@ -29,10 +29,8 @@ jobs: lint-test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: 16 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 - run: npm install - run: npm run lint # Force update snapshots, https://github.com/avajs/ava/discussions/2754 @@ -40,20 +38,20 @@ jobs: env: AVA_FORCE_CI: not-ci - run: git diff --exit-code - - uses: codecov/codecov-action@v1 + - uses: codecov/codecov-action@v2 with: fail_ci_if_error: true run-rules-on-codebase: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 - run: npm install - run: npm run run-rules-on-codebase integration: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 - run: npm install - run: npm run integration diff --git a/.github/workflows/smoke-test.yml b/.github/workflows/smoke-test.yml index 5965492dc5..53adfdcddc 100644 --- a/.github/workflows/smoke-test.yml +++ b/.github/workflows/smoke-test.yml @@ -9,10 +9,8 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 16 + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 - run: | npm install npm link diff --git a/configs/recommended.js b/configs/recommended.js index 27dc9d1f22..8e97273e22 100644 --- a/configs/recommended.js +++ b/configs/recommended.js @@ -105,8 +105,7 @@ module.exports = { 'unicorn/prefer-string-trim-start-end': 'error', 'unicorn/prefer-switch': 'error', 'unicorn/prefer-ternary': 'error', - // TODO: Enable this by default when targeting Node.js 14. - 'unicorn/prefer-top-level-await': 'off', + 'unicorn/prefer-top-level-await': 'error', 'unicorn/prefer-type-error': 'error', 'unicorn/prevent-abbreviations': 'error', 'unicorn/relative-url-style': 'error', diff --git a/docs/rules/prefer-top-level-await.md b/docs/rules/prefer-top-level-await.md index f7a8bf2008..b5f625ead0 100644 --- a/docs/rules/prefer-top-level-await.md +++ b/docs/rules/prefer-top-level-await.md @@ -2,6 +2,8 @@ +✅ *This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.* + 💡 *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).* diff --git a/package.json b/package.json index e1026b0da7..a97a1ca8bc 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "url": "https://sindresorhus.com" }, "engines": { - "node": ">=12" + "node": ">=14" }, "scripts": { "create-rule": "node ./scripts/create-rule.mjs && npm run generate-rule-notices && npm run generate-rules-table", diff --git a/readme.md b/readme.md index 86ef7663ed..c7cf97b09a 100644 --- a/readme.md +++ b/readme.md @@ -144,7 +144,7 @@ Each rule has emojis denoting: | [prefer-string-trim-start-end](docs/rules/prefer-string-trim-start-end.md) | Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()`. | ✅ | 🔧 | | | [prefer-switch](docs/rules/prefer-switch.md) | Prefer `switch` over multiple `else-if`. | ✅ | 🔧 | | | [prefer-ternary](docs/rules/prefer-ternary.md) | Prefer ternary expressions over simple `if-else` statements. | ✅ | 🔧 | | -| [prefer-top-level-await](docs/rules/prefer-top-level-await.md) | Prefer top-level await over top-level promises and async function calls. | | | 💡 | +| [prefer-top-level-await](docs/rules/prefer-top-level-await.md) | Prefer top-level await over top-level promises and async function calls. | ✅ | | 💡 | | [prefer-type-error](docs/rules/prefer-type-error.md) | Enforce throwing `TypeError` in type checking conditions. | ✅ | 🔧 | | | [prevent-abbreviations](docs/rules/prevent-abbreviations.md) | Prevent abbreviations. | ✅ | 🔧 | | | [relative-url-style](docs/rules/relative-url-style.md) | Enforce consistent relative URL style. | ✅ | 🔧 | 💡 | diff --git a/scripts/rename-rule.mjs b/scripts/rename-rule.mjs index c8c521c59e..852cb16677 100644 --- a/scripts/rename-rule.mjs +++ b/scripts/rename-rule.mjs @@ -1,5 +1,4 @@ #!/usr/bin/env node -import process from 'node:process'; import fs, {promises as fsAsync} from 'node:fs'; import enquirer from 'enquirer'; import unicorn from '../index.js'; @@ -60,7 +59,7 @@ async function renameRule(from, to) { } } -(async () => { +const run = async () => { const originalRuleId = await new enquirer.AutoComplete({ message: 'Select the rule you want rename:', limit: 10, @@ -83,7 +82,6 @@ async function renameRule(from, to) { checkFiles(ruleId); renameRule(originalRuleId, ruleId); -})().catch(error => { - console.error(error); - process.exit(1); -}); +}; + +await run(); diff --git a/test/integration/test.mjs b/test/integration/test.mjs index 66b0cef786..e639347dc7 100644 --- a/test/integration/test.mjs +++ b/test/integration/test.mjs @@ -148,40 +148,40 @@ const list = new Listr([ renderer: isCI ? 'verbose' : 'default', }); -list.run() - .catch(async error => { - if (error.errors) { - for (const error2 of error.errors) { - console.error('\n', chalk.red.bold.underline(error2.packageName), chalk.gray('(' + error2.cliArgs.join(' ') + ')')); - console.error(error2.message); - - if (error2.stderr) { - console.error(chalk.gray(error2.stderr)); - } - - if (error2.eslintMessage) { - const {file, project, destination} = error2.eslintJob; - const {line} = error2.eslintMessage; +async function logError(error) { + if (error.errors) { + for (const error2 of error.errors) { + console.error('\n', chalk.red.bold.underline(error2.packageName), chalk.gray('(' + error2.cliArgs.join(' ') + ')')); + console.error(error2.message); + + if (error2.stderr) { + console.error(chalk.gray(error2.stderr)); + } - if (project.repository) { - // eslint-disable-next-line no-await-in-loop - const branch = await getBranch(destination); - console.error(chalk.gray(`${project.repository}/blob/${branch}/${path.relative(destination, file.filePath)}#L${line}`)); - } else { - console.error(chalk.gray(`${path.relative(destination, file.filePath)}#L${line}`)); - } + if (error2.eslintMessage) { + const {file, project, destination} = error2.eslintJob; + const {line} = error2.eslintMessage; - console.error(chalk.gray(JSON.stringify(error2.eslintMessage, undefined, 2))); + if (project.repository) { + // eslint-disable-next-line no-await-in-loop + const branch = await getBranch(destination); + console.error(chalk.gray(`${project.repository}/blob/${branch}/${path.relative(destination, file.filePath)}#L${line}`)); + } else { + console.error(chalk.gray(`${path.relative(destination, file.filePath)}#L${line}`)); } + + console.error(chalk.gray(JSON.stringify(error2.eslintMessage, undefined, 2))); } - } else { - console.error(error); } - - process.exit(1); - }) - // Catch errors in last `.catch` - .catch(error => { + } else { console.error(error); - process.exit(1); - }); + } + + process.exit(1); +} + +try { + await list.run(); +} catch (error) { + await logError(error); +} diff --git a/test/run-rules-on-codebase/lint.mjs b/test/run-rules-on-codebase/lint.mjs index 81aa37a28c..d533711107 100644 --- a/test/run-rules-on-codebase/lint.mjs +++ b/test/run-rules-on-codebase/lint.mjs @@ -31,7 +31,6 @@ const eslint = new ESLint({ 'unicorn/no-unsafe-regex': 'off', // Not ready yet 'unicorn/prefer-string-replace-all': 'off', - 'unicorn/prefer-top-level-await': 'off', 'unicorn/prefer-at': 'off', // TODO: Turn this on when `xo` updated `eslint-plugin-unicorn` 'unicorn/text-encoding-identifier-case': 'off', @@ -52,7 +51,7 @@ const eslint = new ESLint({ const sum = (collection, fieldName) => collection.reduce((total, {[fieldName]: value}) => total + value, 0); -(async function () { +async function run() { const results = await eslint.lintFiles(files); if (fix) { @@ -85,7 +84,6 @@ const sum = (collection, fieldName) => } process.exit(errorCount); -})().catch(error => { - process.exitCode = 1; - console.error(error); -}); +} + +await run();