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