From 57616ad865126cd21ac4196c6d0c5bd6e35a5c82 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Jun 2021 22:20:47 +0800 Subject: [PATCH] Enable `no-return-await` and `require-await` rule, fix `clean-cspell` script (#11079) --- .eslintrc.js | 2 + scripts/clean-cspell.mjs | 38 +++++++++++++++---- scripts/release/steps/validate-new-version.js | 2 +- src/cli/format.js | 6 +-- tests/integration/runPrettier.js | 2 + 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 44ceb9ec1e54..23dcbf961bba 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -38,6 +38,7 @@ module.exports = { // `!foo === bar` and `!foo !== bar` 'BinaryExpression[operator=/^[!=]==$/] > UnaryExpression.left[operator="!"]', ], + "no-return-await": "error", "no-unneeded-ternary": "error", "no-useless-return": "error", "no-unused-vars": [ @@ -85,6 +86,7 @@ module.exports = { avoidEscape: true, }, ], + "require-await": "error", strict: "error", "symbol-description": "error", yoda: [ diff --git a/scripts/clean-cspell.mjs b/scripts/clean-cspell.mjs index 39511389f93e..eaa4ea01ab2a 100644 --- a/scripts/clean-cspell.mjs +++ b/scripts/clean-cspell.mjs @@ -4,18 +4,19 @@ import fs from "node:fs/promises"; import execa from "execa"; const CSPELL_CONFIG_FILE = new URL("../cspell.json", import.meta.url); - -const updateConfig = async (config) => - await fs.writeFile(CSPELL_CONFIG_FILE, JSON.stringify(config, undefined, 4)); +const updateConfig = (config) => + fs.writeFile(CSPELL_CONFIG_FILE, JSON.stringify(config, undefined, 4)); +const runSpellcheck = () => execa("yarn", ["lint:spellcheck"]); (async () => { console.log("Empty words ..."); const config = JSON.parse(await fs.readFile(CSPELL_CONFIG_FILE, "utf8")); - updateConfig({ ...config, words: [] }); + const oldWords = config.words; + await updateConfig({ ...config, words: [] }); console.log("Running spellcheck with empty words ..."); try { - await execa("yarn lint:spellcheck"); + await runSpellcheck(); } catch ({ stdout }) { let words = [...stdout.matchAll(/ - Unknown word \((.*?)\)/g)].map( ([, word]) => word @@ -34,13 +35,34 @@ const updateConfig = async (config) => config.words = words; } + const newWords = config.words; + const removed = oldWords.filter((word) => !newWords.includes(word)); + if (removed.length > 0) { + console.log( + `${removed.length} words removed: \n${removed + .map((word) => ` - ${word}`) + .join("\n")}` + ); + } + const added = newWords.filter((word) => !oldWords.includes(word)); + if (added.length > 0) { + console.log( + `${added.length} words added: \n${added + .map((word) => ` - ${word}`) + .join("\n")}` + ); + } + console.log("Updating words ..."); - updateConfig(config); + await updateConfig(config); console.log("Running spellcheck with new words ..."); - const subprocess = execa("yarn lint:spellcheck"); + const subprocess = runSpellcheck(); subprocess.stdout.pipe(process.stdout); await subprocess; console.log("CSpell config file updated."); -})(); +})().catch((error) => { + console.error(error); + process.exit(1); +}); diff --git a/scripts/release/steps/validate-new-version.js b/scripts/release/steps/validate-new-version.js index 96de8c92ea92..fd3ab548f2e6 100644 --- a/scripts/release/steps/validate-new-version.js +++ b/scripts/release/steps/validate-new-version.js @@ -3,7 +3,7 @@ const chalk = require("chalk"); const semver = require("semver"); -module.exports = async function ({ version, previousVersion }) { +module.exports = function ({ version, previousVersion }) { if (!semver.valid(version)) { throw new Error("Invalid version specified"); } diff --git a/src/cli/format.js b/src/cli/format.js index 4eda1037c3bd..93dfc36502ba 100644 --- a/src/cli/format.js +++ b/src/cli/format.js @@ -105,7 +105,7 @@ function listDifferent(context, input, options, filename) { return true; } -async function format(context, input, opt) { +function format(context, input, opt) { if (!opt.parser && !opt.filepath) { throw new errors.UndefinedParserError( "No parser and no file path given, couldn't infer a parser." @@ -262,7 +262,7 @@ async function formatStdin(context) { return; } - writeOutput(context, await format(context, input, options), options); + writeOutput(context, format(context, input, options), options); } catch (error) { handleError(context, relativeFilepath || "stdin", error); } @@ -350,7 +350,7 @@ async function formatFiles(context) { let output; try { - result = await format(context, input, options); + result = format(context, input, options); output = result.formatted; } catch (error) { handleError(context, filename, error, printedFilename); diff --git a/tests/integration/runPrettier.js b/tests/integration/runPrettier.js index 6c510df52a28..3f0e0b62c18f 100644 --- a/tests/integration/runPrettier.js +++ b/tests/integration/runPrettier.js @@ -44,6 +44,7 @@ async function run(dir, args, options) { jest .spyOn(fs.promises, "writeFile") + // eslint-disable-next-line require-await .mockImplementation(async (filename, content) => { write.push({ filename, content }); }); @@ -83,6 +84,7 @@ async function run(dir, args, options) { // "get-stream" module to mock. jest .spyOn(require(thirdParty), "getStdin") + // eslint-disable-next-line require-await .mockImplementation(async () => options.input || ""); jest .spyOn(require(thirdParty), "isCI")