@d3lm/lint-preset 1.5.4 → 1.5.6

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.cjs CHANGED
@@ -293,6 +293,8 @@ function hasWidow(lines) {
293
293
  }
294
294
  var FULLNESS_WEIGHT = 0.07;
295
295
  var PARENTHETICAL_SPLIT_WEIGHT = 2;
296
+ var SENTENCE_START_COST = 25;
297
+ var MAX_SENTENCE_START_WORDS = 2;
296
298
  var MAX_STEP = 15;
297
299
  var EXTRA_LINES = 2;
298
300
  function fewestLines(starts, target) {
@@ -380,7 +382,7 @@ function balanceTokens(tokens, firstLimit, restLimit, count, options, preferredL
380
382
  afterOrphan: tokens.map((_, index) => {
381
383
  return options.avoidSentenceOrphans && index >= 2 && isOrphan(tokens[index - 2], tokens[index - 1]);
382
384
  }),
383
- splitCosts
385
+ splitCosts: withSentenceStartCosts(tokens, splitCosts, options)
384
386
  };
385
387
  const targets = [];
386
388
  for (let target = restLimit; target >= Math.max(longest, Math.floor(restLimit / 2)); target -= 1) {
@@ -446,6 +448,22 @@ function splitCostsOf(tokens, limit) {
446
448
  }
447
449
  return splitCosts;
448
450
  }
451
+ function withSentenceStartCosts(tokens, splitCosts, options) {
452
+ if (!options.avoidSentenceOrphans || options.ignoreSentenceStarts) {
453
+ return splitCosts;
454
+ }
455
+ const costs = new Map(splitCosts);
456
+ for (const [index, token] of tokens.entries()) {
457
+ if (!endsSentence(token)) {
458
+ continue;
459
+ }
460
+ const last = Math.min(index + 1 + MAX_SENTENCE_START_WORDS, tokens.length - 1);
461
+ for (let end = index + 2; end <= last && !endsSentence(tokens[end - 1]); end += 1) {
462
+ costs.set(end, (_nullishCoalesce(costs.get(end), () => ( 0))) + SENTENCE_START_COST);
463
+ }
464
+ }
465
+ return costs;
466
+ }
449
467
  function hasParentheticals(tokens, restLimit, options) {
450
468
  if (options.parentheticalOverflow === void 0) {
451
469
  return false;
@@ -1043,9 +1061,9 @@ function analyzeParagraph(paragraph, options) {
1043
1061
  parentheticalOverflow: options.maxWidth === void 0 ? void 0 : options.maxWidth - options.width,
1044
1062
  keepFirstLine: paragraph.kind === "tag"
1045
1063
  };
1046
- const layOut = (from, settings) => {
1047
- const even2 = layoutTokens(from, firstLimit, restLimit, settings);
1048
- return { even: even2, wrapped: breakAfterSentence(even2, firstLimit, restLimit, settings) };
1064
+ const layOut = (from, settings, extra) => {
1065
+ const even2 = layoutTokens(from, firstLimit + extra, restLimit + extra, settings);
1066
+ return { even: even2, wrapped: breakAfterSentence(even2, firstLimit + extra, restLimit + extra, settings) };
1049
1067
  };
1050
1068
  const units = bindLineStarts(paragraph.kind === "tag" ? bindTagHead(tokens) : tokens, startsLine);
1051
1069
  const hardLimit = restLimit + (options.maxWidth === void 0 ? options.tolerance : options.maxWidth - options.width);
@@ -1062,9 +1080,9 @@ function analyzeParagraph(paragraph, options) {
1062
1080
  return { ...first, text, verbatim: false, hardBreak: HARD_BREAK.test(text) };
1063
1081
  });
1064
1082
  };
1065
- const settle = (settings) => {
1083
+ const settle = (settings, extra = 0) => {
1066
1084
  let current = units;
1067
- let { even: even2, wrapped: wrapped2 } = layOut(current, settings);
1085
+ let { even: even2, wrapped: wrapped2 } = layOut(current, settings, extra);
1068
1086
  for (; ; ) {
1069
1087
  const body = bodyOf(wrapped2);
1070
1088
  const rewrap = structureOf(body);
@@ -1079,11 +1097,19 @@ function analyzeParagraph(paragraph, options) {
1079
1097
  return { tier: "manual", reason: `rewrap would read as ${reading}` };
1080
1098
  }
1081
1099
  current = current.toSpliced(start - 1, 2, pair);
1082
- ({ even: even2, wrapped: wrapped2 } = layOut(current, settings));
1100
+ ({ even: even2, wrapped: wrapped2 } = layOut(current, settings, extra));
1083
1101
  }
1084
1102
  };
1085
- const settled = settle(layoutOptions);
1086
- const layout = "wrapped" in settled ? settled : settle({ ...layoutOptions, keepSteps: true });
1103
+ const layoutAt = (extra) => {
1104
+ const settings = {
1105
+ ...layoutOptions,
1106
+ tolerance: layoutOptions.tolerance - extra,
1107
+ parentheticalOverflow: layoutOptions.parentheticalOverflow === void 0 ? void 0 : layoutOptions.parentheticalOverflow - extra
1108
+ };
1109
+ const settled = settle(settings, extra);
1110
+ return "wrapped" in settled ? settled : settle({ ...settings, keepSteps: true }, extra);
1111
+ };
1112
+ const layout = layoutAt(0);
1087
1113
  if (!("wrapped" in layout)) {
1088
1114
  return keep(layout);
1089
1115
  }
@@ -1108,6 +1134,10 @@ function analyzeParagraph(paragraph, options) {
1108
1134
  const free = settle({ ...layoutOptions, avoidSentenceOrphans: false });
1109
1135
  return !("wrapped" in free) || sameLayout(free.wrapped, currentLines);
1110
1136
  };
1137
+ const keepsOnlySentenceStarts = () => {
1138
+ const free = settle({ ...layoutOptions, ignoreSentenceStarts: true });
1139
+ return "wrapped" in free && (sameLayout(free.even, currentLines) || sameLayout(free.wrapped, currentLines));
1140
+ };
1111
1141
  const splitsOnlyParenthetical = (line) => {
1112
1142
  if (layoutOptions.parentheticalOverflow === void 0) {
1113
1143
  return false;
@@ -1135,7 +1165,7 @@ function analyzeParagraph(paragraph, options) {
1135
1165
  wrapped.findIndex((words, position) => words.join(" ") !== _optionalChain([currentLines, 'access', _38 => _38[position], 'optionalAccess', _39 => _39.join, 'call', _40 => _40(" ")])),
1136
1166
  0
1137
1167
  );
1138
- if (lineIssues.length === 0 && sameLayout(even, currentLines)) {
1168
+ if (lineIssues.length === 0 && (sameLayout(even, currentLines) || keepsOnlySentenceStarts())) {
1139
1169
  lineIssues.push({ type: "orphan", line });
1140
1170
  } else if (lineIssues.length === 0) {
1141
1171
  lineIssues.push({ type: splitsOnlyParenthetical(line) ? "split-parenthetical" : "unbalanced", line });
@@ -1144,10 +1174,22 @@ function analyzeParagraph(paragraph, options) {
1144
1174
  }
1145
1175
  }
1146
1176
  }
1147
- const suggestion = wrapped.map(
1177
+ const isVerdict = (issue) => ISSUE_TIERS[issue.type] === "verdict";
1178
+ const matchesWiderLayout = () => {
1179
+ for (let extra = 1; extra <= layoutOptions.tolerance; extra += 1) {
1180
+ const wider = layoutAt(extra);
1181
+ if ("wrapped" in wider && sameLayout(wider.wrapped, currentLines)) {
1182
+ return true;
1183
+ }
1184
+ }
1185
+ return false;
1186
+ };
1187
+ const accepted = lineIssues.some((issue) => isVerdict(issue)) && matchesWiderLayout();
1188
+ const suggested = accepted ? currentLines : wrapped;
1189
+ const suggestion = suggested.map(
1148
1190
  (words, position) => `${prefix}${position === 0 ? paragraph.marker : hang}${words.join(" ")}`
1149
1191
  );
1150
- const wrapIssues = lineIssues.map((issue) => lineIssue(issue.type, paragraph.lines[issue.line], options));
1192
+ const wrapIssues = lineIssues.filter((issue) => !accepted || !isVerdict(issue)).map((issue) => lineIssue(issue.type, paragraph.lines[issue.line], options));
1151
1193
  const issues = byLine([...overflow, ...wrapIssues, ...styleIssues(paragraph, options)]);
1152
1194
  return { ...base, reflowable: true, issues, suggestion };
1153
1195
  }
@@ -1339,7 +1381,7 @@ When rules pull in different directions, the earlier one wins.
1339
1381
  ## Layout
1340
1382
 
1341
1383
  - After you finish, the tool rewraps prose paragraphs, list items, and JSDoc tag descriptions. The width is a preferred target. A parenthetical may extend a line up to the configured maximum so it stays together, and lines may go up to the configured tolerance past the width when that saves a line. In longer paragraphs, balanced lines take priority over keeping every parenthetical on one line. The tool also avoids short last lines where possible. Leave these line breaks to the tool, except for the sentence-orphan case below.
1342
- - Don't end a line with the first word of a new sentence, clause, or parenthetical, like \`This\` after a period, \`the\` after a comma, or \`(the\`. Move it to the next line instead. A long word, like an identifier or a code span, may stay at the end of a line when moving it would leave that line far shorter than the lines around it. Then leave the line breaks as they are.
1384
+ - Don't end a line with the first word of a new sentence, clause, or parenthetical, like \`This\` after a period, \`the\` after a comma, or \`(the\`. Move it to the next line instead. The same goes for the first two words of a sentence, like \`It allocates\` after a period, when the note points at them. A long word, like an identifier or a code span, may stay at the end of a line when moving it would leave that line far shorter than the lines around it. Then leave the line breaks as they are.
1343
1385
  - In a paragraph of two lines, the first line ends with a sentence whenever the rest fits on the second line and is no longer than the first. Don't end it with the start of the next sentence, like \`A wrap\` in \`...lay out differently. A wrap\`. Move those words to the second line instead.
1344
1386
  - A comment whose whole text is one paragraph stays on a single line when that line fits a separate, larger single-line width, even past the width. Only a longer comment wraps to the width.
1345
1387
  - Separate paragraphs with an empty comment line.