Reduce use of Array#reduce() (#10863)

Co-authored-by: Georgii Dolzhykov <thorn.mailbox@gmail.com>
This commit is contained in:
fisker CheungandGeorgii Dolzhykov authored and GitHub committed 2021-05-16 12:43:13 +08:00
1 parent 90ea7e4a4f
commit 2e1021ca79
4 files changed
+33 -38

No files matched your search

+1 -3
View File
@@ -73,9 +73,7 @@ function createChoiceUsages(choices, margin, indentation) {
(choice) => !choice.deprecated && choice.since !== null (choice) => !choice.deprecated && choice.since !== null
); );
const threshold = const threshold =
activeChoices Math.max(0, ...activeChoices.map((choice) => choice.value.length)) + margin;
.map((choice) => choice.value.length)
.reduce((current, length) => Math.max(current, length), 0) + margin;
return activeChoices.map((choice) => return activeChoices.map((choice) =>
indent( indent(
createOptionUsageRow(choice.value, choice.description, threshold), createOptionUsageRow(choice.value, choice.description, threshold),
+19 -26
View File
@@ -303,8 +303,8 @@ function extractInterpolation(ast, options) {
* - add `isWhitespaceSensitive`, `isIndentationSensitive` field for text nodes * - add `isWhitespaceSensitive`, `isIndentationSensitive` field for text nodes
* - remove insensitive whitespaces * - remove insensitive whitespaces
*/ */
const WHITESPACE_NODE = { type: "whitespace" };
function extractWhitespaces(ast /*, options*/) { function extractWhitespaces(ast /*, options*/) {
const TYPE_WHITESPACE = "whitespace";
return ast.map((node) => { return ast.map((node) => {
if (!node.children) { if (!node.children) {
return node; return node;
@@ -330,9 +330,9 @@ function extractWhitespaces(ast /*, options*/) {
isIndentationSensitive, isIndentationSensitive,
children: node.children children: node.children
// extract whitespace nodes // extract whitespace nodes
.reduce((newChildren, child) => { .flatMap((child) => {
if (child.type !== "text" || isWhitespaceSensitive) { if (child.type !== "text" || isWhitespaceSensitive) {
return [...newChildren, child]; return child;
} }
const localChildren = []; const localChildren = [];
@@ -341,7 +341,7 @@ function extractWhitespaces(ast /*, options*/) {
getLeadingAndTrailingHtmlWhitespace(child.value); getLeadingAndTrailingHtmlWhitespace(child.value);
if (leadingWhitespace) { if (leadingWhitespace) {
localChildren.push({ type: TYPE_WHITESPACE }); localChildren.push(WHITESPACE_NODE);
} }
if (text) { if (text) {
@@ -356,32 +356,25 @@ function extractWhitespaces(ast /*, options*/) {
} }
if (trailingWhitespace) { if (trailingWhitespace) {
localChildren.push({ type: TYPE_WHITESPACE }); localChildren.push(WHITESPACE_NODE);
} }
return [...newChildren, ...localChildren]; return localChildren;
}, []) })
// set hasLeadingSpaces/hasTrailingSpaces and filter whitespace nodes // set hasLeadingSpaces/hasTrailingSpaces
.reduce((newChildren, child, i, children) => { .map((child, index, children) => {
if (child.type === TYPE_WHITESPACE) { if (child === WHITESPACE_NODE) {
return newChildren; return;
} }
const hasLeadingSpaces = return {
i !== 0 && children[i - 1].type === TYPE_WHITESPACE; ...child,
const hasTrailingSpaces = hasLeadingSpaces: children[index - 1] === WHITESPACE_NODE,
i !== children.length - 1 && hasTrailingSpaces: children[index + 1] === WHITESPACE_NODE,
children[i + 1].type === TYPE_WHITESPACE; };
})
return [ // filter whitespace nodes
...newChildren, .filter(Boolean),
{
...child,
hasLeadingSpaces,
hasTrailingSpaces,
},
];
}, []),
}); });
}); });
} }
+12 -8
View File
@@ -69,15 +69,19 @@ function parse(text, opts) {
// Create a new object {parserName: parseFn}. Uses defineProperty() to only call // Create a new object {parserName: parseFn}. Uses defineProperty() to only call
// the parsers getters when actually calling the parser `parse` function. // the parsers getters when actually calling the parser `parse` function.
const parsersForCustomParserApi = Object.keys(parsers).reduce( const parsersForCustomParserApi = Object.defineProperties(
(object, parserName) => {},
Object.defineProperty(object, parserName, { Object.fromEntries(
enumerable: true, Object.keys(parsers).map((parserName) => [
get() { parserName,
return parsers[parserName].parse; {
enumerable: true,
get() {
return parsers[parserName].parse;
},
}, },
}), ])
{} )
); );
const parser = resolveParser(opts, parsers); const parser = resolveParser(opts, parsers);
+1 -1
View File
@@ -30,7 +30,7 @@ function getSupportInfo({
const version = currentVersion.split("-", 1)[0]; const version = currentVersion.split("-", 1)[0];
const languages = plugins const languages = plugins
.reduce((all, plugin) => [...all, ...(plugin.languages || [])], []) .flatMap((plugin) => plugin.languages || [])
.filter(filterSince); .filter(filterSince);
const options = arrayify( const options = arrayify(