@kongyo2/ts-comment-scanner 1.20.0 → 1.21.0

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.
Files changed (57) hide show
  1. package/dist/args.d.ts.map +1 -1
  2. package/dist/args.js +23 -19
  3. package/dist/args.js.map +1 -1
  4. package/dist/astgrep.d.ts +1 -1
  5. package/dist/astgrep.d.ts.map +1 -1
  6. package/dist/astgrep.js +3 -8
  7. package/dist/astgrep.js.map +1 -1
  8. package/dist/directives.d.ts +8 -0
  9. package/dist/directives.d.ts.map +1 -1
  10. package/dist/directives.js +27 -20
  11. package/dist/directives.js.map +1 -1
  12. package/dist/files.d.ts +2 -3
  13. package/dist/files.d.ts.map +1 -1
  14. package/dist/files.js +45 -60
  15. package/dist/files.js.map +1 -1
  16. package/dist/imports.d.ts.map +1 -1
  17. package/dist/imports.js +14 -8
  18. package/dist/imports.js.map +1 -1
  19. package/dist/jsx.d.ts.map +1 -1
  20. package/dist/jsx.js +5 -6
  21. package/dist/jsx.js.map +1 -1
  22. package/dist/lines.d.ts +13 -5
  23. package/dist/lines.d.ts.map +1 -1
  24. package/dist/lines.js +20 -16
  25. package/dist/lines.js.map +1 -1
  26. package/dist/ranges.d.ts +8 -0
  27. package/dist/ranges.d.ts.map +1 -1
  28. package/dist/ranges.js +12 -0
  29. package/dist/ranges.js.map +1 -1
  30. package/dist/remove.d.ts +6 -6
  31. package/dist/remove.d.ts.map +1 -1
  32. package/dist/remove.js +230 -204
  33. package/dist/remove.js.map +1 -1
  34. package/dist/run.d.ts.map +1 -1
  35. package/dist/run.js +11 -19
  36. package/dist/run.js.map +1 -1
  37. package/dist/scanner.d.ts +5 -0
  38. package/dist/scanner.d.ts.map +1 -1
  39. package/dist/scanner.js +15 -26
  40. package/dist/scanner.js.map +1 -1
  41. package/dist/splice.d.ts +6 -0
  42. package/dist/splice.d.ts.map +1 -1
  43. package/dist/splice.js +22 -5
  44. package/dist/splice.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/args.ts +21 -21
  47. package/src/astgrep.ts +4 -8
  48. package/src/directives.ts +24 -17
  49. package/src/files.ts +43 -59
  50. package/src/imports.ts +15 -8
  51. package/src/jsx.ts +5 -6
  52. package/src/lines.ts +23 -14
  53. package/src/ranges.ts +17 -0
  54. package/src/remove.ts +262 -220
  55. package/src/run.ts +11 -24
  56. package/src/scanner.ts +20 -27
  57. package/src/splice.ts +29 -6
package/dist/remove.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { astGrepReach, isFileWidePosition, rowStartOffsets } from "./astgrep.js";
2
2
  import { activePositionalDirectives, bunMarkerOffset, carriesAstGrepSuppression, isLegalComment, } from "./directives.js";
3
- import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
4
- import { firstIndexAtOrAfter } from "./ranges.js";
3
+ import { LINE_TERMINATOR, isBlank, isHorizontalSpace, lastLine, lineIndexAt, lineStartOffsets } from "./lines.js";
4
+ import { byStart, firstIndexAtOrAfter, rangesWithin } from "./ranges.js";
5
5
  import { analyzeSource } from "./scanner.js";
6
6
  import { applyEdits, blankLineEdits } from "./splice.js";
7
7
  /**
@@ -69,6 +69,7 @@ export function removeComments(source, options = {}) {
69
69
  containerOf,
70
70
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
71
71
  preceding: analysis.preceding,
72
+ directives: analysis.directives,
72
73
  };
73
74
  const search = containerSearch(containers);
74
75
  // Collapsing blank lines can only move a surviving comment to the very
@@ -87,8 +88,8 @@ export function removeComments(source, options = {}) {
87
88
  return { code: source, removed, kept, skipped, changed: false };
88
89
  }
89
90
  const removing = new Set(removed);
90
- search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
91
- const edits = removalEdits(removed, containerOf, search.droppable, holders);
91
+ search.track(new Set(containers.filter((container) => emptiedBy(container, removing))));
92
+ const edits = removalEdits(removed, containerOf, search.droppable, holders, shielding.lineBreaks);
92
93
  const deletions = edits.filter((edit) => edit.replacement === undefined);
93
94
  const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
94
95
  const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
@@ -133,9 +134,15 @@ export function removeComments(source, options = {}) {
133
134
  function unexpectedResult() {
134
135
  return new Error("comment removal produced an unexpected result; refusing to continue");
135
136
  }
136
- /** Sort comparator for document order: ranges are ordered by where they start. */
137
- function byStart(a, b) {
138
- return a.start - b.start;
137
+ /**
138
+ * The items of the source's list and of the output's that correspond, in
139
+ * pairs; throws when the lists differ in length, since removing comments
140
+ * never adds or drops what they list.
141
+ */
142
+ function corresponding(before, after) {
143
+ if (before.length !== after.length)
144
+ throw unexpectedResult();
145
+ return before.map((item, index) => [item, after[index]]);
139
146
  }
140
147
  /**
141
148
  * Compares the spliced output's comments against the comments meant to
@@ -149,12 +156,7 @@ function byStart(a, b) {
149
156
  * must not hide behind the unchanged name.
150
157
  */
151
158
  function firstChangedSurvivor(source, code, actual, kept, skipped) {
152
- const expected = [...kept, ...skipped].sort(byStart);
153
- if (actual.length !== expected.length) {
154
- throw unexpectedResult();
155
- }
156
- for (const [index, comment] of expected.entries()) {
157
- const survivor = actual[index];
159
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), actual)) {
158
160
  if (survivor.text !== comment.text) {
159
161
  throw unexpectedResult();
160
162
  }
@@ -210,11 +212,8 @@ function closestRemovalBefore(removals, comment) {
210
212
  * group are sorted, and so can an empty line only the output has.
211
213
  */
212
214
  function gapHolders(source, before, after, removals, holders) {
213
- if (before.length !== after.length)
214
- throw unexpectedResult();
215
215
  const found = [];
216
- for (const [index, gap] of before.entries()) {
217
- const { splits, spaced } = after[index];
216
+ for (const [gap, { splits, spaced }] of corresponding(before, after)) {
218
217
  const intact = gap.splits.every((sorter) => splits.includes(sorter)) && gap.spaced.every((linter) => spaced.includes(linter));
219
218
  if (intact)
220
219
  continue;
@@ -257,19 +256,13 @@ function isProtected(comment, options) {
257
256
  function attachContainers(source, layouts, comments) {
258
257
  const containers = new Map();
259
258
  for (const layout of layouts) {
260
- const inside = [];
261
- let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
262
- for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
263
- inside.push(comment);
264
- index += 1;
265
- }
266
259
  containers.set(layout, {
267
260
  start: layout.start,
268
261
  end: layout.end,
269
262
  segment: layout.segment,
270
263
  suspect: layout.suspect,
271
264
  next: undefined,
272
- comments: inside,
265
+ comments: rangesWithin(comments, layout),
273
266
  mode: "drop",
274
267
  spaceRange: spaceRange(source, layout),
275
268
  });
@@ -280,7 +273,6 @@ function attachContainers(source, layouts, comments) {
280
273
  }
281
274
  return [...containers.values()];
282
275
  }
283
- const isHorizontalSpace = (char) => char === " " || char === "\t";
284
276
  /**
285
277
  * The range `{" "}` would take the place of: a one-line container and the
286
278
  * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
@@ -290,16 +282,13 @@ const isHorizontalSpace = (char) => char === " " || char === "\t";
290
282
  function spaceRange(source, container) {
291
283
  if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
292
284
  return undefined;
293
- let lineStart = container.start;
294
- while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1]))
295
- lineStart -= 1;
296
285
  let before = container.start;
297
286
  while (isHorizontalSpace(source[before - 1]))
298
287
  before -= 1;
299
288
  let after = container.end;
300
289
  while (isHorizontalSpace(source[after]))
301
290
  after += 1;
302
- if (!isBlank(source.slice(lineStart, container.start))) {
291
+ if (!opensItsLine(source, container)) {
303
292
  return before < container.start ? { start: before, end: container.end } : undefined;
304
293
  }
305
294
  const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
@@ -313,45 +302,51 @@ function spaceRange(source, container) {
313
302
  * the found code further down, which shields more, not less.
314
303
  */
315
304
  function skippableRanges(comments, containerOf, candidates) {
316
- const ranges = [];
305
+ return mergeContainers(comments, containerOf, (container) => emptiedBy(container, candidates));
306
+ }
307
+ /**
308
+ * The comments, in document order, with those of every container that
309
+ * `merges` accepts merged into the container itself: one unit spanning its
310
+ * braces in place of the comments inside them.
311
+ */
312
+ function mergeContainers(comments, containerOf, merges) {
313
+ const units = [];
317
314
  for (const comment of comments) {
318
315
  const container = containerOf.get(comment);
319
- if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
320
- ranges.push(comment);
321
- }
322
- else if (ranges[ranges.length - 1] !== container) {
323
- ranges.push(container);
324
- }
316
+ if (container === undefined || !merges(container))
317
+ units.push(comment);
318
+ else if (units.at(-1) !== container)
319
+ units.push(container);
325
320
  }
326
- return ranges;
321
+ return units;
322
+ }
323
+ /** Whether the container's comments are all among the given ones, so that its braces can go with them. */
324
+ function emptiedBy(container, comments) {
325
+ return container.comments.every((comment) => comments.has(comment));
327
326
  }
328
327
  /**
329
328
  * The edits that remove the comments: one per comment, or one per container
330
329
  * going with its braces. A holder of a gap around the imports (see
331
- * gapHolders) leaves an empty line in its place.
330
+ * gapHolders) leaves an empty line in its place, and a comment that would
331
+ * otherwise join or split the lines a directive covers one of its own line
332
+ * breaks (see Shielding.lineBreaks).
332
333
  */
333
- function removalEdits(removed, containerOf, droppable, holders) {
334
- const edits = [];
335
- for (const comment of removed) {
336
- const container = containerOf.get(comment);
337
- if (container === undefined || !droppable.has(container) || container.mode === "keep") {
338
- edits.push({ start: comment.start, end: comment.end, blankLine: holders.has(comment) });
339
- }
340
- else if (comment === container.comments[0]) {
341
- edits.push(container.mode === "drop"
342
- ? { start: container.start, end: container.end, jsx: true }
343
- : { ...container.spaceRange, replacement: '{" "}' });
334
+ function removalEdits(removed, containerOf, droppable, holders, lineBreaks) {
335
+ const units = mergeContainers(removed, containerOf, (container) => droppable.has(container) && container.mode !== "keep");
336
+ return units.map((unit) => {
337
+ if (!("comments" in unit)) {
338
+ return { start: unit.start, end: unit.end, blankLine: holders.has(unit), lineBreak: lineBreaks.has(unit) };
344
339
  }
345
- }
346
- return edits;
340
+ return unit.mode === "drop"
341
+ ? { start: unit.start, end: unit.end, jsx: true }
342
+ : { ...unit.spaceRange, replacement: '{" "}' };
343
+ });
347
344
  }
348
345
  /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
349
346
  function changedSegments(before, after) {
350
- if (before.length !== after.length)
351
- throw unexpectedResult();
352
347
  const changed = new Set();
353
- for (const [index, rendering] of before.entries()) {
354
- if (rendering !== after[index])
348
+ for (const [index, [rendering, now]] of corresponding(before, after).entries()) {
349
+ if (rendering !== now)
355
350
  changed.add(index);
356
351
  }
357
352
  return changed;
@@ -441,15 +436,19 @@ function containerSearch(containers) {
441
436
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
442
437
  * following code up, silently changing which line the directive applies to —
443
438
  * and with it the compiler or linter behaviour. Nor may a removal join
444
- * another line to one a directive covers, the line a line directive
445
- * (`eslint-disable-line`, ...) stands on included. Directives whose tools
446
- * reach further get more: below a tsc suppression the first line each
447
- * compiler cannot step over stays, oxlint's line-feed rows add the lines they
448
- * run over, between a directive that only counts as the last comment before
449
- * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
450
- * only applies to the node right after it and that target every comment
451
- * stays, and a JSX comment container right after a directive in a container
439
+ * another line to one a directive covers, the lines a line directive
440
+ * (`eslint-disable-line`, `nosemgrep` after code, ...) stands on included:
441
+ * a comment that would leaves one of its line breaks in its place (see
442
+ * Shielding.lineBreaks). Directives whose tools reach further get more:
443
+ * below a tsc suppression the first line each compiler cannot step over
444
+ * stays, oxlint's line-feed rows add the lines they run over, between a
445
+ * directive that only counts as the last comment before its target
446
+ * (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul only
447
+ * applies to the node right after it and that target every comment stays,
448
+ * and a JSX comment container right after a directive in a container
452
449
  * (`{/* prettier-ignore *​/}`, which applies to the next sibling) stays too.
450
+ * Each directive a comment carries shields what it reaches, whichever one
451
+ * names the comment.
453
452
  *
454
453
  * The line checks work on whole lines, not single comments: a line holding
455
454
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
@@ -462,6 +461,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
462
461
  const lineOffsets = lineStartOffsets(source);
463
462
  const totalLines = lineOffsets.length;
464
463
  const shieldedLines = new Set();
464
+ const lineBreaks = new Set();
465
465
  const blockingLines = new Set();
466
466
  const shieldedGaps = [];
467
467
  const siblingTargets = new Set();
@@ -472,7 +472,34 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
472
472
  const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
473
473
  let rows;
474
474
  /** The (1-based) line holding the offset. */
475
- const lineOf = (offset) => firstIndexAtOrAfter(lineOffsets, offset + 1, (start) => start);
475
+ const lineOf = (offset) => lineIndexAt(lineOffsets, offset) + 1;
476
+ /**
477
+ * Whether Semgrep can read the `nosemgrep` comment as a suppression of the
478
+ * line below it (see SEMGREP_PREVIOUS_LINE): the line the comment ends on,
479
+ * with the comments in front of it cut out as the removal may cut them,
480
+ * opens with the marker.
481
+ */
482
+ const readsAsSemgrepPreviousLine = (comment) => {
483
+ let cursor = lineOffsets[comment.endLine - 1];
484
+ let before = "";
485
+ const ranges = context.skippable;
486
+ for (let index = firstIndexAtOrAfter(ranges, cursor + 1, (range) => range.end); index < ranges.length; index += 1) {
487
+ const range = ranges[index];
488
+ if (range.start >= comment.start)
489
+ break;
490
+ before += source.slice(cursor, Math.max(cursor, range.start));
491
+ cursor = range.end;
492
+ }
493
+ if (cursor < comment.start)
494
+ before += source.slice(cursor, comment.start);
495
+ return SEMGREP_PREVIOUS_LINE.test(before + lastLine(comment.text));
496
+ };
497
+ /** The given (1-based) line, which the shields only name when the source has it, and the units on it. */
498
+ const unitsOnLine = (line) => {
499
+ const start = lineOffsets[line - 1];
500
+ const end = lineOffsets[line] ?? source.length;
501
+ return { start, end, units: currentUnits().filter((unit) => unit.start < end && unit.end > start) };
502
+ };
476
503
  const collectShields = () => {
477
504
  let added = false;
478
505
  const shieldLines = (from, to) => {
@@ -483,23 +510,8 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
483
510
  added = true;
484
511
  }
485
512
  };
486
- for (const comment of [...kept, ...skipped]) {
487
- if (processed.has(comment))
488
- continue;
489
- processed.add(comment);
490
- const directive = comment.directive;
491
- if (directive === undefined)
492
- continue;
493
- if (carriesAstGrepSuppression(comment.text)) {
494
- const reach = astGrepReach(source, (rows ??= rowStartOffsets(source)), comment, context.preceding.get(comment));
495
- astGrepReaches.push(reach);
496
- if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
497
- for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
498
- shieldedLines.add(line);
499
- }
500
- }
501
- added = true;
502
- }
513
+ /** Collects what one of the directives the comment carries reaches. */
514
+ const shieldReach = (comment, directive) => {
503
515
  const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
504
516
  if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
505
517
  const codeStart = codeStartAfter(source, comment.end, context.skippable);
@@ -508,7 +520,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
508
520
  added = true;
509
521
  }
510
522
  if (lastComment)
511
- continue;
523
+ return;
512
524
  }
513
525
  if (TSC_SUPPRESSIONS.has(directive)) {
514
526
  for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
@@ -524,12 +536,12 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
524
536
  if (reach !== undefined)
525
537
  shieldLines(reach.from, reach.to);
526
538
  }
527
- // A line directive covers the line it stands on, which no removal may
528
- // join to another (see joiningLineGroup).
529
- if (isOwnLineDirective(directive))
530
- shieldLines(comment.line, comment.line);
539
+ // A line directive covers the lines it stands on, which no removal may
540
+ // join to another (see joiningUnits).
541
+ if (coversOwnLine(directive))
542
+ shieldLines(comment.line, comment.endLine);
531
543
  if (!isNextLineDirective(directive))
532
- continue;
544
+ return;
533
545
  // In JSX, such a directive in braces applies to the sibling after it,
534
546
  // whatever line that is on (prettier leaves the element after
535
547
  // `{/* prettier-ignore */}` as is): a comment-only container standing
@@ -540,61 +552,70 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
540
552
  added = true;
541
553
  }
542
554
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
543
- // `secret(); // nosemgrep`) targets its own line, so it shields nothing
544
- // below it. In JSX, the container holding the comment is what has to
545
- // stand on lines of its own.
555
+ // `secret(); // skipcq`) targets what it trails, not the line below it.
556
+ // In JSX, the container holding the comment is what has to stand on
557
+ // lines of its own. Semgrep's reach is a matter of what precedes the
558
+ // marker on its line.
546
559
  if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment))
547
- continue;
560
+ return;
561
+ if (directive === "nosemgrep" && !readsAsSemgrepPreviousLine(comment))
562
+ return;
548
563
  // Counted pragmas like `c8 ignore next 3` cover several lines.
549
- shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment));
564
+ shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment, directive));
565
+ };
566
+ for (const comment of [...kept, ...skipped]) {
567
+ if (processed.has(comment))
568
+ continue;
569
+ processed.add(comment);
570
+ if (comment.directive === undefined)
571
+ continue;
572
+ if (carriesAstGrepSuppression(comment.text)) {
573
+ const reach = astGrepReach(source, (rows ??= rowStartOffsets(source)), comment, context.preceding.get(comment));
574
+ astGrepReaches.push(reach);
575
+ if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
576
+ shieldLines(lineOf(reach.nextRow.start), lineOf(reach.nextRow.end - 1));
577
+ }
578
+ added = true;
579
+ }
580
+ for (const directive of context.directives.get(comment))
581
+ shieldReach(comment, directive);
550
582
  }
551
583
  return added;
552
584
  };
585
+ /** Takes the comments, all still being removed, out of the removal and keeps them. */
553
586
  const protect = (group) => {
587
+ if (group.length === 0)
588
+ return;
554
589
  const grouped = new Set(group);
555
590
  stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
556
591
  kept.push(...group);
557
592
  units = undefined;
558
593
  };
594
+ /** Protects what the shields collected so far hold in place; true when that kept anything. */
559
595
  const protectShields = () => {
560
- let changed = false;
596
+ const removing = stillRemoved.length;
561
597
  for (const line of shieldedLines) {
562
- const group = vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
563
- joiningLineGroup(source, line, lineOffsets, currentUnits());
564
- if (group === undefined)
565
- continue;
566
- protect(group);
567
- changed = true;
568
- }
569
- for (const line of blockingLines) {
570
- const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
571
- if (group === undefined)
572
- continue;
573
- protect(group);
574
- changed = true;
598
+ // A shielded line may neither vanish nor be joined to another. A
599
+ // comment leaves a line break behind instead; a container's braces have
600
+ // none to leave without changing what the element renders, so they stay.
601
+ protect(vanishingLineGroup(source, unitsOnLine(line)));
602
+ for (const unit of joiningUnits(source, unitsOnLine(line))) {
603
+ if (unit.braces)
604
+ protect(unit.comments);
605
+ else
606
+ lineBreaks.add(unit.comments[0]);
607
+ }
575
608
  }
609
+ for (const line of blockingLines)
610
+ protect(lineOpeningGroup(source, unitsOnLine(line)));
576
611
  for (const [from, to] of shieldedGaps) {
577
- const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
578
- if (group.length === 0)
579
- continue;
580
- protect(group);
581
- changed = true;
612
+ protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
582
613
  }
583
- for (const target of siblingTargets) {
584
- const group = stillRemoved.filter((comment) => target.comments.includes(comment));
585
- if (group.length === 0)
586
- continue;
587
- protect(group);
588
- changed = true;
589
- }
590
- for (const reach of astGrepReaches) {
591
- const group = currentUnits().filter((unit) => reach.holds(unit));
592
- if (group.length === 0)
593
- continue;
594
- protect(group.flatMap((unit) => unit.comments));
595
- changed = true;
596
- }
597
- return changed;
614
+ for (const target of siblingTargets)
615
+ protect(stillRemoved.filter((comment) => target.comments.includes(comment)));
616
+ for (const reach of astGrepReaches)
617
+ protect(commentsOf(currentUnits().filter((unit) => reach.holds(unit))));
618
+ return stillRemoved.length < removing;
598
619
  };
599
620
  collectShields();
600
621
  while (protectShields()) {
@@ -604,7 +625,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
604
625
  if (stillRemoved.length !== removed.length) {
605
626
  kept.sort(byStart);
606
627
  }
607
- return { removed: stillRemoved, lines: shieldedLines };
628
+ return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
608
629
  }
609
630
  /**
610
631
  * The removals as the edits delete them: a comment-only container whose
@@ -613,19 +634,16 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
613
634
  */
614
635
  function removalUnits(removals, containerOf) {
615
636
  const removing = new Set(removals);
616
- const units = [];
617
- for (const comment of removals) {
618
- const container = containerOf.get(comment);
619
- if (container === undefined ||
620
- container.mode !== "drop" ||
621
- !container.comments.every((inside) => removing.has(inside))) {
622
- units.push({ start: comment.start, end: comment.end, comments: [comment] });
623
- }
624
- else if (units[units.length - 1]?.container !== container) {
625
- units.push({ start: container.start, end: container.end, comments: container.comments, container });
626
- }
627
- }
628
- return units;
637
+ const units = mergeContainers(removals, containerOf, (container) => container.mode === "drop" && emptiedBy(container, removing));
638
+ return units.map((unit) => ({
639
+ start: unit.start,
640
+ end: unit.end,
641
+ ...("comments" in unit ? { comments: unit.comments, braces: true } : { comments: [unit], braces: false }),
642
+ }));
643
+ }
644
+ /** The comments the units delete. */
645
+ function commentsOf(units) {
646
+ return units.flatMap((unit) => unit.comments);
629
647
  }
630
648
  /**
631
649
  * Offset of the first character at or after `from` that is neither
@@ -671,7 +689,7 @@ function firstBlockingLines(source, comment, lineOffsets, ranges) {
671
689
  const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
672
690
  if (codeStart >= source.length)
673
691
  return [];
674
- const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
692
+ const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
675
693
  const found = [];
676
694
  for (const passable of TSC_PASSABLE_LINES) {
677
695
  for (let line = comment.endLine + 1; line < codeLine; line += 1) {
@@ -686,61 +704,45 @@ function firstBlockingLines(source, comment, lineOffsets, ranges) {
686
704
  /**
687
705
  * The removals on the given line when one of them opens it (holds its first
688
706
  * non-whitespace character), so that deleting them would change how the
689
- * line starts. Returns undefined when something that survives opens the line
690
- * instead, or nothing on it is to be removed.
707
+ * line starts. None when something that survives opens the line instead, or
708
+ * nothing on it is to be removed.
691
709
  */
692
- function lineOpeningGroup(source, line, lineOffsets, units) {
693
- // A blocking line always lies above the code line, so the next offset exists.
694
- const lineStart = lineOffsets[line - 1];
695
- const lineEnd = lineOffsets[line];
696
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
697
- let first = lineStart;
698
- while (first < lineEnd && isBlank(source[first]))
710
+ function lineOpeningGroup(source, line) {
711
+ let first = line.start;
712
+ while (first < line.end && isBlank(source[first]))
699
713
  first += 1;
700
- return group.some((unit) => unit.start <= first && first < unit.end)
701
- ? group.flatMap((unit) => unit.comments)
702
- : undefined;
714
+ return line.units.some((unit) => unit.start <= first && first < unit.end) ? commentsOf(line.units) : [];
703
715
  }
704
716
  /**
705
717
  * The removals that would make the given line disappear: every
706
718
  * non-whitespace character of the line lies inside one of them, so deleting
707
- * them deletes the physical line. Returns undefined when the line survives
708
- * (it holds code, a kept comment, or nothing to remove at all).
719
+ * them deletes the physical line. None when the line survives (it holds
720
+ * code, a kept comment, or nothing to remove at all).
709
721
  */
710
- function vanishingLineGroup(source, line, lineOffsets, units) {
711
- // collectShields never shields a line beyond the last one, so the offset exists.
712
- const lineStart = lineOffsets[line - 1];
713
- const lineEnd = lineOffsets[line] ?? source.length;
714
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
715
- if (group.length === 0)
716
- return undefined;
717
- for (let index = lineStart; index < lineEnd; index += 1) {
722
+ function vanishingLineGroup(source, line) {
723
+ if (line.units.length === 0)
724
+ return [];
725
+ for (let index = line.start; index < line.end; index += 1) {
718
726
  if (isBlank(source[index]))
719
727
  continue;
720
- if (!group.some((unit) => unit.start <= index && index < unit.end))
721
- return undefined;
728
+ if (!line.units.some((unit) => unit.start <= index && index < unit.end))
729
+ return [];
722
730
  }
723
- return group.flatMap((unit) => unit.comments);
731
+ return commentsOf(line.units);
724
732
  }
725
733
  /**
726
- * The removals that would join the given line to another: they reach past
727
- * one of its ends, with something other than whitespace beyond them on the
728
- * line they run into. Deleting such a comment takes its line breaks along —
729
- * with nothing in their place right after an opening bracket or before
730
- * closing punctuation (see applyEdits) — so what stands beyond it can land on
731
- * the line, or what stands on the line move off it, and a directive covering
732
- * the line would cover other code. (With only whitespace beyond, the lines
733
- * the comment held vanish, which vanishingLineGroup weighs.)
734
+ * The removal units that would join the given line to another: they reach
735
+ * past one of its ends, with something other than whitespace beyond them on
736
+ * the line they run into. Deleting such a comment takes its line breaks
737
+ * along — with nothing in their place right after an opening bracket or
738
+ * before closing punctuation, and a line feed elsewhere (see applyEdits) — so
739
+ * what stands beyond it can land on the line, or what stands on the line move
740
+ * off it, and a directive covering the line would cover other code. (With
741
+ * only whitespace beyond, the lines the comment held vanish, which
742
+ * vanishingLineGroup weighs.)
734
743
  */
735
- function joiningLineGroup(source, line, lineOffsets, units) {
736
- const lineStart = lineOffsets[line - 1];
737
- const nextLineStart = lineOffsets[line];
738
- const lineEnd = nextLineStart ?? source.length;
739
- const group = units.filter((unit) => unit.start < lineEnd &&
740
- unit.end > lineStart &&
741
- ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
742
- (unit.start < lineStart && !opensItsLine(source, unit))));
743
- return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
744
+ function joiningUnits(source, line) {
745
+ return line.units.filter((unit) => (unit.end > line.end && !closesItsLine(source, unit)) || (unit.start < line.start && !opensItsLine(source, unit)));
744
746
  }
745
747
  // The directives oxlint reads on rows of its own (see oxlintReachLines).
746
748
  const OXLINT_ROW_DIRECTIVES = new Set([
@@ -762,7 +764,7 @@ const OXLINT_ROW_DIRECTIVES = new Set([
762
764
  * rest of the file, which no removal changes.
763
765
  */
764
766
  function oxlintReachLines(source, rows, lineOf, comment, directive) {
765
- const rowOf = (offset) => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
767
+ const rowOf = (offset) => lineIndexAt(rows, offset);
766
768
  if (!directive.endsWith("-next-line")) {
767
769
  return { from: lineOf(rows[rowOf(comment.start)]), to: comment.line };
768
770
  }
@@ -773,12 +775,36 @@ function oxlintReachLines(source, rows, lineOf, comment, directive) {
773
775
  return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
774
776
  }
775
777
  /**
776
- * Whether the directive covers the line it stands on: the `-line` forms of
777
- * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...).
778
+ * Whether the directive covers the lines it stands on: the `-line` forms of
779
+ * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...), the
780
+ * ones that cover the line holding them (see OWN_LINE_DIRECTIVES), and the
781
+ * `ignore next` hints c8 counts lines for (see COUNTED_NEXT_LINE): it
782
+ * ignores the line holding one along with the lines below it.
778
783
  */
779
- function isOwnLineDirective(name) {
780
- return name.endsWith("-line") && !name.endsWith("-next-line");
784
+ function coversOwnLine(name) {
785
+ return ((name.endsWith("-line") && !name.endsWith("-next-line")) ||
786
+ OWN_LINE_DIRECTIVES.has(name) ||
787
+ COUNTED_NEXT_LINE.has(name));
781
788
  }
789
+ // Suppressions that apply to the line holding them: Semgrep's to findings
790
+ // that start on it, CodeQL's `lgtm` to its alerts up to the comment,
791
+ // DeepSource's, SonarQube's, gitleaks', TruffleHog's and detect-secrets' to
792
+ // the line, and codespell's to its words.
793
+ const OWN_LINE_DIRECTIVES = new Set([
794
+ "nosemgrep",
795
+ "lgtm",
796
+ "skipcq",
797
+ "NOSONAR",
798
+ "gitleaks:allow",
799
+ "trufflehog:ignore",
800
+ "pragma-allowlist-secret",
801
+ "codespell:ignore",
802
+ ]);
803
+ // Semgrep applies a `nosemgrep` to the line below where nothing but
804
+ // non-alphanumerics precede it on its line (`nosem_previous_line_re` in its
805
+ // Nosemgrep.ml): `// nosemgrep: rule-id` and `}); // nosemgrep` cover the next
806
+ // line, `// TODO nosemgrep` and `foo(); // nosemgrep` only their own.
807
+ const SEMGREP_PREVIOUS_LINE = /^[^a-zA-Z0-9]* *nosem(?:grep)?/i;
782
808
  // These suppressions only apply to the following node/line when they stand on
783
809
  // their own line — trailing after code they target (at most) their own line.
784
810
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -790,7 +816,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
790
816
  "lgtm",
791
817
  "codeql",
792
818
  "skipcq",
793
- "nosemgrep",
794
819
  ]);
795
820
  // The suppressions tsc looks past blank and `//` lines for (see
796
821
  // firstBlockingLines). They stay next-line directives as well: Flow, which
@@ -905,12 +930,13 @@ function isNextLineDirective(name) {
905
930
  // reason text.
906
931
  const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
907
932
  /**
908
- * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
909
- * 3). monocart reads the count past any whitespace or none, and c8 and
910
- * monocart stop at the last digit, so `next3` and `next 3x` count 3 as well.
933
+ * Number of following lines a next-line pragma the comment carries covers
934
+ * (`c8 ignore next 3` → 3). monocart reads the count past any whitespace or
935
+ * none, and c8 and monocart stop at the last digit, so `next3` and `next 3x`
936
+ * count 3 as well.
911
937
  */
912
- function shieldedLineCount(comment) {
913
- if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive))
938
+ function shieldedLineCount(comment, directive) {
939
+ if (!COUNTED_NEXT_LINE.has(directive))
914
940
  return 1;
915
941
  const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
916
942
  return match ? Math.max(1, Number(match[1])) : 1;
@@ -925,21 +951,21 @@ function occupiesWholeLines(source, range) {
925
951
  }
926
952
  /** True when nothing but whitespace follows the range on its last line. */
927
953
  function closesItsLine(source, range) {
928
- for (let index = range.end; index < source.length; index += 1) {
929
- const char = source[index];
930
- if (LINE_TERMINATOR.test(char))
931
- break;
932
- if (!isBlank(char))
933
- return false;
934
- }
935
- return true;
954
+ return blankToLineEdge(source, range.end, 1);
936
955
  }
937
956
  /** True when nothing but whitespace precedes the range on its first line. */
938
957
  function opensItsLine(source, range) {
939
- for (let index = range.start - 1; index >= 0; index -= 1) {
958
+ return blankToLineEdge(source, range.start - 1, -1);
959
+ }
960
+ /**
961
+ * True when nothing but whitespace lies from the offset on, in the direction
962
+ * of `step`, up to the line break or the edge of the source.
963
+ */
964
+ function blankToLineEdge(source, from, step) {
965
+ for (let index = from; index >= 0 && index < source.length; index += step) {
940
966
  const char = source[index];
941
967
  if (LINE_TERMINATOR.test(char))
942
- break;
968
+ return true;
943
969
  if (!isBlank(char))
944
970
  return false;
945
971
  }