Refactor: Use normalizeParts from document/utils in printer-markdown (#8496)
* Use normalizeParts from utils in printer-markdown * Remove flatting for concat from normalizeDoc * Fix undef parts * Move normalizeDoc to doc-utils * Add test for normalizeDoc * Modify tests to avoid specllcheck errors
This commit is contained in:
1 parent
6d42c08a14
commit
b4d1053bb4
3 files changed
+56
-41
No files matched your search
@@ -250,6 +250,18 @@ function normalizeParts(parts) {
|
||||
return newParts;
|
||||
}
|
||||
|
||||
function normalizeDoc(doc) {
|
||||
return mapDoc(doc, (currentDoc) => {
|
||||
if (!currentDoc.parts) {
|
||||
return currentDoc;
|
||||
}
|
||||
return {
|
||||
...currentDoc,
|
||||
parts: normalizeParts(currentDoc.parts),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isEmpty,
|
||||
willBreak,
|
||||
@@ -261,4 +273,5 @@ module.exports = {
|
||||
removeLines,
|
||||
stripTrailingHardline,
|
||||
normalizeParts,
|
||||
normalizeDoc,
|
||||
};
|
||||
@@ -20,7 +20,7 @@ const {
|
||||
indent,
|
||||
group,
|
||||
},
|
||||
utils: { mapDoc },
|
||||
utils: { normalizeDoc },
|
||||
printer: { printDocToString },
|
||||
} = require("../document");
|
||||
const {
|
||||
@@ -851,32 +851,6 @@ function shouldRemainTheSameContent(path) {
|
||||
);
|
||||
}
|
||||
|
||||
function normalizeDoc(doc) {
|
||||
return mapDoc(doc, (currentDoc) => {
|
||||
if (!currentDoc.parts) {
|
||||
return currentDoc;
|
||||
}
|
||||
|
||||
if (currentDoc.type === "concat" && currentDoc.parts.length === 1) {
|
||||
return currentDoc.parts[0];
|
||||
}
|
||||
|
||||
const parts = currentDoc.parts.reduce((parts, part) => {
|
||||
if (part.type === "concat") {
|
||||
parts.push(...part.parts);
|
||||
} else if (part !== "") {
|
||||
parts.push(part);
|
||||
}
|
||||
return parts;
|
||||
}, []);
|
||||
|
||||
return {
|
||||
...currentDoc,
|
||||
parts: normalizeParts(parts),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function printUrl(url, dangerousCharOrChars) {
|
||||
const dangerousChars = [" "].concat(dangerousCharOrChars || []);
|
||||
return new RegExp(dangerousChars.map((x) => `\\${x}`).join("|")).test(url)
|
||||
@@ -913,20 +887,6 @@ function printTitle(title, options, printSpace) {
|
||||
return `${quote}${title}${quote}`;
|
||||
}
|
||||
|
||||
function normalizeParts(parts) {
|
||||
return parts.reduce((current, part) => {
|
||||
const lastPart = privateUtil.getLast(current);
|
||||
|
||||
if (typeof lastPart === "string" && typeof part === "string") {
|
||||
current.splice(-1, 1, lastPart + part);
|
||||
} else {
|
||||
current.push(part);
|
||||
}
|
||||
|
||||
return current;
|
||||
}, []);
|
||||
}
|
||||
|
||||
function clamp(value, min, max) {
|
||||
return value < min ? min : value > max ? max : value;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
"use strict";
|
||||
|
||||
const prettier = require("prettier/local");
|
||||
const docBuilders = prettier.doc.builders;
|
||||
const docUtils = prettier.doc.utils;
|
||||
|
||||
const { normalizeDoc } = docUtils;
|
||||
const { group, concat, fill } = docBuilders;
|
||||
|
||||
describe("normalizeDoc", () => {
|
||||
test.each([
|
||||
[
|
||||
"removes empty strings",
|
||||
concat(["", "foo", fill(["", "bar", ""]), ""]),
|
||||
concat(["foo", fill(["bar"])]),
|
||||
],
|
||||
[
|
||||
"flattens nested concat",
|
||||
concat(["foo ", "", concat(["bar ", "", concat(["baz", ""])])]),
|
||||
concat(["foo bar baz"]),
|
||||
],
|
||||
[
|
||||
"flattens nested concat in other docs",
|
||||
group(concat(["foo ", concat(["bar ", "", concat(["baz", ""])])])),
|
||||
group(concat(["foo bar baz"])),
|
||||
],
|
||||
[
|
||||
"keeps groups",
|
||||
concat([group("foo"), group("bar"), group("baz")]),
|
||||
concat([group("foo"), group("bar"), group("baz")]),
|
||||
],
|
||||
[
|
||||
"keeps fills",
|
||||
fill(["foo", fill(["bar", fill(["baz"])])]),
|
||||
fill(["foo", fill(["bar", fill(["baz"])])]),
|
||||
],
|
||||
])("%s", (_, doc, expected) => {
|
||||
const result = normalizeDoc(doc);
|
||||
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user