@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.
@@ -9,7 +9,8 @@ interface WrapOptions {
9
9
  */
10
10
  readonly avoidSentenceOrphans: boolean;
11
11
  /**
12
- * Sets how many columns a line may exceed its limit before it counts as overflowing.
12
+ * Sets how many columns a line may exceed its limit before it counts as overflowing. A layout
13
+ * also uses them when that saves a line.
13
14
  */
14
15
  readonly tolerance: number;
15
16
  /**
@@ -17,7 +18,7 @@ interface WrapOptions {
17
18
  */
18
19
  readonly parentheticalOverflow?: number;
19
20
  }
20
- type LineIssueType = 'overflow' | 'underfill' | 'orphan' | 'widow' | 'unbalanced';
21
+ type LineIssueType = 'overflow' | 'underfill' | 'orphan' | 'widow' | 'unbalanced' | 'split-parenthetical';
21
22
  /**
22
23
  * JSDoc writes types and `{@link ...}` tags as `{...}` groups, so they stay intact like code spans.
23
24
  */
@@ -42,8 +43,8 @@ interface AnalyzeOptions extends WrapOptions {
42
43
  readonly maxWidth?: number;
43
44
  /**
44
45
  * Sets the maximum length of the line that holds a comment whose whole text is one
45
- * prose paragraph. Such a comment stays on one line up to this length, even past
46
- * `width`. It defaults to `width`.
46
+ * prose paragraph. Such a comment stays on one line up to this length plus `tolerance`,
47
+ * even past `width`, but never past `maxWidth`. It defaults to `width`.
47
48
  */
48
49
  readonly singleLineWidth?: number;
49
50
  readonly tabWidth: number;
@@ -205,13 +206,15 @@ interface CommentsConfig {
205
206
  readonly maxWidth?: number;
206
207
  /**
207
208
  * Sets the maximum length of the line that holds a comment whose whole text is one prose
208
- * paragraph. Such a comment stays on one line up to this length, even past `width`, and is
209
- * wrapped to `width` beyond it. Defaults to 120 and never exceeds `maxWidth`.
209
+ * paragraph. Such a comment stays on one line up to this length plus `tolerance`, even past
210
+ * `width`, and is wrapped to `width` beyond it. Defaults to 120, and the line never exceeds
211
+ * `maxWidth`.
210
212
  */
211
213
  readonly singleLineWidth?: number;
212
214
  /**
213
- * Sets a legacy hard limit of `width + tolerance` when `maxWidth` is omitted.
214
- * An explicit `maxWidth` takes precedence.
215
+ * Sets how many columns prose lines may go past `width` when that saves a line in a paragraph of
216
+ * three or more lines. It never goes past `maxWidth`. When `maxWidth` is omitted, it also sets a
217
+ * legacy hard limit of `width + tolerance`.
215
218
  */
216
219
  readonly tolerance?: number;
217
220
  /**
@@ -9,7 +9,8 @@ interface WrapOptions {
9
9
  */
10
10
  readonly avoidSentenceOrphans: boolean;
11
11
  /**
12
- * Sets how many columns a line may exceed its limit before it counts as overflowing.
12
+ * Sets how many columns a line may exceed its limit before it counts as overflowing. A layout
13
+ * also uses them when that saves a line.
13
14
  */
14
15
  readonly tolerance: number;
15
16
  /**
@@ -17,7 +18,7 @@ interface WrapOptions {
17
18
  */
18
19
  readonly parentheticalOverflow?: number;
19
20
  }
20
- type LineIssueType = 'overflow' | 'underfill' | 'orphan' | 'widow' | 'unbalanced';
21
+ type LineIssueType = 'overflow' | 'underfill' | 'orphan' | 'widow' | 'unbalanced' | 'split-parenthetical';
21
22
  /**
22
23
  * JSDoc writes types and `{@link ...}` tags as `{...}` groups, so they stay intact like code spans.
23
24
  */
@@ -42,8 +43,8 @@ interface AnalyzeOptions extends WrapOptions {
42
43
  readonly maxWidth?: number;
43
44
  /**
44
45
  * Sets the maximum length of the line that holds a comment whose whole text is one
45
- * prose paragraph. Such a comment stays on one line up to this length, even past
46
- * `width`. It defaults to `width`.
46
+ * prose paragraph. Such a comment stays on one line up to this length plus `tolerance`,
47
+ * even past `width`, but never past `maxWidth`. It defaults to `width`.
47
48
  */
48
49
  readonly singleLineWidth?: number;
49
50
  readonly tabWidth: number;
@@ -205,13 +206,15 @@ interface CommentsConfig {
205
206
  readonly maxWidth?: number;
206
207
  /**
207
208
  * Sets the maximum length of the line that holds a comment whose whole text is one prose
208
- * paragraph. Such a comment stays on one line up to this length, even past `width`, and is
209
- * wrapped to `width` beyond it. Defaults to 120 and never exceeds `maxWidth`.
209
+ * paragraph. Such a comment stays on one line up to this length plus `tolerance`, even past
210
+ * `width`, and is wrapped to `width` beyond it. Defaults to 120, and the line never exceeds
211
+ * `maxWidth`.
210
212
  */
211
213
  readonly singleLineWidth?: number;
212
214
  /**
213
- * Sets a legacy hard limit of `width + tolerance` when `maxWidth` is omitted.
214
- * An explicit `maxWidth` takes precedence.
215
+ * Sets how many columns prose lines may go past `width` when that saves a line in a paragraph of
216
+ * three or more lines. It never goes past `maxWidth`. When `maxWidth` is omitted, it also sets a
217
+ * legacy hard limit of `width + tolerance`.
215
218
  */
216
219
  readonly tolerance?: number;
217
220
  /**
package/dist/comments.js CHANGED
@@ -199,6 +199,9 @@ function parentheticalGroups(tokens, limit) {
199
199
  }
200
200
  return groups;
201
201
  }
202
+ function parentheticalAround(tokens, at, limit) {
203
+ return parentheticalGroups(tokens, limit).find((group) => group.start < at && at < group.end);
204
+ }
202
205
  function bindParentheticals(tokens, limit) {
203
206
  const groups = new Map(parentheticalGroups(tokens, limit).map((group) => [group.start, group.end]));
204
207
  const units = [];
@@ -535,6 +538,19 @@ function layoutLines(tokens, firstLimit, restLimit, options) {
535
538
  return lines;
536
539
  }
537
540
  }
541
+ if (options.tolerance > 0 && greedy.length > 2) {
542
+ const fewer = balanceTokens(
543
+ tokens,
544
+ firstLimit + options.tolerance,
545
+ restLimit + options.tolerance,
546
+ greedy.length - 1,
547
+ options,
548
+ restLimit
549
+ );
550
+ if (fewer && !hasStep(fewer, restLimit - firstLimit)) {
551
+ return fewer;
552
+ }
553
+ }
538
554
  const balanced = balanceTokens(tokens, firstLimit, restLimit, greedy.length, options);
539
555
  if (balanced) {
540
556
  return balanced;
@@ -590,6 +606,7 @@ var ISSUE_TIERS = {
590
606
  widow: "verdict",
591
607
  unbalanced: "verdict",
592
608
  orphan: "hint",
609
+ "split-parenthetical": "hint",
593
610
  banner: "hint",
594
611
  "em-dash": "hint",
595
612
  semicolon: "hint",
@@ -1085,9 +1102,44 @@ function analyzeParagraph(paragraph, options) {
1085
1102
  if (hasWidow(currentLines) && !hasWidow(wrapped)) {
1086
1103
  lineIssues.push({ type: "widow", line: currentLines.length - 1 });
1087
1104
  }
1088
- if (lineIssues.length === 0 && overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1089
- const line = wrapped.findIndex((words, position) => words.join(" ") !== currentLines[position]?.join(" "));
1090
- lineIssues.push({ type: sameLayout(even, currentLines) ? "orphan" : "unbalanced", line: Math.max(line, 0) });
1105
+ const keepsOnlyOrphans = () => {
1106
+ const free = settle({ ...layoutOptions, avoidSentenceOrphans: false });
1107
+ return !("wrapped" in free) || sameLayout(free.wrapped, currentLines);
1108
+ };
1109
+ const splitsOnlyParenthetical = (line) => {
1110
+ if (layoutOptions.parentheticalOverflow === void 0) {
1111
+ return false;
1112
+ }
1113
+ const tokenCount = (lines) => tokenize(lines.flat().join(" ")).length;
1114
+ const end = tokenCount(currentLines.slice(0, line + 1));
1115
+ const group = parentheticalAround(tokens, end, restLimit + layoutOptions.parentheticalOverflow);
1116
+ if (!group) {
1117
+ return false;
1118
+ }
1119
+ const keepsGroup = wrapped.every((_, position) => {
1120
+ const breakAt = tokenCount(wrapped.slice(0, position + 1));
1121
+ return breakAt <= group.start || breakAt >= group.end;
1122
+ });
1123
+ if (!keepsGroup) {
1124
+ return false;
1125
+ }
1126
+ const restOptions = { ...layoutOptions, keepFirstLine: false };
1127
+ const rest = bindLineStarts(tokens.slice(end), startsLine);
1128
+ const restLines = layoutTokens(rest, restLimit, restLimit, restOptions);
1129
+ return sameLayout(breakAfterSentence(restLines, restLimit, restLimit, restOptions), currentLines.slice(line + 1));
1130
+ };
1131
+ if (overflow.length === 0 && (currentLines.length === wrapped.length || options.maxWidth !== void 0)) {
1132
+ const line = Math.max(
1133
+ wrapped.findIndex((words, position) => words.join(" ") !== currentLines[position]?.join(" ")),
1134
+ 0
1135
+ );
1136
+ if (lineIssues.length === 0 && sameLayout(even, currentLines)) {
1137
+ lineIssues.push({ type: "orphan", line });
1138
+ } else if (lineIssues.length === 0) {
1139
+ lineIssues.push({ type: splitsOnlyParenthetical(line) ? "split-parenthetical" : "unbalanced", line });
1140
+ } else if (lineIssues.every((issue) => issue.type === "orphan") && !sameLayout(even, currentLines) && !keepsOnlyOrphans()) {
1141
+ lineIssues.push({ type: "unbalanced", line });
1142
+ }
1091
1143
  }
1092
1144
  }
1093
1145
  const suggestion = wrapped.map(
@@ -1220,7 +1272,9 @@ function inspectSource(source, filename, options) {
1220
1272
  options.width,
1221
1273
  Math.min(options.singleLineWidth ?? options.width, options.maxWidth ?? Infinity)
1222
1274
  );
1223
- const paragraphOptions = shape.paragraphs.length === 1 && singleLineLength(shape.paragraphs[0], options) <= singleLineWidth ? { ...options, width: singleLineWidth } : options;
1275
+ const singleLineLimit = Math.min(singleLineWidth + options.tolerance, options.maxWidth ?? Infinity);
1276
+ const singleLine = shape.paragraphs.length === 1 ? singleLineLength(shape.paragraphs[0], options) : Infinity;
1277
+ const paragraphOptions = singleLine <= singleLineLimit ? { ...options, width: Math.max(singleLineWidth, singleLine) } : options;
1224
1278
  const lines = sourceLines.slice(shape.startLine, shape.endLine + 1);
1225
1279
  reports.push({
1226
1280
  file: filename,
@@ -1282,8 +1336,8 @@ When rules pull in different directions, the earlier one wins.
1282
1336
 
1283
1337
  ## Layout
1284
1338
 
1285
- - 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.
1286
- - 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.
1339
+ - 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.
1340
+ - 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.
1287
1341
  - 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.
1288
1342
  - 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.
1289
1343
  - Separate paragraphs with an empty comment line.