@d3lm/lint-preset 1.5.0 → 1.5.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/dist/comments.js CHANGED
@@ -288,6 +288,8 @@ function hasWidow(lines) {
288
288
  }
289
289
  var FULLNESS_WEIGHT = 0.07;
290
290
  var PARENTHETICAL_SPLIT_WEIGHT = 2;
291
+ var MAX_STEP = 15;
292
+ var EXTRA_LINES = 2;
291
293
  function fewestLines(starts, target) {
292
294
  const total = starts.length - 1;
293
295
  const fewest = new Int32Array(total + 1);
@@ -420,7 +422,56 @@ function balanceTokens(tokens, firstLimit, restLimit, count, options, preferredL
420
422
  const bounds = [0, ...best.breaks, total];
421
423
  return bounds.slice(0, -1).map((start, index) => tokens.slice(start, bounds[index + 1]));
422
424
  }
425
+ function hasStep(lines, offset = 0) {
426
+ return lines.some((line, index) => {
427
+ if (index === 0) {
428
+ return false;
429
+ }
430
+ const before = lineLength(lines[index - 1]) + (index === 1 ? offset : 0);
431
+ return lineLength(line) - before > MAX_STEP;
432
+ });
433
+ }
434
+ function splitCostsOf(tokens, limit) {
435
+ const splitCosts = /* @__PURE__ */ new Map();
436
+ for (const group of parentheticalGroups(tokens, limit)) {
437
+ const length = lineLength(tokens.slice(group.start, group.end));
438
+ for (let at = group.start + 1; at < group.end; at += 1) {
439
+ splitCosts.set(at, length * PARENTHETICAL_SPLIT_WEIGHT);
440
+ }
441
+ }
442
+ return splitCosts;
443
+ }
444
+ function hasParentheticals(tokens, restLimit, options) {
445
+ if (options.parentheticalOverflow === void 0) {
446
+ return false;
447
+ }
448
+ return bindParentheticals(tokens, restLimit + options.parentheticalOverflow).some((token) => isParenthetical(token));
449
+ }
423
450
  function layoutTokens(tokens, firstLimit, restLimit, options) {
451
+ const lines = layoutLines(tokens, firstLimit, restLimit, options);
452
+ const offset = restLimit - firstLimit;
453
+ if (options.keepFirstLine || options.keepSteps || !hasStep(lines, offset)) {
454
+ return lines;
455
+ }
456
+ const overflow = hasParentheticals(tokens, restLimit, options) ? options.parentheticalOverflow ?? 0 : 0;
457
+ const splitCosts = overflow > 0 ? splitCostsOf(tokens, restLimit + overflow) : /* @__PURE__ */ new Map();
458
+ for (let count = lines.length + 1; count <= lines.length + EXTRA_LINES; count += 1) {
459
+ const even = balanceTokens(
460
+ tokens,
461
+ firstLimit + overflow,
462
+ restLimit + overflow,
463
+ count,
464
+ options,
465
+ restLimit,
466
+ splitCosts
467
+ );
468
+ if (even && !hasStep(even, offset)) {
469
+ return even;
470
+ }
471
+ }
472
+ return lines;
473
+ }
474
+ function layoutLines(tokens, firstLimit, restLimit, options) {
424
475
  if (options.parentheticalOverflow !== void 0) {
425
476
  const grouped = bindParentheticals(tokens, restLimit + options.parentheticalOverflow);
426
477
  if (grouped.some((token) => isParenthetical(token))) {
@@ -454,14 +505,7 @@ function layoutTokens(tokens, firstLimit, restLimit, options) {
454
505
  return expanded;
455
506
  });
456
507
  }
457
- const splitCosts = /* @__PURE__ */ new Map();
458
- const groups = parentheticalGroups(tokens, restLimit + options.parentheticalOverflow);
459
- for (const group of groups) {
460
- const length = lineLength(tokens.slice(group.start, group.end));
461
- for (let at = group.start + 1; at < group.end; at += 1) {
462
- splitCosts.set(at, length * PARENTHETICAL_SPLIT_WEIGHT);
463
- }
464
- }
508
+ const splitCosts = splitCostsOf(tokens, restLimit + options.parentheticalOverflow);
465
509
  const preferred = wrapTokens(tokens, firstLimit, restLimit, { ...options, parentheticalOverflow: void 0 });
466
510
  for (let count = minimumLines; count <= Math.max(preferred.length + 1, preserved.length); count += 1) {
467
511
  const balanced2 = balanceTokens(
@@ -980,11 +1024,11 @@ function analyzeParagraph(paragraph, options) {
980
1024
  parentheticalOverflow: options.maxWidth === void 0 ? void 0 : options.maxWidth - options.width,
981
1025
  keepFirstLine: paragraph.kind === "tag"
982
1026
  };
983
- const layOut = (from) => {
984
- const even2 = layoutTokens(from, firstLimit, restLimit, layoutOptions);
985
- return { even: even2, wrapped: breakAfterSentence(even2, firstLimit, restLimit, layoutOptions) };
1027
+ const layOut = (from, settings) => {
1028
+ const even2 = layoutTokens(from, firstLimit, restLimit, settings);
1029
+ return { even: even2, wrapped: breakAfterSentence(even2, firstLimit, restLimit, settings) };
986
1030
  };
987
- let units = bindLineStarts(paragraph.kind === "tag" ? bindTagHead(tokens) : tokens, startsLine);
1031
+ const units = bindLineStarts(paragraph.kind === "tag" ? bindTagHead(tokens) : tokens, startsLine);
988
1032
  const hardLimit = restLimit + (options.maxWidth === void 0 ? options.tolerance : options.maxWidth - options.width);
989
1033
  const written = currentLines.map((words) => ` ${words.join(" ")} `);
990
1034
  const staysInLimit = (unit) => {
@@ -993,29 +1037,38 @@ function analyzeParagraph(paragraph, options) {
993
1037
  if (!units.every((unit) => staysInLimit(unit))) {
994
1038
  return keep({ tier: "manual", reason: "unbreakable text across lines" });
995
1039
  }
996
- let { even, wrapped } = layOut(units);
997
1040
  const bodyOf = (lines) => {
998
1041
  return lines.map((words, position) => {
999
1042
  const text = `${position === 0 ? paragraph.marker : hang}${words.join(" ")}`;
1000
1043
  return { ...first, text, verbatim: false, hardBreak: HARD_BREAK.test(text) };
1001
1044
  });
1002
1045
  };
1003
- for (; ; ) {
1004
- const body = bodyOf(wrapped);
1005
- const rewrap = structureOf(body);
1006
- if (!rewrap) {
1007
- break;
1008
- }
1009
- const tipping = body.findIndex((_, position) => structureOf(body.slice(0, position + 1)) !== void 0);
1010
- const start = wrapped.slice(0, Math.max(tipping, 1)).flat().length;
1011
- const pair = `${units[start - 1]} ${units[start]}`;
1012
- if (body.length < 2 || !staysInLimit(pair)) {
1013
- const reading = rewrap.tier === "code-like" ? "code" : rewrap.reason;
1014
- return keep({ tier: "manual", reason: `rewrap would read as ${reading}` });
1046
+ const settle = (settings) => {
1047
+ let current = units;
1048
+ let { even: even2, wrapped: wrapped2 } = layOut(current, settings);
1049
+ for (; ; ) {
1050
+ const body = bodyOf(wrapped2);
1051
+ const rewrap = structureOf(body);
1052
+ if (!rewrap) {
1053
+ return { even: even2, wrapped: wrapped2 };
1054
+ }
1055
+ const tipping = body.findIndex((_, position) => structureOf(body.slice(0, position + 1)) !== void 0);
1056
+ const start = wrapped2.slice(0, Math.max(tipping, 1)).flat().length;
1057
+ const pair = `${current[start - 1]} ${current[start]}`;
1058
+ if (body.length < 2 || !staysInLimit(pair)) {
1059
+ const reading = rewrap.tier === "code-like" ? "code" : rewrap.reason;
1060
+ return { tier: "manual", reason: `rewrap would read as ${reading}` };
1061
+ }
1062
+ current = current.toSpliced(start - 1, 2, pair);
1063
+ ({ even: even2, wrapped: wrapped2 } = layOut(current, settings));
1015
1064
  }
1016
- units = units.toSpliced(start - 1, 2, pair);
1017
- ({ even, wrapped } = layOut(units));
1065
+ };
1066
+ const settled = settle(layoutOptions);
1067
+ const layout = "wrapped" in settled ? settled : settle({ ...layoutOptions, keepSteps: true });
1068
+ if (!("wrapped" in layout)) {
1069
+ return keep(layout);
1018
1070
  }
1071
+ const { even, wrapped } = layout;
1019
1072
  const matchesLayout = sameLayout(wrapped, currentLines);
1020
1073
  const lineIssues = [];
1021
1074
  const overflow = options.maxWidth === void 0 ? [] : overflowIssues(paragraph.lines, options);