Refactor some Object.assign (#7437)
This commit is contained in:
1 parent
947a174954
commit
0370f47cb3
8 files changed
+90
-108
No files matched your search
@@ -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),
|
||||
|
||||
@@ -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: {
|
||||
|
||||
+8
-11
@@ -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)
|
||||
);
|
||||
};
|
||||
+47
-57
@@ -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;
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
|
||||
+8
-9
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user