MCPcopy Index your code
hub / github.com/prettier/prettier / fits

Function fits

src/document/printer/printer.js:53–169  ·  view source on GitHub ↗

* @param {Command} next * @param {Command[]} restCommands * @param {number} remainingWidth * @param {boolean} hasLineSuffix * @param {GroupModeMap} groupModeMap * @param {boolean} [mustBeFlat] * @returns {boolean}

(
  next,
  restCommands,
  remainingWidth,
  hasLineSuffix,
  groupModeMap,
  mustBeFlat,
)

Source from the content-addressed store, hash-verified

51 * @returns {boolean}
52 */
53function fits(
54 next,
55 restCommands,
56 remainingWidth,
57 hasLineSuffix,
58 groupModeMap,
59 mustBeFlat,
60) {
61 if (remainingWidth === Number.POSITIVE_INFINITY) {
62 return true;
63 }
64
65 let restCommandsIndex = restCommands.length;
66 let hasPendingSpace = false;
67 /** @type {Array<Omit<Command, 'indent'>>} */
68 const commands = [next];
69 // `output` is only used for width counting because `trim` requires to look
70 // backwards for space characters.
71 let output = "";
72 while (remainingWidth >= 0) {
73 if (commands.length === 0) {
74 if (restCommandsIndex === 0) {
75 return true;
76 }
77 commands.push(restCommands[--restCommandsIndex]);
78
79 continue;
80 }
81
82 const { mode, doc } = commands.pop();
83 const docType = getDocType(doc);
84 switch (docType) {
85 case DOC_TYPE_STRING:
86 if (doc) {
87 if (hasPendingSpace) {
88 output += " ";
89 remainingWidth -= 1;
90 hasPendingSpace = false;
91 }
92
93 output += doc;
94 remainingWidth -= getStringWidth(doc);
95 }
96 break;
97
98 case DOC_TYPE_ARRAY:
99 case DOC_TYPE_FILL: {
100 const parts = docType === DOC_TYPE_ARRAY ? doc : doc.parts;
101 const end = doc[DOC_FILL_PRINTED_LENGTH] ?? 0;
102 for (let index = parts.length - 1; index >= end; index--) {
103 commands.push({ mode, doc: parts[index] });
104 }
105 break;
106 }
107
108 case DOC_TYPE_INDENT:
109 case DOC_TYPE_ALIGN:
110 case DOC_TYPE_INDENT_IF_BREAK:

Callers 2

printDocToStringFunction · 0.85
yFunction · 0.85

Calls 4

getDocTypeFunction · 0.90
trimIndentationFunction · 0.90
getStringWidthFunction · 0.85
atMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…