diff --git a/src/document/doc-debug.js b/src/document/doc-debug.js index 21400e0b0..6d7050929 100644 --- a/src/document/doc-debug.js +++ b/src/document/doc-debug.js @@ -3,6 +3,10 @@ const { isConcat, getDocParts } = require("./doc-utils"); function flattenDoc(doc) { + if (!doc) { + return ""; + } + if (isConcat(doc)) { const res = []; for (const part of getDocParts(doc)) { @@ -22,8 +26,8 @@ function flattenDoc(doc) { if (doc.type === "if-break") { return { ...doc, - breakContents: doc.breakContents ? flattenDoc(doc.breakContents) : null, - flatContents: doc.flatContents ? flattenDoc(doc.flatContents) : null, + breakContents: flattenDoc(doc.breakContents), + flatContents: flattenDoc(doc.flatContents), }; } @@ -31,9 +35,7 @@ function flattenDoc(doc) { return { ...doc, contents: flattenDoc(doc.contents), - expandedStates: doc.expandedStates - ? doc.expandedStates.map(flattenDoc) - : doc.expandedStates, + expandedStates: doc.expandedStates && doc.expandedStates.map(flattenDoc), }; } diff --git a/tests_integration/__tests__/debug-api.js b/tests_integration/__tests__/debug-api.js index 776f8e070..3cdcab017 100644 --- a/tests_integration/__tests__/debug-api.js +++ b/tests_integration/__tests__/debug-api.js @@ -63,6 +63,7 @@ describe("API", () => { hardline, literalline, fill, + ifBreak, indentIfBreak, group, line, @@ -91,5 +92,9 @@ describe("API", () => { expect(formatDoc(label("foo", group(["1", line, "2"])))).toBe( 'label("foo", group(["1", line, "2"]))' ); + + expect(formatDoc([ifBreak("a", "b"), ifBreak("a"), ifBreak("", "b")])).toBe( + '[ifBreak("a", "b"), ifBreak("a"), ifBreak("", "b")]' + ); }); });