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