prettier 2.7.1 → 2.8.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
@@ -297,7 +297,7 @@
297
297
  regex = /\r\n/g;
298
298
  break;
299
299
  default:
300
- throw new Error('Unexpected "eol" '.concat(JSON.stringify(eol), "."));
300
+ throw new Error(`Unexpected "eol" ${JSON.stringify(eol)}.`);
301
301
  }
302
302
  const endOfLines = text.match(regex);
303
303
  return endOfLines ? endOfLines.length : 0;
@@ -335,7 +335,7 @@
335
335
  });
336
336
  function stripAnsi(string) {
337
337
  if (typeof string !== "string") {
338
- throw new TypeError("Expected a `string`, got `".concat(typeof string, "`"));
338
+ throw new TypeError(`Expected a \`string\`, got \`${typeof string}\``);
339
339
  }
340
340
  return string.replace(ansiRegex(), "");
341
341
  }
@@ -852,7 +852,7 @@
852
852
  lastSpaces += part.n;
853
853
  break;
854
854
  default:
855
- throw new Error("Unexpected type '".concat(part.type, "'"));
855
+ throw new Error(`Unexpected type '${part.type}'`);
856
856
  }
857
857
  }
858
858
  flushSpaces();
@@ -908,7 +908,7 @@
908
908
  }
909
909
  return trimCount;
910
910
  }
911
- function fits(next, restCommands, width, options, hasLineSuffix, mustBeFlat) {
911
+ function fits(next, restCommands, width, hasLineSuffix, mustBeFlat) {
912
912
  let restIdx = restCommands.length;
913
913
  const cmds = [next];
914
914
  const out = [];
@@ -917,26 +917,34 @@
917
917
  if (restIdx === 0) {
918
918
  return true;
919
919
  }
920
- cmds.push(restCommands[restIdx - 1]);
921
- restIdx--;
920
+ cmds.push(restCommands[--restIdx]);
922
921
  continue;
923
922
  }
924
- const [ind, mode, doc] = cmds.pop();
923
+ const {
924
+ mode,
925
+ doc
926
+ } = cmds.pop();
925
927
  if (typeof doc === "string") {
926
928
  out.push(doc);
927
929
  width -= getStringWidth(doc);
928
- } else if (isConcat(doc)) {
930
+ } else if (isConcat(doc) || doc.type === "fill") {
929
931
  const parts = getDocParts(doc);
930
932
  for (let i = parts.length - 1; i >= 0; i--) {
931
- cmds.push([ind, mode, parts[i]]);
933
+ cmds.push({
934
+ mode,
935
+ doc: parts[i]
936
+ });
932
937
  }
933
938
  } else {
934
939
  switch (doc.type) {
935
940
  case "indent":
936
- cmds.push([makeIndent(ind, options), mode, doc.contents]);
937
- break;
938
941
  case "align":
939
- cmds.push([makeAlign(ind, doc.n, options), mode, doc.contents]);
942
+ case "indent-if-break":
943
+ case "label":
944
+ cmds.push({
945
+ mode,
946
+ doc: doc.contents
947
+ });
940
948
  break;
941
949
  case "trim":
942
950
  width += trim(out);
@@ -946,47 +954,31 @@
946
954
  return false;
947
955
  }
948
956
  const groupMode = doc.break ? MODE_BREAK : mode;
949
- cmds.push([ind, groupMode, doc.expandedStates && groupMode === MODE_BREAK ? getLast(doc.expandedStates) : doc.contents]);
950
- if (doc.id) {
951
- groupModeMap[doc.id] = groupMode;
952
- }
957
+ const contents = doc.expandedStates && groupMode === MODE_BREAK ? getLast(doc.expandedStates) : doc.contents;
958
+ cmds.push({
959
+ mode: groupMode,
960
+ doc: contents
961
+ });
953
962
  break;
954
963
  }
955
- case "fill":
956
- for (let i = doc.parts.length - 1; i >= 0; i--) {
957
- cmds.push([ind, mode, doc.parts[i]]);
958
- }
959
- break;
960
- case "if-break":
961
- case "indent-if-break": {
962
- const groupMode = doc.groupId ? groupModeMap[doc.groupId] : mode;
963
- if (groupMode === MODE_BREAK) {
964
- const breakContents = doc.type === "if-break" ? doc.breakContents : doc.negate ? doc.contents : indent(doc.contents);
965
- if (breakContents) {
966
- cmds.push([ind, mode, breakContents]);
967
- }
968
- }
969
- if (groupMode === MODE_FLAT) {
970
- const flatContents = doc.type === "if-break" ? doc.flatContents : doc.negate ? indent(doc.contents) : doc.contents;
971
- if (flatContents) {
972
- cmds.push([ind, mode, flatContents]);
973
- }
964
+ case "if-break": {
965
+ const groupMode = doc.groupId ? groupModeMap[doc.groupId] || MODE_FLAT : mode;
966
+ const contents = groupMode === MODE_BREAK ? doc.breakContents : doc.flatContents;
967
+ if (contents) {
968
+ cmds.push({
969
+ mode,
970
+ doc: contents
971
+ });
974
972
  }
975
973
  break;
976
974
  }
977
975
  case "line":
978
- switch (mode) {
979
- case MODE_FLAT:
980
- if (!doc.hard) {
981
- if (!doc.soft) {
982
- out.push(" ");
983
- width -= 1;
984
- }
985
- break;
986
- }
987
- return true;
988
- case MODE_BREAK:
989
- return true;
976
+ if (mode === MODE_BREAK || doc.hard) {
977
+ return true;
978
+ }
979
+ if (!doc.soft) {
980
+ out.push(" ");
981
+ width--;
990
982
  }
991
983
  break;
992
984
  case "line-suffix":
@@ -997,9 +989,6 @@
997
989
  return false;
998
990
  }
999
991
  break;
1000
- case "label":
1001
- cmds.push([ind, mode, doc.contents]);
1002
- break;
1003
992
  }
1004
993
  }
1005
994
  }
@@ -1010,12 +999,20 @@
1010
999
  const width = options.printWidth;
1011
1000
  const newLine = convertEndOfLineToChars(options.endOfLine);
1012
1001
  let pos = 0;
1013
- const cmds = [[rootIndent(), MODE_BREAK, doc]];
1002
+ const cmds = [{
1003
+ ind: rootIndent(),
1004
+ mode: MODE_BREAK,
1005
+ doc
1006
+ }];
1014
1007
  const out = [];
1015
1008
  let shouldRemeasure = false;
1016
- let lineSuffix = [];
1009
+ const lineSuffix = [];
1017
1010
  while (cmds.length > 0) {
1018
- const [ind, mode, doc2] = cmds.pop();
1011
+ const {
1012
+ ind,
1013
+ mode,
1014
+ doc: doc2
1015
+ } = cmds.pop();
1019
1016
  if (typeof doc2 === "string") {
1020
1017
  const formatted = newLine !== "\n" ? doc2.replace(/\n/g, newLine) : doc2;
1021
1018
  out.push(formatted);
@@ -1023,7 +1020,11 @@
1023
1020
  } else if (isConcat(doc2)) {
1024
1021
  const parts = getDocParts(doc2);
1025
1022
  for (let i = parts.length - 1; i >= 0; i--) {
1026
- cmds.push([ind, mode, parts[i]]);
1023
+ cmds.push({
1024
+ ind,
1025
+ mode,
1026
+ doc: parts[i]
1027
+ });
1027
1028
  }
1028
1029
  } else {
1029
1030
  switch (doc2.type) {
@@ -1031,10 +1032,18 @@
1031
1032
  out.push(cursor.placeholder);
1032
1033
  break;
1033
1034
  case "indent":
1034
- cmds.push([makeIndent(ind, options), mode, doc2.contents]);
1035
+ cmds.push({
1036
+ ind: makeIndent(ind, options),
1037
+ mode,
1038
+ doc: doc2.contents
1039
+ });
1035
1040
  break;
1036
1041
  case "align":
1037
- cmds.push([makeAlign(ind, doc2.n, options), mode, doc2.contents]);
1042
+ cmds.push({
1043
+ ind: makeAlign(ind, doc2.n, options),
1044
+ mode,
1045
+ doc: doc2.contents
1046
+ });
1038
1047
  break;
1039
1048
  case "trim":
1040
1049
  pos -= trim(out);
@@ -1043,31 +1052,51 @@
1043
1052
  switch (mode) {
1044
1053
  case MODE_FLAT:
1045
1054
  if (!shouldRemeasure) {
1046
- cmds.push([ind, doc2.break ? MODE_BREAK : MODE_FLAT, doc2.contents]);
1055
+ cmds.push({
1056
+ ind,
1057
+ mode: doc2.break ? MODE_BREAK : MODE_FLAT,
1058
+ doc: doc2.contents
1059
+ });
1047
1060
  break;
1048
1061
  }
1049
1062
  case MODE_BREAK: {
1050
1063
  shouldRemeasure = false;
1051
- const next = [ind, MODE_FLAT, doc2.contents];
1064
+ const next = {
1065
+ ind,
1066
+ mode: MODE_FLAT,
1067
+ doc: doc2.contents
1068
+ };
1052
1069
  const rem = width - pos;
1053
1070
  const hasLineSuffix = lineSuffix.length > 0;
1054
- if (!doc2.break && fits(next, cmds, rem, options, hasLineSuffix)) {
1071
+ if (!doc2.break && fits(next, cmds, rem, hasLineSuffix)) {
1055
1072
  cmds.push(next);
1056
1073
  } else {
1057
1074
  if (doc2.expandedStates) {
1058
1075
  const mostExpanded = getLast(doc2.expandedStates);
1059
1076
  if (doc2.break) {
1060
- cmds.push([ind, MODE_BREAK, mostExpanded]);
1077
+ cmds.push({
1078
+ ind,
1079
+ mode: MODE_BREAK,
1080
+ doc: mostExpanded
1081
+ });
1061
1082
  break;
1062
1083
  } else {
1063
1084
  for (let i = 1; i < doc2.expandedStates.length + 1; i++) {
1064
1085
  if (i >= doc2.expandedStates.length) {
1065
- cmds.push([ind, MODE_BREAK, mostExpanded]);
1086
+ cmds.push({
1087
+ ind,
1088
+ mode: MODE_BREAK,
1089
+ doc: mostExpanded
1090
+ });
1066
1091
  break;
1067
1092
  } else {
1068
1093
  const state = doc2.expandedStates[i];
1069
- const cmd = [ind, MODE_FLAT, state];
1070
- if (fits(cmd, cmds, rem, options, hasLineSuffix)) {
1094
+ const cmd = {
1095
+ ind,
1096
+ mode: MODE_FLAT,
1097
+ doc: state
1098
+ };
1099
+ if (fits(cmd, cmds, rem, hasLineSuffix)) {
1071
1100
  cmds.push(cmd);
1072
1101
  break;
1073
1102
  }
@@ -1075,14 +1104,18 @@
1075
1104
  }
1076
1105
  }
1077
1106
  } else {
1078
- cmds.push([ind, MODE_BREAK, doc2.contents]);
1107
+ cmds.push({
1108
+ ind,
1109
+ mode: MODE_BREAK,
1110
+ doc: doc2.contents
1111
+ });
1079
1112
  }
1080
1113
  }
1081
1114
  break;
1082
1115
  }
1083
1116
  }
1084
1117
  if (doc2.id) {
1085
- groupModeMap[doc2.id] = getLast(cmds)[1];
1118
+ groupModeMap[doc2.id] = getLast(cmds).mode;
1086
1119
  }
1087
1120
  break;
1088
1121
  case "fill": {
@@ -1094,9 +1127,17 @@
1094
1127
  break;
1095
1128
  }
1096
1129
  const [content, whitespace] = parts;
1097
- const contentFlatCmd = [ind, MODE_FLAT, content];
1098
- const contentBreakCmd = [ind, MODE_BREAK, content];
1099
- const contentFits = fits(contentFlatCmd, [], rem, options, lineSuffix.length > 0, true);
1130
+ const contentFlatCmd = {
1131
+ ind,
1132
+ mode: MODE_FLAT,
1133
+ doc: content
1134
+ };
1135
+ const contentBreakCmd = {
1136
+ ind,
1137
+ mode: MODE_BREAK,
1138
+ doc: content
1139
+ };
1140
+ const contentFits = fits(contentFlatCmd, [], rem, lineSuffix.length > 0, true);
1100
1141
  if (parts.length === 1) {
1101
1142
  if (contentFits) {
1102
1143
  cmds.push(contentFlatCmd);
@@ -1105,8 +1146,16 @@
1105
1146
  }
1106
1147
  break;
1107
1148
  }
1108
- const whitespaceFlatCmd = [ind, MODE_FLAT, whitespace];
1109
- const whitespaceBreakCmd = [ind, MODE_BREAK, whitespace];
1149
+ const whitespaceFlatCmd = {
1150
+ ind,
1151
+ mode: MODE_FLAT,
1152
+ doc: whitespace
1153
+ };
1154
+ const whitespaceBreakCmd = {
1155
+ ind,
1156
+ mode: MODE_BREAK,
1157
+ doc: whitespace
1158
+ };
1110
1159
  if (parts.length === 2) {
1111
1160
  if (contentFits) {
1112
1161
  cmds.push(whitespaceFlatCmd, contentFlatCmd);
@@ -1116,10 +1165,18 @@
1116
1165
  break;
1117
1166
  }
1118
1167
  parts.splice(0, 2);
1119
- const remainingCmd = [ind, mode, fill(parts)];
1168
+ const remainingCmd = {
1169
+ ind,
1170
+ mode,
1171
+ doc: fill(parts)
1172
+ };
1120
1173
  const secondContent = parts[0];
1121
- const firstAndSecondContentFlatCmd = [ind, MODE_FLAT, [content, whitespace, secondContent]];
1122
- const firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], rem, options, lineSuffix.length > 0, true);
1174
+ const firstAndSecondContentFlatCmd = {
1175
+ ind,
1176
+ mode: MODE_FLAT,
1177
+ doc: [content, whitespace, secondContent]
1178
+ };
1179
+ const firstAndSecondContentFits = fits(firstAndSecondContentFlatCmd, [], rem, lineSuffix.length > 0, true);
1123
1180
  if (firstAndSecondContentFits) {
1124
1181
  cmds.push(remainingCmd, whitespaceFlatCmd, contentFlatCmd);
1125
1182
  } else if (contentFits) {
@@ -1135,26 +1192,42 @@
1135
1192
  if (groupMode === MODE_BREAK) {
1136
1193
  const breakContents = doc2.type === "if-break" ? doc2.breakContents : doc2.negate ? doc2.contents : indent(doc2.contents);
1137
1194
  if (breakContents) {
1138
- cmds.push([ind, mode, breakContents]);
1195
+ cmds.push({
1196
+ ind,
1197
+ mode,
1198
+ doc: breakContents
1199
+ });
1139
1200
  }
1140
1201
  }
1141
1202
  if (groupMode === MODE_FLAT) {
1142
1203
  const flatContents = doc2.type === "if-break" ? doc2.flatContents : doc2.negate ? indent(doc2.contents) : doc2.contents;
1143
1204
  if (flatContents) {
1144
- cmds.push([ind, mode, flatContents]);
1205
+ cmds.push({
1206
+ ind,
1207
+ mode,
1208
+ doc: flatContents
1209
+ });
1145
1210
  }
1146
1211
  }
1147
1212
  break;
1148
1213
  }
1149
1214
  case "line-suffix":
1150
- lineSuffix.push([ind, mode, doc2.contents]);
1215
+ lineSuffix.push({
1216
+ ind,
1217
+ mode,
1218
+ doc: doc2.contents
1219
+ });
1151
1220
  break;
1152
1221
  case "line-suffix-boundary":
1153
1222
  if (lineSuffix.length > 0) {
1154
- cmds.push([ind, mode, {
1155
- type: "line",
1156
- hard: true
1157
- }]);
1223
+ cmds.push({
1224
+ ind,
1225
+ mode,
1226
+ doc: {
1227
+ type: "line",
1228
+ hard: true
1229
+ }
1230
+ });
1158
1231
  }
1159
1232
  break;
1160
1233
  case "line":
@@ -1171,8 +1244,12 @@
1171
1244
  }
1172
1245
  case MODE_BREAK:
1173
1246
  if (lineSuffix.length > 0) {
1174
- cmds.push([ind, mode, doc2], ...lineSuffix.reverse());
1175
- lineSuffix = [];
1247
+ cmds.push({
1248
+ ind,
1249
+ mode,
1250
+ doc: doc2
1251
+ }, ...lineSuffix.reverse());
1252
+ lineSuffix.length = 0;
1176
1253
  break;
1177
1254
  }
1178
1255
  if (doc2.literal) {
@@ -1192,14 +1269,18 @@
1192
1269
  }
1193
1270
  break;
1194
1271
  case "label":
1195
- cmds.push([ind, mode, doc2.contents]);
1272
+ cmds.push({
1273
+ ind,
1274
+ mode,
1275
+ doc: doc2.contents
1276
+ });
1196
1277
  break;
1197
1278
  default:
1198
1279
  }
1199
1280
  }
1200
1281
  if (cmds.length === 0 && lineSuffix.length > 0) {
1201
1282
  cmds.push(...lineSuffix.reverse());
1202
- lineSuffix = [];
1283
+ lineSuffix.length = 0;
1203
1284
  }
1204
1285
  }
1205
1286
  const cursorPlaceholderIndex = out.indexOf(cursor.placeholder);
@@ -1287,7 +1368,7 @@
1287
1368
  }
1288
1369
  if (isConcat(doc2)) {
1289
1370
  const printed = getDocParts(doc2).map(printDoc).filter(Boolean);
1290
- return printed.length === 1 ? printed[0] : "[".concat(printed.join(", "), "]");
1371
+ return printed.length === 1 ? printed[0] : `[${printed.join(", ")}]`;
1291
1372
  }
1292
1373
  if (doc2.type === "line") {
1293
1374
  const withBreakParent = Array.isArray(parentParts) && parentParts[index + 1] && parentParts[index + 1].type === "break-parent";
@@ -1316,7 +1397,7 @@
1316
1397
  return doc2.n === Number.NEGATIVE_INFINITY ? "dedentToRoot(" + printDoc(doc2.contents) + ")" : doc2.n < 0 ? "dedent(" + printDoc(doc2.contents) + ")" : doc2.n.type === "root" ? "markAsRoot(" + printDoc(doc2.contents) + ")" : "align(" + JSON.stringify(doc2.n) + ", " + printDoc(doc2.contents) + ")";
1317
1398
  }
1318
1399
  if (doc2.type === "if-break") {
1319
- return "ifBreak(" + printDoc(doc2.breakContents) + (doc2.flatContents ? ", " + printDoc(doc2.flatContents) : "") + (doc2.groupId ? (!doc2.flatContents ? ', ""' : "") + ", { groupId: ".concat(printGroupId(doc2.groupId), " }") : "") + ")";
1400
+ return "ifBreak(" + printDoc(doc2.breakContents) + (doc2.flatContents ? ", " + printDoc(doc2.flatContents) : "") + (doc2.groupId ? (!doc2.flatContents ? ', ""' : "") + `, { groupId: ${printGroupId(doc2.groupId)} }` : "") + ")";
1320
1401
  }
1321
1402
  if (doc2.type === "indent-if-break") {
1322
1403
  const optionsParts = [];
@@ -1324,10 +1405,10 @@
1324
1405
  optionsParts.push("negate: true");
1325
1406
  }
1326
1407
  if (doc2.groupId) {
1327
- optionsParts.push("groupId: ".concat(printGroupId(doc2.groupId)));
1408
+ optionsParts.push(`groupId: ${printGroupId(doc2.groupId)}`);
1328
1409
  }
1329
- const options = optionsParts.length > 0 ? ", { ".concat(optionsParts.join(", "), " }") : "";
1330
- return "indentIfBreak(".concat(printDoc(doc2.contents)).concat(options, ")");
1410
+ const options = optionsParts.length > 0 ? `, { ${optionsParts.join(", ")} }` : "";
1411
+ return `indentIfBreak(${printDoc(doc2.contents)}${options})`;
1331
1412
  }
1332
1413
  if (doc2.type === "group") {
1333
1414
  const optionsParts = [];
@@ -1335,16 +1416,16 @@
1335
1416
  optionsParts.push("shouldBreak: true");
1336
1417
  }
1337
1418
  if (doc2.id) {
1338
- optionsParts.push("id: ".concat(printGroupId(doc2.id)));
1419
+ optionsParts.push(`id: ${printGroupId(doc2.id)}`);
1339
1420
  }
1340
- const options = optionsParts.length > 0 ? ", { ".concat(optionsParts.join(", "), " }") : "";
1421
+ const options = optionsParts.length > 0 ? `, { ${optionsParts.join(", ")} }` : "";
1341
1422
  if (doc2.expandedStates) {
1342
- return "conditionalGroup([".concat(doc2.expandedStates.map((part) => printDoc(part)).join(","), "]").concat(options, ")");
1423
+ return `conditionalGroup([${doc2.expandedStates.map((part) => printDoc(part)).join(",")}]${options})`;
1343
1424
  }
1344
- return "group(".concat(printDoc(doc2.contents)).concat(options, ")");
1425
+ return `group(${printDoc(doc2.contents)}${options})`;
1345
1426
  }
1346
1427
  if (doc2.type === "fill") {
1347
- return "fill([".concat(doc2.parts.map((part) => printDoc(part)).join(", "), "])");
1428
+ return `fill([${doc2.parts.map((part) => printDoc(part)).join(", ")}])`;
1348
1429
  }
1349
1430
  if (doc2.type === "line-suffix") {
1350
1431
  return "lineSuffix(" + printDoc(doc2.contents) + ")";
@@ -1353,7 +1434,7 @@
1353
1434
  return "lineSuffixBoundary";
1354
1435
  }
1355
1436
  if (doc2.type === "label") {
1356
- return "label(".concat(JSON.stringify(doc2.label), ", ").concat(printDoc(doc2.contents), ")");
1437
+ return `label(${JSON.stringify(doc2.label)}, ${printDoc(doc2.contents)})`;
1357
1438
  }
1358
1439
  throw new Error("Unknown doc type " + doc2.type);
1359
1440
  }
@@ -1366,10 +1447,10 @@
1366
1447
  }
1367
1448
  const prefix = String(id).slice(7, -1) || "symbol";
1368
1449
  for (let counter = 0; ; counter++) {
1369
- const key = prefix + (counter > 0 ? " #".concat(counter) : "");
1450
+ const key = prefix + (counter > 0 ? ` #${counter}` : "");
1370
1451
  if (!usedKeysForSymbols.has(key)) {
1371
1452
  usedKeysForSymbols.add(key);
1372
- return printedSymbols[id] = "Symbol.for(".concat(JSON.stringify(key), ")");
1453
+ return printedSymbols[id] = `Symbol.for(${JSON.stringify(key)})`;
1373
1454
  }
1374
1455
  }
1375
1456
  }