From c286d847d986d10dbb89e26c17c2fe153815796c Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 24 May 2021 06:26:00 +0800 Subject: [PATCH] Remove more `Array#reduce()` (#10907) --- scripts/build/rollup-plugins/externals.mjs | 13 +++++++------ scripts/draft-blog-post.mjs | 9 ++++----- tests/integration/__tests__/support-info.js | 18 +++++++++--------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/scripts/build/rollup-plugins/externals.mjs b/scripts/build/rollup-plugins/externals.mjs index 3bb6e4e48..5961f0be7 100644 --- a/scripts/build/rollup-plugins/externals.mjs +++ b/scripts/build/rollup-plugins/externals.mjs @@ -1,17 +1,18 @@ import path from "node:path"; export default function (modules = []) { - const requires = modules.reduce((obj, mod) => { - obj[mod] = path.basename(mod).replace(/\.js$/, ""); - return obj; - }, {}); + const requires = new Map( + modules.map((mod) => [mod, path.basename(mod).replace(/\.js$/, "")]) + ); return { name: "externals", load(importee) { - if (requires[importee]) { - return `module.exports = eval("require")("./${requires[importee]}");`; + if (requires.has(importee)) { + return `module.exports = eval("require")("./${requires.get( + importee + )}");`; } }, }; diff --git a/scripts/draft-blog-post.mjs b/scripts/draft-blog-post.mjs index 4e9b9d875..2bc1a389c 100644 --- a/scripts/draft-blog-post.mjs +++ b/scripts/draft-blog-post.mjs @@ -46,10 +46,9 @@ const categories = [ { dir: "cli", title: "CLI" }, ]; -const categoriesByDir = categories.reduce((result, category) => { - result[category.dir] = category; - return result; -}, {}); +const categoriesByDir = new Map( + categories.map((category) => [category.dir, category]) +); const dirs = fs .readdirSync(changelogUnreleasedDir, { withFileTypes: true }) @@ -57,7 +56,7 @@ const dirs = fs for (const dir of dirs) { const dirPath = path.join(changelogUnreleasedDir, dir.name); - const category = categoriesByDir[dir.name]; + const category = categoriesByDir.get(dir.name); if (!category) { throw new Error("Unknown category: " + dir.name); diff --git a/tests/integration/__tests__/support-info.js b/tests/integration/__tests__/support-info.js index 9b211902f..3886c1f33 100644 --- a/tests/integration/__tests__/support-info.js +++ b/tests/integration/__tests__/support-info.js @@ -13,13 +13,14 @@ describe("CLI --support-info", () => { function getCoreInfo() { const supportInfo = prettier.getSupportInfo(); - const languages = supportInfo.languages.reduce( - (obj, language) => ({ [language.name]: language.parsers, ...obj }), - {} + const languages = Object.fromEntries( + supportInfo.languages.map(({ name, parsers }) => [name, parsers]) ); - const options = supportInfo.options.reduce( - (obj, option) => ({ - [option.name]: { + + const options = Object.fromEntries( + supportInfo.options.map((option) => [ + option.name, + { type: option.type, default: option.default, ...(option.type === "int" @@ -28,9 +29,8 @@ function getCoreInfo() { ? { choices: option.choices.map((choice) => choice.value) } : null), }, - ...obj, - }), - {} + ]) ); + return { languages, options }; }