@kongyo2/ts-comment-scanner 1.21.0 → 1.23.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/README.md +240 -52
- package/dist/astgrep.d.ts +4 -2
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +7 -4
- package/dist/astgrep.js.map +1 -1
- package/dist/containers.d.ts +48 -0
- package/dist/containers.d.ts.map +1 -0
- package/dist/containers.js +82 -0
- package/dist/containers.js.map +1 -0
- package/dist/coverage.d.ts +45 -0
- package/dist/coverage.d.ts.map +1 -0
- package/dist/coverage.js +92 -0
- package/dist/coverage.js.map +1 -0
- package/dist/directives.d.ts +13 -3
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +31 -11
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +5 -0
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +103 -41
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts +12 -5
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +33 -10
- package/dist/imports.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/lines.d.ts +11 -0
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +30 -0
- package/dist/lines.js.map +1 -1
- package/dist/remove.d.ts +16 -12
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +51 -638
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +11 -10
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +29 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +122 -5
- package/dist/scanner.js.map +1 -1
- package/dist/shields.d.ts +77 -0
- package/dist/shields.d.ts.map +1 -0
- package/dist/shields.js +758 -0
- package/dist/shields.js.map +1 -0
- package/dist/splice.d.ts +40 -15
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +185 -69
- package/dist/splice.js.map +1 -1
- package/package.json +6 -5
- package/src/astgrep.ts +7 -5
- package/src/containers.ts +128 -0
- package/src/coverage.ts +110 -0
- package/src/directives.ts +45 -11
- package/src/files.ts +147 -53
- package/src/imports.ts +42 -17
- package/src/index.ts +1 -0
- package/src/lines.ts +33 -1
- package/src/remove.ts +58 -748
- package/src/run.ts +11 -10
- package/src/scanner.ts +160 -5
- package/src/shields.ts +852 -0
- package/src/splice.ts +216 -75
package/src/remove.ts
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
bunMarkerOffset,
|
|
5
|
-
carriesAstGrepSuppression,
|
|
6
|
-
isLegalComment,
|
|
7
|
-
type DirectivePlacement,
|
|
8
|
-
} from "./directives.js";
|
|
1
|
+
import { isFileWidePosition } from "./astgrep.js";
|
|
2
|
+
import { attachContainers, emptiedBy, mergeContainers, type Container } from "./containers.js";
|
|
3
|
+
import { activePositionalDirectives, bunMarkerOffset, isLegalComment, type DirectivePlacement } from "./directives.js";
|
|
9
4
|
import type { ImportGroupGap } from "./imports.js";
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import { applyEdits, blankLineEdits, type Edit } from "./splice.js";
|
|
5
|
+
import { LINE_TERMINATOR, isBlank } from "./lines.js";
|
|
6
|
+
import { byStart, type Range } from "./ranges.js";
|
|
7
|
+
import { analyzeSource, type ScanOptions, type SourceAnalysis } from "./scanner.js";
|
|
8
|
+
import { shieldDirectives, type ShieldContext } from "./shields.js";
|
|
9
|
+
import { applyEdits, blankLineEdits, heldLines, type Edit, type ShieldedLines } from "./splice.js";
|
|
15
10
|
import type { Comment } from "./types.js";
|
|
16
11
|
|
|
17
12
|
export interface RemoveOptions {
|
|
18
13
|
jsx?: boolean;
|
|
14
|
+
/** The source is JavaScript (see ScanOptions.javascript). */
|
|
15
|
+
javascript?: boolean;
|
|
19
16
|
/** Also remove compiler/linter directive comments. Default: false (kept for safety). */
|
|
20
17
|
removeDirectives?: boolean;
|
|
21
18
|
/** Also remove license/legal comments. Default: false (kept for safety). */
|
|
@@ -37,13 +34,10 @@ export interface RemoveResult {
|
|
|
37
34
|
removed: Comment[];
|
|
38
35
|
/**
|
|
39
36
|
* Comments kept because they are protected: directives, license headers,
|
|
40
|
-
* comments the `keep` predicate claims, comments
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* would join another line to one a directive covers, comments that sit
|
|
45
|
-
* between a directive and the code it reaches past them for (or the JSX
|
|
46
|
-
* sibling it applies to), and comments whose removal would move a
|
|
37
|
+
* comments the `keep` predicate claims, and the comments a directive that
|
|
38
|
+
* stays is read with — one that stands between it and the code it would
|
|
39
|
+
* otherwise apply to, or in front of it where it would otherwise open its
|
|
40
|
+
* line (see shieldDirectives) — or whose removal would move a
|
|
47
41
|
* position-dependent directive (prettier's first-comment pragma, Deno's
|
|
48
42
|
* coverage file pragma, Bun's file-start marker) into effect.
|
|
49
43
|
*/
|
|
@@ -60,11 +54,16 @@ export interface RemoveResult {
|
|
|
60
54
|
* - AST-based ranges: strings, template literals and regexes are never affected.
|
|
61
55
|
* - Directives (`@ts-expect-error`, `eslint-disable`, ...) and license headers are
|
|
62
56
|
* kept by default, since deleting them changes build/lint behaviour.
|
|
63
|
-
* - Surviving comments keep their directive semantics
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
57
|
+
* - Surviving comments keep their directive semantics. The lines a directive
|
|
58
|
+
* covers stay lines of their own: where deleting the comments on one would
|
|
59
|
+
* empty it, an empty line stays in its place, so that the code below moves
|
|
60
|
+
* up under no next-line directive or counted coverage hint, and where it
|
|
61
|
+
* would join one to another line, the comment's own line breaks stay. A
|
|
62
|
+
* comment stays only where a tool reads the directive with it: one between
|
|
63
|
+
* a directive and the code it would otherwise apply to, or one whose
|
|
64
|
+
* removal would newly activate a position-dependent directive (prettier's
|
|
65
|
+
* `@format` docblock pragma counts only as the file's first comment, Bun's
|
|
66
|
+
* `// @bun` only at the very start or right after a shebang line).
|
|
68
67
|
* - A space is inserted where removing a block comment would merge two tokens,
|
|
69
68
|
* and a line break is kept where the comment acted as one (ASI stays intact).
|
|
70
69
|
* - A JSX comment goes with its braces (`{/* note */}`), unless that would
|
|
@@ -91,8 +90,8 @@ export interface RemoveResult {
|
|
|
91
90
|
* set, an error is thrown instead of returning corrupted output.
|
|
92
91
|
*/
|
|
93
92
|
export function removeComments(source: string, options: RemoveOptions = {}): RemoveResult {
|
|
94
|
-
const
|
|
95
|
-
const analysis = analyzeSource(source,
|
|
93
|
+
const scan: ScanOptions = { jsx: options.jsx === true, javascript: options.javascript === true };
|
|
94
|
+
const analysis = analyzeSource(source, scan);
|
|
96
95
|
const comments = analysis.comments;
|
|
97
96
|
|
|
98
97
|
let removed: Comment[] = [];
|
|
@@ -114,10 +113,13 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
114
113
|
for (const comment of container.comments) containerOf.set(comment, container);
|
|
115
114
|
}
|
|
116
115
|
const context: ShieldContext = {
|
|
116
|
+
comments,
|
|
117
117
|
containerOf,
|
|
118
118
|
skippable: skippableRanges(comments, containerOf, new Set(removed)),
|
|
119
119
|
preceding: analysis.preceding,
|
|
120
120
|
directives: analysis.directives,
|
|
121
|
+
shadows: analysis.shadows,
|
|
122
|
+
casts: analysis.casts,
|
|
121
123
|
};
|
|
122
124
|
const search = containerSearch(containers);
|
|
123
125
|
// Collapsing blank lines can only move a surviving comment to the very
|
|
@@ -131,7 +133,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
131
133
|
const holders = new Set<Comment>();
|
|
132
134
|
|
|
133
135
|
for (;;) {
|
|
134
|
-
const shielding =
|
|
136
|
+
const shielding = shieldDirectives(source, removed, kept, skipped, context);
|
|
135
137
|
removed = shielding.removed;
|
|
136
138
|
|
|
137
139
|
if (removed.length === 0) {
|
|
@@ -140,11 +142,15 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
140
142
|
|
|
141
143
|
const removing = new Set(removed);
|
|
142
144
|
search.track(new Set(containers.filter((container) => emptiedBy(container, removing))));
|
|
143
|
-
const edits = removalEdits(removed, containerOf, search.droppable, holders
|
|
145
|
+
const edits = removalEdits(removed, containerOf, search.droppable, holders);
|
|
144
146
|
const deletions = edits.filter((edit) => edit.replacement === undefined);
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
147
|
+
const lines: ShieldedLines = {
|
|
148
|
+
shielded: shielding.lines,
|
|
149
|
+
held: heldLines(source, deletions, comments, shielding.lines),
|
|
150
|
+
};
|
|
151
|
+
const blankEdits = tidyBlankLines ? blankLineEdits(source, deletions, comments, lines, analysis.jsx) : [];
|
|
152
|
+
const code = editKeepingBom(source, [...edits, ...blankEdits].sort(byStart), lines);
|
|
153
|
+
const output = analyzeSource(code, scan);
|
|
148
154
|
|
|
149
155
|
if (search.adjust(changedSegments(analysis.jsx.segments, output.jsx.segments))) continue;
|
|
150
156
|
|
|
@@ -158,7 +164,7 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
158
164
|
continue;
|
|
159
165
|
}
|
|
160
166
|
|
|
161
|
-
const survivor = firstChangedSurvivor(source, code, output
|
|
167
|
+
const survivor = firstChangedSurvivor(source, analysis, code, output, kept, skipped);
|
|
162
168
|
if (survivor === undefined) {
|
|
163
169
|
return { code, removed, kept, skipped, changed: true };
|
|
164
170
|
}
|
|
@@ -169,7 +175,9 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
169
175
|
|
|
170
176
|
// A surviving comment changed meaning: with everything before it deleted
|
|
171
177
|
// it became the file's first comment (or its very first bytes), turning a
|
|
172
|
-
// previously inert positional directive live
|
|
178
|
+
// previously inert positional directive live — or, in JavaScript, a
|
|
179
|
+
// multi-line comment in front of it went for a line break, after which
|
|
180
|
+
// tsc reads it as the JSDoc of the code behind it. Re-protect the removal
|
|
173
181
|
// candidate closest before it and try again: that comment then keeps
|
|
174
182
|
// holding the survivor away from the activating position, exactly as the
|
|
175
183
|
// input did. (Looping, because the re-protected comment can itself be a
|
|
@@ -204,24 +212,28 @@ function corresponding<T>(before: readonly T[], after: readonly T[]): Array<[T,
|
|
|
204
212
|
* survive, in document order (removal never reorders them). Throws when the
|
|
205
213
|
* texts themselves differ (the output would be corrupted) and returns the
|
|
206
214
|
* first surviving comment whose directive semantics changed, or undefined
|
|
207
|
-
* when the output is exactly as expected.
|
|
208
|
-
*
|
|
215
|
+
* when the output is exactly as expected. Every directive a comment carries
|
|
216
|
+
* is compared, and what tsc reads from it as JSDoc in JavaScript. The
|
|
217
|
+
* position-dependent pragmas are compared on their own besides: a comment
|
|
209
218
|
* can carry an inert `@format` underneath the directive an earlier rule
|
|
210
|
-
* already named (an eslint config block, say), and
|
|
211
|
-
*
|
|
219
|
+
* already named (an eslint config block, say), and where an ast-grep
|
|
220
|
+
* suppression covers the file does not show in its name.
|
|
212
221
|
*/
|
|
213
222
|
function firstChangedSurvivor(
|
|
214
223
|
source: string,
|
|
224
|
+
analysis: SourceAnalysis,
|
|
215
225
|
code: string,
|
|
216
|
-
|
|
226
|
+
output: SourceAnalysis,
|
|
217
227
|
kept: Comment[],
|
|
218
228
|
skipped: Comment[],
|
|
219
229
|
): Comment | undefined {
|
|
220
|
-
|
|
230
|
+
const carried = (of: SourceAnalysis, comment: Comment): string =>
|
|
231
|
+
JSON.stringify([of.directives.get(comment) ?? [], of.typed.get(comment) ?? []]);
|
|
232
|
+
for (const [comment, survivor] of corresponding([...kept, ...skipped].sort(byStart), output.comments)) {
|
|
221
233
|
if (survivor.text !== comment.text) {
|
|
222
234
|
throw unexpectedResult();
|
|
223
235
|
}
|
|
224
|
-
if (survivor
|
|
236
|
+
if (carried(output, survivor) !== carried(analysis, comment)) return comment;
|
|
225
237
|
const before = activePositionalDirectives(comment.kind, comment.text, positionalPlacement(source, comment));
|
|
226
238
|
const after = activePositionalDirectives(survivor.kind, survivor.text, positionalPlacement(code, survivor));
|
|
227
239
|
if (before.join("\n") !== after.join("\n")) return comment;
|
|
@@ -302,14 +314,14 @@ function gapHolders(
|
|
|
302
314
|
* comment-only first line took it along: the mark is an encoding artifact,
|
|
303
315
|
* not line content.
|
|
304
316
|
*/
|
|
305
|
-
function editKeepingBom(source: string, edits: readonly Edit[]): string {
|
|
306
|
-
if (!source.startsWith("")) return applyEdits(source, edits);
|
|
317
|
+
function editKeepingBom(source: string, edits: readonly Edit[], lines: ShieldedLines): string {
|
|
318
|
+
if (!source.startsWith("")) return applyEdits(source, edits, lines);
|
|
307
319
|
// The editing itself works on the BOM-less body (with ranges shifted to
|
|
308
320
|
// match) so its line handling never sees the mark, then the mark is
|
|
309
321
|
// re-attached; this keeps `// gone\n...` from losing the mark with
|
|
310
322
|
// the dropped line.
|
|
311
323
|
const shifted = edits.map((edit) => ({ ...edit, start: edit.start - 1, end: edit.end - 1 }));
|
|
312
|
-
return `${applyEdits(source.slice(1), shifted)}`;
|
|
324
|
+
return `${applyEdits(source.slice(1), shifted, lines)}`;
|
|
313
325
|
}
|
|
314
326
|
|
|
315
327
|
function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
@@ -319,65 +331,6 @@ function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
|
319
331
|
return false;
|
|
320
332
|
}
|
|
321
333
|
|
|
322
|
-
/**
|
|
323
|
-
* How a comment-only JSX container goes once all its comments do: braces
|
|
324
|
-
* and all; replaced by `{" "}` together with the spaces that held it off its
|
|
325
|
-
* neighbour; or with its braces kept, only the comments going.
|
|
326
|
-
*/
|
|
327
|
-
type ContainerMode = "drop" | "space" | "keep";
|
|
328
|
-
|
|
329
|
-
/** A comment-only JSX container (`{/* note */}`) with the comments in it. */
|
|
330
|
-
interface Container extends Range {
|
|
331
|
-
segment: number;
|
|
332
|
-
suspect: boolean;
|
|
333
|
-
/** The comment-only container right after it among the siblings, if any. */
|
|
334
|
-
next: Container | undefined;
|
|
335
|
-
comments: Comment[];
|
|
336
|
-
mode: ContainerMode;
|
|
337
|
-
/** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
|
|
338
|
-
spaceRange: Range | undefined;
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[] {
|
|
342
|
-
const containers = new Map<JsxContainer, Container>();
|
|
343
|
-
for (const layout of layouts) {
|
|
344
|
-
containers.set(layout, {
|
|
345
|
-
start: layout.start,
|
|
346
|
-
end: layout.end,
|
|
347
|
-
segment: layout.segment,
|
|
348
|
-
suspect: layout.suspect,
|
|
349
|
-
next: undefined,
|
|
350
|
-
comments: rangesWithin(comments, layout),
|
|
351
|
-
mode: "drop",
|
|
352
|
-
spaceRange: spaceRange(source, layout),
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
for (const [layout, container] of containers) {
|
|
356
|
-
if (layout.next !== undefined) container.next = containers.get(layout.next);
|
|
357
|
-
}
|
|
358
|
-
return [...containers.values()];
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* The range `{" "}` would take the place of: a one-line container and the
|
|
363
|
-
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
364
|
-
* the spaces after it when it opens its line and code follows. Undefined when
|
|
365
|
-
* no spaces hold it off anything on its line.
|
|
366
|
-
*/
|
|
367
|
-
function spaceRange(source: string, container: Range): Range | undefined {
|
|
368
|
-
if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
|
|
369
|
-
let before = container.start;
|
|
370
|
-
while (isHorizontalSpace(source[before - 1])) before -= 1;
|
|
371
|
-
let after = container.end;
|
|
372
|
-
while (isHorizontalSpace(source[after])) after += 1;
|
|
373
|
-
|
|
374
|
-
if (!opensItsLine(source, container)) {
|
|
375
|
-
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
376
|
-
}
|
|
377
|
-
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
|
|
378
|
-
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
334
|
/**
|
|
382
335
|
* Comments, and the comment-only containers that may go along with theirs,
|
|
383
336
|
* as sorted ranges: what the search for the code after a directive skips.
|
|
@@ -393,43 +346,16 @@ function skippableRanges(
|
|
|
393
346
|
return mergeContainers(comments, containerOf, (container) => emptiedBy(container, candidates));
|
|
394
347
|
}
|
|
395
348
|
|
|
396
|
-
/**
|
|
397
|
-
* The comments, in document order, with those of every container that
|
|
398
|
-
* `merges` accepts merged into the container itself: one unit spanning its
|
|
399
|
-
* braces in place of the comments inside them.
|
|
400
|
-
*/
|
|
401
|
-
function mergeContainers(
|
|
402
|
-
comments: readonly Comment[],
|
|
403
|
-
containerOf: ReadonlyMap<Comment, Container>,
|
|
404
|
-
merges: (container: Container) => boolean,
|
|
405
|
-
): Array<Comment | Container> {
|
|
406
|
-
const units: Array<Comment | Container> = [];
|
|
407
|
-
for (const comment of comments) {
|
|
408
|
-
const container = containerOf.get(comment);
|
|
409
|
-
if (container === undefined || !merges(container)) units.push(comment);
|
|
410
|
-
else if (units.at(-1) !== container) units.push(container);
|
|
411
|
-
}
|
|
412
|
-
return units;
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/** Whether the container's comments are all among the given ones, so that its braces can go with them. */
|
|
416
|
-
function emptiedBy(container: Container, comments: ReadonlySet<Comment>): boolean {
|
|
417
|
-
return container.comments.every((comment) => comments.has(comment));
|
|
418
|
-
}
|
|
419
|
-
|
|
420
349
|
/**
|
|
421
350
|
* The edits that remove the comments: one per comment, or one per container
|
|
422
351
|
* going with its braces. A holder of a gap around the imports (see
|
|
423
|
-
* gapHolders) leaves an empty line in its place
|
|
424
|
-
* otherwise join or split the lines a directive covers one of its own line
|
|
425
|
-
* breaks (see Shielding.lineBreaks).
|
|
352
|
+
* gapHolders) leaves an empty line in its place.
|
|
426
353
|
*/
|
|
427
354
|
function removalEdits(
|
|
428
355
|
removed: readonly Comment[],
|
|
429
356
|
containerOf: ReadonlyMap<Comment, Container>,
|
|
430
357
|
droppable: ReadonlySet<Container>,
|
|
431
358
|
holders: ReadonlySet<Comment>,
|
|
432
|
-
lineBreaks: ReadonlySet<Comment>,
|
|
433
359
|
): Edit[] {
|
|
434
360
|
const units = mergeContainers(
|
|
435
361
|
removed,
|
|
@@ -437,9 +363,7 @@ function removalEdits(
|
|
|
437
363
|
(container) => droppable.has(container) && container.mode !== "keep",
|
|
438
364
|
);
|
|
439
365
|
return units.map((unit): Edit => {
|
|
440
|
-
if (!("comments" in unit)) {
|
|
441
|
-
return { start: unit.start, end: unit.end, blankLine: holders.has(unit), lineBreak: lineBreaks.has(unit) };
|
|
442
|
-
}
|
|
366
|
+
if (!("comments" in unit)) return { start: unit.start, end: unit.end, blankLine: holders.has(unit) };
|
|
443
367
|
return unit.mode === "drop"
|
|
444
368
|
? { start: unit.start, end: unit.end, jsx: true }
|
|
445
369
|
: { ...(unit.spaceRange as Range), replacement: '{" "}' };
|
|
@@ -540,617 +464,3 @@ function containerSearch(containers: readonly Container[]): ContainerSearch {
|
|
|
540
464
|
},
|
|
541
465
|
};
|
|
542
466
|
}
|
|
543
|
-
|
|
544
|
-
interface ShieldContext {
|
|
545
|
-
/** The comment-only JSX container each comment in one sits in. */
|
|
546
|
-
containerOf: ReadonlyMap<Comment, Container>;
|
|
547
|
-
/** Comments and the containers that may go with theirs, sorted: what code starts after. */
|
|
548
|
-
skippable: readonly Range[];
|
|
549
|
-
/** What stands in front of each comment carrying an ast-grep suppression (see SourceAnalysis). */
|
|
550
|
-
preceding: ReadonlyMap<Comment, PrecedingNode>;
|
|
551
|
-
/** Every directive each directive comment carries (see SourceAnalysis). */
|
|
552
|
-
directives: ReadonlyMap<Comment, readonly string[]>;
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
interface Shielding {
|
|
556
|
-
removed: Comment[];
|
|
557
|
-
/**
|
|
558
|
-
* Lines (1-based) that a surviving directive reaches: the ones below a
|
|
559
|
-
* next-line directive, and the ones a line directive stands on.
|
|
560
|
-
*/
|
|
561
|
-
lines: Set<number>;
|
|
562
|
-
/**
|
|
563
|
-
* Comments with code on both sides whose deletion would join one of those
|
|
564
|
-
* lines to another, where no line break would take their place, or split
|
|
565
|
-
* one, where a line feed would stand in for line breaks the tools that only
|
|
566
|
-
* count line feeds do not count. One of the comment's own line breaks takes
|
|
567
|
-
* its place (see Edit.lineBreak).
|
|
568
|
-
*/
|
|
569
|
-
lineBreaks: Set<Comment>;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
/**
|
|
573
|
-
* What deleting part of the removal takes out of the source: a single
|
|
574
|
-
* comment, or a comment-only JSX container going braces and all.
|
|
575
|
-
*/
|
|
576
|
-
interface RemovalUnit extends Range {
|
|
577
|
-
comments: readonly Comment[];
|
|
578
|
-
/** The unit is a container's braces, which go with the comments in them. */
|
|
579
|
-
braces: boolean;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/**
|
|
583
|
-
* Re-protects comments whose removal would delete a physical line sitting
|
|
584
|
-
* under a surviving next-line directive (`@ts-expect-error`,
|
|
585
|
-
* `eslint-disable-next-line`, ...). Dropping such a line would shift the
|
|
586
|
-
* following code up, silently changing which line the directive applies to —
|
|
587
|
-
* and with it the compiler or linter behaviour. Nor may a removal join
|
|
588
|
-
* another line to one a directive covers, the lines a line directive
|
|
589
|
-
* (`eslint-disable-line`, `nosemgrep` after code, ...) stands on included:
|
|
590
|
-
* a comment that would leaves one of its line breaks in its place (see
|
|
591
|
-
* Shielding.lineBreaks). Directives whose tools reach further get more:
|
|
592
|
-
* below a tsc suppression the first line each compiler cannot step over
|
|
593
|
-
* stays, oxlint's line-feed rows add the lines they run over, between a
|
|
594
|
-
* directive that only counts as the last comment before its target
|
|
595
|
-
* (`@deno-types`, ...) or a coverage hint that ast-v8-to-istanbul only
|
|
596
|
-
* applies to the node right after it and that target every comment stays,
|
|
597
|
-
* and a JSX comment container right after a directive in a container
|
|
598
|
-
* (`{/* prettier-ignore */}`, which applies to the next sibling) stays too.
|
|
599
|
-
* Each directive a comment carries shields what it reaches, whichever one
|
|
600
|
-
* names the comment.
|
|
601
|
-
*
|
|
602
|
-
* The line checks work on whole lines, not single comments: a line holding
|
|
603
|
-
* several removable comments (`/* one */ /* two */`) vanishes just the same,
|
|
604
|
-
* so every comment on it has to stay — and a comment-only JSX container takes
|
|
605
|
-
* its braces along. Runs to a fixpoint because a re-protected comment can
|
|
606
|
-
* itself be a next-line directive shielding further lines (when directives
|
|
607
|
-
* are being removed selectively).
|
|
608
|
-
*/
|
|
609
|
-
function shieldNextLineDirectives(
|
|
610
|
-
source: string,
|
|
611
|
-
removed: Comment[],
|
|
612
|
-
kept: Comment[],
|
|
613
|
-
skipped: Comment[],
|
|
614
|
-
context: ShieldContext,
|
|
615
|
-
): Shielding {
|
|
616
|
-
const lineOffsets = lineStartOffsets(source);
|
|
617
|
-
const totalLines = lineOffsets.length;
|
|
618
|
-
const shieldedLines = new Set<number>();
|
|
619
|
-
const lineBreaks = new Set<Comment>();
|
|
620
|
-
const blockingLines = new Set<number>();
|
|
621
|
-
const shieldedGaps: Array<readonly [number, number]> = [];
|
|
622
|
-
const siblingTargets = new Set<Container>();
|
|
623
|
-
const astGrepReaches: AstGrepReach[] = [];
|
|
624
|
-
const processed = new Set<Comment>();
|
|
625
|
-
let stillRemoved = removed;
|
|
626
|
-
let units: RemovalUnit[] | undefined;
|
|
627
|
-
const currentUnits = (): RemovalUnit[] => (units ??= removalUnits(stillRemoved, context.containerOf));
|
|
628
|
-
let rows: number[] | undefined;
|
|
629
|
-
/** The (1-based) line holding the offset. */
|
|
630
|
-
const lineOf = (offset: number): number => lineIndexAt(lineOffsets, offset) + 1;
|
|
631
|
-
/**
|
|
632
|
-
* Whether Semgrep can read the `nosemgrep` comment as a suppression of the
|
|
633
|
-
* line below it (see SEMGREP_PREVIOUS_LINE): the line the comment ends on,
|
|
634
|
-
* with the comments in front of it cut out as the removal may cut them,
|
|
635
|
-
* opens with the marker.
|
|
636
|
-
*/
|
|
637
|
-
const readsAsSemgrepPreviousLine = (comment: Comment): boolean => {
|
|
638
|
-
let cursor = lineOffsets[comment.endLine - 1] as number;
|
|
639
|
-
let before = "";
|
|
640
|
-
const ranges = context.skippable;
|
|
641
|
-
for (let index = firstIndexAtOrAfter(ranges, cursor + 1, (range) => range.end); index < ranges.length; index += 1) {
|
|
642
|
-
const range = ranges[index] as Range;
|
|
643
|
-
if (range.start >= comment.start) break;
|
|
644
|
-
before += source.slice(cursor, Math.max(cursor, range.start));
|
|
645
|
-
cursor = range.end;
|
|
646
|
-
}
|
|
647
|
-
if (cursor < comment.start) before += source.slice(cursor, comment.start);
|
|
648
|
-
return SEMGREP_PREVIOUS_LINE.test(before + lastLine(comment.text));
|
|
649
|
-
};
|
|
650
|
-
/** The given (1-based) line, which the shields only name when the source has it, and the units on it. */
|
|
651
|
-
const unitsOnLine = (line: number): LineUnits => {
|
|
652
|
-
const start = lineOffsets[line - 1] as number;
|
|
653
|
-
const end = lineOffsets[line] ?? source.length;
|
|
654
|
-
return { start, end, units: currentUnits().filter((unit) => unit.start < end && unit.end > start) };
|
|
655
|
-
};
|
|
656
|
-
|
|
657
|
-
const collectShields = (): boolean => {
|
|
658
|
-
let added = false;
|
|
659
|
-
const shieldLines = (from: number, to: number): void => {
|
|
660
|
-
for (let line = from; line <= Math.min(to, totalLines); line += 1) {
|
|
661
|
-
if (shieldedLines.has(line)) continue;
|
|
662
|
-
shieldedLines.add(line);
|
|
663
|
-
added = true;
|
|
664
|
-
}
|
|
665
|
-
};
|
|
666
|
-
/** Collects what one of the directives the comment carries reaches. */
|
|
667
|
-
const shieldReach = (comment: Comment, directive: string): void => {
|
|
668
|
-
const lastComment = LAST_COMMENT_DIRECTIVES.has(directive);
|
|
669
|
-
if (lastComment || NEXT_TOKEN_DIRECTIVES.has(directive)) {
|
|
670
|
-
const codeStart = codeStartAfter(source, comment.end, context.skippable);
|
|
671
|
-
if (codeStart > comment.end && codeStart < source.length) {
|
|
672
|
-
shieldedGaps.push([comment.end, codeStart]);
|
|
673
|
-
added = true;
|
|
674
|
-
}
|
|
675
|
-
if (lastComment) return;
|
|
676
|
-
}
|
|
677
|
-
if (TSC_SUPPRESSIONS.has(directive)) {
|
|
678
|
-
for (const line of firstBlockingLines(source, comment, lineOffsets, context.skippable)) {
|
|
679
|
-
if (blockingLines.has(line)) continue;
|
|
680
|
-
blockingLines.add(line);
|
|
681
|
-
added = true;
|
|
682
|
-
}
|
|
683
|
-
}
|
|
684
|
-
// oxlint reaches its rows' lines past the ones counted below.
|
|
685
|
-
if (OXLINT_ROW_DIRECTIVES.has(directive)) {
|
|
686
|
-
const reach = oxlintReachLines(source, (rows ??= rowStartOffsets(source)), lineOf, comment, directive);
|
|
687
|
-
if (reach !== undefined) shieldLines(reach.from, reach.to);
|
|
688
|
-
}
|
|
689
|
-
// A line directive covers the lines it stands on, which no removal may
|
|
690
|
-
// join to another (see joiningUnits).
|
|
691
|
-
if (coversOwnLine(directive)) shieldLines(comment.line, comment.endLine);
|
|
692
|
-
if (!isNextLineDirective(directive)) return;
|
|
693
|
-
// In JSX, such a directive in braces applies to the sibling after it,
|
|
694
|
-
// whatever line that is on (prettier leaves the element after
|
|
695
|
-
// `{/* prettier-ignore */}` as is): a comment-only container standing
|
|
696
|
-
// between them has to stay. Range and file pragmas target no sibling.
|
|
697
|
-
const holder = context.containerOf.get(comment);
|
|
698
|
-
if (holder?.next !== undefined && !siblingTargets.has(holder.next)) {
|
|
699
|
-
siblingTargets.add(holder.next);
|
|
700
|
-
added = true;
|
|
701
|
-
}
|
|
702
|
-
// A trailing formatter or scanner suppression (`a = [1]; // oxfmt-ignore`,
|
|
703
|
-
// `secret(); // skipcq`) targets what it trails, not the line below it.
|
|
704
|
-
// In JSX, the container holding the comment is what has to stand on
|
|
705
|
-
// lines of its own. Semgrep's reach is a matter of what precedes the
|
|
706
|
-
// marker on its line.
|
|
707
|
-
if (TRAILING_TARGETS_OWN_LINE.has(directive) && !occupiesWholeLines(source, holder ?? comment)) return;
|
|
708
|
-
if (directive === "nosemgrep" && !readsAsSemgrepPreviousLine(comment)) return;
|
|
709
|
-
// Counted pragmas like `c8 ignore next 3` cover several lines.
|
|
710
|
-
shieldLines(comment.endLine + 1, comment.endLine + shieldedLineCount(comment, directive));
|
|
711
|
-
};
|
|
712
|
-
for (const comment of [...kept, ...skipped]) {
|
|
713
|
-
if (processed.has(comment)) continue;
|
|
714
|
-
processed.add(comment);
|
|
715
|
-
if (comment.directive === undefined) continue;
|
|
716
|
-
if (carriesAstGrepSuppression(comment.text)) {
|
|
717
|
-
const reach = astGrepReach(
|
|
718
|
-
source,
|
|
719
|
-
(rows ??= rowStartOffsets(source)),
|
|
720
|
-
comment,
|
|
721
|
-
context.preceding.get(comment) as PrecedingNode,
|
|
722
|
-
);
|
|
723
|
-
astGrepReaches.push(reach);
|
|
724
|
-
if (reach.nextRow !== undefined && reach.nextRow.end > reach.nextRow.start) {
|
|
725
|
-
shieldLines(lineOf(reach.nextRow.start), lineOf(reach.nextRow.end - 1));
|
|
726
|
-
}
|
|
727
|
-
added = true;
|
|
728
|
-
}
|
|
729
|
-
for (const directive of context.directives.get(comment) as readonly string[]) shieldReach(comment, directive);
|
|
730
|
-
}
|
|
731
|
-
return added;
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
/** Takes the comments, all still being removed, out of the removal and keeps them. */
|
|
735
|
-
const protect = (group: readonly Comment[]): void => {
|
|
736
|
-
if (group.length === 0) return;
|
|
737
|
-
const grouped = new Set(group);
|
|
738
|
-
stillRemoved = stillRemoved.filter((comment) => !grouped.has(comment));
|
|
739
|
-
kept.push(...group);
|
|
740
|
-
units = undefined;
|
|
741
|
-
};
|
|
742
|
-
|
|
743
|
-
/** Protects what the shields collected so far hold in place; true when that kept anything. */
|
|
744
|
-
const protectShields = (): boolean => {
|
|
745
|
-
const removing = stillRemoved.length;
|
|
746
|
-
for (const line of shieldedLines) {
|
|
747
|
-
// A shielded line may neither vanish nor be joined to another. A
|
|
748
|
-
// comment leaves a line break behind instead; a container's braces have
|
|
749
|
-
// none to leave without changing what the element renders, so they stay.
|
|
750
|
-
protect(vanishingLineGroup(source, unitsOnLine(line)));
|
|
751
|
-
for (const unit of joiningUnits(source, unitsOnLine(line))) {
|
|
752
|
-
if (unit.braces) protect(unit.comments);
|
|
753
|
-
else lineBreaks.add(unit.comments[0] as Comment);
|
|
754
|
-
}
|
|
755
|
-
}
|
|
756
|
-
for (const line of blockingLines) protect(lineOpeningGroup(source, unitsOnLine(line)));
|
|
757
|
-
for (const [from, to] of shieldedGaps) {
|
|
758
|
-
protect(stillRemoved.filter((comment) => comment.start >= from && comment.start < to));
|
|
759
|
-
}
|
|
760
|
-
for (const target of siblingTargets) protect(stillRemoved.filter((comment) => target.comments.includes(comment)));
|
|
761
|
-
for (const reach of astGrepReaches) protect(commentsOf(currentUnits().filter((unit) => reach.holds(unit))));
|
|
762
|
-
return stillRemoved.length < removing;
|
|
763
|
-
};
|
|
764
|
-
|
|
765
|
-
collectShields();
|
|
766
|
-
while (protectShields()) {
|
|
767
|
-
if (!collectShields()) break;
|
|
768
|
-
}
|
|
769
|
-
if (stillRemoved.length !== removed.length) {
|
|
770
|
-
kept.sort(byStart);
|
|
771
|
-
}
|
|
772
|
-
return { removed: stillRemoved, lines: shieldedLines, lineBreaks };
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
/**
|
|
776
|
-
* The removals as the edits delete them: a comment-only container whose
|
|
777
|
-
* comments all go — and that is to go braces and all — is one unit spanning
|
|
778
|
-
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
779
|
-
*/
|
|
780
|
-
function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[] {
|
|
781
|
-
const removing = new Set(removals);
|
|
782
|
-
const units = mergeContainers(
|
|
783
|
-
removals,
|
|
784
|
-
containerOf,
|
|
785
|
-
(container) => container.mode === "drop" && emptiedBy(container, removing),
|
|
786
|
-
);
|
|
787
|
-
return units.map((unit) => ({
|
|
788
|
-
start: unit.start,
|
|
789
|
-
end: unit.end,
|
|
790
|
-
...("comments" in unit ? { comments: unit.comments, braces: true } : { comments: [unit], braces: false }),
|
|
791
|
-
}));
|
|
792
|
-
}
|
|
793
|
-
|
|
794
|
-
/** The comments the units delete. */
|
|
795
|
-
function commentsOf(units: readonly RemovalUnit[]): Comment[] {
|
|
796
|
-
return units.flatMap((unit) => unit.comments);
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
/**
|
|
800
|
-
* Offset of the first character at or after `from` that is neither
|
|
801
|
-
* whitespace nor inside one of the (sorted, non-overlapping) ranges, or the
|
|
802
|
-
* source length when only ranges follow. `from` lies past the start of at
|
|
803
|
-
* least one range (the directive the search is for); when the closest of
|
|
804
|
-
* them runs past `from` — a block comment opened on the directive's line —
|
|
805
|
-
* the search resumes at its end.
|
|
806
|
-
*/
|
|
807
|
-
function codeStartAfter(source: string, from: number, ranges: readonly Range[]): number {
|
|
808
|
-
let next = firstIndexAtOrAfter(ranges, from, (range) => range.start);
|
|
809
|
-
let index = Math.max(from, (ranges[next - 1] as Range).end);
|
|
810
|
-
while (index < source.length) {
|
|
811
|
-
const range = ranges[next];
|
|
812
|
-
if (range !== undefined && range.start === index) {
|
|
813
|
-
index = range.end;
|
|
814
|
-
next += 1;
|
|
815
|
-
} else if (isBlank(source[index] as string)) {
|
|
816
|
-
index += 1;
|
|
817
|
-
} else {
|
|
818
|
-
return index;
|
|
819
|
-
}
|
|
820
|
-
}
|
|
821
|
-
return source.length;
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
/**
|
|
825
|
-
* For each TypeScript compiler, the first line between a tsc suppression and
|
|
826
|
-
* the code on the lines below it that the compiler does not step over, if
|
|
827
|
-
* any. Looking for the line a suppression applies to, a compiler skips the
|
|
828
|
-
* lines it reads as blank or as a `//` comment (see TSC_PASSABLE_LINES); any
|
|
829
|
-
* other line — one opening with a block comment, say — ends the search,
|
|
830
|
-
* leaving the suppression without a target for that compiler. Deleting that
|
|
831
|
-
* line would let the suppression reach the code instead, so it has to stay.
|
|
832
|
-
* The two compilers can stop at different lines: a line holding only a
|
|
833
|
-
* vertical tab stops tsc 7, while tsc 6 searches on to the next line it
|
|
834
|
-
* cannot pass.
|
|
835
|
-
*/
|
|
836
|
-
function firstBlockingLines(
|
|
837
|
-
source: string,
|
|
838
|
-
comment: Comment,
|
|
839
|
-
lineOffsets: readonly number[],
|
|
840
|
-
ranges: readonly Range[],
|
|
841
|
-
): number[] {
|
|
842
|
-
// A suppression applies from the line below it on, so code sharing its line
|
|
843
|
-
// (`/* @ts-ignore */ foo();`) does not end the search.
|
|
844
|
-
const codeStart = codeStartAfter(source, lineOffsets[comment.endLine] ?? source.length, ranges);
|
|
845
|
-
if (codeStart >= source.length) return [];
|
|
846
|
-
const codeLine = lineIndexAt(lineOffsets, codeStart) + 1;
|
|
847
|
-
const found: number[] = [];
|
|
848
|
-
for (const passable of TSC_PASSABLE_LINES) {
|
|
849
|
-
for (let line = comment.endLine + 1; line < codeLine; line += 1) {
|
|
850
|
-
if (!passable.test(source.slice(lineOffsets[line - 1], lineOffsets[line]))) {
|
|
851
|
-
found.push(line);
|
|
852
|
-
break;
|
|
853
|
-
}
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
return found;
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
/** A line, from its start to the next line's (past its line break), with the removal units holding part of it. */
|
|
860
|
-
interface LineUnits extends Range {
|
|
861
|
-
units: RemovalUnit[];
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
/**
|
|
865
|
-
* The removals on the given line when one of them opens it (holds its first
|
|
866
|
-
* non-whitespace character), so that deleting them would change how the
|
|
867
|
-
* line starts. None when something that survives opens the line instead, or
|
|
868
|
-
* nothing on it is to be removed.
|
|
869
|
-
*/
|
|
870
|
-
function lineOpeningGroup(source: string, line: LineUnits): Comment[] {
|
|
871
|
-
let first = line.start;
|
|
872
|
-
while (first < line.end && isBlank(source[first] as string)) first += 1;
|
|
873
|
-
return line.units.some((unit) => unit.start <= first && first < unit.end) ? commentsOf(line.units) : [];
|
|
874
|
-
}
|
|
875
|
-
|
|
876
|
-
/**
|
|
877
|
-
* The removals that would make the given line disappear: every
|
|
878
|
-
* non-whitespace character of the line lies inside one of them, so deleting
|
|
879
|
-
* them deletes the physical line. None when the line survives (it holds
|
|
880
|
-
* code, a kept comment, or nothing to remove at all).
|
|
881
|
-
*/
|
|
882
|
-
function vanishingLineGroup(source: string, line: LineUnits): Comment[] {
|
|
883
|
-
if (line.units.length === 0) return [];
|
|
884
|
-
for (let index = line.start; index < line.end; index += 1) {
|
|
885
|
-
if (isBlank(source[index] as string)) continue;
|
|
886
|
-
if (!line.units.some((unit) => unit.start <= index && index < unit.end)) return [];
|
|
887
|
-
}
|
|
888
|
-
return commentsOf(line.units);
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
/**
|
|
892
|
-
* The removal units that would join the given line to another: they reach
|
|
893
|
-
* past one of its ends, with something other than whitespace beyond them on
|
|
894
|
-
* the line they run into. Deleting such a comment takes its line breaks
|
|
895
|
-
* along — with nothing in their place right after an opening bracket or
|
|
896
|
-
* before closing punctuation, and a line feed elsewhere (see applyEdits) — so
|
|
897
|
-
* what stands beyond it can land on the line, or what stands on the line move
|
|
898
|
-
* off it, and a directive covering the line would cover other code. (With
|
|
899
|
-
* only whitespace beyond, the lines the comment held vanish, which
|
|
900
|
-
* vanishingLineGroup weighs.)
|
|
901
|
-
*/
|
|
902
|
-
function joiningUnits(source: string, line: LineUnits): RemovalUnit[] {
|
|
903
|
-
return line.units.filter(
|
|
904
|
-
(unit) =>
|
|
905
|
-
(unit.end > line.end && !closesItsLine(source, unit)) || (unit.start < line.start && !opensItsLine(source, unit)),
|
|
906
|
-
);
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
// The directives oxlint reads on rows of its own (see oxlintReachLines).
|
|
910
|
-
const OXLINT_ROW_DIRECTIVES = new Set([
|
|
911
|
-
"eslint-disable-next-line",
|
|
912
|
-
"oxlint-disable-next-line",
|
|
913
|
-
"eslint-disable-line",
|
|
914
|
-
"oxlint-disable-line",
|
|
915
|
-
]);
|
|
916
|
-
|
|
917
|
-
/**
|
|
918
|
-
* The lines (1-based, inclusive) that oxlint applies an `eslint-` or
|
|
919
|
-
* `oxlint-` line or next-line comment to, past the ones the line-based
|
|
920
|
-
* shields take in. oxlint finds the stretch on rows that only a line feed
|
|
921
|
-
* ends (`split_inclusive('\n')` and `lines()` in its `disable_directives.rs`):
|
|
922
|
-
* a next-line comment covers the rest of the row its text ends on and the
|
|
923
|
-
* row after it, a line comment its own row up to the comment. A lone CR,
|
|
924
|
-
* U+2028 or U+2029 — a line break to tsc and ESLint — ends no row there, so
|
|
925
|
-
* a row can run over several of the lines counted here. Undefined when a
|
|
926
|
-
* next-line comment stands on the file's last row: oxlint then covers the
|
|
927
|
-
* rest of the file, which no removal changes.
|
|
928
|
-
*/
|
|
929
|
-
function oxlintReachLines(
|
|
930
|
-
source: string,
|
|
931
|
-
rows: readonly number[],
|
|
932
|
-
lineOf: (offset: number) => number,
|
|
933
|
-
comment: Comment,
|
|
934
|
-
directive: string,
|
|
935
|
-
): { from: number; to: number } | undefined {
|
|
936
|
-
const rowOf = (offset: number): number => lineIndexAt(rows, offset);
|
|
937
|
-
if (!directive.endsWith("-next-line")) {
|
|
938
|
-
return { from: lineOf(rows[rowOf(comment.start)] as number), to: comment.line };
|
|
939
|
-
}
|
|
940
|
-
// The comment's text ends in front of a block comment's `*/`.
|
|
941
|
-
const row = rowOf(comment.kind === "block" ? comment.end - 2 : comment.end);
|
|
942
|
-
if (rows[row + 1] === undefined) return undefined;
|
|
943
|
-
return { from: comment.endLine + 1, to: lineOf((rows[row + 2] ?? source.length) - 1) };
|
|
944
|
-
}
|
|
945
|
-
|
|
946
|
-
/**
|
|
947
|
-
* Whether the directive covers the lines it stands on: the `-line` forms of
|
|
948
|
-
* the suppressions (`eslint-disable-line`, `tslint:disable-line`, ...), the
|
|
949
|
-
* ones that cover the line holding them (see OWN_LINE_DIRECTIVES), and the
|
|
950
|
-
* `ignore next` hints c8 counts lines for (see COUNTED_NEXT_LINE): it
|
|
951
|
-
* ignores the line holding one along with the lines below it.
|
|
952
|
-
*/
|
|
953
|
-
function coversOwnLine(name: string): boolean {
|
|
954
|
-
return (
|
|
955
|
-
(name.endsWith("-line") && !name.endsWith("-next-line")) ||
|
|
956
|
-
OWN_LINE_DIRECTIVES.has(name) ||
|
|
957
|
-
COUNTED_NEXT_LINE.has(name)
|
|
958
|
-
);
|
|
959
|
-
}
|
|
960
|
-
|
|
961
|
-
// Suppressions that apply to the line holding them: Semgrep's to findings
|
|
962
|
-
// that start on it, CodeQL's `lgtm` to its alerts up to the comment,
|
|
963
|
-
// DeepSource's, SonarQube's, gitleaks', TruffleHog's and detect-secrets' to
|
|
964
|
-
// the line, and codespell's to its words.
|
|
965
|
-
const OWN_LINE_DIRECTIVES = new Set([
|
|
966
|
-
"nosemgrep",
|
|
967
|
-
"lgtm",
|
|
968
|
-
"skipcq",
|
|
969
|
-
"NOSONAR",
|
|
970
|
-
"gitleaks:allow",
|
|
971
|
-
"trufflehog:ignore",
|
|
972
|
-
"pragma-allowlist-secret",
|
|
973
|
-
"codespell:ignore",
|
|
974
|
-
]);
|
|
975
|
-
|
|
976
|
-
// Semgrep applies a `nosemgrep` to the line below where nothing but
|
|
977
|
-
// non-alphanumerics precede it on its line (`nosem_previous_line_re` in its
|
|
978
|
-
// Nosemgrep.ml): `// nosemgrep: rule-id` and `}); // nosemgrep` cover the next
|
|
979
|
-
// line, `// TODO nosemgrep` and `foo(); // nosemgrep` only their own.
|
|
980
|
-
const SEMGREP_PREVIOUS_LINE = /^[^a-zA-Z0-9]* *nosem(?:grep)?/i;
|
|
981
|
-
|
|
982
|
-
// These suppressions only apply to the following node/line when they stand on
|
|
983
|
-
// their own line — trailing after code they target (at most) their own line.
|
|
984
|
-
// Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
|
|
985
|
-
// line even when they trail code.
|
|
986
|
-
const TRAILING_TARGETS_OWN_LINE = new Set([
|
|
987
|
-
"prettier-ignore",
|
|
988
|
-
"oxfmt-ignore",
|
|
989
|
-
"dprint-ignore",
|
|
990
|
-
"lgtm",
|
|
991
|
-
"codeql",
|
|
992
|
-
"skipcq",
|
|
993
|
-
]);
|
|
994
|
-
|
|
995
|
-
// The suppressions tsc looks past blank and `//` lines for (see
|
|
996
|
-
// firstBlockingLines). They stay next-line directives as well: Flow, which
|
|
997
|
-
// honours them in TypeScript files, applies them to the very next line.
|
|
998
|
-
const TSC_SUPPRESSIONS = new Set(["@ts-ignore", "@ts-expect-error"]);
|
|
999
|
-
|
|
1000
|
-
// The lines each compiler steps over on its way up from a diagnostic to a
|
|
1001
|
-
// suppression, matched against the whole line, line break included. tsc 6
|
|
1002
|
-
// trims the line and accepts nothing or a `//` comment; tsc 7 accepts only
|
|
1003
|
-
// spaces and tabs before the line break or the `//`.
|
|
1004
|
-
const TSC_PASSABLE_LINES = [/^\s*(?:\/\/|$)/, /^[ \t]*(?:[\r\n]|\/\/)/];
|
|
1005
|
-
|
|
1006
|
-
// Directives that only count while no other comment sits between them and
|
|
1007
|
-
// their target: deno_graph reads `@deno-types`/`@ts-types` from the last
|
|
1008
|
-
// comment before an import, ts-prune and graphql-tag-pluck from the closest
|
|
1009
|
-
// leading comment, and ESLint's no-fallthrough and default-case from the last
|
|
1010
|
-
// comment before the next case or the closing brace. Deleting the comments in
|
|
1011
|
-
// that gap would newly activate such a directive, so the whole gap is
|
|
1012
|
-
// shielded — trailing comments in front of the code included. With no code
|
|
1013
|
-
// left there is nothing to target, and the switch-clause comments are only
|
|
1014
|
-
// detected in front of the `case`, `default` or `}` their rules read them
|
|
1015
|
-
// before.
|
|
1016
|
-
const LAST_COMMENT_DIRECTIVES = new Set([
|
|
1017
|
-
"@deno-types",
|
|
1018
|
-
"@ts-types",
|
|
1019
|
-
"ts-prune-ignore-next",
|
|
1020
|
-
"GraphQL",
|
|
1021
|
-
"fallthrough",
|
|
1022
|
-
"no-default",
|
|
1023
|
-
]);
|
|
1024
|
-
|
|
1025
|
-
// The coverage hints ast-v8-to-istanbul, Vitest's v8 provider, applies to the
|
|
1026
|
-
// node that starts at the very token after them: with another comment in
|
|
1027
|
-
// between, the hint has no target, and deleting that comment would bring it
|
|
1028
|
-
// into effect. The gap up to the code is shielded like the one above, on top
|
|
1029
|
-
// of the lines below the hint, which c8, Node and monocart count.
|
|
1030
|
-
const NEXT_TOKEN_DIRECTIVES = new Set([
|
|
1031
|
-
"istanbul-ignore-next",
|
|
1032
|
-
"istanbul-ignore-if",
|
|
1033
|
-
"istanbul-ignore-else",
|
|
1034
|
-
"c8-ignore-next",
|
|
1035
|
-
"c8-ignore-if",
|
|
1036
|
-
"c8-ignore-else",
|
|
1037
|
-
"v8-ignore-next",
|
|
1038
|
-
"v8-ignore-if",
|
|
1039
|
-
"v8-ignore-else",
|
|
1040
|
-
"node:coverage-ignore",
|
|
1041
|
-
"node:coverage-ignore-if",
|
|
1042
|
-
"node:coverage-ignore-else",
|
|
1043
|
-
]);
|
|
1044
|
-
|
|
1045
|
-
// Directives that target the line or node directly below them. File- and
|
|
1046
|
-
// range-scoped pragmas (`istanbul-ignore-file`, `c8-ignore-start`, ...) are
|
|
1047
|
-
// absent: they do not target the following line, so they never shield it.
|
|
1048
|
-
// They go by their names here: Vitest's v8 provider reads a range hint
|
|
1049
|
-
// spelled out on any line of this file, and would drop the rest of the file
|
|
1050
|
-
// from this project's coverage.
|
|
1051
|
-
const NEXT_LINE_DIRECTIVES = new Set([
|
|
1052
|
-
"@ts-ignore",
|
|
1053
|
-
"@ts-expect-error",
|
|
1054
|
-
"biome-ignore",
|
|
1055
|
-
"deno-lint-ignore",
|
|
1056
|
-
"deno-fmt-ignore",
|
|
1057
|
-
"deno-coverage-ignore",
|
|
1058
|
-
"prettier-ignore",
|
|
1059
|
-
"oxfmt-ignore",
|
|
1060
|
-
"dprint-ignore",
|
|
1061
|
-
"rome-ignore",
|
|
1062
|
-
"istanbul-ignore-next",
|
|
1063
|
-
"istanbul-ignore-if",
|
|
1064
|
-
"istanbul-ignore-else",
|
|
1065
|
-
"c8-ignore-next",
|
|
1066
|
-
"c8-ignore-if",
|
|
1067
|
-
"c8-ignore-else",
|
|
1068
|
-
"v8-ignore-next",
|
|
1069
|
-
"v8-ignore-if",
|
|
1070
|
-
"v8-ignore-else",
|
|
1071
|
-
"node:coverage-ignore",
|
|
1072
|
-
"node:coverage-ignore-if",
|
|
1073
|
-
"node:coverage-ignore-else",
|
|
1074
|
-
// Directives that hang off the node or line directly below them: a
|
|
1075
|
-
// whole-line comment between them and their target would change what
|
|
1076
|
-
// they apply to, so the line below stays shielded.
|
|
1077
|
-
"million-ignore",
|
|
1078
|
-
"@million-skip",
|
|
1079
|
-
"@million-jsx-skip",
|
|
1080
|
-
"deepcode-ignore",
|
|
1081
|
-
"skipcq",
|
|
1082
|
-
"lgtm",
|
|
1083
|
-
"codeql",
|
|
1084
|
-
"nosemgrep",
|
|
1085
|
-
"no-dd-sa",
|
|
1086
|
-
"datadog-disable",
|
|
1087
|
-
"noinspection",
|
|
1088
|
-
"language-injection",
|
|
1089
|
-
"@next-codemod-error",
|
|
1090
|
-
"@next-codemod-ignore",
|
|
1091
|
-
"$FlowFixMe",
|
|
1092
|
-
"$FlowExpectedError",
|
|
1093
|
-
"$FlowIssue",
|
|
1094
|
-
"$FlowIgnore",
|
|
1095
|
-
"pragma-allowlist-nextline-secret",
|
|
1096
|
-
"cspell:disable-next",
|
|
1097
|
-
"HTML",
|
|
1098
|
-
]);
|
|
1099
|
-
|
|
1100
|
-
function isNextLineDirective(name: string): boolean {
|
|
1101
|
-
return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
// The coverage pragmas a line count after `ignore next` applies to: c8
|
|
1105
|
-
// (v8-to-istanbul), Node's test-runner coverage and monocart-coverage-reports
|
|
1106
|
-
// count lines. istanbul and ast-v8-to-istanbul, Vitest's providers, apply the
|
|
1107
|
-
// hint to the next node whatever count follows, so `istanbul ignore next 3`
|
|
1108
|
-
// covers one line. Everything else — `@ts-expect-error`,
|
|
1109
|
-
// `eslint-disable-next-line` and friends — covers exactly one line too, no
|
|
1110
|
-
// matter what prose like "we ignore next 3 legacy calls" appears in the
|
|
1111
|
-
// reason text.
|
|
1112
|
-
const COUNTED_NEXT_LINE = new Set(["c8-ignore-next", "v8-ignore-next", "node:coverage-ignore"]);
|
|
1113
|
-
|
|
1114
|
-
/**
|
|
1115
|
-
* Number of following lines a next-line pragma the comment carries covers
|
|
1116
|
-
* (`c8 ignore next 3` → 3). monocart reads the count past any whitespace or
|
|
1117
|
-
* none, and c8 and monocart stop at the last digit, so `next3` and `next 3x`
|
|
1118
|
-
* count 3 as well.
|
|
1119
|
-
*/
|
|
1120
|
-
function shieldedLineCount(comment: Comment, directive: string): number {
|
|
1121
|
-
if (!COUNTED_NEXT_LINE.has(directive)) return 1;
|
|
1122
|
-
const match = /\bignore\s*next\s*(\d+)/.exec(comment.text);
|
|
1123
|
-
return match ? Math.max(1, Number(match[1])) : 1;
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
/**
|
|
1127
|
-
* True when nothing but whitespace shares the range's first and last lines.
|
|
1128
|
-
* Lines are delimited by any ECMAScript line terminator, matching how the
|
|
1129
|
-
* scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
|
|
1130
|
-
*/
|
|
1131
|
-
function occupiesWholeLines(source: string, range: Range): boolean {
|
|
1132
|
-
return opensItsLine(source, range) && closesItsLine(source, range);
|
|
1133
|
-
}
|
|
1134
|
-
|
|
1135
|
-
/** True when nothing but whitespace follows the range on its last line. */
|
|
1136
|
-
function closesItsLine(source: string, range: Range): boolean {
|
|
1137
|
-
return blankToLineEdge(source, range.end, 1);
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
/** True when nothing but whitespace precedes the range on its first line. */
|
|
1141
|
-
function opensItsLine(source: string, range: Range): boolean {
|
|
1142
|
-
return blankToLineEdge(source, range.start - 1, -1);
|
|
1143
|
-
}
|
|
1144
|
-
|
|
1145
|
-
/**
|
|
1146
|
-
* True when nothing but whitespace lies from the offset on, in the direction
|
|
1147
|
-
* of `step`, up to the line break or the edge of the source.
|
|
1148
|
-
*/
|
|
1149
|
-
function blankToLineEdge(source: string, from: number, step: 1 | -1): boolean {
|
|
1150
|
-
for (let index = from; index >= 0 && index < source.length; index += step) {
|
|
1151
|
-
const char = source[index] as string;
|
|
1152
|
-
if (LINE_TERMINATOR.test(char)) return true;
|
|
1153
|
-
if (!isBlank(char)) return false;
|
|
1154
|
-
}
|
|
1155
|
-
return true;
|
|
1156
|
-
}
|