* @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, )
| 51 | * @returns {boolean} |
| 52 | */ |
| 53 | function 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: |
no test coverage detected
searching dependent graphs…