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