@kongyo2/ts-comment-scanner 1.11.1 → 1.17.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/astgrep.d.ts +51 -0
- package/dist/astgrep.d.ts.map +1 -0
- package/dist/astgrep.js +85 -0
- package/dist/astgrep.js.map +1 -0
- package/dist/directives.d.ts +32 -4
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +241 -95
- package/dist/directives.js.map +1 -1
- package/dist/imports.d.ts +36 -0
- package/dist/imports.d.ts.map +1 -0
- package/dist/imports.js +119 -0
- package/dist/imports.js.map +1 -0
- package/dist/remove.d.ts +14 -7
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +254 -81
- package/dist/remove.js.map +1 -1
- package/dist/scanner.d.ts +6 -11
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +79 -23
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +19 -9
- package/dist/splice.js.map +1 -1
- package/package.json +1 -1
- package/src/astgrep.ts +115 -0
- package/src/directives.ts +275 -99
- package/src/imports.ts +145 -0
- package/src/remove.ts +318 -86
- package/src/scanner.ts +87 -24
- package/src/splice.ts +19 -9
package/src/remove.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
9
|
+
import type { ImportGroupGap } from "./imports.js";
|
|
2
10
|
import type { JsxContainer } from "./jsx.js";
|
|
3
11
|
import { LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
4
|
-
import { firstIndexAtOrAfter, type Range } from "./ranges.js";
|
|
5
|
-
import { analyzeSource
|
|
12
|
+
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
13
|
+
import { analyzeSource } from "./scanner.js";
|
|
6
14
|
import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
|
|
7
15
|
import type { Comment } from "./types.js";
|
|
8
16
|
|
|
@@ -29,13 +37,17 @@ export interface RemoveResult {
|
|
|
29
37
|
removed: Comment[];
|
|
30
38
|
/**
|
|
31
39
|
* Comments kept because they are protected: directives, license headers,
|
|
32
|
-
* comments the `keep` predicate claims, comments
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
40
|
+
* comments the `keep` predicate claims, comments whose removal would
|
|
41
|
+
* change what an ast-grep suppression covers (the ones in front of it on
|
|
42
|
+
* its row decide which node it trails), comments whose removal would shift
|
|
43
|
+
* code under a next-line directive or join another line to one a directive
|
|
44
|
+
* covers, or that sit between a directive and the code it reaches past
|
|
45
|
+
* them for (or the JSX sibling it applies to), comments whose removal
|
|
46
|
+
* would move a position-dependent directive
|
|
47
|
+
* (prettier's first-comment pragma, Deno's coverage file pragma, Bun's
|
|
48
|
+
* file-start marker) into effect, and comments whose removal would join
|
|
49
|
+
* two groups of imports or exports that TypeScript's organizeImports or
|
|
50
|
+
* dprint sorts apart, or split one.
|
|
39
51
|
*/
|
|
40
52
|
kept: Comment[];
|
|
41
53
|
/** Comments excluded from removal by the `shouldRemove` predicate. */
|
|
@@ -68,6 +80,9 @@ export interface RemoveResult {
|
|
|
68
80
|
* go with it: no pile-up of blank lines where a comment stood between two
|
|
69
81
|
* of them, and at the start or end of the file or of a block only the blank
|
|
70
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 or dprint sorts apart, or split one, the comment stays.
|
|
71
86
|
* - A leading byte-order mark is preserved. Reported positions index into the
|
|
72
87
|
* `source` string exactly as `scanComments(source)` reports them (a BOM
|
|
73
88
|
* counts as its first character).
|
|
@@ -97,7 +112,11 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
97
112
|
for (const container of containers) {
|
|
98
113
|
for (const comment of container.comments) containerOf.set(comment, container);
|
|
99
114
|
}
|
|
100
|
-
const context: ShieldContext = {
|
|
115
|
+
const context: ShieldContext = {
|
|
116
|
+
containerOf,
|
|
117
|
+
skippable: skippableRanges(comments, containerOf, new Set(removed)),
|
|
118
|
+
preceding: analysis.preceding,
|
|
119
|
+
};
|
|
101
120
|
const search = containerSearch(containers);
|
|
102
121
|
// Collapsing blank lines can only move a surviving comment to the very
|
|
103
122
|
// start of the file (or of the line after a shebang), where Bun reads its
|
|
@@ -123,6 +142,19 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
123
142
|
|
|
124
143
|
if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
|
|
125
144
|
|
|
145
|
+
// Where the output joins two groups of imports or exports, or splits one,
|
|
146
|
+
// a sorter would sort them differently: the removal that does it is
|
|
147
|
+
// re-protected, and the attempt repeated.
|
|
148
|
+
const holders = importGroupHolders(source, analysis.importGroups, output.importGroups, removed);
|
|
149
|
+
if (holders.length > 0) {
|
|
150
|
+
const holding = new Set(holders);
|
|
151
|
+
removed = removed.filter((comment) => !holding.has(comment));
|
|
152
|
+
kept.push(...holders);
|
|
153
|
+
kept.sort(byStart);
|
|
154
|
+
tidyBlankLines = true;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
126
158
|
const survivor = firstChangedSurvivor(source, code, output.comments, kept, skipped);
|
|
127
159
|
if (survivor === undefined) {
|
|
128
160
|
return { code, removed, kept, skipped, changed: true };
|
|
@@ -199,8 +231,8 @@ function firstChangedSurvivor(
|
|
|
199
231
|
* without a parse: the comment is the file's first comment when nothing but
|
|
200
232
|
* whitespace (or a shebang) precedes it — any earlier comment's text keeps
|
|
201
233
|
* 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
|
|
203
|
-
*
|
|
234
|
+
* reads its marker, and in ast-grep's file-wide position when it opens the
|
|
235
|
+
* first row with the next node two or more rows down. Header status
|
|
204
236
|
* needs the token structure, but removing comments never moves a comment
|
|
205
237
|
* across the first token, so the positional check does not consult it.
|
|
206
238
|
*/
|
|
@@ -223,6 +255,99 @@ function closestRemovalBefore(removals: readonly Comment[], comment: Comment): C
|
|
|
223
255
|
return closest;
|
|
224
256
|
}
|
|
225
257
|
|
|
258
|
+
/**
|
|
259
|
+
* The removals to re-protect so that the output starts exactly the groups of
|
|
260
|
+
* imports and exports the source starts. The gaps belong to the same
|
|
261
|
+
* declarations in both texts, since removing comments leaves the statements
|
|
262
|
+
* as they are. Where the output joins two groups at a gap for some sorter,
|
|
263
|
+
* the removal holding the gap open stays (see joiningRemoval); where it
|
|
264
|
+
* splits one there, the multi-line comment a line break took the place of
|
|
265
|
+
* stays (see partingRemoval).
|
|
266
|
+
*/
|
|
267
|
+
function importGroupHolders(
|
|
268
|
+
source: string,
|
|
269
|
+
before: readonly ImportGroupGap[],
|
|
270
|
+
after: readonly ImportGroupGap[],
|
|
271
|
+
removals: readonly Comment[],
|
|
272
|
+
): Comment[] {
|
|
273
|
+
if (before.length !== after.length) throw unexpectedResult();
|
|
274
|
+
const holders = new Set<Comment>();
|
|
275
|
+
const hold = (holder: Comment | undefined): void => {
|
|
276
|
+
if (holder === undefined) throw unexpectedResult();
|
|
277
|
+
holders.add(holder);
|
|
278
|
+
};
|
|
279
|
+
for (const [index, gap] of before.entries()) {
|
|
280
|
+
const splits = (after[index] as ImportGroupGap).splits;
|
|
281
|
+
const joined = gap.splits.filter((sorter) => !splits.includes(sorter));
|
|
282
|
+
const parted = splits.some((sorter) => !gap.splits.includes(sorter));
|
|
283
|
+
if (joined.length === 0 && !parted) continue;
|
|
284
|
+
const inGap = removals.filter((comment) => comment.start >= gap.start && comment.end <= gap.end);
|
|
285
|
+
if (joined.length > 0) hold(joiningRemoval(source, gap, inGap, joined.includes("typescript")));
|
|
286
|
+
if (parted) hold(partingRemoval(source, gap, inGap, removals));
|
|
287
|
+
}
|
|
288
|
+
return [...holders];
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The removal in the gap whose deletion takes line breaks out of it: one
|
|
293
|
+
* spanning lines, or one on a line below the token the gap follows. When only
|
|
294
|
+
* dprint joins the groups, it counted line breaks inside a comment, and the
|
|
295
|
+
* first comment spanning lines stays: the removals after it then trail it
|
|
296
|
+
* and go without their line breaks, while in front of a later one a line
|
|
297
|
+
* break would take their place. Otherwise the last of them that ends above
|
|
298
|
+
* the declaration's line stays, holding a line of its own with the line break
|
|
299
|
+
* TypeScript counts and the line dprint counts; failing that, the last of
|
|
300
|
+
* them, which still keeps the declaration's line down.
|
|
301
|
+
*/
|
|
302
|
+
function joiningRemoval(
|
|
303
|
+
source: string,
|
|
304
|
+
gap: Range,
|
|
305
|
+
inGap: readonly Comment[],
|
|
306
|
+
typescriptJoins: boolean,
|
|
307
|
+
): Comment | undefined {
|
|
308
|
+
const breaking = inGap.filter(
|
|
309
|
+
(comment) => comment.endLine > comment.line || LINE_TERMINATOR.test(source.slice(gap.start, comment.start)),
|
|
310
|
+
);
|
|
311
|
+
const spanning = typescriptJoins ? undefined : breaking.find((comment) => comment.endLine > comment.line);
|
|
312
|
+
return (
|
|
313
|
+
spanning ??
|
|
314
|
+
breaking.findLast((comment) => LINE_TERMINATOR.test(source.slice(comment.end, gap.end))) ??
|
|
315
|
+
breaking.at(-1)
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* The removal in the gap that a line break takes the place of (see
|
|
321
|
+
* applyEdits): a multi-line comment with something that survives the
|
|
322
|
+
* removals in front of it on its first line, and anything after it on its
|
|
323
|
+
* last line — the token before the gap and the declaration after it count.
|
|
324
|
+
* Its own line breaks are no NewLineTrivia to TypeScript, but the one put in
|
|
325
|
+
* its place is.
|
|
326
|
+
*/
|
|
327
|
+
function partingRemoval(
|
|
328
|
+
source: string,
|
|
329
|
+
gap: Range,
|
|
330
|
+
inGap: readonly Comment[],
|
|
331
|
+
removals: readonly Comment[],
|
|
332
|
+
): Comment | undefined {
|
|
333
|
+
// Whether the line holds what `stands` accepts, from `from` on, stepping by `step` until `to`.
|
|
334
|
+
const onLine = (from: number, to: number, step: 1 | -1, stands: (index: number) => boolean): boolean => {
|
|
335
|
+
for (let index = from; index !== to; index += step) {
|
|
336
|
+
if (LINE_TERMINATOR.test(source[index] as string)) return false;
|
|
337
|
+
if (stands(index)) return true;
|
|
338
|
+
}
|
|
339
|
+
return true;
|
|
340
|
+
};
|
|
341
|
+
const content = (index: number): boolean => !isBlank(source[index] as string);
|
|
342
|
+
const survivor = (index: number): boolean => content(index) && rangeAt(removals, index) === undefined;
|
|
343
|
+
return inGap.findLast(
|
|
344
|
+
(comment) =>
|
|
345
|
+
comment.endLine > comment.line &&
|
|
346
|
+
onLine(comment.start - 1, gap.start - 1, -1, survivor) &&
|
|
347
|
+
onLine(comment.end, gap.end, 1, content),
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
|
|
226
351
|
/**
|
|
227
352
|
* Applies the edits, restoring the byte-order mark when dropping a
|
|
228
353
|
* comment-only first line took it along: the mark is an encoding artifact,
|
|
@@ -461,11 +586,16 @@ interface ShieldContext {
|
|
|
461
586
|
containerOf: ReadonlyMap<Comment, Container>;
|
|
462
587
|
/** Comments and the containers that may go with theirs, sorted: what code starts after. */
|
|
463
588
|
skippable: readonly Range[];
|
|
589
|
+
/** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
|
|
590
|
+
preceding: ReadonlyMap<Comment, PrecedingNode>;
|
|
464
591
|
}
|
|
465
592
|
|
|
466
593
|
interface Shielding {
|
|
467
594
|
removed: Comment[];
|
|
468
|
-
/**
|
|
595
|
+
/**
|
|
596
|
+
* Lines (1-based) that a surviving directive reaches: the ones below a
|
|
597
|
+
* next-line directive, and the one a line directive stands on.
|
|
598
|
+
*/
|
|
469
599
|
lines: Set<number>;
|
|
470
600
|
}
|
|
471
601
|
|
|
@@ -483,13 +613,16 @@ interface RemovalUnit extends Range {
|
|
|
483
613
|
* under a surviving next-line directive (`@ts-expect-error`,
|
|
484
614
|
* `eslint-disable-next-line`, ...). Dropping such a line would shift the
|
|
485
615
|
* following code up, silently changing which line the directive applies to —
|
|
486
|
-
* and with it the compiler or linter behaviour.
|
|
487
|
-
*
|
|
488
|
-
*
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
616
|
+
* and with it the compiler or linter behaviour. Nor may a removal join
|
|
617
|
+
* another line to one a directive covers, the line a line directive
|
|
618
|
+
* (`eslint-disable-line`, ...) stands on included. Directives whose tools
|
|
619
|
+
* reach further get more: below a tsc suppression the first line each
|
|
620
|
+
* compiler cannot step over stays, oxlint's line-feed rows add the lines they
|
|
621
|
+
* run over, between a directive that only counts as the last comment before
|
|
622
|
+
* its target (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul
|
|
623
|
+
* only applies to the node right after it and that target every comment
|
|
624
|
+
* stays, and a JSX comment container right after a directive in a container
|
|
625
|
+
* (`{/* prettier-ignore */}`, which applies to the next sibling) stays too.
|
|
493
626
|
*
|
|
494
627
|
* The line checks work on whole lines, not single comments: a line holding
|
|
495
628
|
* several removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
@@ -511,29 +644,52 @@ function shieldNextLineDirectives(
|
|
|
511
644
|
const blockingLines = new Set<number>();
|
|
512
645
|
const shieldedGaps: Array<readonly [number, number]> = [];
|
|
513
646
|
const siblingTargets = new Set<Container>();
|
|
514
|
-
|
|
515
|
-
const linePrefixes: Comment[] = [];
|
|
516
|
-
/** Directives whose last line, from where they end, has to stay as it is. */
|
|
517
|
-
const lineSuffixes: Comment[] = [];
|
|
647
|
+
const astGrepReaches: AstGrepReach[] = [];
|
|
518
648
|
const processed = new Set<Comment>();
|
|
519
649
|
let stillRemoved = removed;
|
|
520
650
|
let units: RemovalUnit[] | undefined;
|
|
521
651
|
const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
|
|
652
|
+
let rows: number[] | undefined;
|
|
653
|
+
/** The (1-based) line holding the offset. */
|
|
654
|
+
const lineOf = (offset: number): number => firstIndexAtOrAfter(lineOffsets, offset + 1, (start) => start);
|
|
522
655
|
|
|
523
656
|
const collectShields = (): boolean => {
|
|
524
657
|
let added = false;
|
|
658
|
+
const shieldLines = (from: number, to: number): void => {
|
|
659
|
+
for (let line = from; line <= Math.min(to, totalLines); line += 1) {
|
|
660
|
+
if (shieldedLines.has(line)) continue;
|
|
661
|
+
shieldedLines.add(line);
|
|
662
|
+
added = true;
|
|
663
|
+
}
|
|
664
|
+
};
|
|
525
665
|
for (const comment of [...kept, ...skipped]) {
|
|
526
666
|
if (processed.has(comment)) continue;
|
|
527
667
|
processed.add(comment);
|
|
528
668
|
const directive = comment.directive;
|
|
529
669
|
if (directive === undefined) continue;
|
|
530
|
-
if (
|
|
670
|
+
if (carriesAstGrepSuppression(comment.text)) {
|
|
671
|
+
const reach = astGrepReach(
|
|
672
|
+
source,
|
|
673
|
+
(rows ??= rowStartOffsets(source)),
|
|
674
|
+
comment,
|
|
675
|
+
context.preceding.get(comment) as PrecedingNode,
|
|
676
|
+
);
|
|
677
|
+
astGrepReaches.push(reach);
|
|
678
|
+
if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
|
|
679
|
+
for (let line = lineOf(reach.nextRow.start); line <= lineOf(reach.nextRow.end - 1); line += 1) {
|
|
680
|
+
shieldedLines.add(line);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
added = true;
|
|
684
|
+
}
|
|
685
|
+
const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
|
|
686
|
+
if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
|
|
531
687
|
const codeStart = codeStartAfter(source, comment.end, context.skippable);
|
|
532
688
|
if (codeStart > comment.end && codeStart < source.length) {
|
|
533
689
|
shieldedGaps.push([comment.end, codeStart]);
|
|
534
690
|
added = true;
|
|
535
691
|
}
|
|
536
|
-
continue;
|
|
692
|
+
if (lastComment) continue;
|
|
537
693
|
}
|
|
538
694
|
if (TSC_SUPPRESSIONS.has(directive)) {
|
|
539
695
|
for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
|
|
@@ -542,11 +698,14 @@ function shieldNextLineDirectives(
|
|
|
542
698
|
added = true;
|
|
543
699
|
}
|
|
544
700
|
}
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
701
|
+
// oxlint reaches its rows' lines past the ones counted below.
|
|
702
|
+
if (OXLINT_ROW_DIRECTIVES.has(directive)) {
|
|
703
|
+
const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
|
|
704
|
+
if (reach !== undefined) shieldLines(reach.from, reach.to);
|
|
549
705
|
}
|
|
706
|
+
// A line directive covers the line it stands on, which no removal may
|
|
707
|
+
// join to another (see joiningLineGroup).
|
|
708
|
+
if (isOwnLineDirective(directive)) shieldLines(comment.line, comment.line);
|
|
550
709
|
if (!isNextLineDirective(directive)) continue;
|
|
551
710
|
// In JSX, such a directive in braces applies to the sibling after it,
|
|
552
711
|
// whatever line that is on (prettier leaves the element after
|
|
@@ -560,22 +719,10 @@ function shieldNextLineDirectives(
|
|
|
560
719
|
// A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
|
|
561
720
|
// `secret(); // nosemgrep`) targets its own line, so it shields nothing
|
|
562
721
|
// below it. In JSX, the container holding the comment is what has to
|
|
563
|
-
// stand on lines of its own.
|
|
564
|
-
|
|
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;
|
|
722
|
+
// stand on lines of its own.
|
|
723
|
+
if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) continue;
|
|
570
724
|
// Counted pragmas like `c8 ignore next 3` cover several lines.
|
|
571
|
-
|
|
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
|
-
}
|
|
725
|
+
shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment));
|
|
579
726
|
}
|
|
580
727
|
return added;
|
|
581
728
|
};
|
|
@@ -590,7 +737,9 @@ function shieldNextLineDirectives(
|
|
|
590
737
|
const protectShields = (): boolean => {
|
|
591
738
|
let changed = false;
|
|
592
739
|
for (const line of shieldedLines) {
|
|
593
|
-
const group =
|
|
740
|
+
const group =
|
|
741
|
+
vanishingLineGroup(source, line, lineOffsets, currentUnits()) ??
|
|
742
|
+
joiningLineGroup(source, line, lineOffsets, currentUnits());
|
|
594
743
|
if (group === undefined) continue;
|
|
595
744
|
protect(group);
|
|
596
745
|
changed = true;
|
|
@@ -613,21 +762,8 @@ function shieldNextLineDirectives(
|
|
|
613
762
|
protect(group);
|
|
614
763
|
changed = true;
|
|
615
764
|
}
|
|
616
|
-
for (const
|
|
617
|
-
|
|
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);
|
|
765
|
+
for (const reach of astGrepReaches) {
|
|
766
|
+
const group = currentUnits().filter((unit) => reach.holds(unit));
|
|
631
767
|
if (group.length === 0) continue;
|
|
632
768
|
protect(group.flatMap((unit) => unit.comments));
|
|
633
769
|
changed = true;
|
|
@@ -775,6 +911,80 @@ function vanishingLineGroup(
|
|
|
775
911
|
return group.flatMap((unit) => unit.comments);
|
|
776
912
|
}
|
|
777
913
|
|
|
914
|
+
/**
|
|
915
|
+
* The removals that would join the given line to another: they reach past
|
|
916
|
+
* one of its ends, with something other than whitespace beyond them on the
|
|
917
|
+
* line they run into. Deleting such a comment takes its line breaks along —
|
|
918
|
+
* with nothing in their place right after an opening bracket or before
|
|
919
|
+
* closing punctuation (see applyEdits) — so what stands beyond it can land on
|
|
920
|
+
* the line, or what stands on the line move off it, and a directive covering
|
|
921
|
+
* the line would cover other code. (With only whitespace beyond, the lines
|
|
922
|
+
* the comment held vanish, which vanishingLineGroup weighs.)
|
|
923
|
+
*/
|
|
924
|
+
function joiningLineGroup(
|
|
925
|
+
source: string,
|
|
926
|
+
line: number,
|
|
927
|
+
lineOffsets: readonly number[],
|
|
928
|
+
units: readonly RemovalUnit[],
|
|
929
|
+
): Comment[] | undefined {
|
|
930
|
+
const lineStart = lineOffsets[line - 1] as number;
|
|
931
|
+
const nextLineStart = lineOffsets[line];
|
|
932
|
+
const lineEnd = nextLineStart ?? source.length;
|
|
933
|
+
const group = units.filter(
|
|
934
|
+
(unit) =>
|
|
935
|
+
unit.start < lineEnd &&
|
|
936
|
+
unit.end > lineStart &&
|
|
937
|
+
((nextLineStart !== undefined && unit.end > nextLineStart && !closesItsLine(source, unit)) ||
|
|
938
|
+
(unit.start < lineStart && !opensItsLine(source, unit))),
|
|
939
|
+
);
|
|
940
|
+
return group.length === 0 ? undefined : group.flatMap((unit) => unit.comments);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
// The directives oxlint reads on rows of its own (see oxlintReachLines).
|
|
944
|
+
const OXLINT_ROW_DIRECTIVES = new Set([
|
|
945
|
+
"eslint-disable-next-line",
|
|
946
|
+
"oxlint-disable-next-line",
|
|
947
|
+
"eslint-disable-line",
|
|
948
|
+
"oxlint-disable-line",
|
|
949
|
+
]);
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* The lines (1-based, inclusive) that oxlint applies an `eslint-` or
|
|
953
|
+
* `oxlint-` line or next-line comment to, past the ones the line-based
|
|
954
|
+
* shields take in. oxlint finds the stretch on rows that only a line feed
|
|
955
|
+
* ends (`split_inclusive('\n')` and `lines()` in its `disable_directives.rs`):
|
|
956
|
+
* a next-line comment covers the rest of the row its text ends on and the
|
|
957
|
+
* row after it, a line comment its own row up to the comment. A lone CR,
|
|
958
|
+
* U+2028 or U+2029 — a line break to tsc and ESLint — ends no row there, so
|
|
959
|
+
* a row can run over several of the lines counted here. Undefined when a
|
|
960
|
+
* next-line comment stands on the file's last row: oxlint then covers the
|
|
961
|
+
* rest of the file, which no removal changes.
|
|
962
|
+
*/
|
|
963
|
+
function oxlintReachLines(
|
|
964
|
+
source: string,
|
|
965
|
+
rows: readonly number[],
|
|
966
|
+
lineOf: (offset: number) => number,
|
|
967
|
+
comment: Comment,
|
|
968
|
+
directive: string,
|
|
969
|
+
): { from: number; to: number } | undefined {
|
|
970
|
+
const rowOf = (offset: number): number => firstIndexAtOrAfter(rows, offset + 1, (start) => start) - 1;
|
|
971
|
+
if (!directive.endsWith("-next-line")) {
|
|
972
|
+
return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
|
|
973
|
+
}
|
|
974
|
+
// The comment's text ends in front of a block comment's `*/`.
|
|
975
|
+
const row = rowOf(comment.kind === "block" ? comment.end - 2 : comment.end);
|
|
976
|
+
if (rows[row + 1] === undefined) return undefined;
|
|
977
|
+
return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
/**
|
|
981
|
+
* Whether the directive covers the line it stands on: the `-line` forms of
|
|
982
|
+
* the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...).
|
|
983
|
+
*/
|
|
984
|
+
function isOwnLineDirective(name: string): boolean {
|
|
985
|
+
return name.endsWith("-line") && !name.endsWith("-next-line");
|
|
986
|
+
}
|
|
987
|
+
|
|
778
988
|
// These suppressions only apply to the following node/line when they stand on
|
|
779
989
|
// their own line — trailing after code they target (at most) their own line.
|
|
780
990
|
// Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
|
|
@@ -789,16 +999,6 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
|
|
|
789
999
|
"nosemgrep",
|
|
790
1000
|
]);
|
|
791
1001
|
|
|
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
1002
|
// The suppressions tsc looks past blank and `//` lines for (see
|
|
803
1003
|
// firstBlockingLines). They stay next-line directives as well: Flow, which
|
|
804
1004
|
// honours them in TypeScript files, applies them to the very next line.
|
|
@@ -829,9 +1029,32 @@ const LAST_COMMENT_DIRECTIVES = new Set([
|
|
|
829
1029
|
"no-default",
|
|
830
1030
|
]);
|
|
831
1031
|
|
|
1032
|
+
// The coverage hints ast-v8-to-istanbul, Vitest's v8 provider, applies to the
|
|
1033
|
+
// node that starts at the very token after them: with another comment in
|
|
1034
|
+
// between, the hint has no target, and deleting that comment would bring it
|
|
1035
|
+
// into effect. The gap up to the code is shielded like the one above, on top
|
|
1036
|
+
// of the lines below the hint, which c8, Node and monocart count.
|
|
1037
|
+
const NEXT_TOKEN_DIRECTIVES = new Set([
|
|
1038
|
+
"istanbul-ignore-next",
|
|
1039
|
+
"istanbul-ignore-if",
|
|
1040
|
+
"istanbul-ignore-else",
|
|
1041
|
+
"c8-ignore-next",
|
|
1042
|
+
"c8-ignore-if",
|
|
1043
|
+
"c8-ignore-else",
|
|
1044
|
+
"v8-ignore-next",
|
|
1045
|
+
"v8-ignore-if",
|
|
1046
|
+
"v8-ignore-else",
|
|
1047
|
+
"node:coverage-ignore",
|
|
1048
|
+
"node:coverage-ignore-if",
|
|
1049
|
+
"node:coverage-ignore-else",
|
|
1050
|
+
]);
|
|
1051
|
+
|
|
832
1052
|
// Directives that target the line or node directly below them. File- and
|
|
833
|
-
// range-scoped pragmas (`
|
|
1053
|
+
// range-scoped pragmas (`istanbul-ignore-file`, `c8-ignore-start`, ...) are
|
|
834
1054
|
// absent: they do not target the following line, so they never shield it.
|
|
1055
|
+
// They go by their names here: Vitest's v8 provider reads a range hint
|
|
1056
|
+
// spelled out on any line of this file, and would drop the rest of the file
|
|
1057
|
+
// from this project's coverage.
|
|
835
1058
|
const NEXT_LINE_DIRECTIVES = new Set([
|
|
836
1059
|
"@ts-ignore",
|
|
837
1060
|
"@ts-expect-error",
|
|
@@ -843,19 +1066,18 @@ const NEXT_LINE_DIRECTIVES = new Set([
|
|
|
843
1066
|
"oxfmt-ignore",
|
|
844
1067
|
"dprint-ignore",
|
|
845
1068
|
"rome-ignore",
|
|
846
|
-
"istanbul-ignore",
|
|
847
1069
|
"istanbul-ignore-next",
|
|
848
1070
|
"istanbul-ignore-if",
|
|
849
1071
|
"istanbul-ignore-else",
|
|
850
|
-
"c8-ignore",
|
|
851
1072
|
"c8-ignore-next",
|
|
852
1073
|
"c8-ignore-if",
|
|
853
1074
|
"c8-ignore-else",
|
|
854
|
-
"v8-ignore",
|
|
855
1075
|
"v8-ignore-next",
|
|
856
1076
|
"v8-ignore-if",
|
|
857
1077
|
"v8-ignore-else",
|
|
858
1078
|
"node:coverage-ignore",
|
|
1079
|
+
"node:coverage-ignore-if",
|
|
1080
|
+
"node:coverage-ignore-else",
|
|
859
1081
|
// Directives that hang off the node or line directly below them: a
|
|
860
1082
|
// whole-line comment between them and their target would change what
|
|
861
1083
|
// they apply to, so the line below stays shielded.
|
|
@@ -880,24 +1102,30 @@ const NEXT_LINE_DIRECTIVES = new Set([
|
|
|
880
1102
|
"pragma-allowlist-nextline-secret",
|
|
881
1103
|
"cspell:disable-next",
|
|
882
1104
|
"HTML",
|
|
883
|
-
"ast-grep-ignore",
|
|
884
1105
|
]);
|
|
885
1106
|
|
|
886
1107
|
function isNextLineDirective(name: string): boolean {
|
|
887
1108
|
return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
|
|
888
1109
|
}
|
|
889
1110
|
|
|
890
|
-
// The coverage pragmas
|
|
891
|
-
// (
|
|
892
|
-
//
|
|
893
|
-
//
|
|
894
|
-
//
|
|
895
|
-
|
|
1111
|
+
// The coverage pragmas a line count after `ignore next` applies to: c8
|
|
1112
|
+
// (v8-to-istanbul), Node's test-runner coverage and monocart-coverage-reports
|
|
1113
|
+
// count lines. istanbul and ast-v8-to-istanbul, Vitest's providers, apply the
|
|
1114
|
+
// hint to the next node whatever count follows, so `istanbul ignore next 3`
|
|
1115
|
+
// covers one line. Everything else — `@ts-expect-error`,
|
|
1116
|
+
// `eslint-disable-next-line` and friends — covers exactly one line too, no
|
|
1117
|
+
// matter what prose like "we ignore next 3 legacy calls" appears in the
|
|
1118
|
+
// reason text.
|
|
1119
|
+
const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
|
|
896
1120
|
|
|
897
|
-
/**
|
|
1121
|
+
/**
|
|
1122
|
+
* Number of following lines a next-line pragma covers (`c8 ignore next 3` →
|
|
1123
|
+
* 3). monocart reads the count past any whitespace or none, and c8 and
|
|
1124
|
+
* monocart stop at the last digit, so `next3` and `next 3x` count 3 as well.
|
|
1125
|
+
*/
|
|
898
1126
|
function shieldedLineCount(comment: Comment): number {
|
|
899
1127
|
if (comment.directive === undefined || !COUNTED_NEXT_LINE.has(comment.directive)) return 1;
|
|
900
|
-
const match = /\bignore\s
|
|
1128
|
+
const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
|
|
901
1129
|
return match ? Math.max(1, Number(match[1])) : 1;
|
|
902
1130
|
}
|
|
903
1131
|
|
|
@@ -907,7 +1135,11 @@ function shieldedLineCount(comment: Comment): number {
|
|
|
907
1135
|
* scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
|
|
908
1136
|
*/
|
|
909
1137
|
function occupiesWholeLines(source: string, range: Range): boolean {
|
|
910
|
-
|
|
1138
|
+
return opensItsLine(source, range) && closesItsLine(source, range);
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
/** True when nothing but whitespace follows the range on its last line. */
|
|
1142
|
+
function closesItsLine(source: string, range: Range): boolean {
|
|
911
1143
|
for (let index = range.end; index < source.length; index += 1) {
|
|
912
1144
|
const char = source[index] as string;
|
|
913
1145
|
if (LINE_TERMINATOR.test(char)) break;
|