@d3lm/lint-preset 1.5.4 → 1.5.5

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.
@@ -4,8 +4,9 @@ interface WrapOptions {
4
4
  /**
5
5
  * Sets whether a line may not end with the first word of a sentence, clause, or parenthetical,
6
6
  * like `This`, `the` after a comma, or `(the`. Such a word moves to the next line instead.
7
- * It also sets whether the first of two lines ends with a sentence instead of the start of the
8
- * next one, as long as the second line is then no longer than the first.
7
+ * It also sets whether a line avoids ending with the first two words of a sentence, and whether
8
+ * the first of two lines ends with a sentence instead of the start of the next one, as long as
9
+ * the second line is then no longer than the first.
9
10
  */
10
11
  readonly avoidSentenceOrphans: boolean;
11
12
  /**
@@ -221,8 +222,10 @@ interface CommentsConfig {
221
222
  * Keep the first word of a new sentence or parenthetical off the end of a line, e.g.
222
223
  * `...the module. This` or `...reflows (the` followed by the rest on the next line.
223
224
  * A paragraph of two lines also ends its first line with a sentence when the rest fits
224
- * on the second line and is no longer than the first, instead of with the start of the
225
- * next sentence, like `...the module. This one`. Defaults to `true`.
225
+ * on the second line and is no longer than the first, instead of with the start of
226
+ * the next sentence, like `...the module. This one`. Other lines avoid ending with the
227
+ * first two words of a sentence, like `...opens. It allocates`, unless that makes them
228
+ * much less even. Defaults to `true`.
226
229
  */
227
230
  readonly avoidSentenceOrphans?: boolean;
228
231
  /**
@@ -4,8 +4,9 @@ interface WrapOptions {
4
4
  /**
5
5
  * Sets whether a line may not end with the first word of a sentence, clause, or parenthetical,
6
6
  * like `This`, `the` after a comma, or `(the`. Such a word moves to the next line instead.
7
- * It also sets whether the first of two lines ends with a sentence instead of the start of the
8
- * next one, as long as the second line is then no longer than the first.
7
+ * It also sets whether a line avoids ending with the first two words of a sentence, and whether
8
+ * the first of two lines ends with a sentence instead of the start of the next one, as long as
9
+ * the second line is then no longer than the first.
9
10
  */
10
11
  readonly avoidSentenceOrphans: boolean;
11
12
  /**
@@ -221,8 +222,10 @@ interface CommentsConfig {
221
222
  * Keep the first word of a new sentence or parenthetical off the end of a line, e.g.
222
223
  * `...the module. This` or `...reflows (the` followed by the rest on the next line.
223
224
  * A paragraph of two lines also ends its first line with a sentence when the rest fits
224
- * on the second line and is no longer than the first, instead of with the start of the
225
- * next sentence, like `...the module. This one`. Defaults to `true`.
225
+ * on the second line and is no longer than the first, instead of with the start of
226
+ * the next sentence, like `...the module. This one`. Other lines avoid ending with the
227
+ * first two words of a sentence, like `...opens. It allocates`, unless that makes them
228
+ * much less even. Defaults to `true`.
226
229
  */
227
230
  readonly avoidSentenceOrphans?: boolean;
228
231
  /**
package/dist/comments.js CHANGED
@@ -291,6 +291,8 @@ function hasWidow(lines) {
291
291
  }
292
292
  var FULLNESS_WEIGHT = 0.07;
293
293
  var PARENTHETICAL_SPLIT_WEIGHT = 2;
294
+ var SENTENCE_START_COST = 25;
295
+ var MAX_SENTENCE_START_WORDS = 2;
294
296
  var MAX_STEP = 15;
295
297
  var EXTRA_LINES = 2;
296
298
  function fewestLines(starts, target) {
@@ -378,7 +380,7 @@ function balanceTokens(tokens, firstLimit, restLimit, count, options, preferredL
378
380
  afterOrphan: tokens.map((_, index) => {
379
381
  return options.avoidSentenceOrphans && index >= 2 && isOrphan(tokens[index - 2], tokens[index - 1]);
380
382
  }),
381
- splitCosts
383
+ splitCosts: withSentenceStartCosts(tokens, splitCosts, options)
382
384
  };
383
385
  const targets = [];
384
386
  for (let target = restLimit; target >= Math.max(longest, Math.floor(restLimit / 2)); target -= 1) {
@@ -444,6 +446,22 @@ function splitCostsOf(tokens, limit) {
444
446
  }
445
447
  return splitCosts;
446
448
  }
449
+ function withSentenceStartCosts(tokens, splitCosts, options) {
450
+ if (!options.avoidSentenceOrphans || options.ignoreSentenceStarts) {
451
+ return splitCosts;
452
+ }
453
+ const costs = new Map(splitCosts);
454
+ for (const [index, token] of tokens.entries()) {
455
+ if (!endsSentence(token)) {
456
+ continue;
457
+ }
458
+ const last = Math.min(index + 1 + MAX_SENTENCE_START_WORDS, tokens.length - 1);
459
+ for (let end = index + 2; end <= last && !endsSentence(tokens[end - 1]); end += 1) {
460
+ costs.set(end, (costs.get(end) ?? 0) + SENTENCE_START_COST);
461
+ }
462
+ }
463
+ return costs;
464
+ }
447
465
  function hasParentheticals(tokens, restLimit, options) {
448
466
  if (options.parentheticalOverflow === void 0) {
449
467
  return false;
@@ -1106,6 +1124,10 @@ function analyzeParagraph(paragraph, options) {
1106
1124
  const free = settle({ ...layoutOptions, avoidSentenceOrphans: false });
1107
1125
  return !("wrapped" in free) || sameLayout(free.wrapped, currentLines);
1108
1126
  };
1127
+ const keepsOnlySentenceStarts = () => {
1128
+ const free = settle({ ...layoutOptions, ignoreSentenceStarts: true });
1129
+ return "wrapped" in free && (sameLayout(free.even, currentLines) || sameLayout(free.wrapped, currentLines));
1130
+ };
1109
1131
  const splitsOnlyParenthetical = (line) => {
1110
1132
  if (layoutOptions.parentheticalOverflow === void 0) {
1111
1133
  return false;
@@ -1133,7 +1155,7 @@ function analyzeParagraph(paragraph, options) {
1133
1155
  wrapped.findIndex((words, position) => words.join(" ") !== currentLines[position]?.join(" ")),
1134
1156
  0
1135
1157
  );
1136
- if (lineIssues.length === 0 && sameLayout(even, currentLines)) {
1158
+ if (lineIssues.length === 0 && (sameLayout(even, currentLines) || keepsOnlySentenceStarts())) {
1137
1159
  lineIssues.push({ type: "orphan", line });
1138
1160
  } else if (lineIssues.length === 0) {
1139
1161
  lineIssues.push({ type: splitsOnlyParenthetical(line) ? "split-parenthetical" : "unbalanced", line });
@@ -1337,7 +1359,7 @@ When rules pull in different directions, the earlier one wins.
1337
1359
  ## Layout
1338
1360
 
1339
1361
  - 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.
1362
+ - 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.
1341
1363
  - 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.
1342
1364
  - 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.
1343
1365
  - Separate paragraphs with an empty comment line.