Skip to content

Commit

Permalink
Enable no-return-await and require-await rule, fix clean-cspell
Browse files Browse the repository at this point in the history
… script (#11079)
  • Loading branch information
fisker committed Jun 16, 2021
1 parent 82687f1 commit 57616ad
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Expand Up @@ -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": [
Expand Down Expand Up @@ -85,6 +86,7 @@ module.exports = {
avoidEscape: true,
},
],
"require-await": "error",
strict: "error",
"symbol-description": "error",
yoda: [
Expand Down
38 changes: 30 additions & 8 deletions scripts/clean-cspell.mjs
Expand Up @@ -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
Expand All @@ -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);
});
2 changes: 1 addition & 1 deletion scripts/release/steps/validate-new-version.js
Expand Up @@ -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");
}
Expand Down
6 changes: 3 additions & 3 deletions src/cli/format.js
Expand Up @@ -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."
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/runPrettier.js
Expand Up @@ -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 });
});
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 57616ad

Please sign in to comment.