@kongyo2/ts-comment-scanner 1.21.0 → 1.22.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 (61) hide show
  1. package/README.md +240 -52
  2. package/dist/astgrep.d.ts +4 -2
  3. package/dist/astgrep.d.ts.map +1 -1
  4. package/dist/astgrep.js +7 -4
  5. package/dist/astgrep.js.map +1 -1
  6. package/dist/containers.d.ts +48 -0
  7. package/dist/containers.d.ts.map +1 -0
  8. package/dist/containers.js +82 -0
  9. package/dist/containers.js.map +1 -0
  10. package/dist/coverage.d.ts +45 -0
  11. package/dist/coverage.d.ts.map +1 -0
  12. package/dist/coverage.js +92 -0
  13. package/dist/coverage.js.map +1 -0
  14. package/dist/directives.d.ts +13 -3
  15. package/dist/directives.d.ts.map +1 -1
  16. package/dist/directives.js +6 -5
  17. package/dist/directives.js.map +1 -1
  18. package/dist/files.d.ts +5 -0
  19. package/dist/files.d.ts.map +1 -1
  20. package/dist/files.js +103 -41
  21. package/dist/files.js.map +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/lines.d.ts +11 -0
  27. package/dist/lines.d.ts.map +1 -1
  28. package/dist/lines.js +30 -0
  29. package/dist/lines.js.map +1 -1
  30. package/dist/remove.d.ts +16 -12
  31. package/dist/remove.d.ts.map +1 -1
  32. package/dist/remove.js +51 -638
  33. package/dist/remove.js.map +1 -1
  34. package/dist/run.d.ts.map +1 -1
  35. package/dist/run.js +11 -10
  36. package/dist/run.js.map +1 -1
  37. package/dist/scanner.d.ts +29 -0
  38. package/dist/scanner.d.ts.map +1 -1
  39. package/dist/scanner.js +122 -5
  40. package/dist/scanner.js.map +1 -1
  41. package/dist/shields.d.ts +72 -0
  42. package/dist/shields.d.ts.map +1 -0
  43. package/dist/shields.js +624 -0
  44. package/dist/shields.js.map +1 -0
  45. package/dist/splice.d.ts +40 -15
  46. package/dist/splice.d.ts.map +1 -1
  47. package/dist/splice.js +185 -69
  48. package/dist/splice.js.map +1 -1
  49. package/package.json +2 -1
  50. package/src/astgrep.ts +7 -5
  51. package/src/containers.ts +128 -0
  52. package/src/coverage.ts +110 -0
  53. package/src/directives.ts +16 -5
  54. package/src/files.ts +147 -53
  55. package/src/index.ts +1 -0
  56. package/src/lines.ts +33 -1
  57. package/src/remove.ts +58 -748
  58. package/src/run.ts +11 -10
  59. package/src/scanner.ts +160 -5
  60. package/src/shields.ts +701 -0
  61. package/src/splice.ts +216 -75
package/dist/remove.js CHANGED
@@ -1,9 +1,11 @@
1
- import { astGrepReach, isFileWidePosition, rowStartOffsets } from "./astgrep.js";
2
- import { activePositionalDirectives, bunMarkerOffset, carriesAstGrepSuppression, isLegalComment, } from "./directives.js";
3
- import { LINE_TERMINATOR, isBlank, isHorizontalSpace, lastLine, lineIndexAt, lineStartOffsets } from "./lines.js";
4
- import { byStart, firstIndexAtOrAfter, rangesWithin } from "./ranges.js";
1
+ import { isFileWidePosition } from "./astgrep.js";
2
+ import { attachContainers, emptiedBy, mergeContainers } from "./containers.js";
3
+ import { activePositionalDirectives, bunMarkerOffset, isLegalComment } from "./directives.js";
4
+ import { LINE_TERMINATOR, isBlank } from "./lines.js";
5
+ import { byStart } from "./ranges.js";
5
6
  import { analyzeSource } from "./scanner.js";
6
- import { applyEdits, blankLineEdits } from "./splice.js";
7
+ import { shieldDirectives } from "./shields.js";
8
+ import { applyEdits, blankLineEdits, heldLines } from "./splice.js";
7
9
  /**
8
10
  * Removes comments from TypeScript source without touching code.
9
11
  *
@@ -11,11 +13,16 @@ import { applyEdits, blankLineEdits } from "./splice.js";
11
13
  * - AST-based ranges: strings, template literals and regexes are never affected.
12
14
  * - Directives (`@ts-expect-error`, `eslint-disable`, ...) and license headers are
13
15
  * kept by default, since deleting them changes build/lint behaviour.
14
- * - Surviving comments keep their directive semantics: when deleting a leading
15
- * comment would newly activate a position-dependent directive in a kept
16
- * comment (prettier's `@format` docblock pragma counts only as the file's
17
- * first comment, Bun's `// @bun` only at the very start or right after a
18
- * shebang line), the leading comment is kept instead.
16
+ * - Surviving comments keep their directive semantics. The lines a directive
17
+ * covers stay lines of their own: where deleting the comments on one would
18
+ * empty it, an empty line stays in its place, so that the code below moves
19
+ * up under no next-line directive or counted coverage hint, and where it
20
+ * would join one to another line, the comment's own line breaks stay. A
21
+ * comment stays only where a tool reads the directive with it: one between
22
+ * a directive and the code it would otherwise apply to, or one whose
23
+ * removal would newly activate a position-dependent directive (prettier's
24
+ * `@format` docblock pragma counts only as the file's first comment, Bun's
25
+ * `// @bun` only at the very start or right after a shebang line).
19
26
  * - A space is inserted where removing a block comment would merge two tokens,
20
27
  * and a line break is kept where the comment acted as one (ASI stays intact).
21
28
  * - A JSX comment goes with its braces (`{/* note *​/}`), unless that would
@@ -42,8 +49,8 @@ import { applyEdits, blankLineEdits } from "./splice.js";
42
49
  * set, an error is thrown instead of returning corrupted output.
43
50
  */
44
51
  export function removeComments(source, options = {}) {
45
- const jsx = options.jsx === true;
46
- const analysis = analyzeSource(source, { jsx });
52
+ const scan = { jsx: options.jsx === true, javascript: options.javascript === true };
53
+ const analysis = analyzeSource(source, scan);
47
54
  const comments = analysis.comments;
48
55
  let removed = [];
49
56
  const kept = [];
@@ -66,10 +73,13 @@ export function removeComments(source, options = {}) {
66
73
  containerOf.set(comment, container);
67
74
  }
68
75
  const context = {
76
+ comments,
69
77
  containerOf,
70
78
  skippable: skippableRanges(comments, containerOf, new Set(removed)),
71
79
  preceding: analysis.preceding,
72
80
  directives: analysis.directives,
81
+ shadows: analysis.shadows,
82
+ casts: analysis.casts,
73
83
  };
74
84
  const search = containerSearch(containers);
75
85
  // Collapsing blank lines can only move a surviving comment to the very
@@ -82,18 +92,22 @@ export function removeComments(source, options = {}) {
82
92
  // below them (see gapHolders).
83
93
  const holders = new Set();
84
94
  for (;;) {
85
- const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
95
+ const shielding = shieldDirectives(source, removed, kept, skipped, context);
86
96
  removed = shielding.removed;
87
97
  if (removed.length === 0) {
88
98
  return { code: source, removed, kept, skipped, changed: false };
89
99
  }
90
100
  const removing = new Set(removed);
91
101
  search.track(new Set(containers.filter((container) => emptiedBy(container, removing))));
92
- const edits = removalEdits(removed, containerOf, search.droppable, holders, shielding.lineBreaks);
102
+ const edits = removalEdits(removed, containerOf, search.droppable, holders);
93
103
  const deletions = edits.filter((edit) => edit.replacement === undefined);
94
- const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
95
- const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
96
- const output = analyzeSource(code, { jsx });
104
+ const lines = {
105
+ shielded: shielding.lines,
106
+ held: heldLines(source, deletions, comments, shielding.lines),
107
+ };
108
+ const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, lines, analysis.jsx) : [];
109
+ const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart), lines);
110
+ const output = analyzeSource(code, scan);
97
111
  if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments)))
98
112
  continue;
99
113
  // Where the output joins two groups of imports or exports that a sorter
@@ -106,7 +120,7 @@ export function removeComments(source, options = {}) {
106
120
  holders.add(holder);
107
121
  continue;
108
122
  }
109
- const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
123
+ const survivor = firstChangedSurvivor(source, analysis, code, output, kept, skipped);
110
124
  if (survivor === undefined) {
111
125
  return { code, removed, kept, skipped, changed: true };
112
126
  }
@@ -116,7 +130,9 @@ export function removeComments(source, options = {}) {
116
130
  }
117
131
  // A surviving comment changed meaning: with everything before it deleted
118
132
  // it became the file's first comment (or its very first bytes), turning a
119
- // previously inert positional directive live. Re-protect the removal
133
+ // previously inert positional directive live — or, in JavaScript, a
134
+ // multi-line comment in front of it went for a line break, after which
135
+ // tsc reads it as the JSDoc of the code behind it. Re-protect the removal
120
136
  // candidate closest before it and try again: that comment then keeps
121
137
  // holding the survivor away from the activating position, exactly as the
122
138
  // input did. (Looping, because the re-protected comment can itself be a
@@ -149,18 +165,20 @@ function corresponding(before, after) {
149
165
  * survive, in document order (removal never reorders them). Throws when the
150
166
  * texts themselves differ (the output would be corrupted) and returns the
151
167
  * first surviving comment whose directive semantics changed, or undefined
152
- * when the output is exactly as expected. Besides the canonical directive
153
- * name, the position-dependent pragmas are compared on their own: a comment
168
+ * when the output is exactly as expected. Every directive a comment carries
169
+ * is compared, and what tsc reads from it as JSDoc in JavaScript. The
170
+ * position-dependent pragmas are compared on their own besides: a comment
154
171
  * can carry an inert `@format` underneath the directive an earlier rule
155
- * already named (an eslint config block, say), and that pragma going live
156
- * must not hide behind the unchanged name.
172
+ * already named (an eslint config block, say), and where an ast-grep
173
+ * suppression covers the file does not show in its name.
157
174
  */
158
- function firstChangedSurvivor(source, code, actual, kept, skipped) {
159
- for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), actual)) {
175
+ function firstChangedSurvivor(source, analysis, code, output, kept, skipped) {
176
+ const carried = (of, comment) => JSON.stringify([of.directives.get(comment) ?? [], of.typed.get(comment) ?? []]);
177
+ for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), output.comments)) {
160
178
  if (survivor.text !== comment.text) {
161
179
  throw unexpectedResult();
162
180
  }
163
- if (survivor.directive !== comment.directive)
181
+ if (carried(output, survivor) !== carried(analysis, comment))
164
182
  return comment;
165
183
  const before = activePositionalDirectives(comment.kind, comment.text, positionalPlacement(source, comment));
166
184
  const after = activePositionalDirectives(survivor.kind, survivor.text, positionalPlacement(code, survivor));
@@ -234,15 +252,15 @@ function gapHolders(source, before, after, removals, holders) {
234
252
  * comment-only first line took it along: the mark is an encoding artifact,
235
253
  * not line content.
236
254
  */
237
- function editKeepingBom(source, edits) {
255
+ function editKeepingBom(source, edits, lines) {
238
256
  if (!source.startsWith(""))
239
- return applyEdits(source, edits);
257
+ return applyEdits(source, edits, lines);
240
258
  // The editing itself works on the BOM-less body (with ranges shifted to
241
259
  // match) so its line handling never sees the mark, then the mark is
242
260
  // re-attached; this keeps `// gone\n...` from losing the mark with
243
261
  // the dropped line.
244
262
  const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
245
- return `${applyEdits(source.slice(1), shifted)}`;
263
+ return `${applyEdits(source.slice(1), shifted, lines)}`;
246
264
  }
247
265
  function isProtected(comment, options) {
248
266
  if (options.keep?.(comment) === true)
@@ -253,47 +271,6 @@ function isProtected(comment, options) {
253
271
  return true;
254
272
  return false;
255
273
  }
256
- function attachContainers(source, layouts, comments) {
257
- const containers = new Map();
258
- for (const layout of layouts) {
259
- containers.set(layout, {
260
- start: layout.start,
261
- end: layout.end,
262
- segment: layout.segment,
263
- suspect: layout.suspect,
264
- next: undefined,
265
- comments: rangesWithin(comments, layout),
266
- mode: "drop",
267
- spaceRange: spaceRange(source, layout),
268
- });
269
- }
270
- for (const [layout, container] of containers) {
271
- if (layout.next !== undefined)
272
- container.next = containers.get(layout.next);
273
- }
274
- return [...containers.values()];
275
- }
276
- /**
277
- * The range `{" "}` would take the place of: a one-line container and the
278
- * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
279
- * the spaces after it when it opens its line and code follows. Undefined when
280
- * no spaces hold it off anything on its line.
281
- */
282
- function spaceRange(source, container) {
283
- if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
284
- return undefined;
285
- let before = container.start;
286
- while (isHorizontalSpace(source[before - 1]))
287
- before -= 1;
288
- let after = container.end;
289
- while (isHorizontalSpace(source[after]))
290
- after += 1;
291
- if (!opensItsLine(source, container)) {
292
- return before < container.start ? { start: before, end: container.end } : undefined;
293
- }
294
- const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
295
- return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
296
- }
297
274
  /**
298
275
  * Comments, and the comment-only containers that may go along with theirs,
299
276
  * as sorted ranges: what the search for the code after a directive skips.
@@ -304,39 +281,16 @@ function spaceRange(source, container) {
304
281
  function skippableRanges(comments, containerOf, candidates) {
305
282
  return mergeContainers(comments, containerOf, (container) => emptiedBy(container, candidates));
306
283
  }
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 = [];
314
- for (const comment of comments) {
315
- const container = containerOf.get(comment);
316
- if (container === undefined || !merges(container))
317
- units.push(comment);
318
- else if (units.at(-1) !== container)
319
- units.push(container);
320
- }
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));
326
- }
327
284
  /**
328
285
  * The edits that remove the comments: one per comment, or one per container
329
286
  * 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).
287
+ * gapHolders) leaves an empty line in its place.
333
288
  */
334
- function removalEdits(removed, containerOf, droppable, holders, lineBreaks) {
289
+ function removalEdits(removed, containerOf, droppable, holders) {
335
290
  const units = mergeContainers(removed, containerOf, (container) => droppable.has(container) && container.mode !== "keep");
336
291
  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) };
339
- }
292
+ if (!("comments" in unit))
293
+ return { start: unit.start, end: unit.end, blankLine: holders.has(unit) };
340
294
  return unit.mode === "drop"
341
295
  ? { start: unit.start, end: unit.end, jsx: true }
342
296
  : { ...unit.spaceRange, replacement: '{" "}' };
@@ -430,545 +384,4 @@ function containerSearch(containers) {
430
384
  },
431
385
  };
432
386
  }
433
- /**
434
- * Re-protects comments whose removal would delete a physical line sitting
435
- * under a surviving next-line directive (`@ts-expect-error`,
436
- * `eslint-disable-next-line`, ...). Dropping such a line would shift the
437
- * following code up, silently changing which line the directive applies to —
438
- * and with it the compiler or linter behaviour. Nor may a removal join
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
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.
452
- *
453
- * The line checks work on whole lines, not single comments: a line holding
454
- * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
455
- * so every comment on it has to stay — and a comment-only JSX container takes
456
- * its braces along. Runs to a fixpoint because a re-protected comment can
457
- * itself be a next-line directive shielding further lines (when directives
458
- * are being removed selectively).
459
- */
460
- function shieldNextLineDirectives(source, removed, kept, skipped, context) {
461
- const lineOffsets = lineStartOffsets(source);
462
- const totalLines = lineOffsets.length;
463
- const shieldedLines = new Set();
464
- const lineBreaks = new Set();
465
- const blockingLines = new Set();
466
- const shieldedGaps = [];
467
- const siblingTargets = new Set();
468
- const astGrepReaches = [];
469
- const processed = new Set();
470
- let stillRemoved = removed;
471
- let units;
472
- const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
473
- let rows;
474
- /** The (1-based) line holding the offset. */
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
- };
503
- const collectShields = () => {
504
- let added = false;
505
- const shieldLines = (from, to) => {
506
- for (let line = from; line <= Math.min(to, totalLines); line += 1) {
507
- if (shieldedLines.has(line))
508
- continue;
509
- shieldedLines.add(line);
510
- added = true;
511
- }
512
- };
513
- /** Collects what one of the directives the comment carries reaches. */
514
- const shieldReach = (comment, directive) => {
515
- const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
516
- if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
517
- const codeStart = codeStartAfter(source, comment.end, context.skippable);
518
- if (codeStart > comment.end && codeStart < source.length) {
519
- shieldedGaps.push([comment.end, codeStart]);
520
- added = true;
521
- }
522
- if (lastComment)
523
- return;
524
- }
525
- if (TSC_SUPPRESSIONS.has(directive)) {
526
- for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
527
- if (blockingLines.has(line))
528
- continue;
529
- blockingLines.add(line);
530
- added = true;
531
- }
532
- }
533
- // oxlint reaches its rows' lines past the ones counted below.
534
- if (OXLINT_ROW_DIRECTIVES.has(directive)) {
535
- const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
536
- if (reach !== undefined)
537
- shieldLines(reach.from, reach.to);
538
- }
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);
543
- if (!isNextLineDirective(directive))
544
- return;
545
- // In JSX, such a directive in braces applies to the sibling after it,
546
- // whatever line that is on (prettier leaves the element after
547
- // `{/* prettier-ignore */}` as is): a comment-only container standing
548
- // between them has to stay. Range and file pragmas target no sibling.
549
- const holder = context.containerOf.get(comment);
550
- if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
551
- siblingTargets.add(holder.next);
552
- added = true;
553
- }
554
- // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
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.
559
- if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment))
560
- return;
561
- if (directive === "nosemgrep" && !readsAsSemgrepPreviousLine(comment))
562
- return;
563
- // Counted pragmas like `c8 ignore next 3` cover several lines.
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);
582
- }
583
- return added;
584
- };
585
- /** Takes the comments, all still being removed, out of the removal and keeps them. */
586
- const protect = (group) => {
587
- if (group.length === 0)
588
- return;
589
- const grouped = new Set(group);
590
- stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
591
- kept.push(...group);
592
- units = undefined;
593
- };
594
- /** Protects what the shields collected so far hold in place; true when that kept anything. */
595
- const protectShields = () => {
596
- const removing = stillRemoved.length;
597
- for (const line of shieldedLines) {
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
- }
608
- }
609
- for (const line of blockingLines)
610
- protect(lineOpeningGroup(source, unitsOnLine(line)));
611
- for (const [from, to] of shieldedGaps) {
612
- protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
613
- }
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;
619
- };
620
- collectShields();
621
- while (protectShields()) {
622
- if (!collectShields())
623
- break;
624
- }
625
- if (stillRemoved.length !== removed.length) {
626
- kept.sort(byStart);
627
- }
628
- return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
629
- }
630
- /**
631
- * The removals as the edits delete them: a comment-only container whose
632
- * comments all go — and that is to go braces and all — is one unit spanning
633
- * its braces, every other comment a unit of its own. Sorted, as the comments.
634
- */
635
- function removalUnits(removals, containerOf) {
636
- const removing = new Set(removals);
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);
647
- }
648
- /**
649
- * Offset of the first character at or after `from` that is neither
650
- * whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
651
- * source length when only ranges follow. `from` lies past the start of at
652
- * least one range (the directive the search is for); when the closest of
653
- * them runs past `from` — a block comment opened on the directive's line —
654
- * the search resumes at its end.
655
- */
656
- function codeStartAfter(source, from, ranges) {
657
- let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
658
- let index = Math.max(from, ranges[next - 1].end);
659
- while (index < source.length) {
660
- const range = ranges[next];
661
- if (range !== undefined && range.start === index) {
662
- index = range.end;
663
- next += 1;
664
- }
665
- else if (isBlank(source[index])) {
666
- index += 1;
667
- }
668
- else {
669
- return index;
670
- }
671
- }
672
- return source.length;
673
- }
674
- /**
675
- * For each TypeScript compiler, the first line between a tsc suppression and
676
- * the code on the lines below it that the compiler does not step over, if
677
- * any. Looking for the line a suppression applies to, a compiler skips the
678
- * lines it reads as blank or as a `//` comment (see TSC_PASSABLE_LINES); any
679
- * other line — one opening with a block comment, say — ends the search,
680
- * leaving the suppression without a target for that compiler. Deleting that
681
- * line would let the suppression reach the code instead, so it has to stay.
682
- * The two compilers can stop at different lines: a line holding only a
683
- * vertical tab stops tsc 7, while tsc 6 searches on to the next line it
684
- * cannot pass.
685
- */
686
- function firstBlockingLines(source, comment, lineOffsets, ranges) {
687
- // A suppression applies from the line below it on, so code sharing its line
688
- // (`/* @ts-ignore */ foo();`) does not end the search.
689
- const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
690
- if (codeStart >= source.length)
691
- return [];
692
- const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
693
- const found = [];
694
- for (const passable of TSC_PASSABLE_LINES) {
695
- for (let line = comment.endLine + 1; line < codeLine; line += 1) {
696
- if (!passable.test(source.slice(lineOffsets[line - 1], lineOffsets[line]))) {
697
- found.push(line);
698
- break;
699
- }
700
- }
701
- }
702
- return found;
703
- }
704
- /**
705
- * The removals on the given line when one of them opens it (holds its first
706
- * non-whitespace character), so that deleting them would change how the
707
- * line starts. None when something that survives opens the line instead, or
708
- * nothing on it is to be removed.
709
- */
710
- function lineOpeningGroup(source, line) {
711
- let first = line.start;
712
- while (first < line.end && isBlank(source[first]))
713
- first += 1;
714
- return line.units.some((unit) => unit.start <= first && first < unit.end) ? commentsOf(line.units) : [];
715
- }
716
- /**
717
- * The removals that would make the given line disappear: every
718
- * non-whitespace character of the line lies inside one of them, so deleting
719
- * them deletes the physical line. None when the line survives (it holds
720
- * code, a kept comment, or nothing to remove at all).
721
- */
722
- function vanishingLineGroup(source, line) {
723
- if (line.units.length === 0)
724
- return [];
725
- for (let index = line.start; index < line.end; index += 1) {
726
- if (isBlank(source[index]))
727
- continue;
728
- if (!line.units.some((unit) => unit.start <= index && index < unit.end))
729
- return [];
730
- }
731
- return commentsOf(line.units);
732
- }
733
- /**
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.)
743
- */
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)));
746
- }
747
- // The directives oxlint reads on rows of its own (see oxlintReachLines).
748
- const OXLINT_ROW_DIRECTIVES = new Set([
749
- "eslint-disable-next-line",
750
- "oxlint-disable-next-line",
751
- "eslint-disable-line",
752
- "oxlint-disable-line",
753
- ]);
754
- /**
755
- * The lines (1-based, inclusive) that oxlint applies an `eslint-` or
756
- * `oxlint-` line or next-line comment to, past the ones the line-based
757
- * shields take in. oxlint finds the stretch on rows that only a line feed
758
- * ends (`split_inclusive('\n')` and `lines()` in its `disable_directives.rs`):
759
- * a next-line comment covers the rest of the row its text ends on and the
760
- * row after it, a line comment its own row up to the comment. A lone CR,
761
- * U+2028 or U+2029 — a line break to tsc and ESLint — ends no row there, so
762
- * a row can run over several of the lines counted here. Undefined when a
763
- * next-line comment stands on the file's last row: oxlint then covers the
764
- * rest of the file, which no removal changes.
765
- */
766
- function oxlintReachLines(source, rows, lineOf, comment, directive) {
767
- const rowOf = (offset) => lineIndexAt(rows, offset);
768
- if (!directive.endsWith("-next-line")) {
769
- return { from: lineOf(rows[rowOf(comment.start)]), to: comment.line };
770
- }
771
- // The comment's text ends in front of a block comment's `*/`.
772
- const row = rowOf(comment.kind === "block" ? comment.end - 2 : comment.end);
773
- if (rows[row + 1] === undefined)
774
- return undefined;
775
- return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
776
- }
777
- /**
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.
783
- */
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));
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;
808
- // These suppressions only apply to the following node/line when they stand on
809
- // their own line — trailing after code they target (at most) their own line.
810
- // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
811
- // line even when they trail code.
812
- const TRAILING_TARGETS_OWN_LINE = new Set([
813
- "prettier-ignore",
814
- "oxfmt-ignore",
815
- "dprint-ignore",
816
- "lgtm",
817
- "codeql",
818
- "skipcq",
819
- ]);
820
- // The suppressions tsc looks past blank and `//` lines for (see
821
- // firstBlockingLines). They stay next-line directives as well: Flow, which
822
- // honours them in TypeScript files, applies them to the very next line.
823
- const TSC_SUPPRESSIONS = new Set(["@ts-ignore", "@ts-expect-error"]);
824
- // The lines each compiler steps over on its way up from a diagnostic to a
825
- // suppression, matched against the whole line, line break included. tsc 6
826
- // trims the line and accepts nothing or a `//` comment; tsc 7 accepts only
827
- // spaces and tabs before the line break or the `//`.
828
- const TSC_PASSABLE_LINES = [/^\s*(?:\/\/|$)/, /^[ \t]*(?:[\r\n]|\/\/)/];
829
- // Directives that only count while no other comment sits between them and
830
- // their target: deno_graph reads `@deno-types`/`@ts-types` from the last
831
- // comment before an import, ts-prune and graphql-tag-pluck from the closest
832
- // leading comment, and ESLint's no-fallthrough and default-case from the last
833
- // comment before the next case or the closing brace. Deleting the comments in
834
- // that gap would newly activate such a directive, so the whole gap is
835
- // shielded — trailing comments in front of the code included. With no code
836
- // left there is nothing to target, and the switch-clause comments are only
837
- // detected in front of the `case`, `default` or `}` their rules read them
838
- // before.
839
- const LAST_COMMENT_DIRECTIVES = new Set([
840
- "@deno-types",
841
- "@ts-types",
842
- "ts-prune-ignore-next",
843
- "GraphQL",
844
- "fallthrough",
845
- "no-default",
846
- ]);
847
- // The coverage hints ast-v8-to-istanbul, Vitest's v8 provider, applies to the
848
- // node that starts at the very token after them: with another comment in
849
- // between, the hint has no target, and deleting that comment would bring it
850
- // into effect. The gap up to the code is shielded like the one above, on top
851
- // of the lines below the hint, which c8, Node and monocart count.
852
- const NEXT_TOKEN_DIRECTIVES = new Set([
853
- "istanbul-ignore-next",
854
- "istanbul-ignore-if",
855
- "istanbul-ignore-else",
856
- "c8-ignore-next",
857
- "c8-ignore-if",
858
- "c8-ignore-else",
859
- "v8-ignore-next",
860
- "v8-ignore-if",
861
- "v8-ignore-else",
862
- "node:coverage-ignore",
863
- "node:coverage-ignore-if",
864
- "node:coverage-ignore-else",
865
- ]);
866
- // Directives that target the line or node directly below them. File- and
867
- // range-scoped pragmas (`istanbul-ignore-file`, `c8-ignore-start`, ...) are
868
- // absent: they do not target the following line, so they never shield it.
869
- // They go by their names here: Vitest's v8 provider reads a range hint
870
- // spelled out on any line of this file, and would drop the rest of the file
871
- // from this project's coverage.
872
- const NEXT_LINE_DIRECTIVES = new Set([
873
- "@ts-ignore",
874
- "@ts-expect-error",
875
- "biome-ignore",
876
- "deno-lint-ignore",
877
- "deno-fmt-ignore",
878
- "deno-coverage-ignore",
879
- "prettier-ignore",
880
- "oxfmt-ignore",
881
- "dprint-ignore",
882
- "rome-ignore",
883
- "istanbul-ignore-next",
884
- "istanbul-ignore-if",
885
- "istanbul-ignore-else",
886
- "c8-ignore-next",
887
- "c8-ignore-if",
888
- "c8-ignore-else",
889
- "v8-ignore-next",
890
- "v8-ignore-if",
891
- "v8-ignore-else",
892
- "node:coverage-ignore",
893
- "node:coverage-ignore-if",
894
- "node:coverage-ignore-else",
895
- // Directives that hang off the node or line directly below them: a
896
- // whole-line comment between them and their target would change what
897
- // they apply to, so the line below stays shielded.
898
- "million-ignore",
899
- "@million-skip",
900
- "@million-jsx-skip",
901
- "deepcode-ignore",
902
- "skipcq",
903
- "lgtm",
904
- "codeql",
905
- "nosemgrep",
906
- "no-dd-sa",
907
- "datadog-disable",
908
- "noinspection",
909
- "language-injection",
910
- "@next-codemod-error",
911
- "@next-codemod-ignore",
912
- "$FlowFixMe",
913
- "$FlowExpectedError",
914
- "$FlowIssue",
915
- "$FlowIgnore",
916
- "pragma-allowlist-nextline-secret",
917
- "cspell:disable-next",
918
- "HTML",
919
- ]);
920
- function isNextLineDirective(name) {
921
- return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
922
- }
923
- // The coverage pragmas a line count after `ignore next` applies to: c8
924
- // (v8-to-istanbul), Node's test-runner coverage and monocart-coverage-reports
925
- // count lines. istanbul and ast-v8-to-istanbul, Vitest's providers, apply the
926
- // hint to the next node whatever count follows, so `istanbul ignore next 3`
927
- // covers one line. Everything else — `@ts-expect-error`,
928
- // `eslint-disable-next-line` and friends — covers exactly one line too, no
929
- // matter what prose like "we ignore next 3 legacy calls" appears in the
930
- // reason text.
931
- const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
932
- /**
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.
937
- */
938
- function shieldedLineCount(comment, directive) {
939
- if (!COUNTED_NEXT_LINE.has(directive))
940
- return 1;
941
- const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
942
- return match ? Math.max(1, Number(match[1])) : 1;
943
- }
944
- /**
945
- * True when nothing but whitespace shares the range's first and last lines.
946
- * Lines are delimited by any ECMAScript line terminator, matching how the
947
- * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
948
- */
949
- function occupiesWholeLines(source, range) {
950
- return opensItsLine(source, range) && closesItsLine(source, range);
951
- }
952
- /** True when nothing but whitespace follows the range on its last line. */
953
- function closesItsLine(source, range) {
954
- return blankToLineEdge(source, range.end, 1);
955
- }
956
- /** True when nothing but whitespace precedes the range on its first line. */
957
- function opensItsLine(source, range) {
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) {
966
- const char = source[index];
967
- if (LINE_TERMINATOR.test(char))
968
- return true;
969
- if (!isBlank(char))
970
- return false;
971
- }
972
- return true;
973
- }
974
387
  //# sourceMappingURL=remove.js.map