@kurtinatlanta/prettier 3.8.0-dev → 3.8.0-hoyt.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/doc.js CHANGED
@@ -970,10 +970,62 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
970
970
  return { text: trimmed, count: length };
971
971
  }
972
972
 
973
+ // src/document/printer/print-result.js
974
+ var printResult = class {
975
+ /** @type {string[]} */
976
+ #settledTexts = [];
977
+ #unsettledText = "";
978
+ #settledTextLength = 0;
979
+ /** @type {number[]} */
980
+ #settledPositions = [];
981
+ /** @type {number[]} */
982
+ #unsettledPositions = [];
983
+ #settle() {
984
+ const text = this.#unsettledText;
985
+ if (text !== "") {
986
+ this.#settledTexts.push(text);
987
+ this.#settledTextLength += text.length;
988
+ this.#unsettledText = "";
989
+ }
990
+ for (const position of this.#unsettledPositions) {
991
+ this.#settledPositions.push(Math.min(position, this.#settledTextLength));
992
+ }
993
+ this.#unsettledPositions.length = 0;
994
+ }
995
+ markPosition() {
996
+ if (this.#settledPositions.length + this.#unsettledPositions.length >= 2) {
997
+ throw new Error("There are too many 'cursor' in doc.");
998
+ }
999
+ this.#unsettledPositions.push(
1000
+ this.#settledTextLength + this.#unsettledText.length
1001
+ );
1002
+ }
1003
+ /**
1004
+ @param {string} text
1005
+ */
1006
+ write(text) {
1007
+ this.#unsettledText += text;
1008
+ }
1009
+ trim() {
1010
+ const { text: trimmed, count } = trimIndentation(this.#unsettledText);
1011
+ this.#unsettledText = trimmed;
1012
+ this.#settle();
1013
+ return count;
1014
+ }
1015
+ finish() {
1016
+ this.#settle();
1017
+ return {
1018
+ text: this.#settledTexts.join(""),
1019
+ positions: this.#settledPositions
1020
+ };
1021
+ }
1022
+ };
1023
+ var print_result_default = printResult;
1024
+
973
1025
  // src/document/printer/printer.js
974
- var MODE_BREAK = Symbol("MODE_BREAK");
975
- var MODE_FLAT = Symbol("MODE_FLAT");
976
- var DOC_FILL_PRINTED_LENGTH = Symbol("DOC_FILL_PRINTED_LENGTH");
1026
+ var MODE_BREAK = /* @__PURE__ */ Symbol("MODE_BREAK");
1027
+ var MODE_FLAT = /* @__PURE__ */ Symbol("MODE_FLAT");
1028
+ var DOC_FILL_PRINTED_LENGTH = /* @__PURE__ */ Symbol("DOC_FILL_PRINTED_LENGTH");
977
1029
  function fits(next, restCommands, remainingWidth, hasLineSuffix, groupModeMap, mustBeFlat) {
978
1030
  if (remainingWidth === Number.POSITIVE_INFINITY) {
979
1031
  return true;
@@ -1095,13 +1147,9 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1095
1147
  mode: MODE_BREAK,
1096
1148
  doc
1097
1149
  }];
1098
- let output = "";
1099
1150
  let shouldRemeasure = false;
1100
1151
  const lineSuffix2 = [];
1101
- const cursorPositions = [];
1102
- const settledOutput = [];
1103
- const settledCursorPositions = [];
1104
- let settledTextLength = 0;
1152
+ const result = new print_result_default();
1105
1153
  propagateBreaks(doc);
1106
1154
  while (commands.length > 0) {
1107
1155
  const {
@@ -1119,7 +1167,7 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1119
1167
  newLine
1120
1168
  ) : doc2;
1121
1169
  if (formatted2) {
1122
- output += formatted2;
1170
+ result.write(formatted2);
1123
1171
  if (commands.length > 0) {
1124
1172
  position += get_string_width_default(formatted2);
1125
1173
  }
@@ -1136,10 +1184,7 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1136
1184
  }
1137
1185
  break;
1138
1186
  case DOC_TYPE_CURSOR:
1139
- if (cursorPositions.length >= 2) {
1140
- throw new Error("There are too many 'cursor' in doc.");
1141
- }
1142
- cursorPositions.push(settledTextLength + output.length);
1187
+ result.markPosition();
1143
1188
  break;
1144
1189
  case DOC_TYPE_INDENT:
1145
1190
  commands.push({
@@ -1156,88 +1201,64 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1156
1201
  });
1157
1202
  break;
1158
1203
  case DOC_TYPE_TRIM:
1159
- trim2();
1204
+ position -= result.trim();
1160
1205
  break;
1161
1206
  case DOC_TYPE_GROUP:
1162
- switch (mode) {
1163
- case MODE_FLAT:
1164
- if (!shouldRemeasure) {
1165
- commands.push({
1207
+ {
1208
+ const command = (function printGroup() {
1209
+ if (mode === MODE_FLAT && !shouldRemeasure) {
1210
+ return {
1166
1211
  indent: indent2,
1167
1212
  mode: doc2.break ? MODE_BREAK : MODE_FLAT,
1168
1213
  doc: doc2.contents
1169
- });
1170
- break;
1214
+ };
1171
1215
  }
1172
- // fallthrough
1173
- case MODE_BREAK: {
1174
1216
  shouldRemeasure = false;
1175
- const next = {
1217
+ const remainingWidth = width - position;
1218
+ const hasLineSuffix = lineSuffix2.length > 0;
1219
+ const flatCommand = {
1176
1220
  indent: indent2,
1177
1221
  mode: MODE_FLAT,
1178
1222
  doc: doc2.contents
1179
1223
  };
1180
- const remainingWidth = width - position;
1181
- const hasLineSuffix = lineSuffix2.length > 0;
1182
- if (!doc2.break && fits(next, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1183
- commands.push(next);
1184
- } else {
1185
- if (doc2.expandedStates) {
1186
- const mostExpanded = method_at_default(
1187
- /* OPTIONAL_OBJECT: false */
1188
- 0,
1189
- doc2.expandedStates,
1190
- -1
1191
- );
1192
- if (doc2.break) {
1193
- commands.push({
1194
- indent: indent2,
1195
- mode: MODE_BREAK,
1196
- doc: mostExpanded
1197
- });
1198
- break;
1199
- } else {
1200
- for (let index = 1; index < doc2.expandedStates.length + 1; index++) {
1201
- if (index >= doc2.expandedStates.length) {
1202
- commands.push({
1203
- indent: indent2,
1204
- mode: MODE_BREAK,
1205
- doc: mostExpanded
1206
- });
1207
- break;
1208
- } else {
1209
- const state = doc2.expandedStates[index];
1210
- const cmd = {
1211
- indent: indent2,
1212
- mode: MODE_FLAT,
1213
- doc: state
1214
- };
1215
- if (fits(cmd, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1216
- commands.push(cmd);
1217
- break;
1218
- }
1219
- }
1220
- }
1221
- }
1222
- } else {
1223
- commands.push({
1224
+ if (!doc2.break && fits(flatCommand, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1225
+ return flatCommand;
1226
+ }
1227
+ if (!doc2.expandedStates) {
1228
+ return {
1229
+ indent: indent2,
1230
+ mode: MODE_BREAK,
1231
+ doc: doc2.contents
1232
+ };
1233
+ }
1234
+ if (!doc2.break) {
1235
+ for (let index = 1; index < doc2.expandedStates.length - 1; index++) {
1236
+ const flatCommand2 = {
1224
1237
  indent: indent2,
1225
- mode: MODE_BREAK,
1226
- doc: doc2.contents
1227
- });
1238
+ mode: MODE_FLAT,
1239
+ doc: doc2.expandedStates[index]
1240
+ };
1241
+ if (fits(flatCommand2, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1242
+ return flatCommand2;
1243
+ }
1228
1244
  }
1229
1245
  }
1230
- break;
1246
+ return {
1247
+ indent: indent2,
1248
+ mode: MODE_BREAK,
1249
+ doc: method_at_default(
1250
+ /* OPTIONAL_OBJECT: false */
1251
+ 0,
1252
+ doc2.expandedStates,
1253
+ -1
1254
+ )
1255
+ };
1256
+ })();
1257
+ commands.push(command);
1258
+ if (doc2.id) {
1259
+ groupModeMap[doc2.id] = command.mode;
1231
1260
  }
1232
1261
  }
1233
- if (doc2.id) {
1234
- groupModeMap[doc2.id] = method_at_default(
1235
- /* OPTIONAL_OBJECT: false */
1236
- 0,
1237
- commands,
1238
- -1
1239
- ).mode;
1240
- }
1241
1262
  break;
1242
1263
  // Fills each line with as much code as possible before moving to a new
1243
1264
  // line with the same indentation.
@@ -1379,7 +1400,7 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1379
1400
  case MODE_FLAT:
1380
1401
  if (!doc2.hard) {
1381
1402
  if (!doc2.soft) {
1382
- output += " ";
1403
+ result.write(" ");
1383
1404
  position += 1;
1384
1405
  }
1385
1406
  break;
@@ -1398,17 +1419,17 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1398
1419
  break;
1399
1420
  }
1400
1421
  if (doc2.literal) {
1401
- output += newLine;
1422
+ result.write(newLine);
1402
1423
  position = 0;
1403
1424
  if (indent2.root) {
1404
1425
  if (indent2.root.value) {
1405
- output += indent2.root.value;
1426
+ result.write(indent2.root.value);
1406
1427
  }
1407
1428
  position = indent2.root.length;
1408
1429
  }
1409
1430
  } else {
1410
- trim2();
1411
- output += newLine + indent2.value;
1431
+ result.trim();
1432
+ result.write(newLine + indent2.value);
1412
1433
  position = indent2.length;
1413
1434
  }
1414
1435
  break;
@@ -1431,40 +1452,21 @@ Expected it to be ${EXPECTED_TYPE_VALUES}.`;
1431
1452
  lineSuffix2.length = 0;
1432
1453
  }
1433
1454
  }
1434
- const formatted = settledOutput.join("") + output;
1435
- const finalCursorPositions = [...settledCursorPositions, ...cursorPositions];
1436
- if (finalCursorPositions.length !== 2) {
1455
+ const {
1456
+ text: formatted,
1457
+ positions: cursorPositions
1458
+ } = result.finish();
1459
+ if (cursorPositions.length !== 2) {
1437
1460
  return {
1438
1461
  formatted
1439
1462
  };
1440
1463
  }
1441
- const cursorNodeStart = finalCursorPositions[0];
1464
+ const [cursorNodeStart, cursorNodeEnd] = cursorPositions;
1442
1465
  return {
1443
1466
  formatted,
1444
1467
  cursorNodeStart,
1445
- cursorNodeText: formatted.slice(cursorNodeStart, method_at_default(
1446
- /* OPTIONAL_OBJECT: false */
1447
- 0,
1448
- finalCursorPositions,
1449
- -1
1450
- ))
1468
+ cursorNodeText: formatted.slice(cursorNodeStart, cursorNodeEnd)
1451
1469
  };
1452
- function trim2() {
1453
- const {
1454
- text: trimmed,
1455
- count
1456
- } = trimIndentation(output);
1457
- if (trimmed) {
1458
- settledOutput.push(trimmed);
1459
- settledTextLength += trimmed.length;
1460
- }
1461
- output = "";
1462
- position -= count;
1463
- if (cursorPositions.length > 0) {
1464
- settledCursorPositions.push(...cursorPositions.map((position2) => Math.min(position2, settledTextLength)));
1465
- cursorPositions.length = 0;
1466
- }
1467
- }
1468
1470
  }
1469
1471
 
1470
1472
  // src/document/public.js
package/doc.mjs CHANGED
@@ -935,10 +935,62 @@ function trimIndentation(text) {
935
935
  return { text: trimmed, count: length };
936
936
  }
937
937
 
938
+ // src/document/printer/print-result.js
939
+ var printResult = class {
940
+ /** @type {string[]} */
941
+ #settledTexts = [];
942
+ #unsettledText = "";
943
+ #settledTextLength = 0;
944
+ /** @type {number[]} */
945
+ #settledPositions = [];
946
+ /** @type {number[]} */
947
+ #unsettledPositions = [];
948
+ #settle() {
949
+ const text = this.#unsettledText;
950
+ if (text !== "") {
951
+ this.#settledTexts.push(text);
952
+ this.#settledTextLength += text.length;
953
+ this.#unsettledText = "";
954
+ }
955
+ for (const position of this.#unsettledPositions) {
956
+ this.#settledPositions.push(Math.min(position, this.#settledTextLength));
957
+ }
958
+ this.#unsettledPositions.length = 0;
959
+ }
960
+ markPosition() {
961
+ if (this.#settledPositions.length + this.#unsettledPositions.length >= 2) {
962
+ throw new Error("There are too many 'cursor' in doc.");
963
+ }
964
+ this.#unsettledPositions.push(
965
+ this.#settledTextLength + this.#unsettledText.length
966
+ );
967
+ }
968
+ /**
969
+ @param {string} text
970
+ */
971
+ write(text) {
972
+ this.#unsettledText += text;
973
+ }
974
+ trim() {
975
+ const { text: trimmed, count } = trimIndentation(this.#unsettledText);
976
+ this.#unsettledText = trimmed;
977
+ this.#settle();
978
+ return count;
979
+ }
980
+ finish() {
981
+ this.#settle();
982
+ return {
983
+ text: this.#settledTexts.join(""),
984
+ positions: this.#settledPositions
985
+ };
986
+ }
987
+ };
988
+ var print_result_default = printResult;
989
+
938
990
  // src/document/printer/printer.js
939
- var MODE_BREAK = Symbol("MODE_BREAK");
940
- var MODE_FLAT = Symbol("MODE_FLAT");
941
- var DOC_FILL_PRINTED_LENGTH = Symbol("DOC_FILL_PRINTED_LENGTH");
991
+ var MODE_BREAK = /* @__PURE__ */ Symbol("MODE_BREAK");
992
+ var MODE_FLAT = /* @__PURE__ */ Symbol("MODE_FLAT");
993
+ var DOC_FILL_PRINTED_LENGTH = /* @__PURE__ */ Symbol("DOC_FILL_PRINTED_LENGTH");
942
994
  function fits(next, restCommands, remainingWidth, hasLineSuffix, groupModeMap, mustBeFlat) {
943
995
  if (remainingWidth === Number.POSITIVE_INFINITY) {
944
996
  return true;
@@ -1060,13 +1112,9 @@ function printDocToString(doc, options) {
1060
1112
  mode: MODE_BREAK,
1061
1113
  doc
1062
1114
  }];
1063
- let output = "";
1064
1115
  let shouldRemeasure = false;
1065
1116
  const lineSuffix2 = [];
1066
- const cursorPositions = [];
1067
- const settledOutput = [];
1068
- const settledCursorPositions = [];
1069
- let settledTextLength = 0;
1117
+ const result = new print_result_default();
1070
1118
  propagateBreaks(doc);
1071
1119
  while (commands.length > 0) {
1072
1120
  const {
@@ -1084,7 +1132,7 @@ function printDocToString(doc, options) {
1084
1132
  newLine
1085
1133
  ) : doc2;
1086
1134
  if (formatted2) {
1087
- output += formatted2;
1135
+ result.write(formatted2);
1088
1136
  if (commands.length > 0) {
1089
1137
  position += get_string_width_default(formatted2);
1090
1138
  }
@@ -1101,10 +1149,7 @@ function printDocToString(doc, options) {
1101
1149
  }
1102
1150
  break;
1103
1151
  case DOC_TYPE_CURSOR:
1104
- if (cursorPositions.length >= 2) {
1105
- throw new Error("There are too many 'cursor' in doc.");
1106
- }
1107
- cursorPositions.push(settledTextLength + output.length);
1152
+ result.markPosition();
1108
1153
  break;
1109
1154
  case DOC_TYPE_INDENT:
1110
1155
  commands.push({
@@ -1121,88 +1166,64 @@ function printDocToString(doc, options) {
1121
1166
  });
1122
1167
  break;
1123
1168
  case DOC_TYPE_TRIM:
1124
- trim2();
1169
+ position -= result.trim();
1125
1170
  break;
1126
1171
  case DOC_TYPE_GROUP:
1127
- switch (mode) {
1128
- case MODE_FLAT:
1129
- if (!shouldRemeasure) {
1130
- commands.push({
1172
+ {
1173
+ const command = (function printGroup() {
1174
+ if (mode === MODE_FLAT && !shouldRemeasure) {
1175
+ return {
1131
1176
  indent: indent2,
1132
1177
  mode: doc2.break ? MODE_BREAK : MODE_FLAT,
1133
1178
  doc: doc2.contents
1134
- });
1135
- break;
1179
+ };
1136
1180
  }
1137
- // fallthrough
1138
- case MODE_BREAK: {
1139
1181
  shouldRemeasure = false;
1140
- const next = {
1182
+ const remainingWidth = width - position;
1183
+ const hasLineSuffix = lineSuffix2.length > 0;
1184
+ const flatCommand = {
1141
1185
  indent: indent2,
1142
1186
  mode: MODE_FLAT,
1143
1187
  doc: doc2.contents
1144
1188
  };
1145
- const remainingWidth = width - position;
1146
- const hasLineSuffix = lineSuffix2.length > 0;
1147
- if (!doc2.break && fits(next, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1148
- commands.push(next);
1149
- } else {
1150
- if (doc2.expandedStates) {
1151
- const mostExpanded = method_at_default(
1152
- /* OPTIONAL_OBJECT: false */
1153
- 0,
1154
- doc2.expandedStates,
1155
- -1
1156
- );
1157
- if (doc2.break) {
1158
- commands.push({
1159
- indent: indent2,
1160
- mode: MODE_BREAK,
1161
- doc: mostExpanded
1162
- });
1163
- break;
1164
- } else {
1165
- for (let index = 1; index < doc2.expandedStates.length + 1; index++) {
1166
- if (index >= doc2.expandedStates.length) {
1167
- commands.push({
1168
- indent: indent2,
1169
- mode: MODE_BREAK,
1170
- doc: mostExpanded
1171
- });
1172
- break;
1173
- } else {
1174
- const state = doc2.expandedStates[index];
1175
- const cmd = {
1176
- indent: indent2,
1177
- mode: MODE_FLAT,
1178
- doc: state
1179
- };
1180
- if (fits(cmd, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1181
- commands.push(cmd);
1182
- break;
1183
- }
1184
- }
1185
- }
1186
- }
1187
- } else {
1188
- commands.push({
1189
+ if (!doc2.break && fits(flatCommand, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1190
+ return flatCommand;
1191
+ }
1192
+ if (!doc2.expandedStates) {
1193
+ return {
1194
+ indent: indent2,
1195
+ mode: MODE_BREAK,
1196
+ doc: doc2.contents
1197
+ };
1198
+ }
1199
+ if (!doc2.break) {
1200
+ for (let index = 1; index < doc2.expandedStates.length - 1; index++) {
1201
+ const flatCommand2 = {
1189
1202
  indent: indent2,
1190
- mode: MODE_BREAK,
1191
- doc: doc2.contents
1192
- });
1203
+ mode: MODE_FLAT,
1204
+ doc: doc2.expandedStates[index]
1205
+ };
1206
+ if (fits(flatCommand2, commands, remainingWidth, hasLineSuffix, groupModeMap)) {
1207
+ return flatCommand2;
1208
+ }
1193
1209
  }
1194
1210
  }
1195
- break;
1211
+ return {
1212
+ indent: indent2,
1213
+ mode: MODE_BREAK,
1214
+ doc: method_at_default(
1215
+ /* OPTIONAL_OBJECT: false */
1216
+ 0,
1217
+ doc2.expandedStates,
1218
+ -1
1219
+ )
1220
+ };
1221
+ })();
1222
+ commands.push(command);
1223
+ if (doc2.id) {
1224
+ groupModeMap[doc2.id] = command.mode;
1196
1225
  }
1197
1226
  }
1198
- if (doc2.id) {
1199
- groupModeMap[doc2.id] = method_at_default(
1200
- /* OPTIONAL_OBJECT: false */
1201
- 0,
1202
- commands,
1203
- -1
1204
- ).mode;
1205
- }
1206
1227
  break;
1207
1228
  // Fills each line with as much code as possible before moving to a new
1208
1229
  // line with the same indentation.
@@ -1344,7 +1365,7 @@ function printDocToString(doc, options) {
1344
1365
  case MODE_FLAT:
1345
1366
  if (!doc2.hard) {
1346
1367
  if (!doc2.soft) {
1347
- output += " ";
1368
+ result.write(" ");
1348
1369
  position += 1;
1349
1370
  }
1350
1371
  break;
@@ -1363,17 +1384,17 @@ function printDocToString(doc, options) {
1363
1384
  break;
1364
1385
  }
1365
1386
  if (doc2.literal) {
1366
- output += newLine;
1387
+ result.write(newLine);
1367
1388
  position = 0;
1368
1389
  if (indent2.root) {
1369
1390
  if (indent2.root.value) {
1370
- output += indent2.root.value;
1391
+ result.write(indent2.root.value);
1371
1392
  }
1372
1393
  position = indent2.root.length;
1373
1394
  }
1374
1395
  } else {
1375
- trim2();
1376
- output += newLine + indent2.value;
1396
+ result.trim();
1397
+ result.write(newLine + indent2.value);
1377
1398
  position = indent2.length;
1378
1399
  }
1379
1400
  break;
@@ -1396,40 +1417,21 @@ function printDocToString(doc, options) {
1396
1417
  lineSuffix2.length = 0;
1397
1418
  }
1398
1419
  }
1399
- const formatted = settledOutput.join("") + output;
1400
- const finalCursorPositions = [...settledCursorPositions, ...cursorPositions];
1401
- if (finalCursorPositions.length !== 2) {
1420
+ const {
1421
+ text: formatted,
1422
+ positions: cursorPositions
1423
+ } = result.finish();
1424
+ if (cursorPositions.length !== 2) {
1402
1425
  return {
1403
1426
  formatted
1404
1427
  };
1405
1428
  }
1406
- const cursorNodeStart = finalCursorPositions[0];
1429
+ const [cursorNodeStart, cursorNodeEnd] = cursorPositions;
1407
1430
  return {
1408
1431
  formatted,
1409
1432
  cursorNodeStart,
1410
- cursorNodeText: formatted.slice(cursorNodeStart, method_at_default(
1411
- /* OPTIONAL_OBJECT: false */
1412
- 0,
1413
- finalCursorPositions,
1414
- -1
1415
- ))
1433
+ cursorNodeText: formatted.slice(cursorNodeStart, cursorNodeEnd)
1416
1434
  };
1417
- function trim2() {
1418
- const {
1419
- text: trimmed,
1420
- count
1421
- } = trimIndentation(output);
1422
- if (trimmed) {
1423
- settledOutput.push(trimmed);
1424
- settledTextLength += trimmed.length;
1425
- }
1426
- output = "";
1427
- position -= count;
1428
- if (cursorPositions.length > 0) {
1429
- settledCursorPositions.push(...cursorPositions.map((position2) => Math.min(position2, settledTextLength)));
1430
- cursorPositions.length = 0;
1431
- }
1432
- }
1433
1435
  }
1434
1436
 
1435
1437
  // src/document/public.js
package/index.cjs CHANGED
@@ -647,7 +647,7 @@ __export(version_evaluate_exports, {
647
647
  var version_evaluate_default;
648
648
  var init_version_evaluate = __esm({
649
649
  "src/main/version.evaluate.js"() {
650
- version_evaluate_default = "3.8.0-dev";
650
+ version_evaluate_default = "3.8.0-hoyt.1";
651
651
  }
652
652
  });
653
653