@kongyo2/ts-comment-scanner 1.8.0 → 1.10.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.
package/src/remove.ts CHANGED
@@ -1,6 +1,9 @@
1
- import { activePositionalDirectives, isLegalComment, type DirectivePlacement } from "./directives.js";
2
- import { LINE_BREAK, LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
3
- import { scanComments } from "./scanner.js";
1
+ import { activePositionalDirectives, bunMarkerOffset, isLegalComment, type DirectivePlacement } from "./directives.js";
2
+ import type { JsxContainer } from "./jsx.js";
3
+ import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
4
+ import { firstIndexAtOrAfter, type Range } from "./ranges.js";
5
+ import { analyzeSource } from "./scanner.js";
6
+ import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
4
7
  import type { Comment } from "./types.js";
5
8
 
6
9
  export interface RemoveOptions {
@@ -19,9 +22,11 @@ export interface RemoveResult {
19
22
  removed: Comment[];
20
23
  /**
21
24
  * Comments kept because they are protected: directives, license headers,
22
- * comments whose removal would shift code under a next-line directive, and
23
- * comments whose removal would move a position-dependent directive
24
- * (prettier's first-comment pragma, Bun's file-start marker) into effect.
25
+ * comments whose removal would shift code under a next-line directive or
26
+ * that sit between a directive and the code it reaches past them for (or
27
+ * the JSX sibling it applies to), and comments whose removal would move a
28
+ * position-dependent directive (prettier's first-comment pragma, Deno's
29
+ * coverage file pragma, Bun's file-start marker) into effect.
25
30
  */
26
31
  kept: Comment[];
27
32
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -39,10 +44,21 @@ export interface RemoveResult {
39
44
  * - Surviving comments keep their directive semantics: when deleting a leading
40
45
  * comment would newly activate a position-dependent directive in a kept
41
46
  * comment (prettier's `@format` docblock pragma counts only as the file's
42
- * first comment, Bun's `// @bun` only at the very start), the leading
43
- * comment is kept instead.
47
+ * first comment, Bun's `// @bun` only at the very start or right after a
48
+ * shebang line), the leading comment is kept instead.
44
49
  * - A space is inserted where removing a block comment would merge two tokens,
45
50
  * and a line break is kept where the comment acted as one (ASI stays intact).
51
+ * - A JSX comment goes with its braces (`{/* note *​/}`), unless that would
52
+ * change what the element renders: the braces split the text around them
53
+ * into separate strings, so where they stand between text that renders,
54
+ * `{" "}` takes their place when that keeps the strings identical, and
55
+ * otherwise the braces stay as `{}`. The output is re-parsed to prove every
56
+ * element renders the same strings, under the whitespace rules of
57
+ * TypeScript, esbuild and Babel alike.
58
+ * - Blank lines that only separated a removed comment from the code around it
59
+ * go with it: no pile-up of blank lines where a comment stood between two
60
+ * of them, and at the start or end of the file or of a block only the blank
61
+ * lines already against that edge stay.
46
62
  * - A leading byte-order mark is preserved. Reported positions index into the
47
63
  * `source` string exactly as `scanComments(source)` reports them (a BOM
48
64
  * counts as its first character).
@@ -51,7 +67,8 @@ export interface RemoveResult {
51
67
  */
52
68
  export function removeComments(source: string, options: RemoveOptions = {}): RemoveResult {
53
69
  const jsx = options.jsx === true;
54
- const comments = scanComments(source, { jsx });
70
+ const analysis = analyzeSource(source, { jsx });
71
+ const comments = analysis.comments;
55
72
 
56
73
  let removed: Comment[] = [];
57
74
  const kept: Comment[] = [];
@@ -66,18 +83,45 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
66
83
  }
67
84
  }
68
85
 
86
+ const containers = attachContainers(source, analysis.jsx.containers, comments);
87
+ const containerOf = new Map<Comment, Container>();
88
+ for (const container of containers) {
89
+ for (const comment of container.comments) containerOf.set(comment, container);
90
+ }
91
+ const context: ShieldContext = { containerOf, skippable: skippableRanges(comments, containerOf, new Set(removed)) };
92
+ const search = containerSearch(containers);
93
+ // Collapsing blank lines can only move a surviving comment to the very
94
+ // start of the file (or of the line after a shebang), where Bun reads its
95
+ // marker; when that changes a survivor's meaning, the attempt is repeated
96
+ // without the collapse before any removal is given up.
97
+ let tidyBlankLines = true;
98
+
69
99
  for (;;) {
70
- removed = shieldNextLineDirectives(source, removed, kept, skipped);
100
+ const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
101
+ removed = shielding.removed;
71
102
 
72
103
  if (removed.length === 0) {
73
104
  return { code: source, removed, kept, skipped, changed: false };
74
105
  }
75
106
 
76
- const code = spliceKeepingBom(source, removed);
77
- const survivor = firstChangedSurvivor(source, code, scanComments(code, { jsx }), kept, skipped);
107
+ const removing = new Set(removed);
108
+ search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
109
+ const edits = removalEdits(removed, containerOf, search.droppable);
110
+ const deletions = edits.filter((edit) => edit.replacement === undefined);
111
+ const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
112
+ const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
113
+ const output = analyzeSource(code, { jsx });
114
+
115
+ if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
116
+
117
+ const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
78
118
  if (survivor === undefined) {
79
119
  return { code, removed, kept, skipped, changed: true };
80
120
  }
121
+ if (blankEdits.length > 0) {
122
+ tidyBlankLines = false;
123
+ continue;
124
+ }
81
125
 
82
126
  // A surviving comment changed meaning: with everything before it deleted
83
127
  // it became the file's first comment (or its very first bytes), turning a
@@ -93,6 +137,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
93
137
  removed = removed.filter((comment) => comment !== blocker);
94
138
  kept.push(blocker);
95
139
  kept.sort(byStart);
140
+ tidyBlankLines = true;
96
141
  }
97
142
  }
98
143
 
@@ -100,8 +145,8 @@ function unexpectedResult(): Error {
100
145
  return new Error("comment removal produced an unexpected result; refusing to continue");
101
146
  }
102
147
 
103
- /** Sort comparator for document order: comments are ordered by where they start. */
104
- function byStart(a: Comment, b: Comment): number {
148
+ /** Sort comparator for document order: ranges are ordered by where they start. */
149
+ function byStart(a: Range, b: Range): number {
105
150
  return a.start - b.start;
106
151
  }
107
152
 
@@ -144,17 +189,17 @@ function firstChangedSurvivor(
144
189
  * The placement bits the position-dependent directives care about, derived
145
190
  * without a parse: the comment is the file's first comment when nothing but
146
191
  * whitespace (or a shebang) precedes it — any earlier comment's text keeps
147
- * the slice non-blank — and it sits at the file start when it begins at
148
- * offset zero. Header status needs the token structure, but removing
149
- * comments never moves a comment across the first token, so the positional
150
- * check does not consult it.
192
+ * the slice non-blank — and it sits at the file start when it begins where
193
+ * Bun reads its marker. Header status needs the token structure, but
194
+ * removing comments never moves a comment across the first token, so the
195
+ * positional check does not consult it.
151
196
  */
152
197
  function positionalPlacement(text: string, comment: Comment): DirectivePlacement {
153
198
  const shebangLength = /^#!.*/.exec(text)?.[0].length ?? 0;
154
199
  return {
155
200
  header: false,
156
201
  firstComment: isBlank(text.slice(shebangLength, comment.start)),
157
- fileStart: comment.start === 0,
202
+ fileStart: comment.start === bunMarkerOffset(text),
158
203
  };
159
204
  }
160
205
 
@@ -168,19 +213,18 @@ function closestRemovalBefore(removals: readonly Comment[], comment: Comment): C
168
213
  }
169
214
 
170
215
  /**
171
- * Splices the removals out of the source, restoring the byte-order mark when
172
- * dropping a comment-only first line took it along: the mark is an encoding
173
- * artifact, not line content.
216
+ * Applies the edits, restoring the byte-order mark when dropping a
217
+ * comment-only first line took it along: the mark is an encoding artifact,
218
+ * not line content.
174
219
  */
175
- function spliceKeepingBom(source: string, removals: readonly Comment[]): string {
176
- if (!source.startsWith("\uFEFF")) return splice(source, removals);
177
- // The splicing itself works on the BOM-less body (with ranges shifted to
220
+ function editKeepingBom(source: string, edits: readonly Edit[]): string {
221
+ if (!source.startsWith("")) return applyEdits(source, edits);
222
+ // The editing itself works on the BOM-less body (with ranges shifted to
178
223
  // match) so its line handling never sees the mark, then the mark is
179
- // re-attached; this keeps `\uFEFF// gone\n...` from losing the mark with
224
+ // re-attached; this keeps `// gone\n...` from losing the mark with
180
225
  // the dropped line.
181
- const body = source.slice(1);
182
- const shifted = removals.map((comment) => ({ ...comment, start: comment.start - 1, end: comment.end - 1 }));
183
- return `\uFEFF${splice(body, shifted)}`;
226
+ const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
227
+ return `${applyEdits(source.slice(1), shifted)}`;
184
228
  }
185
229
 
186
230
  function isProtected(comment: Comment, options: RemoveOptions): boolean {
@@ -189,36 +233,314 @@ function isProtected(comment: Comment, options: RemoveOptions): boolean {
189
233
  return false;
190
234
  }
191
235
 
236
+ /**
237
+ * How a comment-only JSX container goes once all its comments do: braces
238
+ * and all; replaced by `{" "}` together with the spaces that held it off its
239
+ * neighbour; or with its braces kept, only the comments going.
240
+ */
241
+ type ContainerMode = "drop" | "space" | "keep";
242
+
243
+ /** A comment-only JSX container (`{/* note *​/}`) with the comments in it. */
244
+ interface Container extends Range {
245
+ segment: number;
246
+ suspect: boolean;
247
+ /** The comment-only container right after it among the siblings, if any. */
248
+ next: Container | undefined;
249
+ comments: Comment[];
250
+ mode: ContainerMode;
251
+ /** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
252
+ spaceRange: Range | undefined;
253
+ }
254
+
255
+ function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
256
+ const containers = new Map<JsxContainer, Container>();
257
+ for (const layout of layouts) {
258
+ const inside: Comment[] = [];
259
+ let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
260
+ for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
261
+ inside.push(comment);
262
+ index += 1;
263
+ }
264
+ containers.set(layout, {
265
+ start: layout.start,
266
+ end: layout.end,
267
+ segment: layout.segment,
268
+ suspect: layout.suspect,
269
+ next: undefined,
270
+ comments: inside,
271
+ mode: "drop",
272
+ spaceRange: spaceRange(source, layout),
273
+ });
274
+ }
275
+ for (const [layout, container] of containers) {
276
+ if (layout.next !== undefined) container.next = containers.get(layout.next);
277
+ }
278
+ return [...containers.values()];
279
+ }
280
+
281
+ const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
282
+
283
+ /**
284
+ * The range `{" "}` would take the place of: a one-line container and the
285
+ * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
286
+ * the spaces after it when it opens its line and code follows. Undefined when
287
+ * no spaces hold it off anything on its line.
288
+ */
289
+ function spaceRange(source: string, container: Range): Range | undefined {
290
+ if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
291
+ let lineStart = container.start;
292
+ while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1] as string)) lineStart -= 1;
293
+ let before = container.start;
294
+ while (isHorizontalSpace(source[before - 1])) before -= 1;
295
+ let after = container.end;
296
+ while (isHorizontalSpace(source[after])) after += 1;
297
+
298
+ if (!isBlank(source.slice(lineStart, container.start))) {
299
+ return before < container.start ? { start: before, end: container.end } : undefined;
300
+ }
301
+ const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
302
+ return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
303
+ }
304
+
305
+ /**
306
+ * Comments, and the comment-only containers that may go along with theirs,
307
+ * as sorted ranges: what the search for the code after a directive skips.
308
+ * A container counts as skippable when all its comments are removal
309
+ * candidates, even if some get protected later — skipping it then only moves
310
+ * the found code further down, which shields more, not less.
311
+ */
312
+ function skippableRanges(
313
+ comments: readonly Comment[],
314
+ containerOf: ReadonlyMap<Comment, Container>,
315
+ candidates: ReadonlySet<Comment>,
316
+ ): Range[] {
317
+ const ranges: Range[] = [];
318
+ for (const comment of comments) {
319
+ const container = containerOf.get(comment);
320
+ if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
321
+ ranges.push(comment);
322
+ } else if (ranges[ranges.length - 1] !== container) {
323
+ ranges.push(container);
324
+ }
325
+ }
326
+ return ranges;
327
+ }
328
+
329
+ /** The edits that remove the comments: one per comment, or one per container going with its braces. */
330
+ function removalEdits(
331
+ removed: readonly Comment[],
332
+ containerOf: ReadonlyMap<Comment, Container>,
333
+ droppable: ReadonlySet<Container>,
334
+ ): Edit[] {
335
+ const edits: Edit[] = [];
336
+ for (const comment of removed) {
337
+ const container = containerOf.get(comment);
338
+ if (container === undefined || !droppable.has(container) || container.mode === "keep") {
339
+ edits.push({ start: comment.start, end: comment.end });
340
+ } else if (comment === container.comments[0]) {
341
+ edits.push(
342
+ container.mode === "drop"
343
+ ? { start: container.start, end: container.end, jsx: true }
344
+ : { ...(container.spaceRange as Range), replacement: '{" "}' },
345
+ );
346
+ }
347
+ }
348
+ return edits;
349
+ }
350
+
351
+ /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
352
+ function changedSegments(before: readonly string[], after: readonly string[]): Set<number> {
353
+ if (before.length !== after.length) throw unexpectedResult();
354
+ const changed = new Set<number>();
355
+ for (const [index, rendering] of before.entries()) {
356
+ if (rendering !== after[index]) changed.add(index);
357
+ }
358
+ return changed;
359
+ }
360
+
361
+ interface ContainerSearch {
362
+ /** Containers whose comments all go, so their braces can too. */
363
+ readonly droppable: ReadonlySet<Container>;
364
+ /** Records which containers can go; when that changes (comments got protected), the search starts over. */
365
+ track(droppable: ReadonlySet<Container>): void;
366
+ /** Adjusts the modes after an attempt rendered the given stretches differently; true when anything changed. */
367
+ adjust(changed: ReadonlySet<number>): boolean;
368
+ }
369
+
370
+ /**
371
+ * Decides how each comment-only container goes. Dropping the braces is tried
372
+ * everywhere first; where the re-parsed output then renders a stretch of JSX
373
+ * children differently, that stretch's suspect containers are settled one
374
+ * at a time, in document order — each tries dropping, then `{" "}`, then
375
+ * keeping its braces, while the suspects after it keep theirs — so braces
376
+ * stay only where nothing less keeps the rendering intact. Keeping every
377
+ * suspect's braces always does: the other containers sit between text that
378
+ * renders nothing, which stays so however it is merged or trimmed.
379
+ */
380
+ // The most suspects of one stretch settled one at a time; beyond that they all keep their braces.
381
+ const MAX_SUSPECTS_SETTLED = 16;
382
+
383
+ function containerSearch(containers: readonly Container[]): ContainerSearch {
384
+ let droppable: ReadonlySet<Container> = new Set();
385
+ /** Per stretch that rendered differently: its suspects still to settle, the first being tried. */
386
+ const pending = new Map<number, Container[]>();
387
+
388
+ const advance = (container: Container): boolean => {
389
+ if (container.mode === "keep") return false;
390
+ container.mode = container.mode === "drop" && container.spaceRange !== undefined ? "space" : "keep";
391
+ return true;
392
+ };
393
+
394
+ return {
395
+ get droppable() {
396
+ return droppable;
397
+ },
398
+ track(now) {
399
+ // Protection only ever takes comments out of the removal, so the set
400
+ // only shrinks and its size tells whether it changed.
401
+ if (now.size === droppable.size) return;
402
+ droppable = now;
403
+ pending.clear();
404
+ for (const container of containers) container.mode = "drop";
405
+ },
406
+ adjust(changed) {
407
+ let adjusted = false;
408
+ for (const segment of changed) {
409
+ let queue = pending.get(segment);
410
+ if (queue === undefined) {
411
+ // The stretch's first difference: from now on its suspects are
412
+ // settled one at a time, the first trying to drop its braces while
413
+ // the rest keep theirs. Every try costs a parse, so a stretch with
414
+ // a great many of them keeps all their braces straight away.
415
+ queue = containers.filter((c) => c.segment === segment && c.suspect && droppable.has(c));
416
+ const giveUp = queue.length > MAX_SUSPECTS_SETTLED;
417
+ for (const container of giveUp ? queue : queue.slice(1)) container.mode = "keep";
418
+ pending.set(segment, giveUp ? [] : queue);
419
+ if (giveUp || queue.length > 1) {
420
+ adjusted = true;
421
+ continue;
422
+ }
423
+ }
424
+ // The container being tried still changes the rendering: it tries
425
+ // the next mode. Keeping its braces cannot fail, so running out of
426
+ // modes (or of suspects to blame) means the output is not trustworthy.
427
+ const current = queue[0];
428
+ if (current === undefined || !advance(current)) throw unexpectedResult();
429
+ adjusted = true;
430
+ }
431
+ for (const [segment, queue] of pending) {
432
+ if (changed.has(segment) || queue.length === 0) continue;
433
+ // The stretch renders as before: the container being tried is
434
+ // settled, and the next suspect tries dropping its braces.
435
+ queue.shift();
436
+ const next = queue[0];
437
+ if (next !== undefined) {
438
+ next.mode = "drop";
439
+ adjusted = true;
440
+ }
441
+ }
442
+ return adjusted;
443
+ },
444
+ };
445
+ }
446
+
447
+ interface ShieldContext {
448
+ /** The comment-only JSX container each comment in one sits in. */
449
+ containerOf: ReadonlyMap<Comment, Container>;
450
+ /** Comments and the containers that may go with theirs, sorted: what code starts after. */
451
+ skippable: readonly Range[];
452
+ }
453
+
454
+ interface Shielding {
455
+ removed: Comment[];
456
+ /** Lines (1-based) that a surviving next-line directive reaches. */
457
+ lines: Set<number>;
458
+ }
459
+
460
+ /**
461
+ * What deleting part of the removal takes out of the source: a single
462
+ * comment, or a comment-only JSX container going braces and all.
463
+ */
464
+ interface RemovalUnit extends Range {
465
+ comments: readonly Comment[];
466
+ container?: Container;
467
+ }
468
+
192
469
  /**
193
470
  * Re-protects comments whose removal would delete a physical line sitting
194
471
  * under a surviving next-line directive (`@ts-expect-error`,
195
472
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
196
473
  * following code up, silently changing which line the directive applies to —
197
- * and with it the compiler or linter behaviour.
474
+ * and with it the compiler or linter behaviour. Directives whose tools reach
475
+ * further get more: below a tsc suppression the first line each compiler
476
+ * cannot step over stays, between a directive that only counts as the
477
+ * last comment before its target (`@deno-types`, ...) and that target every
478
+ * comment stays, and a JSX comment container right after a directive in a
479
+ * container (`{/* prettier-ignore *​/}`, which applies to the next sibling)
480
+ * stays too.
198
481
  *
199
- * The check works on whole lines, not single comments: a line holding several
200
- * removable comments (`/* one *​/ /* two *​/`) vanishes just the same, so every
201
- * comment on it has to stay. Runs to a fixpoint because a re-protected
202
- * comment can itself be a next-line directive shielding further lines (when
203
- * directives are being removed selectively).
482
+ * The line checks work on whole lines, not single comments: a line holding
483
+ * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
484
+ * so every comment on it has to stay — and a comment-only JSX container takes
485
+ * its braces along. Runs to a fixpoint because a re-protected comment can
486
+ * itself be a next-line directive shielding further lines (when directives
487
+ * are being removed selectively).
204
488
  */
205
- function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comment[], skipped: Comment[]): Comment[] {
489
+ function shieldNextLineDirectives(
490
+ source: string,
491
+ removed: Comment[],
492
+ kept: Comment[],
493
+ skipped: Comment[],
494
+ context: ShieldContext,
495
+ ): Shielding {
206
496
  const lineOffsets = lineStartOffsets(source);
207
497
  const totalLines = lineOffsets.length;
208
498
  const shieldedLines = new Set<number>();
499
+ const blockingLines = new Set<number>();
500
+ const shieldedGaps: Array<readonly [number, number]> = [];
501
+ const siblingTargets = new Set<Container>();
209
502
  const processed = new Set<Comment>();
210
503
  let stillRemoved = removed;
504
+ let units: RemovalUnit[] | undefined;
505
+ const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
211
506
 
212
507
  const collectShields = (): boolean => {
213
508
  let added = false;
214
509
  for (const comment of [...kept, ...skipped]) {
215
510
  if (processed.has(comment)) continue;
216
511
  processed.add(comment);
217
- if (comment.directive === undefined || !isNextLineDirective(comment.directive)) continue;
512
+ const directive = comment.directive;
513
+ if (directive === undefined) continue;
514
+ if (LAST_COMMENT_DIRECTIVES.has(directive)) {
515
+ const codeStart = codeStartAfter(source, comment.end, context.skippable);
516
+ if (codeStart > comment.end && codeStart < source.length) {
517
+ shieldedGaps.push([comment.end, codeStart]);
518
+ added = true;
519
+ }
520
+ continue;
521
+ }
522
+ if (TSC_SUPPRESSIONS.has(directive)) {
523
+ for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
524
+ if (blockingLines.has(line)) continue;
525
+ blockingLines.add(line);
526
+ added = true;
527
+ }
528
+ }
529
+ if (!isNextLineDirective(directive)) continue;
530
+ // In JSX, such a directive in braces applies to the sibling after it,
531
+ // whatever line that is on (prettier leaves the element after
532
+ // `{/* prettier-ignore */}` as is): a comment-only container standing
533
+ // between them has to stay. Range and file pragmas target no sibling.
534
+ const holder = context.containerOf.get(comment);
535
+ if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
536
+ siblingTargets.add(holder.next);
537
+ added = true;
538
+ }
218
539
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
219
540
  // `secret(); // nosemgrep`) targets its own line, so it shields nothing
220
- // below it.
221
- if (TRAILING_TARGETS_OWN_LINE.has(comment.directive) && !occupiesWholeLines(source, comment)) {
541
+ // below it. In JSX, the container holding the comment is what has to
542
+ // stand on lines of its own.
543
+ if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) {
222
544
  continue;
223
545
  }
224
546
  // Counted pragmas like `c8 ignore next 3` cover several lines.
@@ -234,51 +556,180 @@ function shieldNextLineDirectives(source: string, removed: Comment[], kept: Comm
234
556
  return added;
235
557
  };
236
558
 
237
- const protectShieldedLines = (): boolean => {
559
+ const protect = (group: readonly Comment[]): void => {
560
+ const grouped = new Set(group);
561
+ stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
562
+ kept.push(...group);
563
+ units = undefined;
564
+ };
565
+
566
+ const protectShields = (): boolean => {
238
567
  let changed = false;
239
568
  for (const line of shieldedLines) {
240
- const group = vanishingLineGroup(source, line, lineOffsets, stillRemoved);
569
+ const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
570
+ if (group === undefined) continue;
571
+ protect(group);
572
+ changed = true;
573
+ }
574
+ for (const line of blockingLines) {
575
+ const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
241
576
  if (group === undefined) continue;
242
- const grouped = new Set(group);
243
- stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
244
- kept.push(...group);
577
+ protect(group);
578
+ changed = true;
579
+ }
580
+ for (const [from, to] of shieldedGaps) {
581
+ const group = stillRemoved.filter((comment) => comment.start >= from && comment.start < to);
582
+ if (group.length === 0) continue;
583
+ protect(group);
584
+ changed = true;
585
+ }
586
+ for (const target of siblingTargets) {
587
+ const group = stillRemoved.filter((comment) => target.comments.includes(comment));
588
+ if (group.length === 0) continue;
589
+ protect(group);
245
590
  changed = true;
246
591
  }
247
592
  return changed;
248
593
  };
249
594
 
250
595
  collectShields();
251
- while (protectShieldedLines()) {
596
+ while (protectShields()) {
252
597
  if (!collectShields()) break;
253
598
  }
254
599
  if (stillRemoved.length !== removed.length) {
255
600
  kept.sort(byStart);
256
601
  }
257
- return stillRemoved;
602
+ return { removed: stillRemoved, lines: shieldedLines };
258
603
  }
259
604
 
260
605
  /**
261
- * The removal candidates that would make the given line disappear: every
262
- * non-whitespace character of the line lies inside one of them, so splicing
263
- * them out deletes the physical line. Returns undefined when the line
264
- * survives (it holds code, a kept comment, or nothing to remove at all).
606
+ * The removals as the edits delete them: a comment-only container whose
607
+ * comments all go — and that is to go braces and all — is one unit spanning
608
+ * its braces, every other comment a unit of its own. Sorted, as the comments.
609
+ */
610
+ function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
611
+ const removing = new Set(removals);
612
+ const units: RemovalUnit[] = [];
613
+ for (const comment of removals) {
614
+ const container = containerOf.get(comment);
615
+ if (
616
+ container === undefined ||
617
+ container.mode !== "drop" ||
618
+ !container.comments.every((inside) => removing.has(inside))
619
+ ) {
620
+ units.push({ start: comment.start, end: comment.end, comments: [comment] });
621
+ } else if (units[units.length - 1]?.container !== container) {
622
+ units.push({ start: container.start, end: container.end, comments: container.comments, container });
623
+ }
624
+ }
625
+ return units;
626
+ }
627
+
628
+ /**
629
+ * Offset of the first character at or after `from` that is neither
630
+ * whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
631
+ * source length when only ranges follow. `from` lies past the start of at
632
+ * least one range (the directive the search is for); when the closest of
633
+ * them runs past `from` — a block comment opened on the directive's line —
634
+ * the search resumes at its end.
635
+ */
636
+ function codeStartAfter(source: string, from: number, ranges: readonly Range[]): number {
637
+ let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
638
+ let index = Math.max(from, (ranges[next - 1] as Range).end);
639
+ while (index < source.length) {
640
+ const range = ranges[next];
641
+ if (range !== undefined && range.start === index) {
642
+ index = range.end;
643
+ next += 1;
644
+ } else if (isBlank(source[index] as string)) {
645
+ index += 1;
646
+ } else {
647
+ return index;
648
+ }
649
+ }
650
+ return source.length;
651
+ }
652
+
653
+ /**
654
+ * For each TypeScript compiler, the first line between a tsc suppression and
655
+ * the code on the lines below it that the compiler does not step over, if
656
+ * any. Looking for the line a suppression applies to, a compiler skips the
657
+ * lines it reads as blank or as a `//` comment (see TSC_PASSABLE_LINES); any
658
+ * other line — one opening with a block comment, say — ends the search,
659
+ * leaving the suppression without a target for that compiler. Deleting that
660
+ * line would let the suppression reach the code instead, so it has to stay.
661
+ * The two compilers can stop at different lines: a line holding only a
662
+ * vertical tab stops tsc 7, while tsc 6 searches on to the next line it
663
+ * cannot pass.
664
+ */
665
+ function firstBlockingLines(
666
+ source: string,
667
+ comment: Comment,
668
+ lineOffsets: readonly number[],
669
+ ranges: readonly Range[],
670
+ ): number[] {
671
+ // A suppression applies from the line below it on, so code sharing its line
672
+ // (`/* @ts-ignore */ foo();`) does not end the search.
673
+ const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
674
+ if (codeStart >= source.length) return [];
675
+ const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
676
+ const found: number[] = [];
677
+ for (const passable of TSC_PASSABLE_LINES) {
678
+ for (let line = comment.endLine + 1; line < codeLine; line += 1) {
679
+ if (!passable.test(source.slice(lineOffsets[line - 1], lineOffsets[line]))) {
680
+ found.push(line);
681
+ break;
682
+ }
683
+ }
684
+ }
685
+ return found;
686
+ }
687
+
688
+ /**
689
+ * The removals on the given line when one of them opens it (holds its first
690
+ * non-whitespace character), so that deleting them would change how the
691
+ * line starts. Returns undefined when something that survives opens the line
692
+ * instead, or nothing on it is to be removed.
693
+ */
694
+ function lineOpeningGroup(
695
+ source: string,
696
+ line: number,
697
+ lineOffsets: readonly number[],
698
+ units: readonly RemovalUnit[],
699
+ ): Comment[] | undefined {
700
+ // A blocking line always lies above the code line, so the next offset exists.
701
+ const lineStart = lineOffsets[line - 1] as number;
702
+ const lineEnd = lineOffsets[line] as number;
703
+ const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
704
+ let first = lineStart;
705
+ while (first < lineEnd && isBlank(source[first] as string)) first += 1;
706
+ return group.some((unit) => unit.start <= first && first < unit.end)
707
+ ? group.flatMap((unit) => unit.comments)
708
+ : undefined;
709
+ }
710
+
711
+ /**
712
+ * The removals that would make the given line disappear: every
713
+ * non-whitespace character of the line lies inside one of them, so deleting
714
+ * them deletes the physical line. Returns undefined when the line survives
715
+ * (it holds code, a kept comment, or nothing to remove at all).
265
716
  */
266
717
  function vanishingLineGroup(
267
718
  source: string,
268
719
  line: number,
269
720
  lineOffsets: readonly number[],
270
- removals: readonly Comment[],
721
+ units: readonly RemovalUnit[],
271
722
  ): Comment[] | undefined {
272
723
  // collectShields never shields a line beyond the last one, so the offset exists.
273
724
  const lineStart = lineOffsets[line - 1] as number;
274
725
  const lineEnd = lineOffsets[line] ?? source.length;
275
- const group = removals.filter((comment) => comment.start < lineEnd && comment.end > lineStart);
726
+ const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
276
727
  if (group.length === 0) return undefined;
277
728
  for (let index = lineStart; index < lineEnd; index += 1) {
278
729
  if (isBlank(source[index] as string)) continue;
279
- if (!group.some((comment) => comment.start <= index && index < comment.end)) return undefined;
730
+ if (!group.some((unit) => unit.start <= index && index < unit.end)) return undefined;
280
731
  }
281
- return group;
732
+ return group.flatMap((unit) => unit.comments);
282
733
  }
283
734
 
284
735
  // These suppressions only apply to the following node/line when they stand on
@@ -295,6 +746,36 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
295
746
  "nosemgrep",
296
747
  ]);
297
748
 
749
+ // The suppressions tsc looks past blank and `//` lines for (see
750
+ // firstBlockingLines). They stay next-line directives as well: Flow, which
751
+ // honours them in TypeScript files, applies them to the very next line.
752
+ const TSC_SUPPRESSIONS = new Set(["@ts-ignore", "@ts-expect-error"]);
753
+
754
+ // The lines each compiler steps over on its way up from a diagnostic to a
755
+ // suppression, matched against the whole line, line break included. tsc 6
756
+ // trims the line and accepts nothing or a `//` comment; tsc 7 accepts only
757
+ // spaces and tabs before the line break or the `//`.
758
+ const TSC_PASSABLE_LINES = [/^\s*(?:\/\/|$)/, /^[ \t]*(?:[\r\n]|\/\/)/];
759
+
760
+ // Directives that only count while no other comment sits between them and
761
+ // their target: deno_graph reads `@deno-types`/`@ts-types` from the last
762
+ // comment before an import, ts-prune and graphql-tag-pluck from the closest
763
+ // leading comment, and ESLint's no-fallthrough and default-case from the last
764
+ // comment before the next case or the closing brace. Deleting the comments in
765
+ // that gap would newly activate such a directive, so the whole gap is
766
+ // shielded — trailing comments in front of the code included. With no code
767
+ // left there is nothing to target, and the switch-clause comments are only
768
+ // detected in front of the `case`, `default` or `}` their rules read them
769
+ // before.
770
+ const LAST_COMMENT_DIRECTIVES = new Set([
771
+ "@deno-types",
772
+ "@ts-types",
773
+ "ts-prune-ignore-next",
774
+ "GraphQL",
775
+ "fallthrough",
776
+ "no-default",
777
+ ]);
778
+
298
779
  // Directives that target the line or node directly below them. File- and
299
780
  // range-scoped pragmas (`c8 ignore start`, `istanbul ignore file`, ...) are
300
781
  // absent: they do not target the following line, so they never shield it.
@@ -325,12 +806,9 @@ const NEXT_LINE_DIRECTIVES = new Set([
325
806
  // Directives that hang off the node or line directly below them: a
326
807
  // whole-line comment between them and their target would change what
327
808
  // they apply to, so the line below stays shielded.
328
- "ts-prune-ignore-next",
329
809
  "million-ignore",
330
810
  "@million-skip",
331
811
  "@million-jsx-skip",
332
- "@deno-types",
333
- "@ts-types",
334
812
  "deepcode-ignore",
335
813
  "skipcq",
336
814
  "lgtm",
@@ -348,7 +826,6 @@ const NEXT_LINE_DIRECTIVES = new Set([
348
826
  "$FlowIgnore",
349
827
  "pragma-allowlist-nextline-secret",
350
828
  "cspell:disable-next",
351
- "GraphQL",
352
829
  "HTML",
353
830
  ]);
354
831
 
@@ -371,132 +848,20 @@ function shieldedLineCount(comment: Comment): number {
371
848
  }
372
849
 
373
850
  /**
374
- * True when nothing but whitespace shares the comment's first and last lines.
851
+ * True when nothing but whitespace shares the range's first and last lines.
375
852
  * Lines are delimited by any ECMAScript line terminator, matching how the
376
853
  * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
377
854
  */
378
- function occupiesWholeLines(source: string, comment: Comment): boolean {
379
- for (let index = comment.start - 1; index >= 0; index -= 1) {
855
+ function occupiesWholeLines(source: string, range: Range): boolean {
856
+ for (let index = range.start - 1; index >= 0; index -= 1) {
380
857
  const char = source[index] as string;
381
858
  if (LINE_TERMINATOR.test(char)) break;
382
859
  if (!isBlank(char)) return false;
383
860
  }
384
- for (let index = comment.end; index < source.length; index += 1) {
861
+ for (let index = range.end; index < source.length; index += 1) {
385
862
  const char = source[index] as string;
386
863
  if (LINE_TERMINATOR.test(char)) break;
387
864
  if (!isBlank(char)) return false;
388
865
  }
389
866
  return true;
390
867
  }
391
-
392
- const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
393
-
394
- // A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
395
- const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
396
-
397
- /** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
398
- function lastLineBreakEnd(chunk: string): number {
399
- for (let index = chunk.length - 1; index >= 0; index -= 1) {
400
- if (LINE_TERMINATOR.test(chunk[index] as string)) return index + 1;
401
- }
402
- return -1;
403
- }
404
-
405
- /**
406
- * Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
407
- * Output is accumulated as segments with incremental line-state tracking, so the
408
- * work stays linear in the source size even for comment-dense files.
409
- */
410
- function splice(source: string, removals: readonly Comment[]): string {
411
- const segments: string[] = [];
412
- let cursor = 0;
413
- /** Whether the output's current (unterminated) line is all whitespace so far. */
414
- let lineIsBlank = true;
415
- /** Last character of the output so far ("" while empty). */
416
- let lastChar = "";
417
-
418
- const push = (chunk: string): void => {
419
- if (chunk === "") return;
420
- segments.push(chunk);
421
- const breakEnd = lastLineBreakEnd(chunk);
422
- lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
423
- lastChar = chunk.slice(-1);
424
- };
425
-
426
- /** Deletes everything the output holds after its last line break. */
427
- const dropCurrentLine = (): void => {
428
- while (segments.length > 0) {
429
- const last = segments[segments.length - 1] as string;
430
- const breakEnd = lastLineBreakEnd(last);
431
- if (breakEnd === -1) {
432
- segments.pop();
433
- continue;
434
- }
435
- segments[segments.length - 1] = last.slice(0, breakEnd);
436
- lineIsBlank = true;
437
- lastChar = last.charAt(breakEnd - 1);
438
- return;
439
- }
440
- lineIsBlank = true;
441
- lastChar = "";
442
- };
443
-
444
- /** Deletes trailing spaces/tabs of the output's current line. */
445
- const trimLineEnd = (): void => {
446
- while (segments.length > 0) {
447
- const last = segments[segments.length - 1] as string;
448
- let end = last.length;
449
- while (end > 0 && isHorizontalSpace(last[end - 1])) end -= 1;
450
- if (end === 0) {
451
- segments.pop();
452
- continue;
453
- }
454
- if (end < last.length) segments[segments.length - 1] = last.slice(0, end);
455
- lastChar = (segments[segments.length - 1] as string).slice(-1);
456
- return;
457
- }
458
- lastChar = "";
459
- };
460
-
461
- for (const comment of removals) {
462
- push(source.slice(cursor, comment.start));
463
- cursor = comment.end;
464
-
465
- NEXT_LINE_BREAK.lastIndex = cursor;
466
- const lineBreak = NEXT_LINE_BREAK.exec(source);
467
- const contentEnd = lineBreak === null ? source.length : lineBreak.index;
468
- const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
469
- const restContent = source.slice(cursor, contentEnd);
470
-
471
- if (lineIsBlank && isBlank(restContent)) {
472
- // The comment occupied whole line(s): drop them entirely.
473
- dropCurrentLine();
474
- cursor = lineEnd;
475
- } else if (isBlank(restContent)) {
476
- // Trailing comment after code: trim the gap, keep the line break.
477
- trimLineEnd();
478
- push(source.slice(contentEnd, lineEnd));
479
- cursor = lineEnd;
480
- } else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
481
- // Code on both sides of a block comment that spans lines. Per ECMA-262
482
- // such a comment acts as a line terminator for semicolon insertion, so
483
- // replace it with a real line break (matching the file's style) instead
484
- // of merging the lines.
485
- trimLineEnd();
486
- push(comment.text.includes("\r\n") ? "\r\n" : "\n");
487
- } else {
488
- // Inline comment with code after it on the same line.
489
- const next = source.charAt(cursor);
490
- if (lastChar !== "" && !isBlank(lastChar) && !isBlank(next)) {
491
- // `a/* x */b` would merge tokens: keep them apart.
492
- push(" ");
493
- } else if (lastChar === "" || isBlank(lastChar)) {
494
- // Collapse the horizontal whitespace that followed the comment.
495
- while (cursor < lineEnd && isHorizontalSpace(source[cursor])) cursor += 1;
496
- }
497
- }
498
- }
499
-
500
- segments.push(source.slice(cursor));
501
- return segments.join("");
502
- }