MCPcopy Create free account
hub / github.com/prettier/prettier / printDoc

Function printDoc

src/document/debug.js:72–202  ·  view source on GitHub ↗
(doc, index, parentParts)

Source from the content-addressed store, hash-verified

70 return printDoc(flattenDoc(doc));
71
72 function printDoc(doc, index, parentParts) {
73 if (typeof doc === "string") {
74 return JSON.stringify(doc);
75 }
76
77 if (Array.isArray(doc)) {
78 const printed = doc.map(printDoc).filter(Boolean);
79 return printed.length === 1 ? printed[0] : `[${printed.join(", ")}]`;
80 }
81
82 if (doc.type === DOC_TYPE_LINE) {
83 const withBreakParent =
84 parentParts?.[index + 1]?.type === DOC_TYPE_BREAK_PARENT;
85 if (doc.literal) {
86 return withBreakParent
87 ? "literalline"
88 : "literallineWithoutBreakParent";
89 }
90 if (doc.hard) {
91 return withBreakParent ? "hardline" : "hardlineWithoutBreakParent";
92 }
93 if (doc.soft) {
94 return "softline";
95 }
96 return "line";
97 }
98
99 if (doc.type === DOC_TYPE_BREAK_PARENT) {
100 const afterHardline =
101 parentParts?.[index - 1]?.type === DOC_TYPE_LINE &&
102 parentParts[index - 1].hard;
103 return afterHardline ? undefined : "breakParent";
104 }
105
106 if (doc.type === DOC_TYPE_TRIM) {
107 return "trim";
108 }
109
110 if (doc.type === DOC_TYPE_INDENT) {
111 return "indent(" + printDoc(doc.contents) + ")";
112 }
113
114 if (doc.type === DOC_TYPE_ALIGN) {
115 return doc.n === Number.NEGATIVE_INFINITY
116 ? "dedentToRoot(" + printDoc(doc.contents) + ")"
117 : doc.n < 0
118 ? "dedent(" + printDoc(doc.contents) + ")"
119 : doc.n.type === "root"
120 ? "markAsRoot(" + printDoc(doc.contents) + ")"
121 : "align(" +
122 JSON.stringify(doc.n) +
123 ", " +
124 printDoc(doc.contents) +
125 ")";
126 }
127
128 if (doc.type === DOC_TYPE_IF_BREAK) {
129 return (

Callers 1

printDocToDebugFunction · 0.70

Calls 2

printGroupIdFunction · 0.85
mapMethod · 0.45

Tested by

no test coverage detected