From b4d1053bb42acde9218f0f47a1dca5690b01fc5d Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Fri, 5 Jun 2020 18:47:24 +0900 Subject: [PATCH] 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 --- src/document/doc-utils.js | 13 ++++++ src/language-markdown/printer-markdown.js | 42 +------------------- tests_integration/__tests__/normalize-doc.js | 42 ++++++++++++++++++++ 3 files changed, 56 insertions(+), 41 deletions(-) create mode 100644 tests_integration/__tests__/normalize-doc.js diff --git a/src/document/doc-utils.js b/src/document/doc-utils.js index bf939b416..dfece63b6 100644 --- a/src/document/doc-utils.js +++ b/src/document/doc-utils.js @@ -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, }; diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index 7a3b07b9d..9cc69d2ac 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -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; } diff --git a/tests_integration/__tests__/normalize-doc.js b/tests_integration/__tests__/normalize-doc.js new file mode 100644 index 000000000..e4b362733 --- /dev/null +++ b/tests_integration/__tests__/normalize-doc.js @@ -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); + }); +});