@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/src/astgrep.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LINE_TERMINATOR } from "./lines.js";
|
|
2
|
-
import {
|
|
1
|
+
import { LINE_TERMINATOR, lineIndexAt, lineStartOffsets } from "./lines.js";
|
|
2
|
+
import type { Range } from "./ranges.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Where ast-grep's suppression comments apply (`get_suppression_kind` in its
|
|
@@ -35,11 +35,7 @@ function lineFeedCount(text: string): number {
|
|
|
35
35
|
|
|
36
36
|
/** Start offset of every tree-sitter row: the text's start and each offset just past a line feed. */
|
|
37
37
|
export function rowStartOffsets(text: string): number[] {
|
|
38
|
-
|
|
39
|
-
for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) {
|
|
40
|
-
starts.push(index + 1);
|
|
41
|
-
}
|
|
42
|
-
return starts;
|
|
38
|
+
return lineStartOffsets(text, /\n/);
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
/** What stands in front of a comment in tree-sitter's tree, which ast-grep reads a suppression's reach from. */
|
|
@@ -78,8 +74,10 @@ export interface AstGrepReach {
|
|
|
78
74
|
* when deleted, so those ending on its row in front of it stay, as do those
|
|
79
75
|
* after it on its row and those touching the row its node starts on; when a
|
|
80
76
|
* release covers the next row, so do those starting there, and that row's
|
|
81
|
-
* lines stay (see nextRow). On the first row, the
|
|
82
|
-
*
|
|
77
|
+
* lines stay (see nextRow). On the first row, the node after the comment
|
|
78
|
+
* decides whether it covers the file: where that node starts less than two
|
|
79
|
+
* rows down, the unit it is stays, since whatever follows it could start
|
|
80
|
+
* further down.
|
|
83
81
|
* The releases this follows parse with tree-sitter 0.24 or later (ast-grep
|
|
84
82
|
* 0.31.1 on); before, comments in JSX braces read as standing alone.
|
|
85
83
|
*/
|
|
@@ -89,7 +87,7 @@ export function astGrepReach(
|
|
|
89
87
|
comment: Range,
|
|
90
88
|
preceding: PrecedingNode,
|
|
91
89
|
): AstGrepReach {
|
|
92
|
-
const rowOf = (offset: number): number =>
|
|
90
|
+
const rowOf = (offset: number): number => lineIndexAt(rowStarts, offset);
|
|
93
91
|
const rowStart = (row: number): number => rowStarts[row] ?? text.length;
|
|
94
92
|
const breaks = (unit: Range): boolean => LINE_TERMINATOR.test(text.slice(unit.start, unit.end));
|
|
95
93
|
const row = rowOf(comment.start);
|
|
@@ -103,12 +101,12 @@ export function astGrepReach(
|
|
|
103
101
|
if (precedingRow !== undefined && precedingRow < row) {
|
|
104
102
|
breaking.push({ start: rowStart(precedingRow), end: rowStart(precedingRow + 1) });
|
|
105
103
|
}
|
|
106
|
-
const
|
|
107
|
-
|
|
104
|
+
const next = comment.end + text.slice(comment.end).search(TREE_SITTER_CONTENT);
|
|
105
|
+
const nextNode = opensFirstRow(text, comment.start) && next >= comment.end && next < rowStart(2) ? next : undefined;
|
|
108
106
|
return {
|
|
109
107
|
holds: (unit) =>
|
|
110
108
|
(unit.end > ownRow && unit.end <= comment.start && (unit.start >= preceding.tokenEnd || breaks(unit))) ||
|
|
111
|
-
|
|
109
|
+
unit.start === nextNode ||
|
|
112
110
|
(breaking.some((span) => unit.start < span.end && unit.end >= span.start) && breaks(unit)),
|
|
113
111
|
nextRow: coversNextRow ? { start: comment.end, end: rowStart(row + 2) } : undefined,
|
|
114
112
|
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { JsxContainer } from "./jsx.js";
|
|
2
|
+
import { LINE_TERMINATOR, isHorizontalSpace, opensItsLine } from "./lines.js";
|
|
3
|
+
import { rangesWithin, type Range } from "./ranges.js";
|
|
4
|
+
import type { Comment } from "./types.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* How a comment-only JSX container goes once all its comments do: braces
|
|
8
|
+
* and all; replaced by `{" "}` together with the spaces that held it off its
|
|
9
|
+
* neighbour; or with its braces kept, only the comments going.
|
|
10
|
+
*/
|
|
11
|
+
type ContainerMode = "drop" | "space" | "keep";
|
|
12
|
+
|
|
13
|
+
/** A comment-only JSX container (`{/* note */}`) with the comments in it. */
|
|
14
|
+
export interface Container extends Range {
|
|
15
|
+
segment: number;
|
|
16
|
+
suspect: boolean;
|
|
17
|
+
/** The comment-only container right after it among the siblings, if any. */
|
|
18
|
+
next: Container | undefined;
|
|
19
|
+
comments: Comment[];
|
|
20
|
+
mode: ContainerMode;
|
|
21
|
+
/** What `{" "}` would replace, when the container shares its line and is held off by spaces. */
|
|
22
|
+
spaceRange: Range | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function attachContainers(
|
|
26
|
+
source: string,
|
|
27
|
+
layouts: readonly JsxContainer[],
|
|
28
|
+
comments: readonly Comment[],
|
|
29
|
+
): Container[] {
|
|
30
|
+
const containers = new Map<JsxContainer, Container>();
|
|
31
|
+
for (const layout of layouts) {
|
|
32
|
+
containers.set(layout, {
|
|
33
|
+
start: layout.start,
|
|
34
|
+
end: layout.end,
|
|
35
|
+
segment: layout.segment,
|
|
36
|
+
suspect: layout.suspect,
|
|
37
|
+
next: undefined,
|
|
38
|
+
comments: rangesWithin(comments, layout),
|
|
39
|
+
mode: "drop",
|
|
40
|
+
spaceRange: spaceRange(source, layout),
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
for (const [layout, container] of containers) {
|
|
44
|
+
if (layout.next !== undefined) container.next = containers.get(layout.next);
|
|
45
|
+
}
|
|
46
|
+
return [...containers.values()];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The range `{" "}` would take the place of: a one-line container and the
|
|
51
|
+
* spaces before it when code precedes it on its line (`<b /> {/* x */}`), or
|
|
52
|
+
* the spaces after it when it opens its line and code follows. Undefined when
|
|
53
|
+
* no spaces hold it off anything on its line.
|
|
54
|
+
*/
|
|
55
|
+
function spaceRange(source: string, container: Range): Range | undefined {
|
|
56
|
+
if (LINE_TERMINATOR.test(source.slice(container.start, container.end))) return undefined;
|
|
57
|
+
let before = container.start;
|
|
58
|
+
while (isHorizontalSpace(source[before - 1])) before -= 1;
|
|
59
|
+
let after = container.end;
|
|
60
|
+
while (isHorizontalSpace(source[after])) after += 1;
|
|
61
|
+
|
|
62
|
+
if (!opensItsLine(source, container)) {
|
|
63
|
+
return before < container.start ? { start: before, end: container.end } : undefined;
|
|
64
|
+
}
|
|
65
|
+
const codeAfter = after < source.length && !LINE_TERMINATOR.test(source[after] as string);
|
|
66
|
+
return codeAfter && after > container.end ? { start: container.start, end: after } : undefined;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* The comments, in document order, with those of every container that
|
|
71
|
+
* `merges` accepts merged into the container itself: one unit spanning its
|
|
72
|
+
* braces in place of the comments inside them.
|
|
73
|
+
*/
|
|
74
|
+
export function mergeContainers(
|
|
75
|
+
comments: readonly Comment[],
|
|
76
|
+
containerOf: ReadonlyMap<Comment, Container>,
|
|
77
|
+
merges: (container: Container) => boolean,
|
|
78
|
+
): Array<Comment | Container> {
|
|
79
|
+
const units: Array<Comment | Container> = [];
|
|
80
|
+
for (const comment of comments) {
|
|
81
|
+
const container = containerOf.get(comment);
|
|
82
|
+
if (container === undefined || !merges(container)) units.push(comment);
|
|
83
|
+
else if (units.at(-1) !== container) units.push(container);
|
|
84
|
+
}
|
|
85
|
+
return units;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Whether the container's comments are all among the given ones, so that its braces can go with them. */
|
|
89
|
+
export function emptiedBy(container: Container, comments: ReadonlySet<Comment>): boolean {
|
|
90
|
+
return container.comments.every((comment) => comments.has(comment));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* What deleting part of the removal takes out of the source: a single
|
|
95
|
+
* comment, or a comment-only JSX container going braces and all.
|
|
96
|
+
*/
|
|
97
|
+
export interface RemovalUnit extends Range {
|
|
98
|
+
comments: readonly Comment[];
|
|
99
|
+
/** The unit is a container's braces, which go with the comments in them. */
|
|
100
|
+
braces: boolean;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The removals as the edits delete them: a comment-only container whose
|
|
105
|
+
* comments all go — and that is to go braces and all — is one unit spanning
|
|
106
|
+
* its braces, every other comment a unit of its own. Sorted, as the comments.
|
|
107
|
+
*/
|
|
108
|
+
export function removalUnits(
|
|
109
|
+
removals: readonly Comment[],
|
|
110
|
+
containerOf: ReadonlyMap<Comment, Container>,
|
|
111
|
+
): RemovalUnit[] {
|
|
112
|
+
const removing = new Set(removals);
|
|
113
|
+
const units = mergeContainers(
|
|
114
|
+
removals,
|
|
115
|
+
containerOf,
|
|
116
|
+
(container) => container.mode === "drop" && emptiedBy(container, removing),
|
|
117
|
+
);
|
|
118
|
+
return units.map((unit) => ({
|
|
119
|
+
start: unit.start,
|
|
120
|
+
end: unit.end,
|
|
121
|
+
...("comments" in unit ? { comments: unit.comments, braces: true } : { comments: [unit], braces: false }),
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The comments the units delete. */
|
|
126
|
+
export function commentsOf(units: readonly RemovalUnit[]): Comment[] {
|
|
127
|
+
return units.flatMap((unit) => unit.comments);
|
|
128
|
+
}
|
package/src/coverage.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { CommentKind } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* How the coverage tools that count lines read an `ignore next` hint. Each
|
|
5
|
+
* reads it its own way, on its own lines:
|
|
6
|
+
*
|
|
7
|
+
* - c8 (v8-to-istanbul's `source.js`) and Node's test runner (its
|
|
8
|
+
* `coverage.js`) search the file's text row by row, a row ending at a line
|
|
9
|
+
* feed alone, for the hint spelled as a block comment with single spaces —
|
|
10
|
+
* wherever that text stands: inside another comment, a string or a
|
|
11
|
+
* template too.
|
|
12
|
+
* - monocart-coverage-reports reads the comments themselves, on lines that
|
|
13
|
+
* every ECMAScript line terminator ends.
|
|
14
|
+
*
|
|
15
|
+
* istanbul and ast-v8-to-istanbul, Vitest's providers, apply the hint to the
|
|
16
|
+
* node after it instead, whatever count follows — ast-v8-to-istanbul only to
|
|
17
|
+
* the node that starts at the very token after the comment (see hintsNextNode).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// The text v8-to-istanbul looks for in a row; the form Node reads opens with it too.
|
|
21
|
+
const TEXT_HINT = /\/\* (?:[cv]8|node:coverage) ignore next/g;
|
|
22
|
+
// The count v8-to-istanbul reads right behind it.
|
|
23
|
+
const COUNT = / ([0-9]+)/y;
|
|
24
|
+
// Node's test runner wants the whole comment, a count and its space included.
|
|
25
|
+
const NODE_HINT = /\/\* node:coverage ignore next (\d+ )?\*\//y;
|
|
26
|
+
|
|
27
|
+
/** An `ignore next` hint in the text of a file, as the tools that search the text for it find it. */
|
|
28
|
+
export interface TextHint {
|
|
29
|
+
/** Where the hint's `/*` stands. */
|
|
30
|
+
offset: number;
|
|
31
|
+
/**
|
|
32
|
+
* The rows below its own that v8-to-istanbul ignores for it where the hint
|
|
33
|
+
* opens its row (see opensRow): the count behind it, or one. Anywhere else
|
|
34
|
+
* on a row the hint covers that row alone.
|
|
35
|
+
*/
|
|
36
|
+
rows: number;
|
|
37
|
+
/** The rows below its own that Node's test runner ignores for it; none when Node does not read it. */
|
|
38
|
+
nodeRows: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The `ignore next` hints v8-to-istanbul and Node's test runner find in the text. */
|
|
42
|
+
export function textHints(source: string): TextHint[] {
|
|
43
|
+
const hints: TextHint[] = [];
|
|
44
|
+
for (const match of source.matchAll(TEXT_HINT)) {
|
|
45
|
+
COUNT.lastIndex = match.index + match[0].length;
|
|
46
|
+
NODE_HINT.lastIndex = match.index;
|
|
47
|
+
const node = NODE_HINT.exec(source);
|
|
48
|
+
hints.push({
|
|
49
|
+
offset: match.index,
|
|
50
|
+
rows: Number(COUNT.exec(source)?.[1] ?? 1),
|
|
51
|
+
nodeRows: node === null ? 0 : Number(node[1] ?? 1),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
return hints;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Whether a hint that `prefix` precedes on its row opens the row to
|
|
59
|
+
* v8-to-istanbul, which then counts the rows below it: nothing but non-word
|
|
60
|
+
* characters stands in front of it (`^\W*` in its patterns), so `});` does
|
|
61
|
+
* not keep the hint from counting, and a comment that holds a word does.
|
|
62
|
+
*/
|
|
63
|
+
export function opensRow(prefix: string): boolean {
|
|
64
|
+
return /^\W*$/.test(prefix);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* The lines below the comment's last that monocart-coverage-reports ignores
|
|
69
|
+
* for it, or undefined when it reads no `ignore next` from the comment. It
|
|
70
|
+
* wants the trimmed value to open with `v8 ignore`, `c8 ignore` or
|
|
71
|
+
* `node:coverage`, and `next` (`ignore next` for Node's) to open what follows
|
|
72
|
+
* past any whitespace; then it takes the leading integer of the rest as the
|
|
73
|
+
* count (`next3` and `next 3x` count 3), and one line for anything else.
|
|
74
|
+
* Without a count, a block comment that something follows on its line —
|
|
75
|
+
* `trailing`, the rest of that line — covers no line below: the hint is for
|
|
76
|
+
* what follows it there.
|
|
77
|
+
*/
|
|
78
|
+
export function monocartLines(kind: CommentKind, text: string, trailing: string): number | undefined {
|
|
79
|
+
const value = (kind === "block" ? text.slice(2, -2) : text.slice(2)).trim();
|
|
80
|
+
const tool = ["v8 ignore", "c8 ignore", "node:coverage"].find((name) => value.startsWith(name));
|
|
81
|
+
if (tool === undefined) return undefined;
|
|
82
|
+
const mode = tool === "node:coverage" ? "ignore next" : "next";
|
|
83
|
+
const rest = value.slice(tool.length).trim();
|
|
84
|
+
if (!rest.startsWith(mode)) return undefined;
|
|
85
|
+
const count = rest.slice(mode.length).trim();
|
|
86
|
+
// (monocart calls parseInt without a radix.)
|
|
87
|
+
if (count !== "") return Math.max(1, Number.parseInt(count) || 1);
|
|
88
|
+
return kind === "block" && trailing.trim() !== "" ? 0 : 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// What ast-v8-to-istanbul wants at the start of a comment's text, its delimiters cut off.
|
|
92
|
+
const NEXT_NODE_HINT = /^\s*(?:istanbul|[cv]8|node:coverage)\s+ignore\s+(?:if|else|next)(?=\W|$)/;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Whether ast-v8-to-istanbul, Vitest's v8 provider, reads a hint for the next
|
|
96
|
+
* node from the comment: `ignore next`, `ignore if` or `ignore else` at the
|
|
97
|
+
* start of its text (`getIgnoreHints` in its ignore-hints.ts). It applies
|
|
98
|
+
* such a hint to the node starting at the first token after the comment, so
|
|
99
|
+
* a hint that another comment follows has no target. A hint further into the
|
|
100
|
+
* text (`// see /* c8 ignore next */`) is c8's to read, not this tool's.
|
|
101
|
+
*/
|
|
102
|
+
export function hintsNextNode(text: string): boolean {
|
|
103
|
+
const value = text
|
|
104
|
+
.replace(/^\/\*\*/, "")
|
|
105
|
+
.replace(/^\/\*/, "")
|
|
106
|
+
.replace(/\*\*\/$/, "")
|
|
107
|
+
.replace(/\*\/$/, "")
|
|
108
|
+
.replace(/^\/\//, "");
|
|
109
|
+
return NEXT_NODE_HINT.test(value);
|
|
110
|
+
}
|
package/src/directives.ts
CHANGED
|
@@ -764,6 +764,16 @@ export interface DirectivePlacement {
|
|
|
764
764
|
* `case`, `default` or `}`. Left out too, any position is assumed.
|
|
765
765
|
*/
|
|
766
766
|
nextToken?: string;
|
|
767
|
+
/**
|
|
768
|
+
* The JSDoc tags tsc reads in the comment (`@typedef`, `@param`, `@returns`,
|
|
769
|
+
* `@template`, ...), which it does in a JavaScript file where the comment
|
|
770
|
+
* is the JSDoc of a node: with `checkJs` they are the file's type
|
|
771
|
+
* annotations, and deleting one changes what the file means to the
|
|
772
|
+
* compiler. Each is a directive under its own name. Left out, the comment
|
|
773
|
+
* is assumed to hold none: in a TypeScript file tsc reads no types from
|
|
774
|
+
* JSDoc.
|
|
775
|
+
*/
|
|
776
|
+
jsDoc?: readonly string[];
|
|
767
777
|
}
|
|
768
778
|
|
|
769
779
|
const ANY_PLACEMENT: DirectivePlacement = { header: true, firstComment: true, fileStart: true };
|
|
@@ -905,44 +915,52 @@ const FLOW_BLOCK_SUPPRESSION = /^\/\*[ \t\r\n*]*@ts-(ignore|expect-error)(?![\w-
|
|
|
905
915
|
* `// @bun`) are skipped elsewhere — and the remaining rules still run, so a
|
|
906
916
|
* live directive later in the same comment (`// deno-lint-ignore-file
|
|
907
917
|
* nosemgrep` in mid-file) is not masked by a positionally dead one. A comment
|
|
908
|
-
* no rule names is still
|
|
909
|
-
*
|
|
910
|
-
* code.
|
|
918
|
+
* no rule names is still named for the type tags tsc reads from it in a
|
|
919
|
+
* JavaScript file, `empty-block` where the placement puts it inside an empty
|
|
920
|
+
* block, and a `///` line comment `empty-file` in a file that holds no code.
|
|
911
921
|
*/
|
|
912
922
|
export function detectDirective(kind: CommentKind, text: string, placement?: DirectivePlacement): string | undefined {
|
|
923
|
+
return directiveNames(kind, text, placement)[0];
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/**
|
|
927
|
+
* Every directive the comment carries that is live at the placement, the
|
|
928
|
+
* name detectDirective reports first. A comment can carry several: what a
|
|
929
|
+
* removal has to leave in place for each of them (`// cspell:ignore foo --
|
|
930
|
+
* falls through`, read as a fall-through comment where it is the last
|
|
931
|
+
* comment before a `case`) must not hide behind the name.
|
|
932
|
+
*/
|
|
933
|
+
export function directiveNames(kind: CommentKind, text: string, placement?: DirectivePlacement): string[] {
|
|
913
934
|
const at = placement ?? ANY_PLACEMENT;
|
|
935
|
+
const names: string[] = [];
|
|
914
936
|
if (kind === "line") {
|
|
915
937
|
const tripleSlash = tripleSlashDirective(text);
|
|
916
|
-
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at))
|
|
938
|
+
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at)) names.push(tripleSlash);
|
|
917
939
|
const suppression = TS_LINE_SUPPRESSION.exec(text) ?? FLOW_LINE_SUPPRESSION.exec(text);
|
|
918
|
-
if (suppression) {
|
|
919
|
-
return `@ts-${suppression[1]}`;
|
|
920
|
-
}
|
|
940
|
+
if (suppression) names.push(`@ts-${suppression[1]}`);
|
|
921
941
|
const checkPragma = TS6_LINE_CHECK_PRAGMA.exec(text) ?? TS7_LINE_CHECK_PRAGMA.exec(text);
|
|
922
942
|
if (checkPragma) {
|
|
923
943
|
const name = `@ts-${checkPragma[1]?.toLowerCase()}`;
|
|
924
|
-
if (isActiveAt(name, at))
|
|
944
|
+
if (isActiveAt(name, at)) names.push(name);
|
|
925
945
|
}
|
|
926
946
|
} else {
|
|
927
947
|
const suppression = TS_BLOCK_SUPPRESSION.exec(lastLine(text)) ?? FLOW_BLOCK_SUPPRESSION.exec(text);
|
|
928
|
-
if (suppression) {
|
|
929
|
-
return `@ts-${suppression[1]}`;
|
|
930
|
-
}
|
|
948
|
+
if (suppression) names.push(`@ts-${suppression[1]}`);
|
|
931
949
|
}
|
|
932
950
|
|
|
933
951
|
// A directive that opens the comment names it, however the comment goes on
|
|
934
952
|
// to mention other tools' markers (`// nosemgrep: ... @jsx h` stays a
|
|
935
|
-
// Semgrep suppression
|
|
936
|
-
//
|
|
937
|
-
|
|
953
|
+
// Semgrep suppression); only without one does a marker found after other
|
|
954
|
+
// text name the comment. The type tags of a JSDoc comment come before such
|
|
955
|
+
// mentions too.
|
|
956
|
+
const mentions: string[] = [];
|
|
938
957
|
for (const match of ruleMatches(kind, text)) {
|
|
939
|
-
if (
|
|
940
|
-
if (match.opening) return match.name;
|
|
941
|
-
mention ??= match.name;
|
|
958
|
+
if (isMatchActiveAt(match, at)) (match.opening ? names : mentions).push(match.name);
|
|
942
959
|
}
|
|
943
|
-
|
|
944
|
-
if (
|
|
945
|
-
|
|
960
|
+
names.push(...(at.jsDoc ?? []), ...mentions);
|
|
961
|
+
if (names.length > 0) return [...new Set(names)];
|
|
962
|
+
if (at.emptyBlock === true) return [EMPTY_BLOCK];
|
|
963
|
+
return at.emptyFile === true && kind === "line" && text.startsWith("///") ? [EMPTY_FILE] : [];
|
|
946
964
|
}
|
|
947
965
|
|
|
948
966
|
/**
|