From 0370f47cb376c8759e53a122ec231afa2df5e873 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 31 Jan 2020 08:13:37 +0800 Subject: [PATCH] Refactor some `Object.assign` (#7437) --- scripts/build/bundler.js | 10 +-- scripts/generate-schema.js | 5 +- src/cli/minimist.js | 19 ++--- src/cli/util.js | 104 +++++++++++------------- src/document/doc-builders.js | 5 +- src/language-html/constants.evaluate.js | 26 +++--- src/main/options-normalizer.js | 12 ++- src/main/options.js | 17 ++-- 8 files changed, 90 insertions(+), 108 deletions(-) diff --git a/scripts/build/bundler.js b/scripts/build/bundler.js index 6c25d37af..47c289772 100644 --- a/scripts/build/bundler.js +++ b/scripts/build/bundler.js @@ -133,12 +133,10 @@ function getRollupConfig(bundle) { extensions: [".js", ".json"], preferBuiltins: bundle.target === "node" }), - commonjs( - Object.assign( - bundle.target === "node" ? { ignoreGlobal: true } : {}, - bundle.commonjs - ) - ), + commonjs({ + ignoreGlobal: bundle.target === "node", + ...bundle.commonjs + }), externals(bundle.externals), bundle.target === "universal" && nodeGlobals(), babelConfig && babel(babelConfig), diff --git a/scripts/generate-schema.js b/scripts/generate-schema.js index 8d1884ee2..03a066bf3 100755 --- a/scripts/generate-schema.js +++ b/scripts/generate-schema.js @@ -1,6 +1,7 @@ #!/usr/bin/env node "use strict"; +const fromPairs = require("lodash/fromPairs"); if (require.main !== module) { module.exports = generateSchema; @@ -21,8 +22,8 @@ function generateSchema(options) { definitions: { optionsDefinition: { type: "object", - properties: Object.assign( - ...options.map(option => ({ [option.name]: optionToSchema(option) })) + properties: fromPairs( + options.map(option => [option.name, optionToSchema(option)]) ) }, overridesDefinition: { diff --git a/src/cli/minimist.js b/src/cli/minimist.js index f8a7d10a7..a66cad2d9 100644 --- a/src/cli/minimist.js +++ b/src/cli/minimist.js @@ -1,6 +1,7 @@ "use strict"; const minimist = require("minimist"); +const fromPairs = require("lodash/fromPairs"); const PLACEHOLDER = null; @@ -12,18 +13,14 @@ module.exports = function(args, options) { const defaults = options.default || {}; const booleanWithoutDefault = boolean.filter(key => !(key in defaults)); - const newDefaults = Object.assign( - {}, - defaults, - ...booleanWithoutDefault.map(key => ({ [key]: PLACEHOLDER })) - ); + const newDefaults = { + ...defaults, + ...fromPairs(booleanWithoutDefault.map(key => [key, PLACEHOLDER])) + }; const parsed = minimist(args, { ...options, default: newDefaults }); - return Object.keys(parsed).reduce((reduced, key) => { - if (parsed[key] !== PLACEHOLDER) { - reduced[key] = parsed[key]; - } - return reduced; - }, {}); + return fromPairs( + Object.entries(parsed).filter(([, value]) => value !== PLACEHOLDER) + ); }; diff --git a/src/cli/util.js b/src/cli/util.js index 926c78c15..865ec86c4 100644 --- a/src/cli/util.js +++ b/src/cli/util.js @@ -8,6 +8,8 @@ const globby = require("globby"); const chalk = require("chalk"); const readline = require("readline"); const stringify = require("json-stable-stringify"); +const fromPairs = require("lodash/fromPairs"); +const pick = require("lodash/pick"); const minimist = require("./minimist"); const prettier = require("../../index"); @@ -26,15 +28,11 @@ const CHOICE_USAGE_MARGIN = 3; const CHOICE_USAGE_INDENTATION = 2; function getOptions(argv, detailedOptions) { - return detailedOptions - .filter(option => option.forwardToApi) - .reduce( - (current, option) => - Object.assign(current, { - [option.forwardToApi]: argv[option.name] - }), - {} - ); + return fromPairs( + detailedOptions + .filter(({ forwardToApi }) => forwardToApi) + .map(({ forwardToApi, name }) => [forwardToApi, argv[name]]) + ); } function cliifyOptions(object, apiDetailedOptionMap) { @@ -451,9 +449,10 @@ function formatFiles(context) { return; } - const options = Object.assign(getOptionsForFile(context, filename), { + const options = { + ...getOptionsForFile(context, filename), filepath: filename - }); + }; if (isTTY()) { context.logger.log(filename, { newline: false }); @@ -747,15 +746,6 @@ function groupBy(array, getKey) { }, Object.create(null)); } -function pick(object, keys) { - return !keys - ? object - : keys.reduce( - (reduced, key) => Object.assign(reduced, { [key]: object[key] }), - {} - ); -} - function createLogger(logLevel) { return { warn: createLogFunc("warn", "yellow"), @@ -825,14 +815,11 @@ function normalizeDetailedOption(name, option) { } function normalizeDetailedOptionMap(detailedOptionMap) { - return Object.keys(detailedOptionMap) - .sort() - .reduce((normalized, name) => { - const option = detailedOptionMap[name]; - return Object.assign(normalized, { - [name]: normalizeDetailedOption(name, option) - }); - }, {}); + return fromPairs( + Object.entries(detailedOptionMap) + .sort(([leftName], [rightName]) => leftName.localeCompare(rightName)) + .map(([name, option]) => [name, normalizeDetailedOption(name, option)]) + ); } function createMinimistOptions(detailedOptions) { @@ -864,34 +851,36 @@ function createMinimistOptions(detailedOptions) { } function createApiDetailedOptionMap(detailedOptions) { - return detailedOptions.reduce( - (current, option) => - option.forwardToApi && option.forwardToApi !== option.name - ? Object.assign(current, { [option.forwardToApi]: option }) - : current, - {} + return fromPairs( + detailedOptions + .filter( + option => option.forwardToApi && option.forwardToApi !== option.name + ) + .map(option => [option.forwardToApi, option]) ); } function createDetailedOptionMap(supportOptions) { - return supportOptions.reduce((reduced, option) => { - const newOption = { - ...option, - name: option.cliName || dashify(option.name), - description: option.cliDescription || option.description, - category: option.cliCategory || coreOptions.CATEGORY_FORMAT, - forwardToApi: option.name - }; + return fromPairs( + supportOptions.map(option => { + const newOption = { + ...option, + name: option.cliName || dashify(option.name), + description: option.cliDescription || option.description, + category: option.cliCategory || coreOptions.CATEGORY_FORMAT, + forwardToApi: option.name + }; - if (option.deprecated) { - delete newOption.forwardToApi; - delete newOption.description; - delete newOption.oppositeDescription; - newOption.deprecated = true; - } + if (option.deprecated) { + delete newOption.forwardToApi; + delete newOption.description; + delete newOption.oppositeDescription; + newOption.deprecated = true; + } - return Object.assign(reduced, { [newOption.name]: newOption }); - }, {}); + return [newOption.name, newOption]; + }) + ); } //-----------------------------context-util-start------------------------------- @@ -944,13 +933,14 @@ function updateContextOptions(context, plugins, pluginSearchDirs) { const detailedOptions = arrayify(detailedOptionMap, "name"); - const apiDefaultOptions = supportOptions - .filter(optionInfo => !optionInfo.deprecated) - .reduce( - (reduced, optionInfo) => - Object.assign(reduced, { [optionInfo.name]: optionInfo.default }), - { ...optionsModule.hiddenDefaults } - ); + const apiDefaultOptions = { + ...optionsModule.hiddenDefaults, + ...fromPairs( + supportOptions + .filter(({ deprecated }) => !deprecated) + .map(option => [option.name, option.default]) + ) + }; context.supportOptions = supportOptions; context.detailedOptions = detailedOptions; diff --git a/src/document/doc-builders.js b/src/document/doc-builders.js index 110494063..ca814d601 100644 --- a/src/document/doc-builders.js +++ b/src/document/doc-builders.js @@ -120,10 +120,7 @@ function dedent(contents) { * @returns Doc */ function conditionalGroup(states, opts) { - return group( - states[0], - Object.assign(opts || {}, { expandedStates: states }) - ); + return group(states[0], { ...opts, expandedStates: states }); } /** diff --git a/src/language-html/constants.evaluate.js b/src/language-html/constants.evaluate.js index 8e0dce139..c6747e03e 100644 --- a/src/language-html/constants.evaluate.js +++ b/src/language-html/constants.evaluate.js @@ -1,21 +1,23 @@ "use strict"; const htmlStyles = require("html-styles"); +const fromPairs = require("lodash/fromPairs"); +const flat = require("lodash/flatten"); const getCssStyleTags = property => - htmlStyles - .filter(htmlStyle => htmlStyle.style[property]) - .map(htmlStyle => - htmlStyle.selectorText - .split(",") - .map(selector => selector.trim()) - .filter(selector => /^[a-zA-Z0-9]+$/.test(selector)) - .reduce((reduced, tagName) => { - reduced[tagName] = htmlStyle.style[property]; - return reduced; - }, {}) + fromPairs( + flat( + htmlStyles + .filter(htmlStyle => htmlStyle.style[property]) + .map(htmlStyle => + htmlStyle.selectorText + .split(",") + .map(selector => selector.trim()) + .filter(selector => /^[a-zA-Z0-9]+$/.test(selector)) + .map(tagName => [tagName, htmlStyle.style[property]]) + ) ) - .reduce((reduced, value) => Object.assign(reduced, value), {}); + ); const CSS_DISPLAY_TAGS = { ...getCssStyleTags("display"), diff --git a/src/main/options-normalizer.js b/src/main/options-normalizer.js index 007f35296..9ca816ad0 100644 --- a/src/main/options-normalizer.js +++ b/src/main/options-normalizer.js @@ -195,13 +195,11 @@ function optionInfoToSchema(optionInfo, { isCLI, optionInfos }) { } return optionInfo.array - ? vnopts.ArraySchema.create( - Object.assign( - isCLI ? { preprocess: v => [].concat(v) } : {}, - handlers, - { valueSchema: SchemaConstructor.create(parameters) } - ) - ) + ? vnopts.ArraySchema.create({ + ...(isCLI ? { preprocess: v => [].concat(v) } : {}), + ...handlers, + valueSchema: SchemaConstructor.create(parameters) + }) : SchemaConstructor.create({ ...parameters, ...handlers }); } diff --git a/src/main/options.js b/src/main/options.js index 3082317d4..f0b786848 100644 --- a/src/main/options.js +++ b/src/main/options.js @@ -3,6 +3,7 @@ const fs = require("fs"); const normalizePath = require("normalize-path"); const readlines = require("n-readlines"); +const fromPairs = require("lodash/fromPairs"); const { UndefinedParserError } = require("../common/errors"); const { getSupportInfo } = require("../main/support"); const normalizer = require("./options-normalizer"); @@ -27,16 +28,14 @@ function normalize(options, opts) { showUnreleased: true, showDeprecated: true }).options; - const defaults = Object.assign( - {}, - hiddenDefaults, - ...supportOptions.map(optionInfo => - optionInfo.default !== undefined - ? { [optionInfo.name]: optionInfo.default } - : undefined + const defaults = { + ...hiddenDefaults, + ...fromPairs( + supportOptions + .filter(optionInfo => optionInfo.default !== undefined) + .map(option => [option.name, option.default]) ) - ); - + }; if (!rawOptions.parser) { if (!rawOptions.filepath) { const logger = opts.logger || console;