Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(util): remove duplicate mapDoc #4310

Merged
merged 1 commit into from
Apr 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/common/util-shared.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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,
Expand Down
15 changes: 0 additions & 15 deletions src/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -795,7 +781,6 @@ module.exports = {
punctuationCharRange,
getStringWidth,
splitText,
mapDoc,
getMaxContinuousCount,
getPrecedence,
shouldFlatten,
Expand Down
21 changes: 9 additions & 12 deletions src/doc/doc-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/language-js/embed.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

const util = require("../common/util");
const doc = require("../doc");
const docUtils = doc.utils;
const docBuilders = doc.builders;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/language-markdown/embed.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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")
Expand Down
3 changes: 2 additions & 1 deletion src/language-markdown/printer-markdown.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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;
}
Expand Down