@d3lm/lint-preset 1.5.2 → 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/README.md CHANGED
@@ -261,6 +261,7 @@ Verdicts are layout issues that can be measured without reading the prose. They
261
261
  Hints come from heuristics and can be false positives, because only the meaning of the prose shows whether the rule applies. A hint only sends a comment to the agent, which decides whether to change it. `check` only prints comments with verdicts, so a passing check stays quiet apart from its summary, even when hints remain after review. Use `list --suspicious` to inspect hints, which are marked with `(hint)` and never fail a check:
262
262
 
263
263
  - `orphan` means a line ends with the first word of a sentence, or the first of two lines ends with the start of a sentence that fits on the second without making it longer than the first
264
+ - `split-parenthetical` means a line ends inside a parenthetical that the suggested rewrap keeps on one line within `maxWidth`
264
265
  - `em-dash` means the comment uses `—`
265
266
  - `dash` means the comment uses `–` or `--` between words
266
267
  - `semicolon` means the comment joins clauses with a semicolon
@@ -268,7 +269,9 @@ Hints come from heuristics and can be false positives, because only the meaning
268
269
  - `emphasis` means the comment uses `*emphasis*` or `**bold**`
269
270
  - `banner` means the comment is a divider made of repeated characters
270
271
 
271
- A paragraph with an `orphan` hint is not reported as `unbalanced`, because the suggested rewrap moves the word that the hint is about, and the agent may rightly keep that word where it is. A line that ends early is still an `underfill`, whatever its last word is.
272
+ A paragraph with an `orphan` hint is not reported as `unbalanced` when that word is all that sets it apart from the suggested rewrap, because the agent may rightly keep the word where it is. This holds when the paragraph matches the rewrap that allows orphans. A paragraph that differs from that rewrap too, like a line that ends with the hinted word while more words fit on it, is still `unbalanced`. A line that ends early is still an `underfill`, whatever its last word is.
273
+
274
+ In the same way, a paragraph that splits a parenthetical is not reported as `unbalanced` when the split is all that sets it apart from the suggested rewrap, because the split can read as well and keep the line closer to `width`. This holds when the lines before the split match the rewrap and the lines after it match the rewrap of the text after the split. Otherwise the paragraph is still `unbalanced`.
272
275
 
273
276
  Hints are only checked in prose, never in code spans, code blocks, `@example`, or URLs. Rules that can't be measured, like writing complete sentences, are part of the agent's prompt (see `DEFAULT_RULES` in `src/comments/agent.ts`). The prompt treats the style rules as preferences, so the agent keeps punctuation that carries meaning or reads clearly, and never changes code, code spans, literals, patterns, or quotations.
274
277
 
@@ -294,7 +297,7 @@ A comment with an issue in a part that is kept is handed to the agent, which can
294
297
  */
295
298
  ```
296
299
 
297
- The separate `singleLineWidth` allowance is capped by `maxWidth`. Code and documented layout regions retain their exemptions. An indivisible token in measured prose that exceeds `maxWidth` is reported as overflow; the wrapper preserves it, so resolving that case requires changing the maximum or the comment manually. `tolerance` lets prose lines go up to that many columns past `width` when that saves a line in a paragraph of three or more lines, and it never goes past `maxWidth`. It defaults to 0. When `maxWidth` is omitted, `tolerance` also sets a legacy maximum of `width + tolerance`. Direct `analyzeSource` callers can omit `maxWidth` to retain the legacy width/tolerance behavior.
300
+ A comment that is one paragraph stays on one line up to `singleLineWidth` plus `tolerance`, since that always saves a line, and this allowance is capped by `maxWidth`. Code and documented layout regions retain their exemptions. An indivisible token in measured prose that exceeds `maxWidth` is reported as overflow; the wrapper preserves it, so resolving that case requires changing the maximum or the comment manually. `tolerance` lets prose lines go up to that many columns past `width` when that saves a line in a paragraph of three or more lines, and it never goes past `maxWidth`. It defaults to 0. When `maxWidth` is omitted, `tolerance` also sets a legacy maximum of `width + tolerance`. Direct `analyzeSource` callers can omit `maxWidth` to retain the legacy width/tolerance behavior.
298
301
 
299
302
  Like prettier, `comments` looks for a config file in the current directory and its parents, up to the repository root. The config can be a `comments.config.{ts,mts,js,mjs,cjs}`, `.commentsrc`, or `.commentsrc.json` file, or a `comments` key in `package.json`. All options are optional.
300
303
 
@@ -308,7 +311,7 @@ export default defineConfig({
308
311
  maxWidth: 120,
309
312
  // prose lines may go this many columns past `width` when that saves a line
310
313
  tolerance: 0,
311
- // a comment that is one paragraph stays on one line up to this length, and wraps to `width` beyond it
314
+ // a comment that is one paragraph stays on one line up to this length plus `tolerance`, and wraps to `width` beyond it
312
315
  singleLineWidth: 120,
313
316
  avoidSentenceOrphans: true,
314
317
  // look for possible semicolons, em-dashes, dashes, label colons, emphasis, and banners as hints,
@@ -25,7 +25,7 @@ When rules pull in different directions, the earlier one wins.
25
25
  ## Layout
26
26
 
27
27
  - 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.
28
- - 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.
28
+ - 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.
29
29
  - 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.
30
30
  - 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.
31
31
  - Separate paragraphs with an empty comment line.
@@ -65,7 +65,8 @@ var ISSUE_DESCRIPTIONS = {
65
65
  underfill: "ends early, and words from the next line fit on it",
66
66
  widow: "leaves fewer than three words on the last line of its paragraph",
67
67
  unbalanced: "is in a paragraph whose lines are less even than the suggested rewrap",
68
- orphan: "may end with the start of a new sentence, clause, or parenthetical, which belongs on the next line (leave it if the period or comma before it is part of an abbreviation, a name, or notation like `No. 3`)",
68
+ orphan: "may end with the start of a new sentence, clause, or parenthetical, which belongs on the next line (leave it if the period or comma before it is part of an abbreviation, a name, or notation like `No. 3`, or if it is a long word like an identifier and moving it would leave the line far shorter than the lines around it)",
69
+ "split-parenthetical": "may split a parenthetical that the suggested rewrap keeps on one line, past the width but within the maximum (leave it if the split reads as well, like at a comma inside a list, and the line stays closer to the width)",
69
70
  "em-dash": "may use an em-dash to join two thoughts (leave it if it's in a quotation, a title, or a string literal, or reads more clearly than the alternatives)",
70
71
  banner: "may be a section banner (leave it if it's part of a diagram, a table, a box, a Markdown rule, a separator in an example, or legal text)",
71
72
  semicolon: "may join clauses with a semicolon (leave it if it's code, a literal, not a clause join, or reads more clearly than a period or a joining word)",
@@ -131,7 +132,8 @@ function buildPrompt(options) {
131
132
  `Fix the formatting of the comments below, following the rules above. Paths are relative to ${options.root}. Widths include indentation and the comment prefix. The notes under each comment come from the tool's measurements, so never count columns yourself.`,
132
133
  "",
133
134
  "- After you finish, the tool rewraps every prose paragraph as described under Layout, so a layout note on a prose paragraph needs no change from you. Change only the wording there, and leave its line breaks to the tool.",
134
- "- A note that a line may end with the start of a sentence, clause, or parenthetical is the exception. Only you can tell whether it does, so if it does, move those words to the start of the next line.",
135
+ "- A note that a line may end with the start of a sentence, clause, or parenthetical is the exception. Only you can tell whether it does, so if it does, move those words to the start of the next line, unless the Layout rules let a long word stay.",
136
+ "- A note that a line may split a parenthetical is the other exception. If the parenthetical reads better on one line, move the rest of it up to the end of that line yourself.",
135
137
  "- Parts kept as written are never rewrapped, and their notes say so. Fix those notes by hand, for example by breaking an overflowing prose line or by turning a long single-line comment into a multi-line one. Keep code, code spans, tables, box drawings, hard line breaks, and other intentional layout in them exactly as they are. If a note can only be fixed by changing those, leave it and say why in your reply.",
136
138
  `- Notes marked \`${HINT_MARKER}\` come from heuristics. If the rule doesn't actually apply, don't change anything for that note.`,
137
139
  "- Notes only point at places worth a look. Review each comment against the rules, and leave it as it is when it already follows them.",
@@ -489,6 +491,9 @@ function parentheticalGroups(tokens, limit) {
489
491
  }
490
492
  return groups;
491
493
  }
494
+ function parentheticalAround(tokens, at, limit) {
495
+ return parentheticalGroups(tokens, limit).find((group) => group.start < at && at < group.end);
496
+ }
492
497
  function bindParentheticals(tokens, limit) {
493
498
  const groups = new Map(parentheticalGroups(tokens, limit).map((group) => [group.start, group.end]));
494
499
  const units = [];
@@ -893,6 +898,7 @@ var ISSUE_TIERS = {
893
898
  widow: "verdict",
894
899
  unbalanced: "verdict",
895
900
  orphan: "hint",
901
+ "split-parenthetical": "hint",
896
902
  banner: "hint",
897
903
  "em-dash": "hint",
898
904
  semicolon: "hint",
@@ -1388,9 +1394,44 @@ function analyzeParagraph(paragraph, options) {
1388
1394
  if (hasWidow(currentLines) && !hasWidow(wrapped)) {
1389
1395
  lineIssues.push({ type: "widow", line: currentLines.length - 1 });
1390
1396
  }
1391
- if (lineIssues.length === 0 && overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1392
- const line = wrapped.findIndex((words, position) => words.join(" ") !== currentLines[position]?.join(" "));
1393
- lineIssues.push({ type: sameLayout(even, currentLines) ? "orphan" : "unbalanced", line: Math.max(line, 0) });
1397
+ const keepsOnlyOrphans = () => {
1398
+ const free = settle({ ...layoutOptions, avoidSentenceOrphans: false });
1399
+ return !("wrapped" in free) || sameLayout(free.wrapped, currentLines);
1400
+ };
1401
+ const splitsOnlyParenthetical = (line) => {
1402
+ if (layoutOptions.parentheticalOverflow === void 0) {
1403
+ return false;
1404
+ }
1405
+ const tokenCount = (lines) => tokenize(lines.flat().join(" ")).length;
1406
+ const end = tokenCount(currentLines.slice(0, line + 1));
1407
+ const group = parentheticalAround(tokens, end, restLimit + layoutOptions.parentheticalOverflow);
1408
+ if (!group) {
1409
+ return false;
1410
+ }
1411
+ const keepsGroup = wrapped.every((_, position) => {
1412
+ const breakAt = tokenCount(wrapped.slice(0, position + 1));
1413
+ return breakAt <= group.start || breakAt >= group.end;
1414
+ });
1415
+ if (!keepsGroup) {
1416
+ return false;
1417
+ }
1418
+ const restOptions = { ...layoutOptions, keepFirstLine: false };
1419
+ const rest = bindLineStarts(tokens.slice(end), startsLine);
1420
+ const restLines = layoutTokens(rest, restLimit, restLimit, restOptions);
1421
+ return sameLayout(breakAfterSentence(restLines, restLimit, restLimit, restOptions), currentLines.slice(line + 1));
1422
+ };
1423
+ if (overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1424
+ const line = Math.max(
1425
+ wrapped.findIndex((words, position) => words.join(" ") !== currentLines[position]?.join(" ")),
1426
+ 0
1427
+ );
1428
+ if (lineIssues.length === 0 && sameLayout(even, currentLines)) {
1429
+ lineIssues.push({ type: "orphan", line });
1430
+ } else if (lineIssues.length === 0) {
1431
+ lineIssues.push({ type: splitsOnlyParenthetical(line) ? "split-parenthetical" : "unbalanced", line });
1432
+ } else if (lineIssues.every((issue) => issue.type === "orphan") && !sameLayout(even, currentLines) && !keepsOnlyOrphans()) {
1433
+ lineIssues.push({ type: "unbalanced", line });
1434
+ }
1394
1435
  }
1395
1436
  }
1396
1437
  const suggestion = wrapped.map(
@@ -1520,7 +1561,9 @@ function inspectSource(source, filename, options) {
1520
1561
  options.width,
1521
1562
  Math.min(options.singleLineWidth ?? options.width, options.maxWidth ?? Infinity)
1522
1563
  );
1523
- const paragraphOptions = shape.paragraphs.length === 1 && singleLineLength(shape.paragraphs[0], options) <= singleLineWidth ? { ...options, width: singleLineWidth } : options;
1564
+ const singleLineLimit = Math.min(singleLineWidth + options.tolerance, options.maxWidth ?? Infinity);
1565
+ const singleLine = shape.paragraphs.length === 1 ? singleLineLength(shape.paragraphs[0], options) : Infinity;
1566
+ const paragraphOptions = singleLine <= singleLineLimit ? { ...options, width: Math.max(singleLineWidth, singleLine) } : options;
1524
1567
  const lines = sourceLines.slice(shape.startLine, shape.endLine + 1);
1525
1568
  reports.push({
1526
1569
  file: filename,