@kongyo2/ts-comment-scanner 1.20.0 → 1.22.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/args.d.ts.map +1 -1
- package/dist/args.js +23 -19
- package/dist/args.js.map +1 -1
- package/dist/astgrep.d.ts +5 -3
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +10 -12
- 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 +21 -3
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +31 -23
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +7 -3
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +129 -82
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +14 -8
- 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/jsx.d.ts.map +1 -1
- package/dist/jsx.js +5 -6
- package/dist/jsx.js.map +1 -1
- package/dist/lines.d.ts +24 -5
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +50 -16
- package/dist/lines.js.map +1 -1
- package/dist/ranges.d.ts +8 -0
- package/dist/ranges.d.ts.map +1 -1
- package/dist/ranges.js +12 -0
- package/dist/ranges.js.map +1 -1
- package/dist/remove.d.ts +18 -14
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +69 -630
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +21 -28
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +34 -0
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +137 -31
- package/dist/scanner.js.map +1 -1
- package/dist/shields.d.ts +72 -0
- package/dist/shields.d.ts.map +1 -0
- package/dist/shields.js +624 -0
- package/dist/shields.js.map +1 -0
- package/dist/splice.d.ts +42 -11
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +189 -56
- package/dist/splice.js.map +1 -1
- package/package.json +2 -1
- package/src/args.ts +21 -21
- package/src/astgrep.ts +11 -13
- package/src/containers.ts +128 -0
- package/src/coverage.ts +110 -0
- package/src/directives.ts +38 -20
- package/src/files.ts +169 -91
- package/src/imports.ts +15 -8
- package/src/index.ts +1 -0
- package/src/jsx.ts +5 -6
- package/src/lines.ts +55 -14
- package/src/ranges.ts +17 -0
- package/src/remove.ts +82 -730
- package/src/run.ts +22 -34
- package/src/scanner.ts +180 -32
- package/src/shields.ts +701 -0
- package/src/splice.ts +224 -60
package/dist/astgrep.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Range } from "./ranges.js";
|
|
2
2
|
/**
|
|
3
3
|
* Whether a comment from `start` to `end` sits where an ast-grep suppression
|
|
4
4
|
* covers the whole file (0.38.4 on): it opens the first row, and the next
|
|
@@ -42,8 +42,10 @@ export interface AstGrepReach {
|
|
|
42
42
|
* when deleted, so those ending on its row in front of it stay, as do those
|
|
43
43
|
* after it on its row and those touching the row its node starts on; when a
|
|
44
44
|
* release covers the next row, so do those starting there, and that row's
|
|
45
|
-
* lines stay (see nextRow). On the first row, the
|
|
46
|
-
*
|
|
45
|
+
* lines stay (see nextRow). On the first row, the node after the comment
|
|
46
|
+
* decides whether it covers the file: where that node starts less than two
|
|
47
|
+
* rows down, the unit it is stays, since whatever follows it could start
|
|
48
|
+
* further down.
|
|
47
49
|
* The releases this follows parse with tree-sitter 0.24 or later (ast-grep
|
|
48
50
|
* 0.31.1 on); before, comments in JSX braces read as standing alone.
|
|
49
51
|
*/
|
package/dist/astgrep.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astgrep.d.ts","sourceRoot":"","sources":["../src/astgrep.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"astgrep.d.ts","sourceRoot":"","sources":["../src/astgrep.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAkBzC;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAIpF;AAMD,qGAAqG;AACrG,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAEtD;AAED,gHAAgH;AAChH,MAAM,WAAW,aAAa;IAC5B,6FAA6F;IAC7F,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,2EAA2E;IAC3E,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,iFAAiF;AACjF,MAAM,WAAW,YAAY;IAC3B,kFAAkF;IAClF,KAAK,CAAC,IAAI,EAAE,KAAK,GAAG,OAAO,CAAC;IAC5B;;;OAGG;IACH,OAAO,EAAE,KAAK,GAAG,SAAS,CAAC;CAC5B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,YAAY,CAC1B,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,SAAS,MAAM,EAAE,EAC5B,OAAO,EAAE,KAAK,EACd,SAAS,EAAE,aAAa,GACvB,YAAY,CAwBd"}
|
package/dist/astgrep.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { LINE_TERMINATOR } from "./lines.js";
|
|
2
|
-
import { firstIndexAtOrAfter } from "./ranges.js";
|
|
1
|
+
import { LINE_TERMINATOR, lineIndexAt, lineStartOffsets } from "./lines.js";
|
|
3
2
|
/**
|
|
4
3
|
* Where ast-grep's suppression comments apply (`get_suppression_kind` in its
|
|
5
4
|
* `crates/config/src/combined.rs`). ast-grep reads them off tree-sitter's
|
|
@@ -31,11 +30,7 @@ function lineFeedCount(text) {
|
|
|
31
30
|
}
|
|
32
31
|
/** Start offset of every tree-sitter row: the text's start and each offset just past a line feed. */
|
|
33
32
|
export function rowStartOffsets(text) {
|
|
34
|
-
|
|
35
|
-
for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) {
|
|
36
|
-
starts.push(index + 1);
|
|
37
|
-
}
|
|
38
|
-
return starts;
|
|
33
|
+
return lineStartOffsets(text, /\n/);
|
|
39
34
|
}
|
|
40
35
|
/**
|
|
41
36
|
* What an ast-grep suppression comment covers depends on the node in front
|
|
@@ -54,13 +49,15 @@ export function rowStartOffsets(text) {
|
|
|
54
49
|
* when deleted, so those ending on its row in front of it stay, as do those
|
|
55
50
|
* after it on its row and those touching the row its node starts on; when a
|
|
56
51
|
* release covers the next row, so do those starting there, and that row's
|
|
57
|
-
* lines stay (see nextRow). On the first row, the
|
|
58
|
-
*
|
|
52
|
+
* lines stay (see nextRow). On the first row, the node after the comment
|
|
53
|
+
* decides whether it covers the file: where that node starts less than two
|
|
54
|
+
* rows down, the unit it is stays, since whatever follows it could start
|
|
55
|
+
* further down.
|
|
59
56
|
* The releases this follows parse with tree-sitter 0.24 or later (ast-grep
|
|
60
57
|
* 0.31.1 on); before, comments in JSX braces read as standing alone.
|
|
61
58
|
*/
|
|
62
59
|
export function astGrepReach(text, rowStarts, comment, preceding) {
|
|
63
|
-
const rowOf = (offset) =>
|
|
60
|
+
const rowOf = (offset) => lineIndexAt(rowStarts, offset);
|
|
64
61
|
const rowStart = (row) => rowStarts[row] ?? text.length;
|
|
65
62
|
const breaks = (unit) => LINE_TERMINATOR.test(text.slice(unit.start, unit.end));
|
|
66
63
|
const row = rowOf(comment.start);
|
|
@@ -74,10 +71,11 @@ export function astGrepReach(text, rowStarts, comment, preceding) {
|
|
|
74
71
|
if (precedingRow !== undefined && precedingRow < row) {
|
|
75
72
|
breaking.push({ start: rowStart(precedingRow), end: rowStart(precedingRow + 1) });
|
|
76
73
|
}
|
|
77
|
-
const
|
|
74
|
+
const next = comment.end + text.slice(comment.end).search(TREE_SITTER_CONTENT);
|
|
75
|
+
const nextNode = opensFirstRow(text, comment.start) && next >= comment.end && next < rowStart(2) ? next : undefined;
|
|
78
76
|
return {
|
|
79
77
|
holds: (unit) => (unit.end > ownRow && unit.end <= comment.start && (unit.start >= preceding.tokenEnd || breaks(unit))) ||
|
|
80
|
-
|
|
78
|
+
unit.start === nextNode ||
|
|
81
79
|
(breaking.some((span) => unit.start < span.end && unit.end >= span.start) && breaks(unit)),
|
|
82
80
|
nextRow: coversNextRow ? { start: comment.end, end: rowStart(row + 2) } : undefined,
|
|
83
81
|
};
|
package/dist/astgrep.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"astgrep.js","sourceRoot":"","sources":["../src/astgrep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"astgrep.js","sourceRoot":"","sources":["../src/astgrep.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAG5E;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG,qBAAqB,CAAC;AAChD,MAAM,mBAAmB,GAAG,mBAAmB,CAAC;AAEhD,4GAA4G;AAC5G,SAAS,aAAa,CAAC,IAAY,EAAE,MAAc;IACjD,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IACrC,OAAO,iBAAiB,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;AAClE,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAY,EAAE,KAAa,EAAE,GAAW;IACzE,IAAI,CAAC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G,MAAM,IAAI,IAAI,CAAC,GAAG,IAAI,OAAO,CAAC,KAAK,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,SAAS,CAAC,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;YACtG,IAAI,CAAC,KAAK,KAAK,QAAQ;YACvB,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC;QAC5F,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,QAAQ,CAAC,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS;KACpF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { JsxContainer } from "./jsx.js";
|
|
2
|
+
import { type Range } from "./ranges.js";
|
|
3
|
+
import type { Comment } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* How a comment-only JSX container goes once all its comments do: braces
|
|
6
|
+
* and all; replaced by `{" "}` together with the spaces that held it off its
|
|
7
|
+
* neighbour; or with its braces kept, only the comments going.
|
|
8
|
+
*/
|
|
9
|
+
type ContainerMode = "drop" | "space" | "keep";
|
|
10
|
+
/** A comment-only JSX container (`{/* note */}`) with the comments in it. */
|
|
11
|
+
export interface Container extends Range {
|
|
12
|
+
segment: number;
|
|
13
|
+
suspect: boolean;
|
|
14
|
+
/** The comment-only container right after it among the siblings, if any. */
|
|
15
|
+
next: Container | undefined;
|
|
16
|
+
comments: Comment[];
|
|
17
|
+
mode: ContainerMode;
|
|
18
|
+
/** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
|
|
19
|
+
spaceRange: Range | undefined;
|
|
20
|
+
}
|
|
21
|
+
export declare function attachContainers(source: string, layouts: readonly JsxContainer[], comments: readonly Comment[]): Container[];
|
|
22
|
+
/**
|
|
23
|
+
* The comments, in document order, with those of every container that
|
|
24
|
+
* `merges` accepts merged into the container itself: one unit spanning its
|
|
25
|
+
* braces in place of the comments inside them.
|
|
26
|
+
*/
|
|
27
|
+
export declare function mergeContainers(comments: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>, merges: (container: Container) => boolean): Array<Comment | Container>;
|
|
28
|
+
/** Whether the container's comments are all among the given ones, so that its braces can go with them. */
|
|
29
|
+
export declare function emptiedBy(container: Container, comments: ReadonlySet<Comment>): boolean;
|
|
30
|
+
/**
|
|
31
|
+
* What deleting part of the removal takes out of the source: a single
|
|
32
|
+
* comment, or a comment-only JSX container going braces and all.
|
|
33
|
+
*/
|
|
34
|
+
export interface RemovalUnit extends Range {
|
|
35
|
+
comments: readonly Comment[];
|
|
36
|
+
/** The unit is a container's braces, which go with the comments in them. */
|
|
37
|
+
braces: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
41
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
42
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
43
|
+
*/
|
|
44
|
+
export declare function removalUnits(removals: readonly Comment[], containerOf: ReadonlyMap<Comment, Container>): RemovalUnit[];
|
|
45
|
+
/** The comments the units delete. */
|
|
46
|
+
export declare function commentsOf(units: readonly RemovalUnit[]): Comment[];
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=containers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containers.d.ts","sourceRoot":"","sources":["../src/containers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C,OAAO,EAAgB,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM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C,KAAK,EAAE,SAAS,WAAW,EAAE,GAAG,OAAO,EAAE,CAEnE"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LINE_TERMINATOR, isHorizontalSpace, opensItsLine } from "./lines.js";
|
|
2
|
+
import { rangesWithin } from "./ranges.js";
|
|
3
|
+
export function attachContainers(source, layouts, comments) {
|
|
4
|
+
const containers = new Map();
|
|
5
|
+
for (const layout of layouts) {
|
|
6
|
+
containers.set(layout, {
|
|
7
|
+
start: layout.start,
|
|
8
|
+
end: layout.end,
|
|
9
|
+
segment: layout.segment,
|
|
10
|
+
suspect: layout.suspect,
|
|
11
|
+
next: undefined,
|
|
12
|
+
comments: rangesWithin(comments, layout),
|
|
13
|
+
mode: "drop",
|
|
14
|
+
spaceRange: spaceRange(source, layout),
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
for (const [layout, container] of containers) {
|
|
18
|
+
if (layout.next !== undefined)
|
|
19
|
+
container.next = containers.get(layout.next);
|
|
20
|
+
}
|
|
21
|
+
return [...containers.values()];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The range `{" "}` would take the place of: a one-line container and the
|
|
25
|
+
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
26
|
+
* the spaces after it when it opens its line and code follows. Undefined when
|
|
27
|
+
* no spaces hold it off anything on its line.
|
|
28
|
+
*/
|
|
29
|
+
function spaceRange(source, container) {
|
|
30
|
+
if (LINE_TERMINATOR.test(source.slice(container.start, container.end)))
|
|
31
|
+
return undefined;
|
|
32
|
+
let before = container.start;
|
|
33
|
+
while (isHorizontalSpace(source[before - 1]))
|
|
34
|
+
before -= 1;
|
|
35
|
+
let after = container.end;
|
|
36
|
+
while (isHorizontalSpace(source[after]))
|
|
37
|
+
after += 1;
|
|
38
|
+
if (!opensItsLine(source, container)) {
|
|
39
|
+
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
40
|
+
}
|
|
41
|
+
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after]);
|
|
42
|
+
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* The comments, in document order, with those of every container that
|
|
46
|
+
* `merges` accepts merged into the container itself: one unit spanning its
|
|
47
|
+
* braces in place of the comments inside them.
|
|
48
|
+
*/
|
|
49
|
+
export function mergeContainers(comments, containerOf, merges) {
|
|
50
|
+
const units = [];
|
|
51
|
+
for (const comment of comments) {
|
|
52
|
+
const container = containerOf.get(comment);
|
|
53
|
+
if (container === undefined || !merges(container))
|
|
54
|
+
units.push(comment);
|
|
55
|
+
else if (units.at(-1) !== container)
|
|
56
|
+
units.push(container);
|
|
57
|
+
}
|
|
58
|
+
return units;
|
|
59
|
+
}
|
|
60
|
+
/** Whether the container's comments are all among the given ones, so that its braces can go with them. */
|
|
61
|
+
export function emptiedBy(container, comments) {
|
|
62
|
+
return container.comments.every((comment) => comments.has(comment));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
66
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
67
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
68
|
+
*/
|
|
69
|
+
export function removalUnits(removals, containerOf) {
|
|
70
|
+
const removing = new Set(removals);
|
|
71
|
+
const units = mergeContainers(removals, containerOf, (container) => container.mode === "drop" && emptiedBy(container, removing));
|
|
72
|
+
return units.map((unit) => ({
|
|
73
|
+
start: unit.start,
|
|
74
|
+
end: unit.end,
|
|
75
|
+
...("comments" in unit ? { comments: unit.comments, braces: true } : { comments: [unit], braces: false }),
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
/** The comments the units delete. */
|
|
79
|
+
export function commentsOf(units) {
|
|
80
|
+
return units.flatMap((unit) => unit.comments);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=containers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"containers.js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text for it find it. */
|
|
3
|
+
export interface TextHint {
|
|
4
|
+
/** Where the hint's `/*` stands. */
|
|
5
|
+
offset: number;
|
|
6
|
+
/**
|
|
7
|
+
* The rows below its own that v8-to-istanbul ignores for it where the hint
|
|
8
|
+
* opens its row (see opensRow): the count behind it, or one. Anywhere else
|
|
9
|
+
* on a row the hint covers that row alone.
|
|
10
|
+
*/
|
|
11
|
+
rows: number;
|
|
12
|
+
/** The rows below its own that Node's test runner ignores for it; none when Node does not read it. */
|
|
13
|
+
nodeRows: number;
|
|
14
|
+
}
|
|
15
|
+
/** The `ignore next` hints v8-to-istanbul and Node's test runner find in the text. */
|
|
16
|
+
export declare function textHints(source: string): TextHint[];
|
|
17
|
+
/**
|
|
18
|
+
* Whether a hint that `prefix` precedes on its row opens the row to
|
|
19
|
+
* v8-to-istanbul, which then counts the rows below it: nothing but non-word
|
|
20
|
+
* characters stands in front of it (`^\W*` in its patterns), so `});` does
|
|
21
|
+
* not keep the hint from counting, and a comment that holds a word does.
|
|
22
|
+
*/
|
|
23
|
+
export declare function opensRow(prefix: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* The lines below the comment's last that monocart-coverage-reports ignores
|
|
26
|
+
* for it, or undefined when it reads no `ignore next` from the comment. It
|
|
27
|
+
* wants the trimmed value to open with `v8 ignore`, `c8 ignore` or
|
|
28
|
+
* `node:coverage`, and `next` (`ignore next` for Node's) to open what follows
|
|
29
|
+
* past any whitespace; then it takes the leading integer of the rest as the
|
|
30
|
+
* count (`next3` and `next 3x` count 3), and one line for anything else.
|
|
31
|
+
* Without a count, a block comment that something follows on its line —
|
|
32
|
+
* `trailing`, the rest of that line — covers no line below: the hint is for
|
|
33
|
+
* what follows it there.
|
|
34
|
+
*/
|
|
35
|
+
export declare function monocartLines(kind: CommentKind, text: string, trailing: string): number | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Whether ast-v8-to-istanbul, Vitest's v8 provider, reads a hint for the next
|
|
38
|
+
* node from the comment: `ignore next`, `ignore if` or `ignore else` at the
|
|
39
|
+
* start of its text (`getIgnoreHints` in its ignore-hints.ts). It applies
|
|
40
|
+
* such a hint to the node starting at the first token after the comment, so
|
|
41
|
+
* a hint that another comment follows has no target. A hint further into the
|
|
42
|
+
* text (`// see /* c8 ignore next */`) is c8's to read, not this tool's.
|
|
43
|
+
*/
|
|
44
|
+
export declare function hintsNextNode(text: string): boolean;
|
|
45
|
+
//# sourceMappingURL=coverage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coverage.d.ts","sourceRoot":"","sources":["../src/coverage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AA0B9C,qGAAqG;AACrG,MAAM,WAAW,QAAQ;IACvB,oCAAoC;IACpC,MAAM,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,IAAI,EAAE,MAAM,CAAC;IACb,sGAAsG;IACtG,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,sFAAsF;AACtF,wBAAgB,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,EAAE,CAapD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAWnG;AAKD;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAQnD"}
|
package/dist/coverage.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How the coverage tools that count lines read an `ignore next` hint. Each
|
|
3
|
+
* reads it its own way, on its own lines:
|
|
4
|
+
*
|
|
5
|
+
* - c8 (v8-to-istanbul's `source.js`) and Node's test runner (its
|
|
6
|
+
* `coverage.js`) search the file's text row by row, a row ending at a line
|
|
7
|
+
* feed alone, for the hint spelled as a block comment with single spaces —
|
|
8
|
+
* wherever that text stands: inside another comment, a string or a
|
|
9
|
+
* template too.
|
|
10
|
+
* - monocart-coverage-reports reads the comments themselves, on lines that
|
|
11
|
+
* every ECMAScript line terminator ends.
|
|
12
|
+
*
|
|
13
|
+
* istanbul and ast-v8-to-istanbul, Vitest's providers, apply the hint to the
|
|
14
|
+
* node after it instead, whatever count follows — ast-v8-to-istanbul only to
|
|
15
|
+
* the node that starts at the very token after the comment (see hintsNextNode).
|
|
16
|
+
*/
|
|
17
|
+
// The text v8-to-istanbul looks for in a row; the form Node reads opens with it too.
|
|
18
|
+
const TEXT_HINT = /\/\* (?:[cv]8|node:coverage) ignore next/g;
|
|
19
|
+
// The count v8-to-istanbul reads right behind it.
|
|
20
|
+
const COUNT = / ([0-9]+)/y;
|
|
21
|
+
// Node's test runner wants the whole comment, a count and its space included.
|
|
22
|
+
const NODE_HINT = /\/\* node:coverage ignore next (\d+ )?\*\//y;
|
|
23
|
+
/** The `ignore next` hints v8-to-istanbul and Node's test runner find in the text. */
|
|
24
|
+
export function textHints(source) {
|
|
25
|
+
const hints = [];
|
|
26
|
+
for (const match of source.matchAll(TEXT_HINT)) {
|
|
27
|
+
COUNT.lastIndex = match.index + match[0].length;
|
|
28
|
+
NODE_HINT.lastIndex = match.index;
|
|
29
|
+
const node = NODE_HINT.exec(source);
|
|
30
|
+
hints.push({
|
|
31
|
+
offset: match.index,
|
|
32
|
+
rows: Number(COUNT.exec(source)?.[1] ?? 1),
|
|
33
|
+
nodeRows: node === null ? 0 : Number(node[1] ?? 1),
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return hints;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Whether a hint that `prefix` precedes on its row opens the row to
|
|
40
|
+
* v8-to-istanbul, which then counts the rows below it: nothing but non-word
|
|
41
|
+
* characters stands in front of it (`^\W*` in its patterns), so `});` does
|
|
42
|
+
* not keep the hint from counting, and a comment that holds a word does.
|
|
43
|
+
*/
|
|
44
|
+
export function opensRow(prefix) {
|
|
45
|
+
return /^\W*$/.test(prefix);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The lines below the comment's last that monocart-coverage-reports ignores
|
|
49
|
+
* for it, or undefined when it reads no `ignore next` from the comment. It
|
|
50
|
+
* wants the trimmed value to open with `v8 ignore`, `c8 ignore` or
|
|
51
|
+
* `node:coverage`, and `next` (`ignore next` for Node's) to open what follows
|
|
52
|
+
* past any whitespace; then it takes the leading integer of the rest as the
|
|
53
|
+
* count (`next3` and `next 3x` count 3), and one line for anything else.
|
|
54
|
+
* Without a count, a block comment that something follows on its line —
|
|
55
|
+
* `trailing`, the rest of that line — covers no line below: the hint is for
|
|
56
|
+
* what follows it there.
|
|
57
|
+
*/
|
|
58
|
+
export function monocartLines(kind, text, trailing) {
|
|
59
|
+
const value = (kind === "block" ? text.slice(2, -2) : text.slice(2)).trim();
|
|
60
|
+
const tool = ["v8 ignore", "c8 ignore", "node:coverage"].find((name) => value.startsWith(name));
|
|
61
|
+
if (tool === undefined)
|
|
62
|
+
return undefined;
|
|
63
|
+
const mode = tool === "node:coverage" ? "ignore next" : "next";
|
|
64
|
+
const rest = value.slice(tool.length).trim();
|
|
65
|
+
if (!rest.startsWith(mode))
|
|
66
|
+
return undefined;
|
|
67
|
+
const count = rest.slice(mode.length).trim();
|
|
68
|
+
// (monocart calls parseInt without a radix.)
|
|
69
|
+
if (count !== "")
|
|
70
|
+
return Math.max(1, Number.parseInt(count) || 1);
|
|
71
|
+
return kind === "block" && trailing.trim() !== "" ? 0 : 1;
|
|
72
|
+
}
|
|
73
|
+
// What ast-v8-to-istanbul wants at the start of a comment's text, its delimiters cut off.
|
|
74
|
+
const NEXT_NODE_HINT = /^\s*(?:istanbul|[cv]8|node:coverage)\s+ignore\s+(?:if|else|next)(?=\W|$)/;
|
|
75
|
+
/**
|
|
76
|
+
* Whether ast-v8-to-istanbul, Vitest's v8 provider, reads a hint for the next
|
|
77
|
+
* node from the comment: `ignore next`, `ignore if` or `ignore else` at the
|
|
78
|
+
* start of its text (`getIgnoreHints` in its ignore-hints.ts). It applies
|
|
79
|
+
* such a hint to the node starting at the first token after the comment, so
|
|
80
|
+
* a hint that another comment follows has no target. A hint further into the
|
|
81
|
+
* text (`// see /* c8 ignore next */`) is c8's to read, not this tool's.
|
|
82
|
+
*/
|
|
83
|
+
export function hintsNextNode(text) {
|
|
84
|
+
const value = text
|
|
85
|
+
.replace(/^\/\*\*/, "")
|
|
86
|
+
.replace(/^\/\*/, "")
|
|
87
|
+
.replace(/\*\*\/$/, "")
|
|
88
|
+
.replace(/\*\/$/, "")
|
|
89
|
+
.replace(/^\/\//, "");
|
|
90
|
+
return NEXT_NODE_HINT.test(value);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=coverage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"coverage.js","sourceRoot":"","sources":["../src/coverage.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;GAeG;AAEH,qFAAqF;AACrF,MAAM,SAAS,GAAG,2CAA2C,CAAC;AAC9D,kDAAkD;AAClD,MAAM,KAAK,GAAG,YAAY,CAAC;AAC3B,8EAA8E;AAC9E,MAAM,SAAS,GAAG,6CAA6C,CAAC;AAgBhE,sFAAsF;AACtF,MAAM,UAAU,SAAS,CAAC,MAAc;IACtC,MAAM,KAAK,GAAe,EAAE,CAAC;IAC7B,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAC/C,KAAK,CAAC,SAAS,GAAG,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QAChD,SAAS,CAAC,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC;QAClC,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;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,IAAI,CAAC;QAAE,OAAO,SAAS,CAAC;IAC7C,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;IAC7C,6CAA6C;IAC7C,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAClE,OAAO,IAAI,KAAK,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED,0FAA0F;AAC1F,MAAM,cAAc,GAAG,0EAA0E,CAAC;AAElG;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,MAAM,KAAK,GAAG,IAAI;SACf,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;SACtB,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;SACpB,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;SACtB,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC;SACpB,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IACxB,OAAO,cAAc,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AACpC,CAAC"}
|
package/dist/directives.d.ts
CHANGED
|
@@ -64,6 +64,16 @@ export interface DirectivePlacement {
|
|
|
64
64
|
* `case`, `default` or `}`. Left out too, any position is assumed.
|
|
65
65
|
*/
|
|
66
66
|
nextToken?: string;
|
|
67
|
+
/**
|
|
68
|
+
* The JSDoc tags tsc reads in the comment (`@typedef`, `@param`, `@returns`,
|
|
69
|
+
* `@template`, ...), which it does in a JavaScript file where the comment
|
|
70
|
+
* is the JSDoc of a node: with `checkJs` they are the file's type
|
|
71
|
+
* annotations, and deleting one changes what the file means to the
|
|
72
|
+
* compiler. Each is a directive under its own name. Left out, the comment
|
|
73
|
+
* is assumed to hold none: in a TypeScript file tsc reads no types from
|
|
74
|
+
* JSDoc.
|
|
75
|
+
*/
|
|
76
|
+
jsDoc?: readonly string[];
|
|
67
77
|
}
|
|
68
78
|
/**
|
|
69
79
|
* The offset at which Bun reads its `// @bun` marker (`has_bun_pragma` in its
|
|
@@ -81,11 +91,19 @@ export declare function bunMarkerOffset(text: string): number;
|
|
|
81
91
|
* `// @bun`) are skipped elsewhere — and the remaining rules still run, so a
|
|
82
92
|
* live directive later in the same comment (`// deno-lint-ignore-file
|
|
83
93
|
* nosemgrep` in mid-file) is not masked by a positionally dead one. A comment
|
|
84
|
-
* no rule names is still
|
|
85
|
-
*
|
|
86
|
-
* code.
|
|
94
|
+
* no rule names is still named for the type tags tsc reads from it in a
|
|
95
|
+
* JavaScript file, `empty-block` where the placement puts it inside an empty
|
|
96
|
+
* block, and a `///` line comment `empty-file` in a file that holds no code.
|
|
87
97
|
*/
|
|
88
98
|
export declare function detectDirective(kind: CommentKind, text: string, placement?: DirectivePlacement): string | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Every directive the comment carries that is live at the placement, the
|
|
101
|
+
* name detectDirective reports first. A comment can carry several: what a
|
|
102
|
+
* removal has to leave in place for each of them (`// cspell:ignore foo --
|
|
103
|
+
* falls through`, read as a fall-through comment where it is the last
|
|
104
|
+
* comment before a `case`) must not hide behind the name.
|
|
105
|
+
*/
|
|
106
|
+
export declare function directiveNames(kind: CommentKind, text: string, placement?: DirectivePlacement): string[];
|
|
89
107
|
/**
|
|
90
108
|
* Whether some ast-grep release reads the comment as a suppression. A
|
|
91
109
|
* comment named for another directive can carry one too (`// @ts-ignore
|
package/dist/directives.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"directives.d.ts","sourceRoot":"","sources":["../src/directives.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AA2rB9C;;;;GAIG;AACH,MA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MAAM,GAAG,MAAM,CAIpD;AAsHD;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,kBAAkB,GAAG,MAAM,GAAG,SAAS,CAEnH;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,kBAAkB,GAAG,MAAM,EAAE,CA+BxG;AAgED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE/D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,kBAAkB,GAAG,MAAM,EAAE,CAanH;AAoBD;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAKpD"}
|
package/dist/directives.js
CHANGED
|
@@ -750,50 +750,58 @@ const FLOW_BLOCK_SUPPRESSION = /^\/\*[ \t\r\n*]*@ts-(ignore|expect-error)(?![\w-
|
|
|
750
750
|
* `// @bun`) are skipped elsewhere — and the remaining rules still run, so a
|
|
751
751
|
* live directive later in the same comment (`// deno-lint-ignore-file
|
|
752
752
|
* nosemgrep` in mid-file) is not masked by a positionally dead one. A comment
|
|
753
|
-
* no rule names is still
|
|
754
|
-
*
|
|
755
|
-
* code.
|
|
753
|
+
* no rule names is still named for the type tags tsc reads from it in a
|
|
754
|
+
* JavaScript file, `empty-block` where the placement puts it inside an empty
|
|
755
|
+
* block, and a `///` line comment `empty-file` in a file that holds no code.
|
|
756
756
|
*/
|
|
757
757
|
export function detectDirective(kind, text, placement) {
|
|
758
|
+
return directiveNames(kind, text, placement)[0];
|
|
759
|
+
}
|
|
760
|
+
/**
|
|
761
|
+
* Every directive the comment carries that is live at the placement, the
|
|
762
|
+
* name detectDirective reports first. A comment can carry several: what a
|
|
763
|
+
* removal has to leave in place for each of them (`// cspell:ignore foo --
|
|
764
|
+
* falls through`, read as a fall-through comment where it is the last
|
|
765
|
+
* comment before a `case`) must not hide behind the name.
|
|
766
|
+
*/
|
|
767
|
+
export function directiveNames(kind, text, placement) {
|
|
758
768
|
const at = placement ?? ANY_PLACEMENT;
|
|
769
|
+
const names = [];
|
|
759
770
|
if (kind === "line") {
|
|
760
771
|
const tripleSlash = tripleSlashDirective(text);
|
|
761
772
|
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at))
|
|
762
|
-
|
|
773
|
+
names.push(tripleSlash);
|
|
763
774
|
const suppression = TS_LINE_SUPPRESSION.exec(text) ?? FLOW_LINE_SUPPRESSION.exec(text);
|
|
764
|
-
if (suppression)
|
|
765
|
-
|
|
766
|
-
}
|
|
775
|
+
if (suppression)
|
|
776
|
+
names.push(`@ts-${suppression[1]}`);
|
|
767
777
|
const checkPragma = TS6_LINE_CHECK_PRAGMA.exec(text) ?? TS7_LINE_CHECK_PRAGMA.exec(text);
|
|
768
778
|
if (checkPragma) {
|
|
769
779
|
const name = `@ts-${checkPragma[1]?.toLowerCase()}`;
|
|
770
780
|
if (isActiveAt(name, at))
|
|
771
|
-
|
|
781
|
+
names.push(name);
|
|
772
782
|
}
|
|
773
783
|
}
|
|
774
784
|
else {
|
|
775
785
|
const suppression = TS_BLOCK_SUPPRESSION.exec(lastLine(text)) ?? FLOW_BLOCK_SUPPRESSION.exec(text);
|
|
776
|
-
if (suppression)
|
|
777
|
-
|
|
778
|
-
}
|
|
786
|
+
if (suppression)
|
|
787
|
+
names.push(`@ts-${suppression[1]}`);
|
|
779
788
|
}
|
|
780
789
|
// A directive that opens the comment names it, however the comment goes on
|
|
781
790
|
// to mention other tools' markers (`// nosemgrep: ... @jsx h` stays a
|
|
782
|
-
// Semgrep suppression
|
|
783
|
-
//
|
|
784
|
-
|
|
791
|
+
// Semgrep suppression); only without one does a marker found after other
|
|
792
|
+
// text name the comment. The type tags of a JSDoc comment come before such
|
|
793
|
+
// mentions too.
|
|
794
|
+
const mentions = [];
|
|
785
795
|
for (const match of ruleMatches(kind, text)) {
|
|
786
|
-
if (
|
|
787
|
-
|
|
788
|
-
if (match.opening)
|
|
789
|
-
return match.name;
|
|
790
|
-
mention ??= match.name;
|
|
796
|
+
if (isMatchActiveAt(match, at))
|
|
797
|
+
(match.opening ? names : mentions).push(match.name);
|
|
791
798
|
}
|
|
792
|
-
|
|
793
|
-
|
|
799
|
+
names.push(...(at.jsDoc ?? []), ...mentions);
|
|
800
|
+
if (names.length > 0)
|
|
801
|
+
return [...new Set(names)];
|
|
794
802
|
if (at.emptyBlock === true)
|
|
795
|
-
return EMPTY_BLOCK;
|
|
796
|
-
return at.emptyFile === true && kind === "line" && text.startsWith("///") ? EMPTY_FILE :
|
|
803
|
+
return [EMPTY_BLOCK];
|
|
804
|
+
return at.emptyFile === true && kind === "line" && text.startsWith("///") ? [EMPTY_FILE] : [];
|
|
797
805
|
}
|
|
798
806
|
function isMatchActiveAt(match, placement) {
|
|
799
807
|
return isActiveAt(match.name, placement) && (!match.firstCommentOnly || placement.firstComment);
|