@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/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
  /**
@@ -31,7 +31,10 @@ import { applyEdits, blankLineEdits } from "./splice.js";
31
31
  * lines already against that edge stay.
32
32
  * - Imports and exports stay in the groups TypeScript's organizeImports and
33
33
  * dprint sort apart: where deleting the comments between two groups would
34
- * leave no empty line between them, an empty line takes their place.
34
+ * leave no empty line between them, an empty line takes their place. So it
35
+ * does where deleting the comments after an import or a require would
36
+ * bring the code after it up to the line below, keeping the empty line the
37
+ * newline-after-import rules of eslint-plugin-import and oxlint want.
35
38
  * - A leading byte-order mark is preserved. Reported positions index into the
36
39
  * `source` string exactly as `scanComments(source)` reports them (a BOM
37
40
  * counts as its first character).
@@ -66,6 +69,7 @@ export function removeComments(source, options = {}) {
66
69
  containerOf,
67
70
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
68
71
  preceding: analysis.preceding,
72
+ directives: analysis.directives,
69
73
  };
70
74
  const search = containerSearch(containers);
71
75
  // Collapsing blank lines can only move a surviving comment to the very
@@ -74,7 +78,8 @@ export function removeComments(source, options = {}) {
74
78
  // without the collapse before any removal is given up.
75
79
  let tidyBlankLines = true;
76
80
  // The removals that leave an empty line in their place, holding two groups
77
- // of imports or exports apart (see gapHolders).
81
+ // of imports or exports apart, or the code after the imports two lines
82
+ // below them (see gapHolders).
78
83
  const holders = new Set();
79
84
  for (;;) {
80
85
  const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
@@ -83,8 +88,8 @@ export function removeComments(source, options = {}) {
83
88
  return { code: source, removed, kept, skipped, changed: false };
84
89
  }
85
90
  const removing = new Set(removed);
86
- search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
87
- 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);
88
93
  const deletions = edits.filter((edit) => edit.replacement === undefined);
89
94
  const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
90
95
  const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
@@ -92,8 +97,9 @@ export function removeComments(source, options = {}) {
92
97
  if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments)))
93
98
  continue;
94
99
  // Where the output joins two groups of imports or exports that a sorter
95
- // sorts apart, a removal between them leaves an empty line in its place,
96
- // and the attempt is repeated.
100
+ // sorts apart, or loses the empty line a linter finds after the imports,
101
+ // a removal in the gap leaves an empty line in its place, and the attempt
102
+ // is repeated.
97
103
  const holding = gapHolders(source, analysis.importGroups, output.importGroups, removed, holders);
98
104
  if (holding.length > 0) {
99
105
  for (const holder of holding)
@@ -128,9 +134,15 @@ export function removeComments(source, options = {}) {
128
134
  function unexpectedResult() {
129
135
  return new Error("comment removal produced an unexpected result; refusing to continue");
130
136
  }
131
- /** Sort comparator for document order: ranges are ordered by where they start. */
132
- function byStart(a, b) {
133
- 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]]);
134
146
  }
135
147
  /**
136
148
  * Compares the spliced output's comments against the comments meant to
@@ -144,12 +156,7 @@ function byStart(a, b) {
144
156
  * must not hide behind the unchanged name.
145
157
  */
146
158
  function firstChangedSurvivor(source, code, actual, kept, skipped) {
147
- const expected = [...kept, ...skipped].sort(byStart);
148
- if (actual.length !== expected.length) {
149
- throw unexpectedResult();
150
- }
151
- for (const [index, comment] of expected.entries()) {
152
- const survivor = actual[index];
159
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), actual)) {
153
160
  if (survivor.text !== comment.text) {
154
161
  throw unexpectedResult();
155
162
  }
@@ -193,25 +200,26 @@ function closestRemovalBefore(removals, comment) {
193
200
  /**
194
201
  * The removals that are to leave an empty line in their place (see
195
202
  * Edit.blankLine), so that the output starts every group of imports and
196
- * exports the source starts. The gaps belong to the same declarations in
197
- * both texts, since removing comments leaves the statements as they are.
198
- * Where the output joins two groups at a gap, the deleted comments took the
199
- * gap's line breaks along, and the last of them that held any — one spanning
200
- * lines, or one on a line below the token the gap follows — leaves an empty
201
- * line, which starts a group for both sorters. A group the output starts
202
- * where the source starts none can stand, for both parts of a sorted group
203
- * are sorted.
203
+ * exports the source starts, and keeps every empty line the linters find
204
+ * after its imports. The gaps belong to the same statements in both texts,
205
+ * since removing comments leaves the statements as they are. Where the
206
+ * output joins two groups at a gap, or a linter no longer finds its empty
207
+ * line there, the deleted comments took the gap's line breaks along, and the
208
+ * last of them that held any — one spanning lines, or one on a line below
209
+ * the token the gap follows — leaves an empty line, which starts a group for
210
+ * both sorters and is the empty line both linters want. A group the output
211
+ * starts where the source starts none can stand, for both parts of a sorted
212
+ * group are sorted, and so can an empty line only the output has.
204
213
  */
205
214
  function gapHolders(source, before, after, removals, holders) {
206
- if (before.length !== after.length)
207
- throw unexpectedResult();
208
215
  const found = [];
209
- for (const [index, gap] of before.entries()) {
210
- const splits = after[index].splits;
211
- if (gap.splits.every((sorter) => splits.includes(sorter)))
216
+ for (const [gap, { splits, spaced }] of corresponding(before, after)) {
217
+ const intact = gap.splits.every((sorter) => splits.includes(sorter)) && gap.spaced.every((linter) => spaced.includes(linter));
218
+ if (intact)
212
219
  continue;
213
220
  const inGap = removals.filter((comment) => comment.start >= gap.start && comment.end <= gap.end);
214
- // An empty line in the gap already starts a group for both sorters.
221
+ // An empty line in the gap already starts a group for both sorters and
222
+ // is the empty line both linters want.
215
223
  if (inGap.some((comment) => holders.has(comment)))
216
224
  throw unexpectedResult();
217
225
  const holder = inGap.findLast((comment) => comment.endLine > comment.line || LINE_TERMINATOR.test(source.slice(gap.start, comment.start)));
@@ -248,19 +256,13 @@ function isProtected(comment, options) {
248
256
  function attachContainers(source, layouts, comments) {
249
257
  const containers = new Map();
250
258
  for (const layout of layouts) {
251
- const inside = [];
252
- let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
253
- for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
254
- inside.push(comment);
255
- index += 1;
256
- }
257
259
  containers.set(layout, {
258
260
  start: layout.start,
259
261
  end: layout.end,
260
262
  segment: layout.segment,
261
263
  suspect: layout.suspect,
262
264
  next: undefined,
263
- comments: inside,
265
+ comments: rangesWithin(comments, layout),
264
266
  mode: "drop",
265
267
  spaceRange: spaceRange(source, layout),
266
268
  });
@@ -271,7 +273,6 @@ function attachContainers(source, layouts, comments) {
271
273
  }
272
274
  return [...containers.values()];
273
275
  }
274
- const isHorizontalSpace = (char) => char === " " || char === "\t";
275
276
  /**
276
277
  * The range `{" "}` would take the place of: a one-line container and the
277
278
  * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
@@ -281,16 +282,13 @@ const isHorizontalSpace = (char) => char === " " || char === "\t";
281
282
  function spaceRange(source, container) {
282
283
  if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
283
284
  return undefined;
284
- let lineStart = container.start;
285
- while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1]))
286
- lineStart -= 1;
287
285
  let before = container.start;
288
286
  while (isHorizontalSpace(source[before - 1]))
289
287
  before -= 1;
290
288
  let after = container.end;
291
289
  while (isHorizontalSpace(source[after]))
292
290
  after += 1;
293
- if (!isBlank(source.slice(lineStart, container.start))) {
291
+ if (!opensItsLine(source, container)) {
294
292
  return before < container.start ? { start: before, end: container.end } : undefined;
295
293
  }
296
294
  const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
@@ -304,45 +302,51 @@ function spaceRange(source, container) {
304
302
  * the found code further down, which shields more, not less.
305
303
  */
306
304
  function skippableRanges(comments, containerOf, candidates) {
307
- 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 = [];
308
314
  for (const comment of comments) {
309
315
  const container = containerOf.get(comment);
310
- if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
311
- ranges.push(comment);
312
- }
313
- else if (ranges[ranges.length - 1] !== container) {
314
- ranges.push(container);
315
- }
316
+ if (container === undefined || !merges(container))
317
+ units.push(comment);
318
+ else if (units.at(-1) !== container)
319
+ units.push(container);
316
320
  }
317
- 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));
318
326
  }
319
327
  /**
320
328
  * The edits that remove the comments: one per comment, or one per container
321
- * going with its braces. A holder of a gap between groups of imports leaves an
322
- * empty line in its place.
329
+ * going with its braces. A holder of a gap around the imports (see
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).
323
333
  */
324
- function removalEdits(removed, containerOf, droppable, holders) {
325
- const edits = [];
326
- for (const comment of removed) {
327
- const container = containerOf.get(comment);
328
- if (container === undefined || !droppable.has(container) || container.mode === "keep") {
329
- edits.push({ start: comment.start, end: comment.end, blankLine: holders.has(comment) });
330
- }
331
- else if (comment === container.comments[0]) {
332
- edits.push(container.mode === "drop"
333
- ? { start: container.start, end: container.end, jsx: true }
334
- : { ...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) };
335
339
  }
336
- }
337
- return edits;
340
+ return unit.mode === "drop"
341
+ ? { start: unit.start, end: unit.end, jsx: true }
342
+ : { ...unit.spaceRange, replacement: '{" "}' };
343
+ });
338
344
  }
339
345
  /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
340
346
  function changedSegments(before, after) {
341
- if (before.length !== after.length)
342
- throw unexpectedResult();
343
347
  const changed = new Set();
344
- for (const [index, rendering] of before.entries()) {
345
- if (rendering !== after[index])
348
+ for (const [index, [rendering, now]] of corresponding(before, after).entries()) {
349
+ if (rendering !== now)
346
350
  changed.add(index);
347
351
  }
348
352
  return changed;
@@ -432,15 +436,19 @@ function containerSearch(containers) {
432
436
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
433
437
  * following code up, silently changing which line the directive applies to —
434
438
  * and with it the compiler or linter behaviour. Nor may a removal join
435
- * another line to one a directive covers, the line a line directive
436
- * (`eslint-disable-line`, ...) stands on included. Directives whose tools
437
- * reach further get more: below a tsc suppression the first line each
438
- * compiler cannot step over stays, oxlint's line-feed rows add the lines they
439
- * run over, between a directive that only counts as the last comment before
440
- * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
441
- * only applies to the node right after it and that target every comment
442
- * 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
443
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.
444
452
  *
445
453
  * The line checks work on whole lines, not single comments: a line holding
446
454
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
@@ -453,6 +461,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
453
461
  const lineOffsets = lineStartOffsets(source);
454
462
  const totalLines = lineOffsets.length;
455
463
  const shieldedLines = new Set();
464
+ const lineBreaks = new Set();
456
465
  const blockingLines = new Set();
457
466
  const shieldedGaps = [];
458
467
  const siblingTargets = new Set();
@@ -463,7 +472,34 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
463
472
  const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
464
473
  let rows;
465
474
  /** The (1-based) line holding the offset. */
466
- 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
+ };
467
503
  const collectShields = () => {
468
504
  let added = false;
469
505
  const shieldLines = (from, to) => {
@@ -474,23 +510,8 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
474
510
  added = true;
475
511
  }
476
512
  };
477
- for (const comment of [...kept, ...skipped]) {
478
- if (processed.has(comment))
479
- continue;
480
- processed.add(comment);
481
- const directive = comment.directive;
482
- if (directive === undefined)
483
- continue;
484
- if (carriesAstGrepSuppression(comment.text)) {
485
- const reach = astGrepReach(source, (rows ??= rowStartOffsets(source)), comment, context.preceding.get(comment));
486
- astGrepReaches.push(reach);
487
- if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
488
- for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
489
- shieldedLines.add(line);
490
- }
491
- }
492
- added = true;
493
- }
513
+ /** Collects what one of the directives the comment carries reaches. */
514
+ const shieldReach = (comment, directive) => {
494
515
  const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
495
516
  if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
496
517
  const codeStart = codeStartAfter(source, comment.end, context.skippable);
@@ -499,7 +520,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
499
520
  added = true;
500
521
  }
501
522
  if (lastComment)
502
- continue;
523
+ return;
503
524
  }
504
525
  if (TSC_SUPPRESSIONS.has(directive)) {
505
526
  for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
@@ -515,12 +536,12 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
515
536
  if (reach !== undefined)
516
537
  shieldLines(reach.from, reach.to);
517
538
  }
518
- // A line directive covers the line it stands on, which no removal may
519
- // join to another (see joiningLineGroup).
520
- if (isOwnLineDirective(directive))
521
- 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);
522
543
  if (!isNextLineDirective(directive))
523
- continue;
544
+ return;
524
545
  // In JSX, such a directive in braces applies to the sibling after it,
525
546
  // whatever line that is on (prettier leaves the element after
526
547
  // `{/* prettier-ignore */}` as is): a comment-only container standing
@@ -531,61 +552,70 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
531
552
  added = true;
532
553
  }
533
554
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
534
- // `secret(); // nosemgrep`) targets its own line, so it shields nothing
535
- // below it. In JSX, the container holding the comment is what has to
536
- // 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.
537
559
  if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment))
538
- continue;
560
+ return;
561
+ if (directive === "nosemgrep" && !readsAsSemgrepPreviousLine(comment))
562
+ return;
539
563
  // Counted pragmas like `c8 ignore next 3` cover several lines.
540
- 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);
541
582
  }
542
583
  return added;
543
584
  };
585
+ /** Takes the comments, all still being removed, out of the removal and keeps them. */
544
586
  const protect = (group) => {
587
+ if (group.length === 0)
588
+ return;
545
589
  const grouped = new Set(group);
546
590
  stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
547
591
  kept.push(...group);
548
592
  units = undefined;
549
593
  };
594
+ /** Protects what the shields collected so far hold in place; true when that kept anything. */
550
595
  const protectShields = () => {
551
- let changed = false;
596
+ const removing = stillRemoved.length;
552
597
  for (const line of shieldedLines) {
553
- const group = vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
554
- joiningLineGroup(source, line, lineOffsets, currentUnits());
555
- if (group === undefined)
556
- continue;
557
- protect(group);
558
- changed = true;
559
- }
560
- for (const line of blockingLines) {
561
- const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
562
- if (group === undefined)
563
- continue;
564
- protect(group);
565
- 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
+ }
566
608
  }
609
+ for (const line of blockingLines)
610
+ protect(lineOpeningGroup(source, unitsOnLine(line)));
567
611
  for (const [from, to] of shieldedGaps) {
568
- const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
569
- if (group.length === 0)
570
- continue;
571
- protect(group);
572
- changed = true;
612
+ protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
573
613
  }
574
- for (const target of siblingTargets) {
575
- const group = stillRemoved.filter((comment) => target.comments.includes(comment));
576
- if (group.length === 0)
577
- continue;
578
- protect(group);
579
- changed = true;
580
- }
581
- for (const reach of astGrepReaches) {
582
- const group = currentUnits().filter((unit) => reach.holds(unit));
583
- if (group.length === 0)
584
- continue;
585
- protect(group.flatMap((unit) => unit.comments));
586
- changed = true;
587
- }
588
- 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;
589
619
  };
590
620
  collectShields();
591
621
  while (protectShields()) {
@@ -595,7 +625,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
595
625
  if (stillRemoved.length !== removed.length) {
596
626
  kept.sort(byStart);
597
627
  }
598
- return { removed: stillRemoved, lines: shieldedLines };
628
+ return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
599
629
  }
600
630
  /**
601
631
  * The removals as the edits delete them: a comment-only container whose
@@ -604,19 +634,16 @@ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
604
634
  */
605
635
  function removalUnits(removals, containerOf) {
606
636
  const removing = new Set(removals);
607
- const units = [];
608
- for (const comment of removals) {
609
- const container = containerOf.get(comment);
610
- if (container === undefined ||
611
- container.mode !== "drop" ||
612
- !container.comments.every((inside) => removing.has(inside))) {
613
- units.push({ start: comment.start, end: comment.end, comments: [comment] });
614
- }
615
- else if (units[units.length - 1]?.container !== container) {
616
- units.push({ start: container.start, end: container.end, comments: container.comments, container });
617
- }
618
- }
619
- 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);
620
647
  }
621
648
  /**
622
649
  * Offset of the first character at or after `from` that is neither
@@ -662,7 +689,7 @@ function firstBlockingLines(source, comment, lineOffsets, ranges) {
662
689
  const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
663
690
  if (codeStart >= source.length)
664
691
  return [];
665
- const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
692
+ const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
666
693
  const found = [];
667
694
  for (const passable of TSC_PASSABLE_LINES) {
668
695
  for (let line = comment.endLine + 1; line < codeLine; line += 1) {
@@ -677,61 +704,45 @@ function firstBlockingLines(source, comment, lineOffsets, ranges) {
677
704
  /**
678
705
  * The removals on the given line when one of them opens it (holds its first
679
706
  * non-whitespace character), so that deleting them would change how the
680
- * line starts. Returns undefined when something that survives opens the line
681
- * 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.
682
709
  */
683
- function lineOpeningGroup(source, line, lineOffsets, units) {
684
- // A blocking line always lies above the code line, so the next offset exists.
685
- const lineStart = lineOffsets[line - 1];
686
- const lineEnd = lineOffsets[line];
687
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
688
- let first = lineStart;
689
- while (first < lineEnd && isBlank(source[first]))
710
+ function lineOpeningGroup(source, line) {
711
+ let first = line.start;
712
+ while (first < line.end && isBlank(source[first]))
690
713
  first += 1;
691
- return group.some((unit) => unit.start <= first && first < unit.end)
692
- ? group.flatMap((unit) => unit.comments)
693
- : undefined;
714
+ return line.units.some((unit) => unit.start <= first && first < unit.end) ? commentsOf(line.units) : [];
694
715
  }
695
716
  /**
696
717
  * The removals that would make the given line disappear: every
697
718
  * non-whitespace character of the line lies inside one of them, so deleting
698
- * them deletes the physical line. Returns undefined when the line survives
699
- * (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).
700
721
  */
701
- function vanishingLineGroup(source, line, lineOffsets, units) {
702
- // collectShields never shields a line beyond the last one, so the offset exists.
703
- const lineStart = lineOffsets[line - 1];
704
- const lineEnd = lineOffsets[line] ?? source.length;
705
- const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
706
- if (group.length === 0)
707
- return undefined;
708
- 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) {
709
726
  if (isBlank(source[index]))
710
727
  continue;
711
- if (!group.some((unit) => unit.start <= index && index < unit.end))
712
- return undefined;
728
+ if (!line.units.some((unit) => unit.start <= index && index < unit.end))
729
+ return [];
713
730
  }
714
- return group.flatMap((unit) => unit.comments);
731
+ return commentsOf(line.units);
715
732
  }
716
733
  /**
717
- * The removals that would join the given line to another: they reach past
718
- * one of its ends, with something other than whitespace beyond them on the
719
- * line they run into. Deleting such a comment takes its line breaks along —
720
- * with nothing in their place right after an opening bracket or before
721
- * closing punctuation (see applyEdits) — so what stands beyond it can land on
722
- * the line, or what stands on the line move off it, and a directive covering
723
- * the line would cover other code. (With only whitespace beyond, the lines
724
- * 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.)
725
743
  */
726
- function joiningLineGroup(source, line, lineOffsets, units) {
727
- const lineStart = lineOffsets[line - 1];
728
- const nextLineStart = lineOffsets[line];
729
- const lineEnd = nextLineStart ?? source.length;
730
- const group = units.filter((unit) => unit.start < lineEnd &&
731
- unit.end > lineStart &&
732
- ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
733
- (unit.start < lineStart && !opensItsLine(source, unit))));
734
- 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)));
735
746
  }
736
747
  // The directives oxlint reads on rows of its own (see oxlintReachLines).
737
748
  const OXLINT_ROW_DIRECTIVES = new Set([
@@ -753,7 +764,7 @@ const OXLINT_ROW_DIRECTIVES = new Set([
753
764
  * rest of the file, which no removal changes.
754
765
  */
755
766
  function oxlintReachLines(source, rows, lineOf, comment, directive) {
756
- const rowOf = (offset) => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
767
+ const rowOf = (offset) => lineIndexAt(rows, offset);
757
768
  if (!directive.endsWith("-next-line")) {
758
769
  return { from: lineOf(rows[rowOf(comment.start)]), to: comment.line };
759
770
  }
@@ -764,12 +775,36 @@ function oxlintReachLines(source, rows, lineOf, comment, directive) {
764
775
  return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
765
776
  }
766
777
  /**
767
- * Whether the directive covers the line it stands on: the `-line` forms of
768
- * 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.
769
783
  */
770
- function isOwnLineDirective(name) {
771
- 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));
772
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;
773
808
  // These suppressions only apply to the following node/line when they stand on
774
809
  // their own line — trailing after code they target (at most) their own line.
775
810
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -781,7 +816,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
781
816
  "lgtm",
782
817
  "codeql",
783
818
  "skipcq",
784
- "nosemgrep",
785
819
  ]);
786
820
  // The suppressions tsc looks past blank and `//` lines for (see
787
821
  // firstBlockingLines). They stay next-line directives as well: Flow, which
@@ -896,12 +930,13 @@ function isNextLineDirective(name) {
896
930
  // reason text.
897
931
  const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
898
932
  /**
899
- * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
900
- * 3). monocart reads the count past any whitespace or none, and c8 and
901
- * 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.
902
937
  */
903
- function shieldedLineCount(comment) {
904
- if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive))
938
+ function shieldedLineCount(comment, directive) {
939
+ if (!COUNTED_NEXT_LINE.has(directive))
905
940
  return 1;
906
941
  const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
907
942
  return match ? Math.max(1, Number(match[1])) : 1;
@@ -916,21 +951,21 @@ function occupiesWholeLines(source, range) {
916
951
  }
917
952
  /** True when nothing but whitespace follows the range on its last line. */
918
953
  function closesItsLine(source, range) {
919
- for (let index = range.end; index < source.length; index += 1) {
920
- const char = source[index];
921
- if (LINE_TERMINATOR.test(char))
922
- break;
923
- if (!isBlank(char))
924
- return false;
925
- }
926
- return true;
954
+ return blankToLineEdge(source, range.end, 1);
927
955
  }
928
956
  /** True when nothing but whitespace precedes the range on its first line. */
929
957
  function opensItsLine(source, range) {
930
- 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) {
931
966
  const char = source[index];
932
967
  if (LINE_TERMINATOR.test(char))
933
- break;
968
+ return true;
934
969
  if (!isBlank(char))
935
970
  return false;
936
971
  }