refactor: remove duplicate mapDoc (#4310)
parent
5d7b14856b
commit
1f84880fb1
|
@ -1,6 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
const util = require("./util");
|
||||
const docUtils = require("../doc/doc-utils");
|
||||
|
||||
function isNextLineEmpty(text, node, options) {
|
||||
return util.isNextLineEmpty(text, node, options.locEnd);
|
||||
|
@ -18,7 +19,7 @@ module.exports = {
|
|||
isNextLineEmpty,
|
||||
isNextLineEmptyAfterIndex: util.isNextLineEmptyAfterIndex,
|
||||
getNextNonSpaceNonCommentCharacterIndex,
|
||||
mapDoc: util.mapDoc,
|
||||
mapDoc: docUtils.mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils
|
||||
makeString: util.makeString,
|
||||
addLeadingComment: util.addLeadingComment,
|
||||
addDanglingComment: util.addDanglingComment,
|
||||
|
|
|
@ -600,20 +600,6 @@ function getMaxContinuousCount(str, target) {
|
|||
);
|
||||
}
|
||||
|
||||
function mapDoc(doc, callback) {
|
||||
if (doc.parts) {
|
||||
const parts = doc.parts.map(part => mapDoc(part, callback));
|
||||
return callback(Object.assign({}, doc, { parts }));
|
||||
}
|
||||
|
||||
if (doc.contents) {
|
||||
const contents = mapDoc(doc.contents, callback);
|
||||
return callback(Object.assign({}, doc, { contents }));
|
||||
}
|
||||
|
||||
return callback(doc);
|
||||
}
|
||||
|
||||
/**
|
||||
* split text into whitespaces and words
|
||||
* @param {string} text
|
||||
|
@ -795,7 +781,6 @@ module.exports = {
|
|||
punctuationCharRange,
|
||||
getStringWidth,
|
||||
splitText,
|
||||
mapDoc,
|
||||
getMaxContinuousCount,
|
||||
getPrecedence,
|
||||
shouldFlatten,
|
||||
|
|
|
@ -40,22 +40,19 @@ function traverseDoc(doc, onEnter, onExit, shouldTraverseConditionalGroups) {
|
|||
traverseDocRec(doc);
|
||||
}
|
||||
|
||||
function mapDoc(doc, func) {
|
||||
doc = func(doc);
|
||||
|
||||
function mapDoc(doc, cb) {
|
||||
if (doc.type === "concat" || doc.type === "fill") {
|
||||
return Object.assign({}, doc, {
|
||||
parts: doc.parts.map(d => mapDoc(d, func))
|
||||
});
|
||||
const parts = doc.parts.map(part => mapDoc(part, cb));
|
||||
return cb(Object.assign({}, doc, { parts }));
|
||||
} else if (doc.type === "if-break") {
|
||||
return Object.assign({}, doc, {
|
||||
breakContents: doc.breakContents && mapDoc(doc.breakContents, func),
|
||||
flatContents: doc.flatContents && mapDoc(doc.flatContents, func)
|
||||
});
|
||||
const breakContents = doc.breakContents && mapDoc(doc.breakContents, cb);
|
||||
const flatContents = doc.flatContents && mapDoc(doc.flatContents, cb);
|
||||
return cb(Object.assign({}, doc, { breakContents, flatContents }));
|
||||
} else if (doc.contents) {
|
||||
return Object.assign({}, doc, { contents: mapDoc(doc.contents, func) });
|
||||
const contents = mapDoc(doc.contents, cb);
|
||||
return cb(Object.assign({}, doc, { contents }));
|
||||
}
|
||||
return doc;
|
||||
return cb(doc);
|
||||
}
|
||||
|
||||
function findInDoc(doc, fn, defaultValue) {
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const util = require("../common/util");
|
||||
const doc = require("../doc");
|
||||
const docUtils = doc.utils;
|
||||
const docBuilders = doc.builders;
|
||||
|
@ -199,7 +198,7 @@ function getIndentation(str) {
|
|||
}
|
||||
|
||||
function escapeBackticks(doc) {
|
||||
return util.mapDoc(doc, currentDoc => {
|
||||
return docUtils.mapDoc(doc, currentDoc => {
|
||||
if (!currentDoc.parts) {
|
||||
return currentDoc;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
|
||||
const docUtils = require("../doc/doc-utils");
|
||||
const util = require("../common/util");
|
||||
const support = require("../common/support");
|
||||
const doc = require("../doc");
|
||||
|
@ -55,7 +56,7 @@ function embed(path, print, textToDoc, options) {
|
|||
}
|
||||
|
||||
function replaceNewlinesWithLiterallines(doc) {
|
||||
return util.mapDoc(
|
||||
return docUtils.mapDoc(
|
||||
doc,
|
||||
currentDoc =>
|
||||
typeof currentDoc === "string" && currentDoc.includes("\n")
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"use strict";
|
||||
|
||||
const docUtils = require("../doc/doc-utils");
|
||||
const privateUtil = require("../common/util");
|
||||
const embed = require("./embed");
|
||||
const pragma = require("./pragma");
|
||||
|
@ -719,7 +720,7 @@ function shouldRemainTheSameContent(path) {
|
|||
}
|
||||
|
||||
function normalizeDoc(doc) {
|
||||
return privateUtil.mapDoc(doc, currentDoc => {
|
||||
return docUtils.mapDoc(doc, currentDoc => {
|
||||
if (!currentDoc.parts) {
|
||||
return currentDoc;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue