@kongyo2/ts-comment-scanner 1.11.0 → 1.15.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,8 +1,15 @@
1
- import { activePositionalDirectives, bunMarkerOffset, isLegalComment, type DirectivePlacement } from "./directives.js";
1
+ import { astGrepReach, isFileWidePosition, rowStartOffsets, type AstGrepReach, type PrecedingNode } from "./astgrep.js";
2
+ import {
3
+ activePositionalDirectives,
4
+ bunMarkerOffset,
5
+ carriesAstGrepSuppression,
6
+ isLegalComment,
7
+ type DirectivePlacement,
8
+ } from "./directives.js";
2
9
  import type { JsxContainer } from "./jsx.js";
3
10
  import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
4
11
  import { firstIndexAtOrAfter, type Range } from "./ranges.js";
5
- import { analyzeSource, isFileWidePosition, opensFirstRow } from "./scanner.js";
12
+ import { analyzeSource } from "./scanner.js";
6
13
  import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
7
14
  import type { Comment } from "./types.js";
8
15
 
@@ -29,13 +36,15 @@ export interface RemoveResult {
29
36
  removed: Comment[];
30
37
  /**
31
38
  * Comments kept because they are protected: directives, license headers,
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.
39
+ * comments the `keep` predicate claims, comments whose removal would
40
+ * change what an ast-grep suppression covers (the ones in front of it on
41
+ * its row decide which node it trails), comments whose removal would shift
42
+ * code under a next-line directive or join another line to one a directive
43
+ * 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
+ * would move a position-dependent directive
46
+ * (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
47
+ * file-start marker) into effect.
39
48
  */
40
49
  kept: Comment[];
41
50
  /** Comments excluded from removal by the `shouldRemove` predicate. */
@@ -97,7 +106,11 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
97
106
  for (const container of containers) {
98
107
  for (const comment of container.comments) containerOf.set(comment, container);
99
108
  }
100
- const context: ShieldContext = { containerOf, skippable: skippableRanges(comments, containerOf, new Set(removed)) };
109
+ const context: ShieldContext = {
110
+ containerOf,
111
+ skippable: skippableRanges(comments, containerOf, new Set(removed)),
112
+ preceding: analysis.preceding,
113
+ };
101
114
  const search = containerSearch(containers);
102
115
  // Collapsing blank lines can only move a surviving comment to the very
103
116
  // start of the file (or of the line after a shebang), where Bun reads its
@@ -199,8 +212,8 @@ function firstChangedSurvivor(
199
212
  * without a parse: the comment is the file's first comment when nothing but
200
213
  * whitespace (or a shebang) precedes it — any earlier comment's text keeps
201
214
  * 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
215
+ * reads its marker, and in ast-grep's file-wide position when it opens the
216
+ * first row with the next node two or more rows down. Header status
204
217
  * needs the token structure, but removing comments never moves a comment
205
218
  * across the first token, so the positional check does not consult it.
206
219
  */
@@ -461,11 +474,16 @@ interface ShieldContext {
461
474
  containerOf: ReadonlyMap<Comment, Container>;
462
475
  /** Comments and the containers that may go with theirs, sorted: what code starts after. */
463
476
  skippable: readonly Range[];
477
+ /** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
478
+ preceding: ReadonlyMap<Comment, PrecedingNode>;
464
479
  }
465
480
 
466
481
  interface Shielding {
467
482
  removed: Comment[];
468
- /** Lines (1-based) that a surviving next-line directive reaches. */
483
+ /**
484
+ * Lines (1-based) that a surviving directive reaches: the ones below a
485
+ * next-line directive, and the one a line directive stands on.
486
+ */
469
487
  lines: Set<number>;
470
488
  }
471
489
 
@@ -483,13 +501,16 @@ interface RemovalUnit extends Range {
483
501
  * under a surviving next-line directive (`@ts-expect-error`,
484
502
  * `eslint-disable-next-line`, ...). Dropping such a line would shift the
485
503
  * following code up, silently changing which line the directive applies to —
486
- * and with it the compiler or linter behaviour. Directives whose tools reach
487
- * further get more: below a tsc suppression the first line each compiler
488
- * cannot step over stays, between a directive that only counts as the
489
- * last comment before its target (`@deno-types`, ...) and that target every
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.
504
+ * and with it the compiler or linter behaviour. Nor may a removal join
505
+ * another line to one a directive covers, the line a line directive
506
+ * (`eslint-disable-line`, ...) stands on included. Directives whose tools
507
+ * reach further get more: below a tsc suppression the first line each
508
+ * compiler cannot step over stays, oxlint's line-feed rows add the lines they
509
+ * run over, between a directive that only counts as the last comment before
510
+ * its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
511
+ * only applies to the node right after it and that target every comment
512
+ * stays, and a JSX comment container right after a directive in a container
513
+ * (`{/* prettier-ignore *​/}`, which applies to the next sibling) stays too.
493
514
  *
494
515
  * The line checks work on whole lines, not single comments: a line holding
495
516
  * several removable comments (`/* one *​/ /* two *​/`) vanishes just the same,
@@ -511,29 +532,52 @@ function shieldNextLineDirectives(
511
532
  const blockingLines = new Set<number>();
512
533
  const shieldedGaps: Array<readonly [number, number]> = [];
513
534
  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[] = [];
535
+ const astGrepReaches: AstGrepReach[] = [];
518
536
  const processed = new Set<Comment>();
519
537
  let stillRemoved = removed;
520
538
  let units: RemovalUnit[] | undefined;
521
539
  const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
540
+ let rows: number[] | undefined;
541
+ /** The (1-based) line holding the offset. */
542
+ const lineOf = (offset: number): number => firstIndexAtOrAfter(lineOffsets, offset + 1, (start) => start);
522
543
 
523
544
  const collectShields = (): boolean => {
524
545
  let added = false;
546
+ const shieldLines = (from: number, to: number): void => {
547
+ for (let line = from; line <= Math.min(to, totalLines); line += 1) {
548
+ if (shieldedLines.has(line)) continue;
549
+ shieldedLines.add(line);
550
+ added = true;
551
+ }
552
+ };
525
553
  for (const comment of [...kept, ...skipped]) {
526
554
  if (processed.has(comment)) continue;
527
555
  processed.add(comment);
528
556
  const directive = comment.directive;
529
557
  if (directive === undefined) continue;
530
- if (LAST_COMMENT_DIRECTIVES.has(directive)) {
558
+ if (carriesAstGrepSuppression(comment.text)) {
559
+ const reach = astGrepReach(
560
+ source,
561
+ (rows ??= rowStartOffsets(source)),
562
+ comment,
563
+ context.preceding.get(comment) as PrecedingNode,
564
+ );
565
+ astGrepReaches.push(reach);
566
+ if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
567
+ for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
568
+ shieldedLines.add(line);
569
+ }
570
+ }
571
+ added = true;
572
+ }
573
+ const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
574
+ if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
531
575
  const codeStart = codeStartAfter(source, comment.end, context.skippable);
532
576
  if (codeStart > comment.end && codeStart < source.length) {
533
577
  shieldedGaps.push([comment.end, codeStart]);
534
578
  added = true;
535
579
  }
536
- continue;
580
+ if (lastComment) continue;
537
581
  }
538
582
  if (TSC_SUPPRESSIONS.has(directive)) {
539
583
  for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
@@ -542,11 +586,14 @@ function shieldNextLineDirectives(
542
586
  added = true;
543
587
  }
544
588
  }
545
- if (SIBLING_SENSITIVE.has(directive)) {
546
- linePrefixes.push(comment);
547
- if (opensFirstRow(source, comment.start)) lineSuffixes.push(comment);
548
- added = true;
589
+ // oxlint reaches its rows' lines past the ones counted below.
590
+ if (OXLINT_ROW_DIRECTIVES.has(directive)) {
591
+ const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
592
+ if (reach !== undefined) shieldLines(reach.from, reach.to);
549
593
  }
594
+ // A line directive covers the line it stands on, which no removal may
595
+ // join to another (see joiningLineGroup).
596
+ if (isOwnLineDirective(directive)) shieldLines(comment.line, comment.line);
550
597
  if (!isNextLineDirective(directive)) continue;
551
598
  // In JSX, such a directive in braces applies to the sibling after it,
552
599
  // whatever line that is on (prettier leaves the element after
@@ -560,22 +607,10 @@ function shieldNextLineDirectives(
560
607
  // A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
561
608
  // `secret(); // nosemgrep`) targets its own line, so it shields nothing
562
609
  // 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;
610
+ // stand on lines of its own.
611
+ if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) continue;
570
612
  // Counted pragmas like `c8 ignore next 3` cover several lines.
571
- for (let offset = 1; offset <= shieldedLineCount(comment); offset += 1) {
572
- const line = comment.endLine + offset;
573
- if (line > totalLines) break;
574
- if (!shieldedLines.has(line)) {
575
- shieldedLines.add(line);
576
- added = true;
577
- }
578
- }
613
+ shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment));
579
614
  }
580
615
  return added;
581
616
  };
@@ -590,7 +625,9 @@ function shieldNextLineDirectives(
590
625
  const protectShields = (): boolean => {
591
626
  let changed = false;
592
627
  for (const line of shieldedLines) {
593
- const group = vanishingLineGroup(source, line, lineOffsets, currentUnits());
628
+ const group =
629
+ vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
630
+ joiningLineGroup(source, line, lineOffsets, currentUnits());
594
631
  if (group === undefined) continue;
595
632
  protect(group);
596
633
  changed = true;
@@ -613,21 +650,8 @@ function shieldNextLineDirectives(
613
650
  protect(group);
614
651
  changed = true;
615
652
  }
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);
653
+ for (const reach of astGrepReaches) {
654
+ const group = currentUnits().filter((unit) => reach.holds(unit));
631
655
  if (group.length === 0) continue;
632
656
  protect(group.flatMap((unit) => unit.comments));
633
657
  changed = true;
@@ -775,6 +799,80 @@ function vanishingLineGroup(
775
799
  return group.flatMap((unit) => unit.comments);
776
800
  }
777
801
 
802
+ /**
803
+ * The removals that would join the given line to another: they reach past
804
+ * one of its ends, with something other than whitespace beyond them on the
805
+ * line they run into. Deleting such a comment takes its line breaks along —
806
+ * with nothing in their place right after an opening bracket or before
807
+ * closing punctuation (see applyEdits) — so what stands beyond it can land on
808
+ * the line, or what stands on the line move off it, and a directive covering
809
+ * the line would cover other code. (With only whitespace beyond, the lines
810
+ * the comment held vanish, which vanishingLineGroup weighs.)
811
+ */
812
+ function joiningLineGroup(
813
+ source: string,
814
+ line: number,
815
+ lineOffsets: readonly number[],
816
+ units: readonly RemovalUnit[],
817
+ ): Comment[] | undefined {
818
+ const lineStart = lineOffsets[line - 1] as number;
819
+ const nextLineStart = lineOffsets[line];
820
+ const lineEnd = nextLineStart ?? source.length;
821
+ const group = units.filter(
822
+ (unit) =>
823
+ unit.start < lineEnd &&
824
+ unit.end > lineStart &&
825
+ ((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
826
+ (unit.start < lineStart && !opensItsLine(source, unit))),
827
+ );
828
+ return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
829
+ }
830
+
831
+ // The directives oxlint reads on rows of its own (see oxlintReachLines).
832
+ const OXLINT_ROW_DIRECTIVES = new Set([
833
+ "eslint-disable-next-line",
834
+ "oxlint-disable-next-line",
835
+ "eslint-disable-line",
836
+ "oxlint-disable-line",
837
+ ]);
838
+
839
+ /**
840
+ * The lines (1-based, inclusive) that oxlint applies an `eslint-` or
841
+ * `oxlint-` line or next-line comment to, past the ones the line-based
842
+ * shields take in. oxlint finds the stretch on rows that only a line feed
843
+ * ends (`split_inclusive('\n')` and `lines()` in its `disable_directives.rs`):
844
+ * a next-line comment covers the rest of the row its text ends on and the
845
+ * row after it, a line comment its own row up to the comment. A lone CR,
846
+ * U+2028 or U+2029 — a line break to tsc and ESLint — ends no row there, so
847
+ * a row can run over several of the lines counted here. Undefined when a
848
+ * next-line comment stands on the file's last row: oxlint then covers the
849
+ * rest of the file, which no removal changes.
850
+ */
851
+ function oxlintReachLines(
852
+ source: string,
853
+ rows: readonly number[],
854
+ lineOf: (offset: number) => number,
855
+ comment: Comment,
856
+ directive: string,
857
+ ): { from: number; to: number } | undefined {
858
+ const rowOf = (offset: number): number => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
859
+ if (!directive.endsWith("-next-line")) {
860
+ return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
861
+ }
862
+ // The comment's text ends in front of a block comment's `*/`.
863
+ const row = rowOf(comment.kind === "block" ? comment.end - 2 : comment.end);
864
+ if (rows[row + 1] === undefined) return undefined;
865
+ return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
866
+ }
867
+
868
+ /**
869
+ * Whether the directive covers the line it stands on: the `-line` forms of
870
+ * the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...).
871
+ */
872
+ function isOwnLineDirective(name: string): boolean {
873
+ return name.endsWith("-line") && !name.endsWith("-next-line");
874
+ }
875
+
778
876
  // These suppressions only apply to the following node/line when they stand on
779
877
  // their own line — trailing after code they target (at most) their own line.
780
878
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -789,16 +887,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
789
887
  "nosemgrep",
790
888
  ]);
791
889
 
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
-
802
890
  // The suppressions tsc looks past blank and `//` lines for (see
803
891
  // firstBlockingLines). They stay next-line directives as well: Flow, which
804
892
  // honours them in TypeScript files, applies them to the very next line.
@@ -829,9 +917,32 @@ const LAST_COMMENT_DIRECTIVES = new Set([
829
917
  "no-default",
830
918
  ]);
831
919
 
920
+ // The coverage hints ast-v8-to-istanbul, Vitest's v8 provider, applies to the
921
+ // node that starts at the very token after them: with another comment in
922
+ // between, the hint has no target, and deleting that comment would bring it
923
+ // into effect. The gap up to the code is shielded like the one above, on top
924
+ // of the lines below the hint, which c8, Node and monocart count.
925
+ const NEXT_TOKEN_DIRECTIVES = new Set([
926
+ "istanbul-ignore-next",
927
+ "istanbul-ignore-if",
928
+ "istanbul-ignore-else",
929
+ "c8-ignore-next",
930
+ "c8-ignore-if",
931
+ "c8-ignore-else",
932
+ "v8-ignore-next",
933
+ "v8-ignore-if",
934
+ "v8-ignore-else",
935
+ "node:coverage-ignore",
936
+ "node:coverage-ignore-if",
937
+ "node:coverage-ignore-else",
938
+ ]);
939
+
832
940
  // Directives that target the line or node directly below them. File- and
833
- // range-scoped pragmas (`c8 ignore start`, `istanbul ignore file`, ...) are
941
+ // range-scoped pragmas (`istanbul-ignore-file`, `c8-ignore-start`, ...) are
834
942
  // absent: they do not target the following line, so they never shield it.
943
+ // They go by their names here: Vitest's v8 provider reads a range hint
944
+ // spelled out on any line of this file, and would drop the rest of the file
945
+ // from this project's coverage.
835
946
  const NEXT_LINE_DIRECTIVES = new Set([
836
947
  "@ts-ignore",
837
948
  "@ts-expect-error",
@@ -843,19 +954,18 @@ const NEXT_LINE_DIRECTIVES = new Set([
843
954
  "oxfmt-ignore",
844
955
  "dprint-ignore",
845
956
  "rome-ignore",
846
- "istanbul-ignore",
847
957
  "istanbul-ignore-next",
848
958
  "istanbul-ignore-if",
849
959
  "istanbul-ignore-else",
850
- "c8-ignore",
851
960
  "c8-ignore-next",
852
961
  "c8-ignore-if",
853
962
  "c8-ignore-else",
854
- "v8-ignore",
855
963
  "v8-ignore-next",
856
964
  "v8-ignore-if",
857
965
  "v8-ignore-else",
858
966
  "node:coverage-ignore",
967
+ "node:coverage-ignore-if",
968
+ "node:coverage-ignore-else",
859
969
  // Directives that hang off the node or line directly below them: a
860
970
  // whole-line comment between them and their target would change what
861
971
  // they apply to, so the line below stays shielded.
@@ -880,24 +990,30 @@ const NEXT_LINE_DIRECTIVES = new Set([
880
990
  "pragma-allowlist-nextline-secret",
881
991
  "cspell:disable-next",
882
992
  "HTML",
883
- "ast-grep-ignore",
884
993
  ]);
885
994
 
886
995
  function isNextLineDirective(name: string): boolean {
887
996
  return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
888
997
  }
889
998
 
890
- // The coverage pragmas that actually parse a line count after `ignore next`
891
- // (c8 / v8-to-istanbul, Vitest's ast-v8-to-istanbul, and Node's test-runner
892
- // coverage). Everything else — `@ts-expect-error`, `eslint-disable-next-line`
893
- // and friends — covers exactly one line, no matter what prose like
894
- // "we ignore next 3 legacy calls" appears in the reason text.
895
- const COUNTED_NEXT_LINE = new Set(["istanbul-ignore-next", "c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
999
+ // The coverage pragmas a line count after `ignore next` applies to: c8
1000
+ // (v8-to-istanbul), Node's test-runner coverage and monocart-coverage-reports
1001
+ // count lines. istanbul and ast-v8-to-istanbul, Vitest's providers, apply the
1002
+ // hint to the next node whatever count follows, so `istanbul ignore next 3`
1003
+ // covers one line. Everything else — `@ts-expect-error`,
1004
+ // `eslint-disable-next-line` and friends — covers exactly one line too, no
1005
+ // matter what prose like "we ignore next 3 legacy calls" appears in the
1006
+ // reason text.
1007
+ const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
896
1008
 
897
- /** Number of following lines a next-line pragma covers (`c8 ignore next 3` → 3). */
1009
+ /**
1010
+ * Number of following lines a next-line pragma covers (`c8 ignore next 3` →
1011
+ * 3). monocart reads the count past any whitespace or none, and c8 and
1012
+ * monocart stop at the last digit, so `next3` and `next 3x` count 3 as well.
1013
+ */
898
1014
  function shieldedLineCount(comment: Comment): number {
899
1015
  if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive)) return 1;
900
- const match = /\bignore\s+next\s+(\d+)\b/.exec(comment.text);
1016
+ const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
901
1017
  return match ? Math.max(1, Number(match[1])) : 1;
902
1018
  }
903
1019
 
@@ -907,7 +1023,11 @@ function shieldedLineCount(comment: Comment): number {
907
1023
  * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
908
1024
  */
909
1025
  function occupiesWholeLines(source: string, range: Range): boolean {
910
- if (!opensItsLine(source, range)) return false;
1026
+ return opensItsLine(source, range) && closesItsLine(source, range);
1027
+ }
1028
+
1029
+ /** True when nothing but whitespace follows the range on its last line. */
1030
+ function closesItsLine(source: string, range: Range): boolean {
911
1031
  for (let index = range.end; index < source.length; index += 1) {
912
1032
  const char = source[index] as string;
913
1033
  if (LINE_TERMINATOR.test(char)) break;
package/src/scanner.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import ts from "typescript";
2
- import { bunMarkerOffset, detectDirective, type DirectivePlacement } from "./directives.js";
2
+ import { isFileWidePosition, type PrecedingNode } from "./astgrep.js";
3
+ import { bunMarkerOffset, carriesAstGrepSuppression, detectDirective, type DirectivePlacement } from "./directives.js";
3
4
  import { jsxLayout, type JsxLayout } from "./jsx.js";
4
- import { isBlank } from "./lines.js";
5
- import { firstIndexAtOrAfter } from "./ranges.js";
5
+ import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
6
6
  import type { Comment, CommentKind } from "./types.js";
7
7
 
8
8
  export interface ScanOptions {
@@ -17,6 +17,8 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
17
17
  export interface SourceAnalysis {
18
18
  comments: Comment[];
19
19
  jsx: JsxLayout;
20
+ /** What stands in front of each comment carrying an ast-grep suppression (see precedingNode). */
21
+ preceding: ReadonlyMap<Comment, PrecedingNode>;
20
22
  }
21
23
 
22
24
  export function analyzeSource(source: string, options: ScanOptions = {}): SourceAnalysis {
@@ -31,6 +33,14 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
31
33
  const seen = new Set<number>();
32
34
  /** Every token, in document order. */
33
35
  const tokens: ts.Node[] = [];
36
+ /**
37
+ * The insides of the blocks with no statements and the switches with no
38
+ * clauses (see DirectivePlacement.emptyBlock), from just past the opening
39
+ * brace to the block's end — from the `static` keyword on for a class
40
+ * static block — in document order. Nothing but comments sits in such a
41
+ * block, so no two of them overlap.
42
+ */
43
+ const emptyBlocks: Range[] = [];
34
44
 
35
45
  const collect = (found: readonly ts.CommentRange[] | undefined): void => {
36
46
  if (!found) return;
@@ -48,6 +58,17 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
48
58
  while (pending.length > 0) {
49
59
  const node = pending.pop() as ts.Node;
50
60
  if (ts.isJsxElement(node) || ts.isJsxFragment(node)) jsxParents.push(node);
61
+ if ((ts.isBlock(node) && node.statements.length === 0) || (ts.isCaseBlock(node) && node.clauses.length === 0)) {
62
+ // A block the parser had to make up, with no opening brace, holds nothing.
63
+ const start = node.getStart(sourceFile);
64
+ if (source[start] === "{") {
65
+ // oxlint's no-empty-static-block counts the comments of the whole
66
+ // static block, `static /* c */ {}` included (its span starts at the
67
+ // keyword); no comment starts inside the keyword itself.
68
+ const from = ts.isClassStaticBlockDeclaration(node.parent) ? node.parent.getStart(sourceFile) : start + 1;
69
+ emptyBlocks.push({ start: from, end: node.end });
70
+ }
71
+ }
51
72
  // JSDoc nodes start inside the comment text itself; descending into them
52
73
  // would hide the comment (e.g. a trailing JSDoc attached to the
53
74
  // end-of-file token). Treat them as trivia like any other comment.
@@ -96,6 +117,7 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
96
117
  const shebangEnd = ts.getShebang(source)?.length ?? 0;
97
118
  const firstCommentPos = sorted.length > 0 ? (sorted[0] as ts.CommentRange).pos : -1;
98
119
  const bunMarkerPos = bunMarkerOffset(source);
120
+ const emptyFile = holdsNoCode(sourceFile);
99
121
  // The token after a comment, past any further comments: no token overlaps
100
122
  // a comment, so it is the first one ending at or past the comment's end
101
123
  // (the end-of-file token ends at the text's end). Token ends never
@@ -113,7 +135,11 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
113
135
  header: range.pos < firstTokenStart,
114
136
  firstComment: firstComment && /^\s*$/.test(source.slice(shebangEnd, range.pos)),
115
137
  fileStart: range.pos === bunMarkerPos,
138
+ // A comment opening the first row has only whitespace in front of it,
139
+ // so only the first comment needs the check.
116
140
  fileWide: firstComment && isFileWidePosition(source, range.pos, range.end),
141
+ emptyBlock: rangeAt(emptyBlocks, range.pos) !== undefined,
142
+ emptyFile,
117
143
  get fallthroughSlot() {
118
144
  return slotsOf().fallthroughSlot;
119
145
  },
@@ -142,31 +168,42 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
142
168
  };
143
169
  });
144
170
 
145
- return { comments, jsx: jsxLayout(source, sourceFile, jsxParents, comments) };
171
+ const preceding = new Map<Comment, PrecedingNode>();
172
+ for (const [index, comment] of comments.entries()) {
173
+ if (carriesAstGrepSuppression(comment.text)) {
174
+ preceding.set(comment, precedingNode(sourceFile, tokens, comments[index - 1], comment.start));
175
+ }
176
+ }
177
+
178
+ return { comments, jsx: jsxLayout(source, sourceFile, jsxParents, comments), preceding };
146
179
  }
147
180
 
148
181
  /**
149
- * Whether a comment from `start` to `end` sits where ast-grep's suppression
150
- * comment covers the whole file (`get_suppression_kind` in its `combined.rs`):
151
- * on the first line with nothing but whitespace in front of it, and with the
152
- * next node — code or another comment — starting two or more lines further
153
- * down, or with nothing after it at all. Lines are counted the way tree-sitter
154
- * counts its rows, by line feeds alone.
182
+ * What stands in front of the comment at `offset` in tree-sitter's tree.
183
+ * tree-sitter puts a comment in the innermost node with code on both sides
184
+ * of it, so the comment's previous sibling is the comment right before it
185
+ * when no token stands between them, and otherwise the outermost node that
186
+ * ends with the token before it. TypeScript's nodes end where tree-sitter's
187
+ * do, except that tree-sitter's automatic semicolon can take a trailing
188
+ * comment into its statement, which makes the sibling smaller and starting
189
+ * no earlier: the start read off here is never on a later row.
155
190
  */
156
- export function isFileWidePosition(text: string, start: number, end: number): boolean {
157
- if (!opensFirstRow(text, start)) return false;
158
- const next = text.slice(end).search(/\S/);
159
- return next === -1 || lineFeedCount(text.slice(0, end + next)) >= 2;
160
- }
161
-
162
- /** Whether nothing but whitespace, and no line feed, precedes the offset: the start of tree-sitter's first row. */
163
- export function opensFirstRow(text: string, offset: number): boolean {
164
- const before = text.slice(0, offset);
165
- return isBlank(before) && !before.includes("\n");
166
- }
167
-
168
- function lineFeedCount(text: string): number {
169
- return text.split("\n").length - 1;
191
+ function precedingNode(
192
+ sourceFile: ts.SourceFile,
193
+ tokens: readonly ts.Node[],
194
+ previous: Comment | undefined,
195
+ offset: number,
196
+ ): PrecedingNode {
197
+ // Empty lists are leaves too, as are missing tokens; neither holds text.
198
+ let index = firstIndexAtOrAfter(tokens, offset + 1, (token) => token.end) - 1;
199
+ while (index >= 0 && (tokens[index] as ts.Node).getStart(sourceFile) >= (tokens[index] as ts.Node).end) index -= 1;
200
+ const token = tokens[index];
201
+ const tokenEnd = token?.end ?? 0;
202
+ if (previous !== undefined && previous.end > tokenEnd) return { start: previous.start, tokenEnd };
203
+ if (token === undefined) return { tokenEnd };
204
+ let node = token;
205
+ while (!ts.isSourceFile(node.parent) && node.parent.end === token.end) node = node.parent;
206
+ return { start: node.getStart(sourceFile), tokenEnd };
170
207
  }
171
208
 
172
209
  /**
@@ -218,3 +255,21 @@ function switchSlots(token: ts.Node): { fallthroughSlot: boolean; noDefaultSlot:
218
255
  function lacksDefaultClause(caseBlock: ts.CaseBlock): boolean {
219
256
  return caseBlock.clauses.length > 0 && !caseBlock.clauses.some(ts.isDefaultClause);
220
257
  }
258
+
259
+ /**
260
+ * Whether the file holds no code, as the no-empty-file rules of oxlint and
261
+ * eslint-plugin-unicorn read it (see DirectivePlacement.emptyFile): every
262
+ * statement is an empty one or a block of nothing else, past the directive
263
+ * prologue — the string-literal statements that open the file.
264
+ */
265
+ function holdsNoCode(sourceFile: ts.SourceFile): boolean {
266
+ let prologue = true;
267
+ return sourceFile.statements.every((statement) => {
268
+ prologue &&= ts.isExpressionStatement(statement) && ts.isStringLiteral(statement.expression);
269
+ return prologue || isEmptyStatement(statement);
270
+ });
271
+ }
272
+
273
+ function isEmptyStatement(statement: ts.Statement): boolean {
274
+ return ts.isEmptyStatement(statement) || (ts.isBlock(statement) && statement.statements.every(isEmptyStatement));
275
+ }