@kongyo2/ts-comment-scanner 1.9.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/dist/remove.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { activePositionalDirectives, bunMarkerOffset, isLegalComment } from "./directives.js";
2
- import { LINE_BREAK, LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
3
- import { scanComments } from "./scanner.js";
2
+ import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
3
+ import { firstIndexAtOrAfter } from "./ranges.js";
4
+ import { analyzeSource } from "./scanner.js";
5
+ import { applyEdits, blankLineEdits } from "./splice.js";
4
6
  /**
5
7
  * Removes comments from TypeScript source without touching code.
6
8
  *
@@ -15,6 +17,17 @@ import { scanComments } from "./scanner.js";
15
17
  * shebang line), the leading comment is kept instead.
16
18
  * - A space is inserted where removing a block comment would merge two tokens,
17
19
  * and a line break is kept where the comment acted as one (ASI stays intact).
20
+ * - A JSX comment goes with its braces (`{/* note *​/}`), unless that would
21
+ * change what the element renders: the braces split the text around them
22
+ * into separate strings, so where they stand between text that renders,
23
+ * `{" "}` takes their place when that keeps the strings identical, and
24
+ * otherwise the braces stay as `{}`. The output is re-parsed to prove every
25
+ * element renders the same strings, under the whitespace rules of
26
+ * TypeScript, esbuild and Babel alike.
27
+ * - Blank lines that only separated a removed comment from the code around it
28
+ * go with it: no pile-up of blank lines where a comment stood between two
29
+ * of them, and at the start or end of the file or of a block only the blank
30
+ * lines already against that edge stay.
18
31
  * - A leading byte-order mark is preserved. Reported positions index into the
19
32
  * `source` string exactly as `scanComments(source)` reports them (a BOM
20
33
  * counts as its first character).
@@ -23,7 +36,8 @@ import { scanComments } from "./scanner.js";
23
36
  */
24
37
  export function removeComments(source, options = {}) {
25
38
  const jsx = options.jsx === true;
26
- const comments = scanComments(source, { jsx });
39
+ const analysis = analyzeSource(source, { jsx });
40
+ const comments = analysis.comments;
27
41
  let removed = [];
28
42
  const kept = [];
29
43
  const skipped = [];
@@ -38,16 +52,42 @@ export function removeComments(source, options = {}) {
38
52
  removed.push(comment);
39
53
  }
40
54
  }
55
+ const containers = attachContainers(source, analysis.jsx.containers, comments);
56
+ const containerOf = new Map();
57
+ for (const container of containers) {
58
+ for (const comment of container.comments)
59
+ containerOf.set(comment, container);
60
+ }
61
+ const context = { containerOf, skippable: skippableRanges(comments, containerOf, new Set(removed)) };
62
+ const search = containerSearch(containers);
63
+ // Collapsing blank lines can only move a surviving comment to the very
64
+ // start of the file (or of the line after a shebang), where Bun reads its
65
+ // marker; when that changes a survivor's meaning, the attempt is repeated
66
+ // without the collapse before any removal is given up.
67
+ let tidyBlankLines = true;
41
68
  for (;;) {
42
- removed = shieldNextLineDirectives(source, removed, kept, skipped);
69
+ const shielding = shieldNextLineDirectives(source, removed, kept, skipped, context);
70
+ removed = shielding.removed;
43
71
  if (removed.length === 0) {
44
72
  return { code: source, removed, kept, skipped, changed: false };
45
73
  }
46
- const code = spliceKeepingBom(source, removed);
47
- const survivor = firstChangedSurvivor(source, code, scanComments(code, { jsx }), kept, skipped);
74
+ const removing = new Set(removed);
75
+ search.track(new Set(containers.filter((container) => container.comments.every((c) => removing.has(c)))));
76
+ const edits = removalEdits(removed, containerOf, search.droppable);
77
+ const deletions = edits.filter((edit) => edit.replacement === undefined);
78
+ const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, shielding.lines, analysis.jsx) : [];
79
+ const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart));
80
+ const output = analyzeSource(code, { jsx });
81
+ if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments)))
82
+ continue;
83
+ const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
48
84
  if (survivor === undefined) {
49
85
  return { code, removed, kept, skipped, changed: true };
50
86
  }
87
+ if (blankEdits.length > 0) {
88
+ tidyBlankLines = false;
89
+ continue;
90
+ }
51
91
  // A surviving comment changed meaning: with everything before it deleted
52
92
  // it became the file's first comment (or its very first bytes), turning a
53
93
  // previously inert positional directive live. Re-protect the removal
@@ -62,12 +102,13 @@ export function removeComments(source, options = {}) {
62
102
  removed = removed.filter((comment) => comment !== blocker);
63
103
  kept.push(blocker);
64
104
  kept.sort(byStart);
105
+ tidyBlankLines = true;
65
106
  }
66
107
  }
67
108
  function unexpectedResult() {
68
109
  return new Error("comment removal produced an unexpected result; refusing to continue");
69
110
  }
70
- /** Sort comparator for document order: comments are ordered by where they start. */
111
+ /** Sort comparator for document order: ranges are ordered by where they start. */
71
112
  function byStart(a, b) {
72
113
  return a.start - b.start;
73
114
  }
@@ -128,20 +169,19 @@ function closestRemovalBefore(removals, comment) {
128
169
  return closest;
129
170
  }
130
171
  /**
131
- * Splices the removals out of the source, restoring the byte-order mark when
132
- * dropping a comment-only first line took it along: the mark is an encoding
133
- * artifact, not line content.
172
+ * Applies the edits, restoring the byte-order mark when dropping a
173
+ * comment-only first line took it along: the mark is an encoding artifact,
174
+ * not line content.
134
175
  */
135
- function spliceKeepingBom(source, removals) {
136
- if (!source.startsWith("\uFEFF"))
137
- return splice(source, removals);
138
- // The splicing itself works on the BOM-less body (with ranges shifted to
176
+ function editKeepingBom(source, edits) {
177
+ if (!source.startsWith(""))
178
+ return applyEdits(source, edits);
179
+ // The editing itself works on the BOM-less body (with ranges shifted to
139
180
  // match) so its line handling never sees the mark, then the mark is
140
- // re-attached; this keeps `\uFEFF// gone\n...` from losing the mark with
181
+ // re-attached; this keeps `// gone\n...` from losing the mark with
141
182
  // the dropped line.
142
- const body = source.slice(1);
143
- const shifted = removals.map((comment) => ({ ...comment, start: comment.start - 1, end: comment.end - 1 }));
144
- return `\uFEFF${splice(body, shifted)}`;
183
+ const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
184
+ return `${applyEdits(source.slice(1), shifted)}`;
145
185
  }
146
186
  function isProtected(comment, options) {
147
187
  if (comment.directive !== undefined && options.removeDirectives !== true)
@@ -150,6 +190,183 @@ function isProtected(comment, options) {
150
190
  return true;
151
191
  return false;
152
192
  }
193
+ function attachContainers(source, layouts, comments) {
194
+ const containers = new Map();
195
+ for (const layout of layouts) {
196
+ const inside = [];
197
+ let index = firstIndexAtOrAfter(comments, layout.start, (comment) => comment.start);
198
+ for (let comment = comments[index]; comment !== undefined && comment.end < layout.end; comment = comments[index]) {
199
+ inside.push(comment);
200
+ index += 1;
201
+ }
202
+ containers.set(layout, {
203
+ start: layout.start,
204
+ end: layout.end,
205
+ segment: layout.segment,
206
+ suspect: layout.suspect,
207
+ next: undefined,
208
+ comments: inside,
209
+ mode: "drop",
210
+ spaceRange: spaceRange(source, layout),
211
+ });
212
+ }
213
+ for (const [layout, container] of containers) {
214
+ if (layout.next !== undefined)
215
+ container.next = containers.get(layout.next);
216
+ }
217
+ return [...containers.values()];
218
+ }
219
+ const isHorizontalSpace = (char) => char === " " || char === "\t";
220
+ /**
221
+ * The range `{" "}` would take the place of: a one-line container and the
222
+ * spaces before it when code precedes it on its line (`<b /> {/* x *​/}`), or
223
+ * the spaces after it when it opens its line and code follows. Undefined when
224
+ * no spaces hold it off anything on its line.
225
+ */
226
+ function spaceRange(source, container) {
227
+ if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
228
+ return undefined;
229
+ let lineStart = container.start;
230
+ while (lineStart > 0 && !LINE_TERMINATOR.test(source[lineStart - 1]))
231
+ lineStart -= 1;
232
+ let before = container.start;
233
+ while (isHorizontalSpace(source[before - 1]))
234
+ before -= 1;
235
+ let after = container.end;
236
+ while (isHorizontalSpace(source[after]))
237
+ after += 1;
238
+ if (!isBlank(source.slice(lineStart, container.start))) {
239
+ return before < container.start ? { start: before, end: container.end } : undefined;
240
+ }
241
+ const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
242
+ return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
243
+ }
244
+ /**
245
+ * Comments, and the comment-only containers that may go along with theirs,
246
+ * as sorted ranges: what the search for the code after a directive skips.
247
+ * A container counts as skippable when all its comments are removal
248
+ * candidates, even if some get protected later — skipping it then only moves
249
+ * the found code further down, which shields more, not less.
250
+ */
251
+ function skippableRanges(comments, containerOf, candidates) {
252
+ const ranges = [];
253
+ for (const comment of comments) {
254
+ const container = containerOf.get(comment);
255
+ if (container === undefined || !container.comments.every((inside) => candidates.has(inside))) {
256
+ ranges.push(comment);
257
+ }
258
+ else if (ranges[ranges.length - 1] !== container) {
259
+ ranges.push(container);
260
+ }
261
+ }
262
+ return ranges;
263
+ }
264
+ /** The edits that remove the comments: one per comment, or one per container going with its braces. */
265
+ function removalEdits(removed, containerOf, droppable) {
266
+ const edits = [];
267
+ for (const comment of removed) {
268
+ const container = containerOf.get(comment);
269
+ if (container === undefined || !droppable.has(container) || container.mode === "keep") {
270
+ edits.push({ start: comment.start, end: comment.end });
271
+ }
272
+ else if (comment === container.comments[0]) {
273
+ edits.push(container.mode === "drop"
274
+ ? { start: container.start, end: container.end, jsx: true }
275
+ : { ...container.spaceRange, replacement: '{" "}' });
276
+ }
277
+ }
278
+ return edits;
279
+ }
280
+ /** Indexes of the JSX stretches whose rendering differs; throws when the structure itself does. */
281
+ function changedSegments(before, after) {
282
+ if (before.length !== after.length)
283
+ throw unexpectedResult();
284
+ const changed = new Set();
285
+ for (const [index, rendering] of before.entries()) {
286
+ if (rendering !== after[index])
287
+ changed.add(index);
288
+ }
289
+ return changed;
290
+ }
291
+ /**
292
+ * Decides how each comment-only container goes. Dropping the braces is tried
293
+ * everywhere first; where the re-parsed output then renders a stretch of JSX
294
+ * children differently, that stretch's suspect containers are settled one
295
+ * at a time, in document order — each tries dropping, then `{" "}`, then
296
+ * keeping its braces, while the suspects after it keep theirs — so braces
297
+ * stay only where nothing less keeps the rendering intact. Keeping every
298
+ * suspect's braces always does: the other containers sit between text that
299
+ * renders nothing, which stays so however it is merged or trimmed.
300
+ */
301
+ // The most suspects of one stretch settled one at a time; beyond that they all keep their braces.
302
+ const MAX_SUSPECTS_SETTLED = 16;
303
+ function containerSearch(containers) {
304
+ let droppable = new Set();
305
+ /** Per stretch that rendered differently: its suspects still to settle, the first being tried. */
306
+ const pending = new Map();
307
+ const advance = (container) => {
308
+ if (container.mode === "keep")
309
+ return false;
310
+ container.mode = container.mode === "drop" && container.spaceRange !== undefined ? "space" : "keep";
311
+ return true;
312
+ };
313
+ return {
314
+ get droppable() {
315
+ return droppable;
316
+ },
317
+ track(now) {
318
+ // Protection only ever takes comments out of the removal, so the set
319
+ // only shrinks and its size tells whether it changed.
320
+ if (now.size === droppable.size)
321
+ return;
322
+ droppable = now;
323
+ pending.clear();
324
+ for (const container of containers)
325
+ container.mode = "drop";
326
+ },
327
+ adjust(changed) {
328
+ let adjusted = false;
329
+ for (const segment of changed) {
330
+ let queue = pending.get(segment);
331
+ if (queue === undefined) {
332
+ // The stretch's first difference: from now on its suspects are
333
+ // settled one at a time, the first trying to drop its braces while
334
+ // the rest keep theirs. Every try costs a parse, so a stretch with
335
+ // a great many of them keeps all their braces straight away.
336
+ queue = containers.filter((c) => c.segment === segment && c.suspect && droppable.has(c));
337
+ const giveUp = queue.length > MAX_SUSPECTS_SETTLED;
338
+ for (const container of giveUp ? queue : queue.slice(1))
339
+ container.mode = "keep";
340
+ pending.set(segment, giveUp ? [] : queue);
341
+ if (giveUp || queue.length > 1) {
342
+ adjusted = true;
343
+ continue;
344
+ }
345
+ }
346
+ // The container being tried still changes the rendering: it tries
347
+ // the next mode. Keeping its braces cannot fail, so running out of
348
+ // modes (or of suspects to blame) means the output is not trustworthy.
349
+ const current = queue[0];
350
+ if (current === undefined || !advance(current))
351
+ throw unexpectedResult();
352
+ adjusted = true;
353
+ }
354
+ for (const [segment, queue] of pending) {
355
+ if (changed.has(segment) || queue.length === 0)
356
+ continue;
357
+ // The stretch renders as before: the container being tried is
358
+ // settled, and the next suspect tries dropping its braces.
359
+ queue.shift();
360
+ const next = queue[0];
361
+ if (next !== undefined) {
362
+ next.mode = "drop";
363
+ adjusted = true;
364
+ }
365
+ }
366
+ return adjusted;
367
+ },
368
+ };
369
+ }
153
370
  /**
154
371
  * Re-protects comments whose removal would delete a physical line sitting
155
372
  * under a surviving next-line directive (`@ts-expect-error`,
@@ -157,27 +374,30 @@ function isProtected(comment, options) {
157
374
  * following code up, silently changing which line the directive applies to —
158
375
  * and with it the compiler or linter behaviour. Directives whose tools reach
159
376
  * further get more: below a tsc suppression the first line each compiler
160
- * cannot step over stays, and between a directive that only counts as the
377
+ * cannot step over stays, between a directive that only counts as the
161
378
  * last comment before its target (`@deno-types`, ...) and that target every
162
- * comment stays.
379
+ * comment stays, and a JSX comment container right after a directive in a
380
+ * container (`{/* prettier-ignore *​/}`, which applies to the next sibling)
381
+ * stays too.
163
382
  *
164
383
  * The line checks work on whole lines, not single comments: a line holding
165
384
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
166
- * so every comment on it has to stay. Runs to a fixpoint because a
167
- * re-protected comment can itself be a next-line directive shielding further
168
- * lines (when directives are being removed selectively).
385
+ * so every comment on it has to stay — and a comment-only JSX container takes
386
+ * its braces along. Runs to a fixpoint because a re-protected comment can
387
+ * itself be a next-line directive shielding further lines (when directives
388
+ * are being removed selectively).
169
389
  */
170
- function shieldNextLineDirectives(source, removed, kept, skipped) {
390
+ function shieldNextLineDirectives(source, removed, kept, skipped, context) {
171
391
  const lineOffsets = lineStartOffsets(source);
172
392
  const totalLines = lineOffsets.length;
173
- // Where the code after a directive starts does not depend on which comments
174
- // go, so it is looked up among all of them.
175
- const allComments = [...removed, ...kept, ...skipped].sort(byStart);
176
393
  const shieldedLines = new Set();
177
394
  const blockingLines = new Set();
178
395
  const shieldedGaps = [];
396
+ const siblingTargets = new Set();
179
397
  const processed = new Set();
180
398
  let stillRemoved = removed;
399
+ let units;
400
+ const currentUnits = () => (units ??= removalUnits(stillRemoved, context.containerOf));
181
401
  const collectShields = () => {
182
402
  let added = false;
183
403
  for (const comment of [...kept, ...skipped]) {
@@ -188,7 +408,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
188
408
  if (directive === undefined)
189
409
  continue;
190
410
  if (LAST_COMMENT_DIRECTIVES.has(directive)) {
191
- const codeStart = codeStartAfter(source, comment.end, allComments);
411
+ const codeStart = codeStartAfter(source, comment.end, context.skippable);
192
412
  if (codeStart > comment.end && codeStart < source.length) {
193
413
  shieldedGaps.push([comment.end, codeStart]);
194
414
  added = true;
@@ -196,7 +416,7 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
196
416
  continue;
197
417
  }
198
418
  if (TSC_SUPPRESSIONS.has(directive)) {
199
- for (const line of firstBlockingLines(source, comment, lineOffsets, allComments)) {
419
+ for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
200
420
  if (blockingLines.has(line))
201
421
  continue;
202
422
  blockingLines.add(line);
@@ -205,10 +425,20 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
205
425
  }
206
426
  if (!isNextLineDirective(directive))
207
427
  continue;
428
+ // In JSX, such a directive in braces applies to the sibling after it,
429
+ // whatever line that is on (prettier leaves the element after
430
+ // `{/* prettier-ignore */}` as is): a comment-only container standing
431
+ // between them has to stay. Range and file pragmas target no sibling.
432
+ const holder = context.containerOf.get(comment);
433
+ if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
434
+ siblingTargets.add(holder.next);
435
+ added = true;
436
+ }
208
437
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
209
438
  // `secret(); // nosemgrep`) targets its own line, so it shields nothing
210
- // below it.
211
- if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, comment)) {
439
+ // below it. In JSX, the container holding the comment is what has to
440
+ // stand on lines of its own.
441
+ if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) {
212
442
  continue;
213
443
  }
214
444
  // Counted pragmas like `c8 ignore next 3` cover several lines.
@@ -228,18 +458,19 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
228
458
  const grouped = new Set(group);
229
459
  stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
230
460
  kept.push(...group);
461
+ units = undefined;
231
462
  };
232
463
  const protectShields = () => {
233
464
  let changed = false;
234
465
  for (const line of shieldedLines) {
235
- const group = vanishingLineGroup(source, line, lineOffsets, stillRemoved);
466
+ const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
236
467
  if (group === undefined)
237
468
  continue;
238
469
  protect(group);
239
470
  changed = true;
240
471
  }
241
472
  for (const line of blockingLines) {
242
- const group = lineOpeningGroup(source, line, lineOffsets, stillRemoved);
473
+ const group = lineOpeningGroup(source, line, lineOffsets, currentUnits());
243
474
  if (group === undefined)
244
475
  continue;
245
476
  protect(group);
@@ -252,6 +483,13 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
252
483
  protect(group);
253
484
  changed = true;
254
485
  }
486
+ for (const target of siblingTargets) {
487
+ const group = stillRemoved.filter((comment) => target.comments.includes(comment));
488
+ if (group.length === 0)
489
+ continue;
490
+ protect(group);
491
+ changed = true;
492
+ }
255
493
  return changed;
256
494
  };
257
495
  collectShields();
@@ -262,23 +500,44 @@ function shieldNextLineDirectives(source, removed, kept, skipped) {
262
500
  if (stillRemoved.length !== removed.length) {
263
501
  kept.sort(byStart);
264
502
  }
265
- return stillRemoved;
503
+ return { removed: stillRemoved, lines: shieldedLines };
504
+ }
505
+ /**
506
+ * The removals as the edits delete them: a comment-only container whose
507
+ * comments all go — and that is to go braces and all — is one unit spanning
508
+ * its braces, every other comment a unit of its own. Sorted, as the comments.
509
+ */
510
+ function removalUnits(removals, containerOf) {
511
+ const removing = new Set(removals);
512
+ const units = [];
513
+ for (const comment of removals) {
514
+ const container = containerOf.get(comment);
515
+ if (container === undefined ||
516
+ container.mode !== "drop" ||
517
+ !container.comments.every((inside) => removing.has(inside))) {
518
+ units.push({ start: comment.start, end: comment.end, comments: [comment] });
519
+ }
520
+ else if (units[units.length - 1]?.container !== container) {
521
+ units.push({ start: container.start, end: container.end, comments: container.comments, container });
522
+ }
523
+ }
524
+ return units;
266
525
  }
267
526
  /**
268
527
  * Offset of the first character at or after `from` that is neither
269
- * whitespace nor inside one of the (sorted, non-overlapping) comments, or the
270
- * source length when only comments follow. `from` lies past the start of at
271
- * least one comment (the directive the search is for); when the closest of
528
+ * whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
529
+ * source length when only ranges follow. `from` lies past the start of at
530
+ * least one range (the directive the search is for); when the closest of
272
531
  * them runs past `from` — a block comment opened on the directive's line —
273
532
  * the search resumes at its end.
274
533
  */
275
- function codeStartAfter(source, from, comments) {
276
- let next = firstIndexAtOrAfter(comments, from, (comment) => comment.start);
277
- let index = Math.max(from, comments[next - 1].end);
534
+ function codeStartAfter(source, from, ranges) {
535
+ let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
536
+ let index = Math.max(from, ranges[next - 1].end);
278
537
  while (index < source.length) {
279
- const comment = comments[next];
280
- if (comment !== undefined && comment.start === index) {
281
- index = comment.end;
538
+ const range = ranges[next];
539
+ if (range !== undefined && range.start === index) {
540
+ index = range.end;
282
541
  next += 1;
283
542
  }
284
543
  else if (isBlank(source[index])) {
@@ -290,19 +549,6 @@ function codeStartAfter(source, from, comments) {
290
549
  }
291
550
  return source.length;
292
551
  }
293
- /** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
294
- function firstIndexAtOrAfter(items, value, key) {
295
- let low = 0;
296
- let high = items.length;
297
- while (low < high) {
298
- const middle = (low + high) >> 1;
299
- if (key(items[middle]) < value)
300
- low = middle + 1;
301
- else
302
- high = middle;
303
- }
304
- return low;
305
- }
306
552
  /**
307
553
  * For each TypeScript compiler, the first line between a tsc suppression and
308
554
  * the code on the lines below it that the compiler does not step over, if
@@ -315,10 +561,10 @@ function firstIndexAtOrAfter(items, value, key) {
315
561
  * vertical tab stops tsc 7, while tsc 6 searches on to the next line it
316
562
  * cannot pass.
317
563
  */
318
- function firstBlockingLines(source, comment, lineOffsets, comments) {
564
+ function firstBlockingLines(source, comment, lineOffsets, ranges) {
319
565
  // A suppression applies from the line below it on, so code sharing its line
320
566
  // (`/* @ts-ignore */ foo();`) does not end the search.
321
- const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, comments);
567
+ const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
322
568
  if (codeStart >= source.length)
323
569
  return [];
324
570
  const codeLine = firstIndexAtOrAfter(lineOffsets, codeStart + 1, (offset) => offset);
@@ -334,41 +580,43 @@ function firstBlockingLines(source, comment, lineOffsets, comments) {
334
580
  return found;
335
581
  }
336
582
  /**
337
- * The removal candidates on the given line when one of them opens it (holds
338
- * its first non-whitespace character), so that splicing them out would
339
- * change how the line starts. Returns undefined when a surviving comment
340
- * opens the line instead, or nothing on it is to be removed.
583
+ * The removals on the given line when one of them opens it (holds its first
584
+ * non-whitespace character), so that deleting them would change how the
585
+ * line starts. Returns undefined when something that survives opens the line
586
+ * instead, or nothing on it is to be removed.
341
587
  */
342
- function lineOpeningGroup(source, line, lineOffsets, removals) {
588
+ function lineOpeningGroup(source, line, lineOffsets, units) {
343
589
  // A blocking line always lies above the code line, so the next offset exists.
344
590
  const lineStart = lineOffsets[line - 1];
345
591
  const lineEnd = lineOffsets[line];
346
- const group = removals.filter((comment) => comment.start < lineEnd && comment.end > lineStart);
592
+ const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
347
593
  let first = lineStart;
348
594
  while (first < lineEnd && isBlank(source[first]))
349
595
  first += 1;
350
- return group.some((comment) => comment.start <= first && first < comment.end) ? group : undefined;
596
+ return group.some((unit) => unit.start <= first && first < unit.end)
597
+ ? group.flatMap((unit) => unit.comments)
598
+ : undefined;
351
599
  }
352
600
  /**
353
- * The removal candidates that would make the given line disappear: every
354
- * non-whitespace character of the line lies inside one of them, so splicing
355
- * them out deletes the physical line. Returns undefined when the line
356
- * survives (it holds code, a kept comment, or nothing to remove at all).
601
+ * The removals that would make the given line disappear: every
602
+ * non-whitespace character of the line lies inside one of them, so deleting
603
+ * them deletes the physical line. Returns undefined when the line survives
604
+ * (it holds code, a kept comment, or nothing to remove at all).
357
605
  */
358
- function vanishingLineGroup(source, line, lineOffsets, removals) {
606
+ function vanishingLineGroup(source, line, lineOffsets, units) {
359
607
  // collectShields never shields a line beyond the last one, so the offset exists.
360
608
  const lineStart = lineOffsets[line - 1];
361
609
  const lineEnd = lineOffsets[line] ?? source.length;
362
- const group = removals.filter((comment) => comment.start < lineEnd && comment.end > lineStart);
610
+ const group = units.filter((unit) => unit.start < lineEnd && unit.end > lineStart);
363
611
  if (group.length === 0)
364
612
  return undefined;
365
613
  for (let index = lineStart; index < lineEnd; index += 1) {
366
614
  if (isBlank(source[index]))
367
615
  continue;
368
- if (!group.some((comment) => comment.start <= index && index < comment.end))
616
+ if (!group.some((unit) => unit.start <= index && index < unit.end))
369
617
  return undefined;
370
618
  }
371
- return group;
619
+ return group.flatMap((unit) => unit.comments);
372
620
  }
373
621
  // These suppressions only apply to the following node/line when they stand on
374
622
  // their own line — trailing after code they target (at most) their own line.
@@ -479,19 +727,19 @@ function shieldedLineCount(comment) {
479
727
  return match ? Math.max(1, Number(match[1])) : 1;
480
728
  }
481
729
  /**
482
- * True when nothing but whitespace shares the comment's first and last lines.
730
+ * True when nothing but whitespace shares the range's first and last lines.
483
731
  * Lines are delimited by any ECMAScript line terminator, matching how the
484
732
  * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
485
733
  */
486
- function occupiesWholeLines(source, comment) {
487
- for (let index = comment.start - 1; index >= 0; index -= 1) {
734
+ function occupiesWholeLines(source, range) {
735
+ for (let index = range.start - 1; index >= 0; index -= 1) {
488
736
  const char = source[index];
489
737
  if (LINE_TERMINATOR.test(char))
490
738
  break;
491
739
  if (!isBlank(char))
492
740
  return false;
493
741
  }
494
- for (let index = comment.end; index < source.length; index += 1) {
742
+ for (let index = range.end; index < source.length; index += 1) {
495
743
  const char = source[index];
496
744
  if (LINE_TERMINATOR.test(char))
497
745
  break;
@@ -500,114 +748,4 @@ function occupiesWholeLines(source, comment) {
500
748
  }
501
749
  return true;
502
750
  }
503
- const isHorizontalSpace = (char) => char === " " || char === "\t";
504
- // A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
505
- const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
506
- /** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
507
- function lastLineBreakEnd(chunk) {
508
- for (let index = chunk.length - 1; index >= 0; index -= 1) {
509
- if (LINE_TERMINATOR.test(chunk[index]))
510
- return index + 1;
511
- }
512
- return -1;
513
- }
514
- /**
515
- * Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
516
- * Output is accumulated as segments with incremental line-state tracking, so the
517
- * work stays linear in the source size even for comment-dense files.
518
- */
519
- function splice(source, removals) {
520
- const segments = [];
521
- let cursor = 0;
522
- /** Whether the output's current (unterminated) line is all whitespace so far. */
523
- let lineIsBlank = true;
524
- /** Last character of the output so far ("" while empty). */
525
- let lastChar = "";
526
- const push = (chunk) => {
527
- if (chunk === "")
528
- return;
529
- segments.push(chunk);
530
- const breakEnd = lastLineBreakEnd(chunk);
531
- lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
532
- lastChar = chunk.slice(-1);
533
- };
534
- /** Deletes everything the output holds after its last line break. */
535
- const dropCurrentLine = () => {
536
- while (segments.length > 0) {
537
- const last = segments[segments.length - 1];
538
- const breakEnd = lastLineBreakEnd(last);
539
- if (breakEnd === -1) {
540
- segments.pop();
541
- continue;
542
- }
543
- segments[segments.length - 1] = last.slice(0, breakEnd);
544
- lineIsBlank = true;
545
- lastChar = last.charAt(breakEnd - 1);
546
- return;
547
- }
548
- lineIsBlank = true;
549
- lastChar = "";
550
- };
551
- /** Deletes trailing spaces/tabs of the output's current line. */
552
- const trimLineEnd = () => {
553
- while (segments.length > 0) {
554
- const last = segments[segments.length - 1];
555
- let end = last.length;
556
- while (end > 0 && isHorizontalSpace(last[end - 1]))
557
- end -= 1;
558
- if (end === 0) {
559
- segments.pop();
560
- continue;
561
- }
562
- if (end < last.length)
563
- segments[segments.length - 1] = last.slice(0, end);
564
- lastChar = segments[segments.length - 1].slice(-1);
565
- return;
566
- }
567
- lastChar = "";
568
- };
569
- for (const comment of removals) {
570
- push(source.slice(cursor, comment.start));
571
- cursor = comment.end;
572
- NEXT_LINE_BREAK.lastIndex = cursor;
573
- const lineBreak = NEXT_LINE_BREAK.exec(source);
574
- const contentEnd = lineBreak === null ? source.length : lineBreak.index;
575
- const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
576
- const restContent = source.slice(cursor, contentEnd);
577
- if (lineIsBlank && isBlank(restContent)) {
578
- // The comment occupied whole line(s): drop them entirely.
579
- dropCurrentLine();
580
- cursor = lineEnd;
581
- }
582
- else if (isBlank(restContent)) {
583
- // Trailing comment after code: trim the gap, keep the line break.
584
- trimLineEnd();
585
- push(source.slice(contentEnd, lineEnd));
586
- cursor = lineEnd;
587
- }
588
- else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
589
- // Code on both sides of a block comment that spans lines. Per ECMA-262
590
- // such a comment acts as a line terminator for semicolon insertion, so
591
- // replace it with a real line break (matching the file's style) instead
592
- // of merging the lines.
593
- trimLineEnd();
594
- push(comment.text.includes("\r\n") ? "\r\n" : "\n");
595
- }
596
- else {
597
- // Inline comment with code after it on the same line.
598
- const next = source.charAt(cursor);
599
- if (lastChar !== "" && !isBlank(lastChar) && !isBlank(next)) {
600
- // `a/* x */b` would merge tokens: keep them apart.
601
- push(" ");
602
- }
603
- else if (lastChar === "" || isBlank(lastChar)) {
604
- // Collapse the horizontal whitespace that followed the comment.
605
- while (cursor < lineEnd && isHorizontalSpace(source[cursor]))
606
- cursor += 1;
607
- }
608
- }
609
- }
610
- segments.push(source.slice(cursor));
611
- return segments.join("");
612
- }
613
751
  //# sourceMappingURL=remove.js.map