@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/src/remove.ts CHANGED
@@ -8,8 +8,8 @@ import {
8
8
  } from "./directives.js";
9
9
  import type { ImportGroupGap } from "./imports.js";
10
10
  import type { JsxContainer } from "./jsx.js";
11
- import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
12
- import { firstIndexAtOrAfter, type Range } from "./ranges.js";
11
+ import { LINE_TERMINATOR, isBlank, isHorizontalSpace, lastLine, lineIndexAt, lineStartOffsets } from "./lines.js";
12
+ import { byStart, firstIndexAtOrAfter, rangesWithin, type Range } from "./ranges.js";
13
13
  import { analyzeSource } from "./scanner.js";
14
14
  import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
15
15
  import type { Comment } from "./types.js";
@@ -40,12 +40,12 @@ export interface RemoveResult {
40
40
  * comments the `keep` predicate claims, comments whose removal would
41
41
  * change what an ast-grep suppression covers (the ones in front of it on
42
42
  * its row decide which node it trails), comments whose removal would shift
43
- * code under a next-line directive or join another line to one a directive
44
- * covers, or that sit between a directive and the code it reaches past
45
- * them for (or the JSX sibling it applies to), and comments whose removal
46
- * would move a position-dependent directive
47
- * (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
48
- * file-start marker) into effect.
43
+ * code under a next-line directive, JSX comment containers whose removal
44
+ * would join another line to one a directive covers, comments that sit
45
+ * between a directive and the code it reaches past them for (or the JSX
46
+ * sibling it applies to), and comments whose removal would move a
47
+ * position-dependent directive (prettier's first-comment pragma, Deno's
48
+ * coverage file pragma, Bun's file-start marker) into effect.
49
49
  */
50
50
  kept: Comment[];
51
51
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -117,6 +117,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
117
117
  containerOf,
118
118
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
119
119
  preceding: analysis.preceding,
120
+ directives: analysis.directives,
120
121
  };
121
122
  const search = containerSearch(containers);
122
123
  // Collapsing blank lines can only move a surviving comment to the very
@@ -138,8 +139,8 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
138
139
  }
139
140
 
140
141
  const removing = new Set(removed);
141
- search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
142
- const edits = removalEdits(removed, containerOf, search.droppable, holders);
142
+ search.track(new Set(containers.filter((container) => emptiedBy(container, removing))));
143
+ const edits = removalEdits(removed, containerOf, search.droppable, holders, shielding.lineBreaks);
143
144
  const deletions = edits.filter((edit) => edit.replacement === undefined);
144
145
  const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
145
146
  const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
@@ -188,9 +189,14 @@ function unexpectedResult(): Error {
188
189
  return new Error("comment removal produced an unexpected result; refusing to continue");
189
190
  }
190
191
 
191
- /** Sort comparator for document order: ranges are ordered by where they start. */
192
- function byStart(a: Range, b: Range): number {
193
- return a.start - b.start;
192
+ /**
193
+ * The items of the source's list and of the output's that correspond, in
194
+ * pairs; throws when the lists differ in length, since removing comments
195
+ * never adds or drops what they list.
196
+ */
197
+ function corresponding<T>(before: readonly T[], after: readonly T[]): Array<[T, T]> {
198
+ if (before.length !== after.length) throw unexpectedResult();
199
+ return before.map((item, index) => [item, after[index] as T]);
194
200
  }
195
201
 
196
202
  /**
@@ -211,12 +217,7 @@ function firstChangedSurvivor(
211
217
  kept: Comment[],
212
218
  skipped: Comment[],
213
219
  ): Comment | undefined {
214
- const expected = [...kept, ...skipped].sort(byStart);
215
- if (actual.length !== expected.length) {
216
- throw unexpectedResult();
217
- }
218
- for (const [index, comment] of expected.entries()) {
219
- const survivor = actual[index] as Comment;
220
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), actual)) {
220
221
  if (survivor.text !== comment.text) {
221
222
  throw unexpectedResult();
222
223
  }
@@ -278,10 +279,8 @@ function gapHolders(
278
279
  removals: readonly Comment[],
279
280
  holders: ReadonlySet<Comment>,
280
281
  ): Comment[] {
281
- if (before.length !== after.length) throw unexpectedResult();
282
282
  const found: Comment[] = [];
283
- for (const [index, gap] of before.entries()) {
284
- const { splits, spaced } = after[index] as ImportGroupGap;
283
+ for (const [gap, { splits, spaced }] of corresponding(before, after)) {
285
284
  const intact =
286
285
  gap.splits.every((sorter) => splits.includes(sorter)) && gap.spaced.every((linter) => spaced.includes(linter));
287
286
  if (intact) continue;
@@ -342,19 +341,13 @@ interface Container extends Range {
342
341
  function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
343
342
  const containers = new Map<JsxContainer, Container>();
344
343
  for (const layout of layouts) {
345
- const inside: Comment[] = [];
346
- let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
347
- for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
348
- inside.push(comment);
349
- index += 1;
350
- }
351
344
  containers.set(layout, {
352
345
  start: layout.start,
353
346
  end: layout.end,
354
347
  segment: layout.segment,
355
348
  suspect: layout.suspect,
356
349
  next: undefined,
357
- comments: inside,
350
+ comments: rangesWithin(comments, layout),
358
351
  mode: "drop",
359
352
  spaceRange: spaceRange(source, layout),
360
353
  });
@@ -365,8 +358,6 @@ function attachContainers(source: string, layouts: readonly JsxContainer[], comm
365
358
  return [...containers.values()];
366
359
  }
367
360
 
368
- const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
369
-
370
361
  /**
371
362
  * The range `{" "}` would take the place of: a one-line container and the
372
363
  * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
@@ -375,14 +366,12 @@ const isHorizontalSpace = (char: string | undefined): boolean => char === " " ||
375
366
  */
376
367
  function spaceRange(source: string, container: Range): Range | undefined {
377
368
  if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
378
- let lineStart = container.start;
379
- while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1] as string)) lineStart -= 1;
380
369
  let before = container.start;
381
370
  while (isHorizontalSpace(source[before - 1])) before -= 1;
382
371
  let after = container.end;
383
372
  while (isHorizontalSpace(source[after])) after += 1;
384
373
 
385
- if (!isBlank(source.slice(lineStart, container.start))) {
374
+ if (!opensItsLine(source, container)) {
386
375
  return before < container.start ? { start: before, end: container.end } : undefined;
387
376
  }
388
377
  const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
@@ -401,51 +390,67 @@ function skippableRanges(
401
390
  containerOf: ReadonlyMap<Comment, Container>,
402
391
  candidates: ReadonlySet<Comment>,
403
392
  ): Range[] {
404
- const ranges: Range[] = [];
393
+ return mergeContainers(comments, containerOf, (container) => emptiedBy(container, candidates));
394
+ }
395
+
396
+ /**
397
+ * The comments, in document order, with those of every container that
398
+ * `merges` accepts merged into the container itself: one unit spanning its
399
+ * braces in place of the comments inside them.
400
+ */
401
+ function mergeContainers(
402
+ comments: readonly Comment[],
403
+ containerOf: ReadonlyMap<Comment, Container>,
404
+ merges: (container: Container) => boolean,
405
+ ): Array<Comment | Container> {
406
+ const units: Array<Comment | Container> = [];
405
407
  for (const comment of comments) {
406
408
  const container = containerOf.get(comment);
407
- if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
408
- ranges.push(comment);
409
- } else if (ranges[ranges.length - 1] !== container) {
410
- ranges.push(container);
411
- }
409
+ if (container === undefined || !merges(container)) units.push(comment);
410
+ else if (units.at(-1) !== container) units.push(container);
412
411
  }
413
- return ranges;
412
+ return units;
413
+ }
414
+
415
+ /** Whether the container's comments are all among the given ones, so that its braces can go with them. */
416
+ function emptiedBy(container: Container, comments: ReadonlySet<Comment>): boolean {
417
+ return container.comments.every((comment) => comments.has(comment));
414
418
  }
415
419
 
416
420
  /**
417
421
  * The edits that remove the comments: one per comment, or one per container
418
422
  * going with its braces. A holder of a gap around the imports (see
419
- * gapHolders) leaves an empty line in its place.
423
+ * gapHolders) leaves an empty line in its place, and a comment that would
424
+ * otherwise join or split the lines a directive covers one of its own line
425
+ * breaks (see Shielding.lineBreaks).
420
426
  */
421
427
  function removalEdits(
422
428
  removed: readonly Comment[],
423
429
  containerOf: ReadonlyMap<Comment, Container>,
424
430
  droppable: ReadonlySet<Container>,
425
431
  holders: ReadonlySet<Comment>,
432
+ lineBreaks: ReadonlySet<Comment>,
426
433
  ): Edit[] {
427
- const edits: Edit[] = [];
428
- for (const comment of removed) {
429
- const container = containerOf.get(comment);
430
- if (container === undefined || !droppable.has(container) || container.mode === "keep") {
431
- edits.push({ start: comment.start, end: comment.end, blankLine: holders.has(comment) });
432
- } else if (comment === container.comments[0]) {
433
- edits.push(
434
- container.mode === "drop"
435
- ? { start: container.start, end: container.end, jsx: true }
436
- : { ...(container.spaceRange as Range), replacement: '{" "}' },
437
- );
434
+ const units = mergeContainers(
435
+ removed,
436
+ containerOf,
437
+ (container) => droppable.has(container) && container.mode !== "keep",
438
+ );
439
+ return units.map((unit): Edit => {
440
+ if (!("comments" in unit)) {
441
+ return { start: unit.start, end: unit.end, blankLine: holders.has(unit), lineBreak: lineBreaks.has(unit) };
438
442
  }
439
- }
440
- return edits;
443
+ return unit.mode === "drop"
444
+ ? { start: unit.start, end: unit.end, jsx: true }
445
+ : { ...(unit.spaceRange as Range), replacement: '{" "}' };
446
+ });
441
447
  }
442
448
 
443
449
  /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
444
450
  function changedSegments(before: readonly string[], after: readonly string[]): Set<number> {
445
- if (before.length !== after.length) throw unexpectedResult();
446
451
  const changed = new Set<number>();
447
- for (const [index, rendering] of before.entries()) {
448
- if (rendering !== after[index]) changed.add(index);
452
+ for (const [index, [rendering, now]] of corresponding(before, after).entries()) {
453
+ if (rendering !== now) changed.add(index);
449
454
  }
450
455
  return changed;
451
456
  }
@@ -543,15 +548,25 @@ interface ShieldContext {
543
548
  skippable: readonly Range[];
544
549
  /** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
545
550
  preceding: ReadonlyMap<Comment, PrecedingNode>;
551
+ /** Every directive each directive comment carries (see SourceAnalysis). */
552
+ directives: ReadonlyMap<Comment, readonly string[]>;
546
553
  }
547
554
 
548
555
  interface Shielding {
549
556
  removed: Comment[];
550
557
  /**
551
558
  * Lines (1-based) that a surviving directive reaches: the ones below a
552
- * next-line directive, and the one a line directive stands on.
559
+ * next-line directive, and the ones a line directive stands on.
553
560
  */
554
561
  lines: Set<number>;
562
+ /**
563
+ * Comments with code on both sides whose deletion would join one of those
564
+ * lines to another, where no line break would take their place, or split
565
+ * one, where a line feed would stand in for line breaks the tools that only
566
+ * count line feeds do not count. One of the comment's own line breaks takes
567
+ * its place (see Edit.lineBreak).
568
+ */
569
+ lineBreaks: Set<Comment>;
555
570
  }
556
571
 
557
572
  /**
@@ -560,7 +575,8 @@ interface Shielding {
560
575
  */
561
576
  interface RemovalUnit extends Range {
562
577
  comments: readonly Comment[];
563
- container?: Container;
578
+ /** The unit is a container's braces, which go with the comments in them. */
579
+ braces: boolean;
564
580
  }
565
581
 
566
582
  /**
@@ -569,15 +585,19 @@ interface RemovalUnit extends Range {
569
585
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
570
586
  * following code up, silently changing which line the directive applies to —
571
587
  * and with it the compiler or linter behaviour. Nor may a removal join
572
- * another line to one a directive covers, the line a line directive
573
- * (`eslint-disable-line`, ...) stands on included. Directives whose tools
574
- * reach further get more: below a tsc suppression the first line each
575
- * compiler cannot step over stays, oxlint's line-feed rows add the lines they
576
- * run over, between a directive that only counts as the last comment before
577
- * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
578
- * only applies to the node right after it and that target every comment
579
- * stays, and a JSX comment container right after a directive in a container
588
+ * another line to one a directive covers, the lines a line directive
589
+ * (`eslint-disable-line`, `nosemgrep` after code, ...) stands on included:
590
+ * a comment that would leaves one of its line breaks in its place (see
591
+ * Shielding.lineBreaks). Directives whose tools reach further get more:
592
+ * below a tsc suppression the first line each compiler cannot step over
593
+ * stays, oxlint's line-feed rows add the lines they run over, between a
594
+ * directive that only counts as the last comment before its target
595
+ * (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul only
596
+ * applies to the node right after it and that target every comment stays,
597
+ * and a JSX comment container right after a directive in a container
580
598
  * (`{/* prettier-ignore *​/}`, which applies to the next sibling) stays too.
599
+ * Each directive a comment carries shields what it reaches, whichever one
600
+ * names the comment.
581
601
  *
582
602
  * The line checks work on whole lines, not single comments: a line holding
583
603
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
@@ -596,6 +616,7 @@ function shieldNextLineDirectives(
596
616
  const lineOffsets = lineStartOffsets(source);
597
617
  const totalLines = lineOffsets.length;
598
618
  const shieldedLines = new Set<number>();
619
+ const lineBreaks = new Set<Comment>();
599
620
  const blockingLines = new Set<number>();
600
621
  const shieldedGaps: Array<readonly [number, number]> = [];
601
622
  const siblingTargets = new Set<Container>();
@@ -606,7 +627,32 @@ function shieldNextLineDirectives(
606
627
  const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
607
628
  let rows: number[] | undefined;
608
629
  /** The (1-based) line holding the offset. */
609
- const lineOf = (offset: number): number => firstIndexAtOrAfter(lineOffsets, offset + 1, (start) => start);
630
+ const lineOf = (offset: number): number => lineIndexAt(lineOffsets, offset) + 1;
631
+ /**
632
+ * Whether Semgrep can read the `nosemgrep` comment as a suppression of the
633
+ * line below it (see SEMGREP_PREVIOUS_LINE): the line the comment ends on,
634
+ * with the comments in front of it cut out as the removal may cut them,
635
+ * opens with the marker.
636
+ */
637
+ const readsAsSemgrepPreviousLine = (comment: Comment): boolean => {
638
+ let cursor = lineOffsets[comment.endLine - 1] as number;
639
+ let before = "";
640
+ const ranges = context.skippable;
641
+ for (let index = firstIndexAtOrAfter(ranges, cursor + 1, (range) => range.end); index < ranges.length; index += 1) {
642
+ const range = ranges[index] as Range;
643
+ if (range.start >= comment.start) break;
644
+ before += source.slice(cursor, Math.max(cursor, range.start));
645
+ cursor = range.end;
646
+ }
647
+ if (cursor < comment.start) before += source.slice(cursor, comment.start);
648
+ return SEMGREP_PREVIOUS_LINE.test(before + lastLine(comment.text));
649
+ };
650
+ /** The given (1-based) line, which the shields only name when the source has it, and the units on it. */
651
+ const unitsOnLine = (line: number): LineUnits => {
652
+ const start = lineOffsets[line - 1] as number;
653
+ const end = lineOffsets[line] ?? source.length;
654
+ return { start, end, units: currentUnits().filter((unit) => unit.start < end && unit.end > start) };
655
+ };
610
656
 
611
657
  const collectShields = (): boolean => {
612
658
  let added = false;
@@ -617,26 +663,8 @@ function shieldNextLineDirectives(
617
663
  added = true;
618
664
  }
619
665
  };
620
- for (const comment of [...kept, ...skipped]) {
621
- if (processed.has(comment)) continue;
622
- processed.add(comment);
623
- const directive = comment.directive;
624
- if (directive === undefined) continue;
625
- if (carriesAstGrepSuppression(comment.text)) {
626
- const reach = astGrepReach(
627
- source,
628
- (rows ??= rowStartOffsets(source)),
629
- comment,
630
- context.preceding.get(comment) as PrecedingNode,
631
- );
632
- astGrepReaches.push(reach);
633
- if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
634
- for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
635
- shieldedLines.add(line);
636
- }
637
- }
638
- added = true;
639
- }
666
+ /** Collects what one of the directives the comment carries reaches. */
667
+ const shieldReach = (comment: Comment, directive: string): void => {
640
668
  const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
641
669
  if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
642
670
  const codeStart = codeStartAfter(source, comment.end, context.skippable);
@@ -644,7 +672,7 @@ function shieldNextLineDirectives(
644
672
  shieldedGaps.push([comment.end, codeStart]);
645
673
  added = true;
646
674
  }
647
- if (lastComment) continue;
675
+ if (lastComment) return;
648
676
  }
649
677
  if (TSC_SUPPRESSIONS.has(directive)) {
650
678
  for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
@@ -658,10 +686,10 @@ function shieldNextLineDirectives(
658
686
  const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
659
687
  if (reach !== undefined) shieldLines(reach.from, reach.to);
660
688
  }
661
- // A line directive covers the line it stands on, which no removal may
662
- // join to another (see joiningLineGroup).
663
- if (isOwnLineDirective(directive)) shieldLines(comment.line, comment.line);
664
- if (!isNextLineDirective(directive)) continue;
689
+ // A line directive covers the lines it stands on, which no removal may
690
+ // join to another (see joiningUnits).
691
+ if (coversOwnLine(directive)) shieldLines(comment.line, comment.endLine);
692
+ if (!isNextLineDirective(directive)) return;
665
693
  // In JSX, such a directive in braces applies to the sibling after it,
666
694
  // whatever line that is on (prettier leaves the element after
667
695
  // `{/* prettier-ignore */}` as is): a comment-only container standing
@@ -672,58 +700,66 @@ function shieldNextLineDirectives(
672
700
  added = true;
673
701
  }
674
702
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
675
- // `secret(); // nosemgrep`) targets its own line, so it shields nothing
676
- // below it. In JSX, the container holding the comment is what has to
677
- // stand on lines of its own.
678
- if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) continue;
703
+ // `secret(); // skipcq`) targets what it trails, not the line below it.
704
+ // In JSX, the container holding the comment is what has to stand on
705
+ // lines of its own. Semgrep's reach is a matter of what precedes the
706
+ // marker on its line.
707
+ if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) return;
708
+ if (directive === "nosemgrep" && !readsAsSemgrepPreviousLine(comment)) return;
679
709
  // Counted pragmas like `c8 ignore next 3` cover several lines.
680
- shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment));
710
+ shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment, directive));
711
+ };
712
+ for (const comment of [...kept, ...skipped]) {
713
+ if (processed.has(comment)) continue;
714
+ processed.add(comment);
715
+ if (comment.directive === undefined) continue;
716
+ if (carriesAstGrepSuppression(comment.text)) {
717
+ const reach = astGrepReach(
718
+ source,
719
+ (rows ??= rowStartOffsets(source)),
720
+ comment,
721
+ context.preceding.get(comment) as PrecedingNode,
722
+ );
723
+ astGrepReaches.push(reach);
724
+ if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
725
+ shieldLines(lineOf(reach.nextRow.start), lineOf(reach.nextRow.end - 1));
726
+ }
727
+ added = true;
728
+ }
729
+ for (const directive of context.directives.get(comment) as readonly string[]) shieldReach(comment, directive);
681
730
  }
682
731
  return added;
683
732
  };
684
733
 
734
+ /** Takes the comments, all still being removed, out of the removal and keeps them. */
685
735
  const protect = (group: readonly Comment[]): void => {
736
+ if (group.length === 0) return;
686
737
  const grouped = new Set(group);
687
738
  stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
688
739
  kept.push(...group);
689
740
  units = undefined;
690
741
  };
691
742
 
743
+ /** Protects what the shields collected so far hold in place; true when that kept anything. */
692
744
  const protectShields = (): boolean => {
693
- let changed = false;
745
+ const removing = stillRemoved.length;
694
746
  for (const line of shieldedLines) {
695
- const group =
696
- vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
697
- joiningLineGroup(source, line, lineOffsets, currentUnits());
698
- if (group === undefined) continue;
699
- protect(group);
700
- changed = true;
701
- }
702
- for (const line of blockingLines) {
703
- const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
704
- if (group === undefined) continue;
705
- protect(group);
706
- changed = true;
747
+ // A shielded line may neither vanish nor be joined to another. A
748
+ // comment leaves a line break behind instead; a container's braces have
749
+ // none to leave without changing what the element renders, so they stay.
750
+ protect(vanishingLineGroup(source, unitsOnLine(line)));
751
+ for (const unit of joiningUnits(source, unitsOnLine(line))) {
752
+ if (unit.braces) protect(unit.comments);
753
+ else lineBreaks.add(unit.comments[0] as Comment);
754
+ }
707
755
  }
756
+ for (const line of blockingLines) protect(lineOpeningGroup(source, unitsOnLine(line)));
708
757
  for (const [from, to] of shieldedGaps) {
709
- const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
710
- if (group.length === 0) continue;
711
- protect(group);
712
- changed = true;
713
- }
714
- for (const target of siblingTargets) {
715
- const group = stillRemoved.filter((comment) => target.comments.includes(comment));
716
- if (group.length === 0) continue;
717
- protect(group);
718
- changed = true;
758
+ protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
719
759
  }
720
- for (const reach of astGrepReaches) {
721
- const group = currentUnits().filter((unit) => reach.holds(unit));
722
- if (group.length === 0) continue;
723
- protect(group.flatMap((unit) => unit.comments));
724
- changed = true;
725
- }
726
- return changed;
760
+ for (const target of siblingTargets) protect(stillRemoved.filter((comment) => target.comments.includes(comment)));
761
+ for (const reach of astGrepReaches) protect(commentsOf(currentUnits().filter((unit) => reach.holds(unit))));
762
+ return stillRemoved.length < removing;
727
763
  };
728
764
 
729
765
  collectShields();
@@ -733,7 +769,7 @@ function shieldNextLineDirectives(
733
769
  if (stillRemoved.length !== removed.length) {
734
770
  kept.sort(byStart);
735
771
  }
736
- return { removed: stillRemoved, lines: shieldedLines };
772
+ return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
737
773
  }
738
774
 
739
775
  /**
@@ -743,20 +779,21 @@ function shieldNextLineDirectives(
743
779
  */
744
780
  function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
745
781
  const removing = new Set(removals);
746
- const units: RemovalUnit[] = [];
747
- for (const comment of removals) {
748
- const container = containerOf.get(comment);
749
- if (
750
- container === undefined ||
751
- container.mode !== "drop" ||
752
- !container.comments.every((inside) => removing.has(inside))
753
- ) {
754
- units.push({ start: comment.start, end: comment.end, comments: [comment] });
755
- } else if (units[units.length - 1]?.container !== container) {
756
- units.push({ start: container.start, end: container.end, comments: container.comments, container });
757
- }
758
- }
759
- return units;
782
+ const units = mergeContainers(
783
+ removals,
784
+ containerOf,
785
+ (container) => container.mode === "drop" && emptiedBy(container, removing),
786
+ );
787
+ return units.map((unit) => ({
788
+ start: unit.start,
789
+ end: unit.end,
790
+ ...("comments" in unit ? { comments: unit.comments, braces: true } : { comments: [unit], braces: false }),
791
+ }));
792
+ }
793
+
794
+ /** The comments the units delete. */
795
+ function commentsOf(units: readonly RemovalUnit[]): Comment[] {
796
+ return units.flatMap((unit) => unit.comments);
760
797
  }
761
798
 
762
799
  /**
@@ -806,7 +843,7 @@ function firstBlockingLines(
806
843
  // (`/* @ts-ignore */ foo();`) does not end the search.
807
844
  const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
808
845
  if (codeStart >= source.length) return [];
809
- const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
846
+ const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
810
847
  const found: number[] = [];
811
848
  for (const passable of TSC_PASSABLE_LINES) {
812
849
  for (let line = comment.endLine + 1; line < codeLine; line += 1) {
@@ -819,80 +856,54 @@ function firstBlockingLines(
819
856
  return found;
820
857
  }
821
858
 
859
+ /** A line, from its start to the next line's (past its line break), with the removal units holding part of it. */
860
+ interface LineUnits extends Range {
861
+ units: RemovalUnit[];
862
+ }
863
+
822
864
  /**
823
865
  * The removals on the given line when one of them opens it (holds its first
824
866
  * non-whitespace character), so that deleting them would change how the
825
- * line starts. Returns undefined when something that survives opens the line
826
- * instead, or nothing on it is to be removed.
867
+ * line starts. None when something that survives opens the line instead, or
868
+ * nothing on it is to be removed.
827
869
  */
828
- function lineOpeningGroup(
829
- source: string,
830
- line: number,
831
- lineOffsets: readonly number[],
832
- units: readonly RemovalUnit[],
833
- ): Comment[] | undefined {
834
- // A blocking line always lies above the code line, so the next offset exists.
835
- const lineStart = lineOffsets[line - 1] as number;
836
- const lineEnd = lineOffsets[line] as number;
837
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
838
- let first = lineStart;
839
- while (first < lineEnd && isBlank(source[first] as string)) first += 1;
840
- return group.some((unit) => unit.start <= first && first < unit.end)
841
- ? group.flatMap((unit) => unit.comments)
842
- : undefined;
870
+ function lineOpeningGroup(source: string, line: LineUnits): Comment[] {
871
+ let first = line.start;
872
+ while (first < line.end && isBlank(source[first] as string)) first += 1;
873
+ return line.units.some((unit) => unit.start <= first && first < unit.end) ? commentsOf(line.units) : [];
843
874
  }
844
875
 
845
876
  /**
846
877
  * The removals that would make the given line disappear: every
847
878
  * non-whitespace character of the line lies inside one of them, so deleting
848
- * them deletes the physical line. Returns undefined when the line survives
849
- * (it holds code, a kept comment, or nothing to remove at all).
879
+ * them deletes the physical line. None when the line survives (it holds
880
+ * code, a kept comment, or nothing to remove at all).
850
881
  */
851
- function vanishingLineGroup(
852
- source: string,
853
- line: number,
854
- lineOffsets: readonly number[],
855
- units: readonly RemovalUnit[],
856
- ): Comment[] | undefined {
857
- // collectShields never shields a line beyond the last one, so the offset exists.
858
- const lineStart = lineOffsets[line - 1] as number;
859
- const lineEnd = lineOffsets[line] ?? source.length;
860
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
861
- if (group.length === 0) return undefined;
862
- for (let index = lineStart; index < lineEnd; index += 1) {
882
+ function vanishingLineGroup(source: string, line: LineUnits): Comment[] {
883
+ if (line.units.length === 0) return [];
884
+ for (let index = line.start; index < line.end; index += 1) {
863
885
  if (isBlank(source[index] as string)) continue;
864
- if (!group.some((unit) => unit.start <= index && index < unit.end)) return undefined;
886
+ if (!line.units.some((unit) => unit.start <= index && index < unit.end)) return [];
865
887
  }
866
- return group.flatMap((unit) => unit.comments);
888
+ return commentsOf(line.units);
867
889
  }
868
890
 
869
891
  /**
870
- * The removals that would join the given line to another: they reach past
871
- * one of its ends, with something other than whitespace beyond them on the
872
- * line they run into. Deleting such a comment takes its line breaks along —
873
- * with nothing in their place right after an opening bracket or before
874
- * closing punctuation (see applyEdits) — so what stands beyond it can land on
875
- * the line, or what stands on the line move off it, and a directive covering
876
- * the line would cover other code. (With only whitespace beyond, the lines
877
- * the comment held vanish, which vanishingLineGroup weighs.)
892
+ * The removal units that would join the given line to another: they reach
893
+ * past one of its ends, with something other than whitespace beyond them on
894
+ * the line they run into. Deleting such a comment takes its line breaks
895
+ * along — with nothing in their place right after an opening bracket or
896
+ * before closing punctuation, and a line feed elsewhere (see applyEdits) — so
897
+ * what stands beyond it can land on the line, or what stands on the line move
898
+ * off it, and a directive covering the line would cover other code. (With
899
+ * only whitespace beyond, the lines the comment held vanish, which
900
+ * vanishingLineGroup weighs.)
878
901
  */
879
- function joiningLineGroup(
880
- source: string,
881
- line: number,
882
- lineOffsets: readonly number[],
883
- units: readonly RemovalUnit[],
884
- ): Comment[] | undefined {
885
- const lineStart = lineOffsets[line - 1] as number;
886
- const nextLineStart = lineOffsets[line];
887
- const lineEnd = nextLineStart ?? source.length;
888
- const group = units.filter(
902
+ function joiningUnits(source: string, line: LineUnits): RemovalUnit[] {
903
+ return line.units.filter(
889
904
  (unit) =>
890
- unit.start < lineEnd &&
891
- unit.end > lineStart &&
892
- ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
893
- (unit.start < lineStart && !opensItsLine(source, unit))),
905
+ (unit.end > line.end && !closesItsLine(source, unit)) || (unit.start < line.start && !opensItsLine(source, unit)),
894
906
  );
895
- return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
896
907
  }
897
908
 
898
909
  // The directives oxlint reads on rows of its own (see oxlintReachLines).
@@ -922,7 +933,7 @@ function oxlintReachLines(
922
933
  comment: Comment,
923
934
  directive: string,
924
935
  ): { from: number; to: number } | undefined {
925
- const rowOf = (offset: number): number => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
936
+ const rowOf = (offset: number): number => lineIndexAt(rows, offset);
926
937
  if (!directive.endsWith("-next-line")) {
927
938
  return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
928
939
  }
@@ -933,13 +944,41 @@ function oxlintReachLines(
933
944
  }
934
945
 
935
946
  /**
936
- * Whether the directive covers the line it stands on: the `-line` forms of
937
- * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...).
947
+ * Whether the directive covers the lines it stands on: the `-line` forms of
948
+ * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...), the
949
+ * ones that cover the line holding them (see OWN_LINE_DIRECTIVES), and the
950
+ * `ignore next` hints c8 counts lines for (see COUNTED_NEXT_LINE): it
951
+ * ignores the line holding one along with the lines below it.
938
952
  */
939
- function isOwnLineDirective(name: string): boolean {
940
- return name.endsWith("-line") && !name.endsWith("-next-line");
953
+ function coversOwnLine(name: string): boolean {
954
+ return (
955
+ (name.endsWith("-line") && !name.endsWith("-next-line")) ||
956
+ OWN_LINE_DIRECTIVES.has(name) ||
957
+ COUNTED_NEXT_LINE.has(name)
958
+ );
941
959
  }
942
960
 
961
+ // Suppressions that apply to the line holding them: Semgrep's to findings
962
+ // that start on it, CodeQL's `lgtm` to its alerts up to the comment,
963
+ // DeepSource's, SonarQube's, gitleaks', TruffleHog's and detect-secrets' to
964
+ // the line, and codespell's to its words.
965
+ const OWN_LINE_DIRECTIVES = new Set([
966
+ "nosemgrep",
967
+ "lgtm",
968
+ "skipcq",
969
+ "NOSONAR",
970
+ "gitleaks:allow",
971
+ "trufflehog:ignore",
972
+ "pragma-allowlist-secret",
973
+ "codespell:ignore",
974
+ ]);
975
+
976
+ // Semgrep applies a `nosemgrep` to the line below where nothing but
977
+ // non-alphanumerics precede it on its line (`nosem_previous_line_re` in its
978
+ // Nosemgrep.ml): `// nosemgrep: rule-id` and `}); // nosemgrep` cover the next
979
+ // line, `// TODO nosemgrep` and `foo(); // nosemgrep` only their own.
980
+ const SEMGREP_PREVIOUS_LINE = /^[^a-zA-Z0-9]* *nosem(?:grep)?/i;
981
+
943
982
  // These suppressions only apply to the following node/line when they stand on
944
983
  // their own line — trailing after code they target (at most) their own line.
945
984
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -951,7 +990,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
951
990
  "lgtm",
952
991
  "codeql",
953
992
  "skipcq",
954
- "nosemgrep",
955
993
  ]);
956
994
 
957
995
  // The suppressions tsc looks past blank and `//` lines for (see
@@ -1074,12 +1112,13 @@ function isNextLineDirective(name: string): boolean {
1074
1112
  const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
1075
1113
 
1076
1114
  /**
1077
- * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
1078
- * 3). monocart reads the count past any whitespace or none, and c8 and
1079
- * monocart stop at the last digit, so `next3` and `next 3x` count 3 as well.
1115
+ * Number of following lines a next-line pragma the comment carries covers
1116
+ * (`c8 ignore next 3` → 3). monocart reads the count past any whitespace or
1117
+ * none, and c8 and monocart stop at the last digit, so `next3` and `next 3x`
1118
+ * count 3 as well.
1080
1119
  */
1081
- function shieldedLineCount(comment: Comment): number {
1082
- if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive)) return 1;
1120
+ function shieldedLineCount(comment: Comment, directive: string): number {
1121
+ if (!COUNTED_NEXT_LINE.has(directive)) return 1;
1083
1122
  const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
1084
1123
  return match ? Math.max(1, Number(match[1])) : 1;
1085
1124
  }
@@ -1095,19 +1134,22 @@ function occupiesWholeLines(source: string, range: Range): boolean {
1095
1134
 
1096
1135
  /** True when nothing but whitespace follows the range on its last line. */
1097
1136
  function closesItsLine(source: string, range: Range): boolean {
1098
- for (let index = range.end; index < source.length; index += 1) {
1099
- const char = source[index] as string;
1100
- if (LINE_TERMINATOR.test(char)) break;
1101
- if (!isBlank(char)) return false;
1102
- }
1103
- return true;
1137
+ return blankToLineEdge(source, range.end, 1);
1104
1138
  }
1105
1139
 
1106
1140
  /** True when nothing but whitespace precedes the range on its first line. */
1107
1141
  function opensItsLine(source: string, range: Range): boolean {
1108
- for (let index = range.start - 1; index >= 0; index -= 1) {
1142
+ return blankToLineEdge(source, range.start - 1, -1);
1143
+ }
1144
+
1145
+ /**
1146
+ * True when nothing but whitespace lies from the offset on, in the direction
1147
+ * of `step`, up to the line break or the edge of the source.
1148
+ */
1149
+ function blankToLineEdge(source: string, from: number, step: 1 | -1): boolean {
1150
+ for (let index = from; index >= 0 && index < source.length; index += step) {
1109
1151
  const char = source[index] as string;
1110
- if (LINE_TERMINATOR.test(char)) break;
1152
+ if (LINE_TERMINATOR.test(char)) return true;
1111
1153
  if (!isBlank(char)) return false;
1112
1154
  }
1113
1155
  return true;