@d3lm/lint-preset 1.5.1 → 1.5.4

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
@@ -201,6 +201,9 @@ function parentheticalGroups(tokens, limit) {
201
201
  }
202
202
  return groups;
203
203
  }
204
+ function parentheticalAround(tokens, at, limit) {
205
+ return parentheticalGroups(tokens, limit).find((group) => group.start < at && at < group.end);
206
+ }
204
207
  function bindParentheticals(tokens, limit) {
205
208
  const groups = new Map(parentheticalGroups(tokens, limit).map((group) => [group.start, group.end]));
206
209
  const units = [];
@@ -537,6 +540,19 @@ function layoutLines(tokens, firstLimit, restLimit, options) {
537
540
  return lines;
538
541
  }
539
542
  }
543
+ if (options.tolerance > 0 && greedy.length > 2) {
544
+ const fewer = balanceTokens(
545
+ tokens,
546
+ firstLimit + options.tolerance,
547
+ restLimit + options.tolerance,
548
+ greedy.length - 1,
549
+ options,
550
+ restLimit
551
+ );
552
+ if (fewer && !hasStep(fewer, restLimit - firstLimit)) {
553
+ return fewer;
554
+ }
555
+ }
540
556
  const balanced = balanceTokens(tokens, firstLimit, restLimit, greedy.length, options);
541
557
  if (balanced) {
542
558
  return balanced;
@@ -592,6 +608,7 @@ var ISSUE_TIERS = {
592
608
  widow: "verdict",
593
609
  unbalanced: "verdict",
594
610
  orphan: "hint",
611
+ "split-parenthetical": "hint",
595
612
  banner: "hint",
596
613
  "em-dash": "hint",
597
614
  semicolon: "hint",
@@ -1087,9 +1104,44 @@ function analyzeParagraph(paragraph, options) {
1087
1104
  if (hasWidow(currentLines) && !hasWidow(wrapped)) {
1088
1105
  lineIssues.push({ type: "widow", line: currentLines.length - 1 });
1089
1106
  }
1090
- if (lineIssues.length === 0 && overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1091
- const line = wrapped.findIndex((words, position) => words.join(" ") !== _optionalChain([currentLines, 'access', _38 => _38[position], 'optionalAccess', _39 => _39.join, 'call', _40 => _40(" ")]));
1092
- lineIssues.push({ type: sameLayout(even, currentLines) ? "orphan" : "unbalanced", line: Math.max(line, 0) });
1107
+ const keepsOnlyOrphans = () => {
1108
+ const free = settle({ ...layoutOptions, avoidSentenceOrphans: false });
1109
+ return !("wrapped" in free) || sameLayout(free.wrapped, currentLines);
1110
+ };
1111
+ const splitsOnlyParenthetical = (line) => {
1112
+ if (layoutOptions.parentheticalOverflow === void 0) {
1113
+ return false;
1114
+ }
1115
+ const tokenCount = (lines) => tokenize(lines.flat().join(" ")).length;
1116
+ const end = tokenCount(currentLines.slice(0, line + 1));
1117
+ const group = parentheticalAround(tokens, end, restLimit + layoutOptions.parentheticalOverflow);
1118
+ if (!group) {
1119
+ return false;
1120
+ }
1121
+ const keepsGroup = wrapped.every((_, position) => {
1122
+ const breakAt = tokenCount(wrapped.slice(0, position + 1));
1123
+ return breakAt <= group.start || breakAt >= group.end;
1124
+ });
1125
+ if (!keepsGroup) {
1126
+ return false;
1127
+ }
1128
+ const restOptions = { ...layoutOptions, keepFirstLine: false };
1129
+ const rest = bindLineStarts(tokens.slice(end), startsLine);
1130
+ const restLines = layoutTokens(rest, restLimit, restLimit, restOptions);
1131
+ return sameLayout(breakAfterSentence(restLines, restLimit, restLimit, restOptions), currentLines.slice(line + 1));
1132
+ };
1133
+ if (overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1134
+ const line = Math.max(
1135
+ wrapped.findIndex((words, position) => words.join(" ") !== _optionalChain([currentLines, 'access', _38 => _38[position], 'optionalAccess', _39 => _39.join, 'call', _40 => _40(" ")])),
1136
+ 0
1137
+ );
1138
+ if (lineIssues.length === 0 && sameLayout(even, currentLines)) {
1139
+ lineIssues.push({ type: "orphan", line });
1140
+ } else if (lineIssues.length === 0) {
1141
+ lineIssues.push({ type: splitsOnlyParenthetical(line) ? "split-parenthetical" : "unbalanced", line });
1142
+ } else if (lineIssues.every((issue) => issue.type === "orphan") && !sameLayout(even, currentLines) && !keepsOnlyOrphans()) {
1143
+ lineIssues.push({ type: "unbalanced", line });
1144
+ }
1093
1145
  }
1094
1146
  }
1095
1147
  const suggestion = wrapped.map(
@@ -1222,7 +1274,9 @@ function inspectSource(source, filename, options) {
1222
1274
  options.width,
1223
1275
  Math.min(_nullishCoalesce(options.singleLineWidth, () => ( options.width)), _nullishCoalesce(options.maxWidth, () => ( Infinity)))
1224
1276
  );
1225
- const paragraphOptions = shape.paragraphs.length === 1 && singleLineLength(shape.paragraphs[0], options) <= singleLineWidth ? { ...options, width: singleLineWidth } : options;
1277
+ const singleLineLimit = Math.min(singleLineWidth + options.tolerance, _nullishCoalesce(options.maxWidth, () => ( Infinity)));
1278
+ const singleLine = shape.paragraphs.length === 1 ? singleLineLength(shape.paragraphs[0], options) : Infinity;
1279
+ const paragraphOptions = singleLine <= singleLineLimit ? { ...options, width: Math.max(singleLineWidth, singleLine) } : options;
1226
1280
  const lines = sourceLines.slice(shape.startLine, shape.endLine + 1);
1227
1281
  reports.push({
1228
1282
  file: filename,
@@ -1284,8 +1338,8 @@ When rules pull in different directions, the earlier one wins.
1284
1338
 
1285
1339
  ## Layout
1286
1340
 
1287
- - 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. 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.
1288
- - 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.
1341
+ - 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.
1289
1343
  - 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.
1290
1344
  - 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.
1291
1345
  - Separate paragraphs with an empty comment line.