Minor refactor to mapDoc (#13305)
This commit is contained in:
1 parent
88c178c17f
commit
49cd8be0b8
3 files changed
+26
-24
No files matched your search
+23
-18
@@ -83,6 +83,11 @@ function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
|
||||
}
|
||||
|
||||
function mapDoc(doc, cb) {
|
||||
// Avoid creating `Map`
|
||||
if (typeof doc === "string") {
|
||||
return cb(doc);
|
||||
}
|
||||
|
||||
// Within a doc tree, the same subtrees can be found multiple times.
|
||||
// E.g., often this happens in conditional groups.
|
||||
// As an optimization (those subtrees can be huge) and to maintain the
|
||||
@@ -102,25 +107,25 @@ function mapDoc(doc, cb) {
|
||||
}
|
||||
|
||||
function process(doc) {
|
||||
if (Array.isArray(doc)) {
|
||||
return cb(doc.map(rec));
|
||||
}
|
||||
switch (getDocType(doc)) {
|
||||
case DOC_TYPE_ARRAY:
|
||||
return cb(doc.map(rec));
|
||||
case DOC_TYPE_FILL:
|
||||
return cb({ ...doc, parts: doc.parts.map(rec) });
|
||||
case DOC_TYPE_IF_BREAK: {
|
||||
const breakContents = doc.breakContents && rec(doc.breakContents);
|
||||
const flatContents = doc.flatContents && rec(doc.flatContents);
|
||||
return cb({ ...doc, breakContents, flatContents });
|
||||
}
|
||||
case DOC_TYPE_GROUP: {
|
||||
if (doc.expandedStates) {
|
||||
const expandedStates = doc.expandedStates.map(rec);
|
||||
const contents = expandedStates[0];
|
||||
return cb({ ...doc, contents, expandedStates });
|
||||
}
|
||||
|
||||
if (doc.type === DOC_TYPE_ARRAY || doc.type === DOC_TYPE_FILL) {
|
||||
const parts = doc.parts.map(rec);
|
||||
return cb({ ...doc, parts });
|
||||
}
|
||||
|
||||
if (doc.type === DOC_TYPE_IF_BREAK) {
|
||||
const breakContents = doc.breakContents && rec(doc.breakContents);
|
||||
const flatContents = doc.flatContents && rec(doc.flatContents);
|
||||
return cb({ ...doc, breakContents, flatContents });
|
||||
}
|
||||
|
||||
if (doc.type === DOC_TYPE_GROUP && doc.expandedStates) {
|
||||
const expandedStates = doc.expandedStates.map(rec);
|
||||
const contents = expandedStates[0];
|
||||
return cb({ ...doc, contents, expandedStates });
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.contents) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* @typedef {import("../document/builders.js").Doc} Doc
|
||||
*/
|
||||
|
||||
import { fill, group, hardline, literalline } from "../document/builders.js";
|
||||
import { fill, group, hardline } from "../document/builders.js";
|
||||
import { cleanDoc, replaceTextEndOfLine } from "../document/utils.js";
|
||||
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
|
||||
import clean from "./clean.js";
|
||||
@@ -88,8 +88,7 @@ function genericPrint(path, options, print) {
|
||||
return [
|
||||
printOpeningTagPrefix(node, options),
|
||||
...replaceTextEndOfLine(
|
||||
options.originalText.slice(locStart(node), locEnd(node)),
|
||||
literalline
|
||||
options.originalText.slice(locStart(node), locEnd(node))
|
||||
),
|
||||
printClosingTagSuffix(node, options),
|
||||
];
|
||||
|
||||
@@ -8,7 +8,6 @@ import {
|
||||
join,
|
||||
line,
|
||||
lineSuffix,
|
||||
literalline,
|
||||
} from "../document/builders.js";
|
||||
import { replaceTextEndOfLine } from "../document/utils.js";
|
||||
import { isPreviousLineEmpty } from "../common/util.js";
|
||||
@@ -101,8 +100,7 @@ function genericPrint(path, options, print) {
|
||||
replaceTextEndOfLine(
|
||||
options.originalText
|
||||
.slice(node.position.start.offset, node.position.end.offset)
|
||||
.trimEnd(),
|
||||
literalline
|
||||
.trimEnd()
|
||||
)
|
||||
);
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user