@kongyo2/ts-comment-scanner 1.19.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 (59) hide show
  1. package/README.md +2 -2
  2. package/dist/args.d.ts.map +1 -1
  3. package/dist/args.js +23 -19
  4. package/dist/args.js.map +1 -1
  5. package/dist/astgrep.d.ts +1 -1
  6. package/dist/astgrep.d.ts.map +1 -1
  7. package/dist/astgrep.js +3 -8
  8. package/dist/astgrep.js.map +1 -1
  9. package/dist/directives.d.ts +8 -0
  10. package/dist/directives.d.ts.map +1 -1
  11. package/dist/directives.js +27 -20
  12. package/dist/directives.js.map +1 -1
  13. package/dist/files.d.ts +2 -3
  14. package/dist/files.d.ts.map +1 -1
  15. package/dist/files.js +45 -60
  16. package/dist/files.js.map +1 -1
  17. package/dist/imports.d.ts +34 -10
  18. package/dist/imports.d.ts.map +1 -1
  19. package/dist/imports.js +202 -28
  20. package/dist/imports.js.map +1 -1
  21. package/dist/jsx.d.ts.map +1 -1
  22. package/dist/jsx.js +5 -6
  23. package/dist/jsx.js.map +1 -1
  24. package/dist/lines.d.ts +13 -5
  25. package/dist/lines.d.ts.map +1 -1
  26. package/dist/lines.js +20 -16
  27. package/dist/lines.js.map +1 -1
  28. package/dist/ranges.d.ts +8 -0
  29. package/dist/ranges.d.ts.map +1 -1
  30. package/dist/ranges.js +12 -0
  31. package/dist/ranges.js.map +1 -1
  32. package/dist/remove.d.ts +10 -7
  33. package/dist/remove.d.ts.map +1 -1
  34. package/dist/remove.js +254 -219
  35. package/dist/remove.js.map +1 -1
  36. package/dist/run.d.ts.map +1 -1
  37. package/dist/run.js +11 -19
  38. package/dist/run.js.map +1 -1
  39. package/dist/scanner.d.ts +10 -1
  40. package/dist/scanner.d.ts.map +1 -1
  41. package/dist/scanner.js +15 -26
  42. package/dist/scanner.js.map +1 -1
  43. package/dist/splice.d.ts +6 -0
  44. package/dist/splice.d.ts.map +1 -1
  45. package/dist/splice.js +22 -5
  46. package/dist/splice.js.map +1 -1
  47. package/package.json +1 -1
  48. package/src/args.ts +21 -21
  49. package/src/astgrep.ts +4 -8
  50. package/src/directives.ts +24 -17
  51. package/src/files.ts +43 -59
  52. package/src/imports.ts +229 -28
  53. package/src/jsx.ts +5 -6
  54. package/src/lines.ts +23 -14
  55. package/src/ranges.ts +17 -0
  56. package/src/remove.ts +287 -235
  57. package/src/run.ts +11 -24
  58. package/src/scanner.ts +25 -28
  59. 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. */
@@ -80,7 +80,10 @@ export interface RemoveResult {
80
80
  * lines already against that edge stay.
81
81
  * - Imports and exports stay in the groups TypeScript's organizeImports and
82
82
  * dprint sort apart: where deleting the comments between two groups would
83
- * leave no empty line between them, an empty line takes their place.
83
+ * leave no empty line between them, an empty line takes their place. So it
84
+ * does where deleting the comments after an import or a require would
85
+ * bring the code after it up to the line below, keeping the empty line the
86
+ * newline-after-import rules of eslint-plugin-import and oxlint want.
84
87
  * - A leading byte-order mark is preserved. Reported positions index into the
85
88
  * `source` string exactly as `scanComments(source)` reports them (a BOM
86
89
  * counts as its first character).
@@ -114,6 +117,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
114
117
  containerOf,
115
118
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
116
119
  preceding: analysis.preceding,
120
+ directives: analysis.directives,
117
121
  };
118
122
  const search = containerSearch(containers);
119
123
  // Collapsing blank lines can only move a surviving comment to the very
@@ -122,7 +126,8 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
122
126
  // without the collapse before any removal is given up.
123
127
  let tidyBlankLines = true;
124
128
  // The removals that leave an empty line in their place, holding two groups
125
- // of imports or exports apart (see gapHolders).
129
+ // of imports or exports apart, or the code after the imports two lines
130
+ // below them (see gapHolders).
126
131
  const holders = new Set<Comment>();
127
132
 
128
133
  for (;;) {
@@ -134,8 +139,8 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
134
139
  }
135
140
 
136
141
  const removing = new Set(removed);
137
- search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
138
- 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);
139
144
  const deletions = edits.filter((edit) => edit.replacement === undefined);
140
145
  const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
141
146
  const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
@@ -144,8 +149,9 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
144
149
  if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
145
150
 
146
151
  // Where the output joins two groups of imports or exports that a sorter
147
- // sorts apart, a removal between them leaves an empty line in its place,
148
- // and the attempt is repeated.
152
+ // sorts apart, or loses the empty line a linter finds after the imports,
153
+ // a removal in the gap leaves an empty line in its place, and the attempt
154
+ // is repeated.
149
155
  const holding = gapHolders(source, analysis.importGroups, output.importGroups, removed, holders);
150
156
  if (holding.length > 0) {
151
157
  for (const holder of holding) holders.add(holder);
@@ -183,9 +189,14 @@ function unexpectedResult(): Error {
183
189
  return new Error("comment removal produced an unexpected result; refusing to continue");
184
190
  }
185
191
 
186
- /** Sort comparator for document order: ranges are ordered by where they start. */
187
- function byStart(a: Range, b: Range): number {
188
- 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]);
189
200
  }
190
201
 
191
202
  /**
@@ -206,12 +217,7 @@ function firstChangedSurvivor(
206
217
  kept: Comment[],
207
218
  skipped: Comment[],
208
219
  ): Comment | undefined {
209
- const expected = [...kept, ...skipped].sort(byStart);
210
- if (actual.length !== expected.length) {
211
- throw unexpectedResult();
212
- }
213
- for (const [index, comment] of expected.entries()) {
214
- const survivor = actual[index] as Comment;
220
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), actual)) {
215
221
  if (survivor.text !== comment.text) {
216
222
  throw unexpectedResult();
217
223
  }
@@ -255,14 +261,16 @@ function closestRemovalBefore(removals: readonly Comment[], comment: Comment): C
255
261
  /**
256
262
  * The removals that are to leave an empty line in their place (see
257
263
  * Edit.blankLine), so that the output starts every group of imports and
258
- * exports the source starts. The gaps belong to the same declarations in
259
- * both texts, since removing comments leaves the statements as they are.
260
- * Where the output joins two groups at a gap, the deleted comments took the
261
- * gap's line breaks along, and the last of them that held any — one spanning
262
- * lines, or one on a line below the token the gap follows — leaves an empty
263
- * line, which starts a group for both sorters. A group the output starts
264
- * where the source starts none can stand, for both parts of a sorted group
265
- * are sorted.
264
+ * exports the source starts, and keeps every empty line the linters find
265
+ * after its imports. The gaps belong to the same statements in both texts,
266
+ * since removing comments leaves the statements as they are. Where the
267
+ * output joins two groups at a gap, or a linter no longer finds its empty
268
+ * line there, the deleted comments took the gap's line breaks along, and the
269
+ * last of them that held any — one spanning lines, or one on a line below
270
+ * the token the gap follows — leaves an empty line, which starts a group for
271
+ * both sorters and is the empty line both linters want. A group the output
272
+ * starts where the source starts none can stand, for both parts of a sorted
273
+ * group are sorted, and so can an empty line only the output has.
266
274
  */
267
275
  function gapHolders(
268
276
  source: string,
@@ -271,13 +279,14 @@ function gapHolders(
271
279
  removals: readonly Comment[],
272
280
  holders: ReadonlySet<Comment>,
273
281
  ): Comment[] {
274
- if (before.length !== after.length) throw unexpectedResult();
275
282
  const found: Comment[] = [];
276
- for (const [index, gap] of before.entries()) {
277
- const splits = (after[index] as ImportGroupGap).splits;
278
- if (gap.splits.every((sorter) => splits.includes(sorter))) continue;
283
+ for (const [gap, { splits, spaced }] of corresponding(before, after)) {
284
+ const intact =
285
+ gap.splits.every((sorter) => splits.includes(sorter)) && gap.spaced.every((linter) => spaced.includes(linter));
286
+ if (intact) continue;
279
287
  const inGap = removals.filter((comment) => comment.start >= gap.start && comment.end <= gap.end);
280
- // An empty line in the gap already starts a group for both sorters.
288
+ // An empty line in the gap already starts a group for both sorters and
289
+ // is the empty line both linters want.
281
290
  if (inGap.some((comment) => holders.has(comment))) throw unexpectedResult();
282
291
  const holder = inGap.findLast(
283
292
  (comment) => comment.endLine > comment.line || LINE_TERMINATOR.test(source.slice(gap.start, comment.start)),
@@ -332,19 +341,13 @@ interface Container extends Range {
332
341
  function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
333
342
  const containers = new Map<JsxContainer, Container>();
334
343
  for (const layout of layouts) {
335
- const inside: Comment[] = [];
336
- let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
337
- for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
338
- inside.push(comment);
339
- index += 1;
340
- }
341
344
  containers.set(layout, {
342
345
  start: layout.start,
343
346
  end: layout.end,
344
347
  segment: layout.segment,
345
348
  suspect: layout.suspect,
346
349
  next: undefined,
347
- comments: inside,
350
+ comments: rangesWithin(comments, layout),
348
351
  mode: "drop",
349
352
  spaceRange: spaceRange(source, layout),
350
353
  });
@@ -355,8 +358,6 @@ function attachContainers(source: string, layouts: readonly JsxContainer[], comm
355
358
  return [...containers.values()];
356
359
  }
357
360
 
358
- const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
359
-
360
361
  /**
361
362
  * The range `{" "}` would take the place of: a one-line container and the
362
363
  * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
@@ -365,14 +366,12 @@ const isHorizontalSpace = (char: string | undefined): boolean => char === " " ||
365
366
  */
366
367
  function spaceRange(source: string, container: Range): Range | undefined {
367
368
  if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
368
- let lineStart = container.start;
369
- while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1] as string)) lineStart -= 1;
370
369
  let before = container.start;
371
370
  while (isHorizontalSpace(source[before - 1])) before -= 1;
372
371
  let after = container.end;
373
372
  while (isHorizontalSpace(source[after])) after += 1;
374
373
 
375
- if (!isBlank(source.slice(lineStart, container.start))) {
374
+ if (!opensItsLine(source, container)) {
376
375
  return before < container.start ? { start: before, end: container.end } : undefined;
377
376
  }
378
377
  const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
@@ -391,51 +390,67 @@ function skippableRanges(
391
390
  containerOf: ReadonlyMap<Comment, Container>,
392
391
  candidates: ReadonlySet<Comment>,
393
392
  ): Range[] {
394
- 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> = [];
395
407
  for (const comment of comments) {
396
408
  const container = containerOf.get(comment);
397
- if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
398
- ranges.push(comment);
399
- } else if (ranges[ranges.length - 1] !== container) {
400
- ranges.push(container);
401
- }
409
+ if (container === undefined || !merges(container)) units.push(comment);
410
+ else if (units.at(-1) !== container) units.push(container);
402
411
  }
403
- 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));
404
418
  }
405
419
 
406
420
  /**
407
421
  * The edits that remove the comments: one per comment, or one per container
408
- * going with its braces. A holder of a gap between groups of imports leaves an
409
- * empty line in its place.
422
+ * going with its braces. A holder of a gap around the imports (see
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).
410
426
  */
411
427
  function removalEdits(
412
428
  removed: readonly Comment[],
413
429
  containerOf: ReadonlyMap<Comment, Container>,
414
430
  droppable: ReadonlySet<Container>,
415
431
  holders: ReadonlySet<Comment>,
432
+ lineBreaks: ReadonlySet<Comment>,
416
433
  ): Edit[] {
417
- const edits: Edit[] = [];
418
- for (const comment of removed) {
419
- const container = containerOf.get(comment);
420
- if (container === undefined || !droppable.has(container) || container.mode === "keep") {
421
- edits.push({ start: comment.start, end: comment.end, blankLine: holders.has(comment) });
422
- } else if (comment === container.comments[0]) {
423
- edits.push(
424
- container.mode === "drop"
425
- ? { start: container.start, end: container.end, jsx: true }
426
- : { ...(container.spaceRange as Range), replacement: '{" "}' },
427
- );
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) };
428
442
  }
429
- }
430
- return edits;
443
+ return unit.mode === "drop"
444
+ ? { start: unit.start, end: unit.end, jsx: true }
445
+ : { ...(unit.spaceRange as Range), replacement: '{" "}' };
446
+ });
431
447
  }
432
448
 
433
449
  /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
434
450
  function changedSegments(before: readonly string[], after: readonly string[]): Set<number> {
435
- if (before.length !== after.length) throw unexpectedResult();
436
451
  const changed = new Set<number>();
437
- for (const [index, rendering] of before.entries()) {
438
- 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);
439
454
  }
440
455
  return changed;
441
456
  }
@@ -533,15 +548,25 @@ interface ShieldContext {
533
548
  skippable: readonly Range[];
534
549
  /** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
535
550
  preceding: ReadonlyMap<Comment, PrecedingNode>;
551
+ /** Every directive each directive comment carries (see SourceAnalysis). */
552
+ directives: ReadonlyMap<Comment, readonly string[]>;
536
553
  }
537
554
 
538
555
  interface Shielding {
539
556
  removed: Comment[];
540
557
  /**
541
558
  * Lines (1-based) that a surviving directive reaches: the ones below a
542
- * next-line directive, and the one a line directive stands on.
559
+ * next-line directive, and the ones a line directive stands on.
543
560
  */
544
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>;
545
570
  }
546
571
 
547
572
  /**
@@ -550,7 +575,8 @@ interface Shielding {
550
575
  */
551
576
  interface RemovalUnit extends Range {
552
577
  comments: readonly Comment[];
553
- container?: Container;
578
+ /** The unit is a container's braces, which go with the comments in them. */
579
+ braces: boolean;
554
580
  }
555
581
 
556
582
  /**
@@ -559,15 +585,19 @@ interface RemovalUnit extends Range {
559
585
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
560
586
  * following code up, silently changing which line the directive applies to —
561
587
  * and with it the compiler or linter behaviour. Nor may a removal join
562
- * another line to one a directive covers, the line a line directive
563
- * (`eslint-disable-line`, ...) stands on included. Directives whose tools
564
- * reach further get more: below a tsc suppression the first line each
565
- * compiler cannot step over stays, oxlint's line-feed rows add the lines they
566
- * run over, between a directive that only counts as the last comment before
567
- * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
568
- * only applies to the node right after it and that target every comment
569
- * 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
570
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.
571
601
  *
572
602
  * The line checks work on whole lines, not single comments: a line holding
573
603
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
@@ -586,6 +616,7 @@ function shieldNextLineDirectives(
586
616
  const lineOffsets = lineStartOffsets(source);
587
617
  const totalLines = lineOffsets.length;
588
618
  const shieldedLines = new Set<number>();
619
+ const lineBreaks = new Set<Comment>();
589
620
  const blockingLines = new Set<number>();
590
621
  const shieldedGaps: Array<readonly [number, number]> = [];
591
622
  const siblingTargets = new Set<Container>();
@@ -596,7 +627,32 @@ function shieldNextLineDirectives(
596
627
  const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
597
628
  let rows: number[] | undefined;
598
629
  /** The (1-based) line holding the offset. */
599
- 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
+ };
600
656
 
601
657
  const collectShields = (): boolean => {
602
658
  let added = false;
@@ -607,26 +663,8 @@ function shieldNextLineDirectives(
607
663
  added = true;
608
664
  }
609
665
  };
610
- for (const comment of [...kept, ...skipped]) {
611
- if (processed.has(comment)) continue;
612
- processed.add(comment);
613
- const directive = comment.directive;
614
- if (directive === undefined) continue;
615
- if (carriesAstGrepSuppression(comment.text)) {
616
- const reach = astGrepReach(
617
- source,
618
- (rows ??= rowStartOffsets(source)),
619
- comment,
620
- context.preceding.get(comment) as PrecedingNode,
621
- );
622
- astGrepReaches.push(reach);
623
- if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
624
- for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
625
- shieldedLines.add(line);
626
- }
627
- }
628
- added = true;
629
- }
666
+ /** Collects what one of the directives the comment carries reaches. */
667
+ const shieldReach = (comment: Comment, directive: string): void => {
630
668
  const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
631
669
  if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
632
670
  const codeStart = codeStartAfter(source, comment.end, context.skippable);
@@ -634,7 +672,7 @@ function shieldNextLineDirectives(
634
672
  shieldedGaps.push([comment.end, codeStart]);
635
673
  added = true;
636
674
  }
637
- if (lastComment) continue;
675
+ if (lastComment) return;
638
676
  }
639
677
  if (TSC_SUPPRESSIONS.has(directive)) {
640
678
  for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
@@ -648,10 +686,10 @@ function shieldNextLineDirectives(
648
686
  const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
649
687
  if (reach !== undefined) shieldLines(reach.from, reach.to);
650
688
  }
651
- // A line directive covers the line it stands on, which no removal may
652
- // join to another (see joiningLineGroup).
653
- if (isOwnLineDirective(directive)) shieldLines(comment.line, comment.line);
654
- 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;
655
693
  // In JSX, such a directive in braces applies to the sibling after it,
656
694
  // whatever line that is on (prettier leaves the element after
657
695
  // `{/* prettier-ignore */}` as is): a comment-only container standing
@@ -662,58 +700,66 @@ function shieldNextLineDirectives(
662
700
  added = true;
663
701
  }
664
702
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
665
- // `secret(); // nosemgrep`) targets its own line, so it shields nothing
666
- // below it. In JSX, the container holding the comment is what has to
667
- // stand on lines of its own.
668
- 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;
669
709
  // Counted pragmas like `c8 ignore next 3` cover several lines.
670
- 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);
671
730
  }
672
731
  return added;
673
732
  };
674
733
 
734
+ /** Takes the comments, all still being removed, out of the removal and keeps them. */
675
735
  const protect = (group: readonly Comment[]): void => {
736
+ if (group.length === 0) return;
676
737
  const grouped = new Set(group);
677
738
  stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
678
739
  kept.push(...group);
679
740
  units = undefined;
680
741
  };
681
742
 
743
+ /** Protects what the shields collected so far hold in place; true when that kept anything. */
682
744
  const protectShields = (): boolean => {
683
- let changed = false;
745
+ const removing = stillRemoved.length;
684
746
  for (const line of shieldedLines) {
685
- const group =
686
- vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
687
- joiningLineGroup(source, line, lineOffsets, currentUnits());
688
- if (group === undefined) continue;
689
- protect(group);
690
- changed = true;
691
- }
692
- for (const line of blockingLines) {
693
- const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
694
- if (group === undefined) continue;
695
- protect(group);
696
- 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
+ }
697
755
  }
756
+ for (const line of blockingLines) protect(lineOpeningGroup(source, unitsOnLine(line)));
698
757
  for (const [from, to] of shieldedGaps) {
699
- const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
700
- if (group.length === 0) continue;
701
- protect(group);
702
- changed = true;
703
- }
704
- for (const target of siblingTargets) {
705
- const group = stillRemoved.filter((comment) => target.comments.includes(comment));
706
- if (group.length === 0) continue;
707
- protect(group);
708
- changed = true;
758
+ protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
709
759
  }
710
- for (const reach of astGrepReaches) {
711
- const group = currentUnits().filter((unit) => reach.holds(unit));
712
- if (group.length === 0) continue;
713
- protect(group.flatMap((unit) => unit.comments));
714
- changed = true;
715
- }
716
- 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;
717
763
  };
718
764
 
719
765
  collectShields();
@@ -723,7 +769,7 @@ function shieldNextLineDirectives(
723
769
  if (stillRemoved.length !== removed.length) {
724
770
  kept.sort(byStart);
725
771
  }
726
- return { removed: stillRemoved, lines: shieldedLines };
772
+ return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
727
773
  }
728
774
 
729
775
  /**
@@ -733,20 +779,21 @@ function shieldNextLineDirectives(
733
779
  */
734
780
  function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
735
781
  const removing = new Set(removals);
736
- const units: RemovalUnit[] = [];
737
- for (const comment of removals) {
738
- const container = containerOf.get(comment);
739
- if (
740
- container === undefined ||
741
- container.mode !== "drop" ||
742
- !container.comments.every((inside) => removing.has(inside))
743
- ) {
744
- units.push({ start: comment.start, end: comment.end, comments: [comment] });
745
- } else if (units[units.length - 1]?.container !== container) {
746
- units.push({ start: container.start, end: container.end, comments: container.comments, container });
747
- }
748
- }
749
- 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);
750
797
  }
751
798
 
752
799
  /**
@@ -796,7 +843,7 @@ function firstBlockingLines(
796
843
  // (`/* @ts-ignore */ foo();`) does not end the search.
797
844
  const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
798
845
  if (codeStart >= source.length) return [];
799
- const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
846
+ const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
800
847
  const found: number[] = [];
801
848
  for (const passable of TSC_PASSABLE_LINES) {
802
849
  for (let line = comment.endLine + 1; line < codeLine; line += 1) {
@@ -809,80 +856,54 @@ function firstBlockingLines(
809
856
  return found;
810
857
  }
811
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
+
812
864
  /**
813
865
  * The removals on the given line when one of them opens it (holds its first
814
866
  * non-whitespace character), so that deleting them would change how the
815
- * line starts. Returns undefined when something that survives opens the line
816
- * 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.
817
869
  */
818
- function lineOpeningGroup(
819
- source: string,
820
- line: number,
821
- lineOffsets: readonly number[],
822
- units: readonly RemovalUnit[],
823
- ): Comment[] | undefined {
824
- // A blocking line always lies above the code line, so the next offset exists.
825
- const lineStart = lineOffsets[line - 1] as number;
826
- const lineEnd = lineOffsets[line] as number;
827
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
828
- let first = lineStart;
829
- while (first < lineEnd && isBlank(source[first] as string)) first += 1;
830
- return group.some((unit) => unit.start <= first && first < unit.end)
831
- ? group.flatMap((unit) => unit.comments)
832
- : 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) : [];
833
874
  }
834
875
 
835
876
  /**
836
877
  * The removals that would make the given line disappear: every
837
878
  * non-whitespace character of the line lies inside one of them, so deleting
838
- * them deletes the physical line. Returns undefined when the line survives
839
- * (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).
840
881
  */
841
- function vanishingLineGroup(
842
- source: string,
843
- line: number,
844
- lineOffsets: readonly number[],
845
- units: readonly RemovalUnit[],
846
- ): Comment[] | undefined {
847
- // collectShields never shields a line beyond the last one, so the offset exists.
848
- const lineStart = lineOffsets[line - 1] as number;
849
- const lineEnd = lineOffsets[line] ?? source.length;
850
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
851
- if (group.length === 0) return undefined;
852
- 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) {
853
885
  if (isBlank(source[index] as string)) continue;
854
- 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 [];
855
887
  }
856
- return group.flatMap((unit) => unit.comments);
888
+ return commentsOf(line.units);
857
889
  }
858
890
 
859
891
  /**
860
- * The removals that would join the given line to another: they reach past
861
- * one of its ends, with something other than whitespace beyond them on the
862
- * line they run into. Deleting such a comment takes its line breaks along —
863
- * with nothing in their place right after an opening bracket or before
864
- * closing punctuation (see applyEdits) — so what stands beyond it can land on
865
- * the line, or what stands on the line move off it, and a directive covering
866
- * the line would cover other code. (With only whitespace beyond, the lines
867
- * 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.)
868
901
  */
869
- function joiningLineGroup(
870
- source: string,
871
- line: number,
872
- lineOffsets: readonly number[],
873
- units: readonly RemovalUnit[],
874
- ): Comment[] | undefined {
875
- const lineStart = lineOffsets[line - 1] as number;
876
- const nextLineStart = lineOffsets[line];
877
- const lineEnd = nextLineStart ?? source.length;
878
- const group = units.filter(
902
+ function joiningUnits(source: string, line: LineUnits): RemovalUnit[] {
903
+ return line.units.filter(
879
904
  (unit) =>
880
- unit.start < lineEnd &&
881
- unit.end > lineStart &&
882
- ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
883
- (unit.start < lineStart && !opensItsLine(source, unit))),
905
+ (unit.end > line.end && !closesItsLine(source, unit)) || (unit.start < line.start && !opensItsLine(source, unit)),
884
906
  );
885
- return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
886
907
  }
887
908
 
888
909
  // The directives oxlint reads on rows of its own (see oxlintReachLines).
@@ -912,7 +933,7 @@ function oxlintReachLines(
912
933
  comment: Comment,
913
934
  directive: string,
914
935
  ): { from: number; to: number } | undefined {
915
- const rowOf = (offset: number): number => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
936
+ const rowOf = (offset: number): number => lineIndexAt(rows, offset);
916
937
  if (!directive.endsWith("-next-line")) {
917
938
  return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
918
939
  }
@@ -923,13 +944,41 @@ function oxlintReachLines(
923
944
  }
924
945
 
925
946
  /**
926
- * Whether the directive covers the line it stands on: the `-line` forms of
927
- * 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.
928
952
  */
929
- function isOwnLineDirective(name: string): boolean {
930
- 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
+ );
931
959
  }
932
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
+
933
982
  // These suppressions only apply to the following node/line when they stand on
934
983
  // their own line — trailing after code they target (at most) their own line.
935
984
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -941,7 +990,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
941
990
  "lgtm",
942
991
  "codeql",
943
992
  "skipcq",
944
- "nosemgrep",
945
993
  ]);
946
994
 
947
995
  // The suppressions tsc looks past blank and `//` lines for (see
@@ -1064,12 +1112,13 @@ function isNextLineDirective(name: string): boolean {
1064
1112
  const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
1065
1113
 
1066
1114
  /**
1067
- * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
1068
- * 3). monocart reads the count past any whitespace or none, and c8 and
1069
- * 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.
1070
1119
  */
1071
- function shieldedLineCount(comment: Comment): number {
1072
- 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;
1073
1122
  const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
1074
1123
  return match ? Math.max(1, Number(match[1])) : 1;
1075
1124
  }
@@ -1085,19 +1134,22 @@ function occupiesWholeLines(source: string, range: Range): boolean {
1085
1134
 
1086
1135
  /** True when nothing but whitespace follows the range on its last line. */
1087
1136
  function closesItsLine(source: string, range: Range): boolean {
1088
- for (let index = range.end; index < source.length; index += 1) {
1089
- const char = source[index] as string;
1090
- if (LINE_TERMINATOR.test(char)) break;
1091
- if (!isBlank(char)) return false;
1092
- }
1093
- return true;
1137
+ return blankToLineEdge(source, range.end, 1);
1094
1138
  }
1095
1139
 
1096
1140
  /** True when nothing but whitespace precedes the range on its first line. */
1097
1141
  function opensItsLine(source: string, range: Range): boolean {
1098
- 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) {
1099
1151
  const char = source[index] as string;
1100
- if (LINE_TERMINATOR.test(char)) break;
1152
+ if (LINE_TERMINATOR.test(char)) return true;
1101
1153
  if (!isBlank(char)) return false;
1102
1154
  }
1103
1155
  return true;