@kongyo2/ts-comment-scanner 1.15.0 → 1.18.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
@@ -6,9 +6,10 @@ import {
6
6
  isLegalComment,
7
7
  type DirectivePlacement,
8
8
  } from "./directives.js";
9
+ import { detachedComments, type ImportGroupGap } from "./imports.js";
9
10
  import type { JsxContainer } from "./jsx.js";
10
11
  import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
11
- import { firstIndexAtOrAfter, type Range } from "./ranges.js";
12
+ import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
12
13
  import { analyzeSource } from "./scanner.js";
13
14
  import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
14
15
  import type { Comment } from "./types.js";
@@ -41,10 +42,12 @@ export interface RemoveResult {
41
42
  * its row decide which node it trails), comments whose removal would shift
42
43
  * code under a next-line directive or join another line to one a directive
43
44
  * covers, or that sit between a directive and the code it reaches past
44
- * them for (or the JSX sibling it applies to), and comments whose removal
45
+ * them for (or the JSX sibling it applies to), comments whose removal
45
46
  * would move a position-dependent directive
46
47
  * (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
47
- * file-start marker) into effect.
48
+ * file-start marker) into effect, and comments whose removal would join
49
+ * two groups of imports or exports that TypeScript's organizeImports,
50
+ * dprint or Biome sorts apart, or split one.
48
51
  */
49
52
  kept: Comment[];
50
53
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -77,6 +80,10 @@ export interface RemoveResult {
77
80
  * go with it: no pile-up of blank lines where a comment stood between two
78
81
  * of them, and at the start or end of the file or of a block only the blank
79
82
  * lines already against that edge stay.
83
+ * - Imports and exports stay in the groups the import sorters read: where
84
+ * deleting a comment would join two groups that TypeScript's
85
+ * organizeImports, dprint or Biome sorts apart, or split one, the comment
86
+ * stays.
80
87
  * - A leading byte-order mark is preserved. Reported positions index into the
81
88
  * `source` string exactly as `scanComments(source)` reports them (a BOM
82
89
  * counts as its first character).
@@ -136,6 +143,19 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
136
143
 
137
144
  if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
138
145
 
146
+ // Where the output joins two groups of imports or exports, or splits one,
147
+ // a sorter would sort them differently: the removal that does it is
148
+ // re-protected, and the attempt repeated.
149
+ const holders = importGroupHolders(source, comments, analysis.importGroups, output.importGroups, removed);
150
+ if (holders.length > 0) {
151
+ const holding = new Set(holders);
152
+ removed = removed.filter((comment) => !holding.has(comment));
153
+ kept.push(...holders);
154
+ kept.sort(byStart);
155
+ tidyBlankLines = true;
156
+ continue;
157
+ }
158
+
139
159
  const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
140
160
  if (survivor === undefined) {
141
161
  return { code, removed, kept, skipped, changed: true };
@@ -236,6 +256,120 @@ function closestRemovalBefore(removals: readonly Comment[], comment: Comment): C
236
256
  return closest;
237
257
  }
238
258
 
259
+ /**
260
+ * The removals to re-protect so that the output starts exactly the groups of
261
+ * imports and exports the source starts. The gaps belong to the same
262
+ * declarations in both texts, since removing comments leaves the statements
263
+ * as they are. Where the output joins two groups at a gap for some sorter,
264
+ * the removal holding the gap open stays (see joiningRemoval, and
265
+ * detachedRemoval for Biome's chunks); where it splits one there, the
266
+ * multi-line comment a line break took the place of stays (see
267
+ * partingRemoval).
268
+ */
269
+ function importGroupHolders(
270
+ source: string,
271
+ comments: readonly Comment[],
272
+ before: readonly ImportGroupGap[],
273
+ after: readonly ImportGroupGap[],
274
+ removals: readonly Comment[],
275
+ ): Comment[] {
276
+ if (before.length !== after.length) throw unexpectedResult();
277
+ const holders = new Set<Comment>();
278
+ const hold = (holder: Comment | undefined): void => {
279
+ if (holder === undefined) throw unexpectedResult();
280
+ holders.add(holder);
281
+ };
282
+ for (const [index, gap] of before.entries()) {
283
+ const splits = (after[index] as ImportGroupGap).splits;
284
+ const joined = gap.splits.filter((sorter) => !splits.includes(sorter));
285
+ const parted = splits.some((sorter) => !gap.splits.includes(sorter));
286
+ if (joined.length === 0 && !parted) continue;
287
+ const inGap = removals.filter((comment) => comment.start >= gap.start && comment.end <= gap.end);
288
+ // TypeScript and dprint count the gap's line breaks, Biome reads its comments.
289
+ const linesJoined = joined.filter((sorter) => sorter !== "biome");
290
+ if (linesJoined.length > 0) hold(joiningRemoval(source, gap, inGap, linesJoined.includes("typescript")));
291
+ if (joined.includes("biome")) hold(detachedRemoval(source, gap, comments, inGap));
292
+ if (parted) hold(partingRemoval(source, gap, inGap, removals));
293
+ }
294
+ return [...holders];
295
+ }
296
+
297
+ /**
298
+ * The removal that Biome's chunk boundary at the gap rests on: the last of
299
+ * the gap's detached comments that goes (see detachedComments). Kept, it is
300
+ * detached again, for the empty line after it stays: the removals past it all
301
+ * come after the gap's last empty line, and a run of blank lines keeps the
302
+ * ones in front of its first vanishing line (see blankLineEdits).
303
+ */
304
+ function detachedRemoval(
305
+ source: string,
306
+ gap: Range,
307
+ comments: readonly Comment[],
308
+ inGap: readonly Comment[],
309
+ ): Comment | undefined {
310
+ return detachedComments(source, gap, comments).findLast((comment) => inGap.includes(comment));
311
+ }
312
+
313
+ /**
314
+ * The removal in the gap whose deletion takes line breaks out of it: one
315
+ * spanning lines, or one on a line below the token the gap follows. When only
316
+ * dprint joins the groups, it counted line breaks inside a comment, and the
317
+ * first comment spanning lines stays: the removals after it then trail it
318
+ * and go without their line breaks, while in front of a later one a line
319
+ * break would take their place. Otherwise the last of them that ends above
320
+ * the declaration's line stays, holding a line of its own with the line break
321
+ * TypeScript counts and the line dprint counts; failing that, the last of
322
+ * them, which still keeps the declaration's line down.
323
+ */
324
+ function joiningRemoval(
325
+ source: string,
326
+ gap: Range,
327
+ inGap: readonly Comment[],
328
+ typescriptJoins: boolean,
329
+ ): Comment | undefined {
330
+ const breaking = inGap.filter(
331
+ (comment) => comment.endLine > comment.line || LINE_TERMINATOR.test(source.slice(gap.start, comment.start)),
332
+ );
333
+ const spanning = typescriptJoins ? undefined : breaking.find((comment) => comment.endLine > comment.line);
334
+ return (
335
+ spanning ??
336
+ breaking.findLast((comment) => LINE_TERMINATOR.test(source.slice(comment.end, gap.end))) ??
337
+ breaking.at(-1)
338
+ );
339
+ }
340
+
341
+ /**
342
+ * The removal in the gap that a line break takes the place of (see
343
+ * applyEdits): a multi-line comment with something that survives the
344
+ * removals in front of it on its first line, and anything after it on its
345
+ * last line — the token before the gap and the declaration after it count.
346
+ * Its own line breaks are no NewLineTrivia to TypeScript, but the one put in
347
+ * its place is.
348
+ */
349
+ function partingRemoval(
350
+ source: string,
351
+ gap: Range,
352
+ inGap: readonly Comment[],
353
+ removals: readonly Comment[],
354
+ ): Comment | undefined {
355
+ // Whether the line holds what `stands` accepts, from `from` on, stepping by `step` until `to`.
356
+ const onLine = (from: number, to: number, step: 1 | -1, stands: (index: number) => boolean): boolean => {
357
+ for (let index = from; index !== to; index += step) {
358
+ if (LINE_TERMINATOR.test(source[index] as string)) return false;
359
+ if (stands(index)) return true;
360
+ }
361
+ return true;
362
+ };
363
+ const content = (index: number): boolean => !isBlank(source[index] as string);
364
+ const survivor = (index: number): boolean => content(index) && rangeAt(removals, index) === undefined;
365
+ return inGap.findLast(
366
+ (comment) =>
367
+ comment.endLine > comment.line &&
368
+ onLine(comment.start - 1, gap.start - 1, -1, survivor) &&
369
+ onLine(comment.end, gap.end, 1, content),
370
+ );
371
+ }
372
+
239
373
  /**
240
374
  * Applies the edits, restoring the byte-order mark when dropping a
241
375
  * comment-only first line took it along: the mark is an encoding artifact,
package/src/scanner.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import ts from "typescript";
2
2
  import { isFileWidePosition, type PrecedingNode } from "./astgrep.js";
3
3
  import { bunMarkerOffset, carriesAstGrepSuppression, detectDirective, type DirectivePlacement } from "./directives.js";
4
+ import { importGroupGaps, type ImportGroupGap } from "./imports.js";
4
5
  import { jsxLayout, type JsxLayout } from "./jsx.js";
5
6
  import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
6
7
  import type { Comment, CommentKind } from "./types.js";
@@ -19,6 +20,8 @@ export interface SourceAnalysis {
19
20
  jsx: JsxLayout;
20
21
  /** What stands in front of each comment carrying an ast-grep suppression (see precedingNode). */
21
22
  preceding: ReadonlyMap<Comment, PrecedingNode>;
23
+ /** The gaps in front of the import and export declarations the import sorters group (see importGroupGaps). */
24
+ importGroups: ImportGroupGap[];
22
25
  }
23
26
 
24
27
  export function analyzeSource(source: string, options: ScanOptions = {}): SourceAnalysis {
@@ -175,7 +178,12 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
175
178
  }
176
179
  }
177
180
 
178
- return { comments, jsx: jsxLayout(source, sourceFile, jsxParents, comments), preceding };
181
+ return {
182
+ comments,
183
+ jsx: jsxLayout(source, sourceFile, jsxParents, comments),
184
+ preceding,
185
+ importGroups: importGroupGaps(source, sourceFile, comments),
186
+ };
179
187
  }
180
188
 
181
189
  /**