@kongyo2/ts-comment-scanner 1.11.1 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/astgrep.d.ts +51 -0
- package/dist/astgrep.d.ts.map +1 -0
- package/dist/astgrep.js +85 -0
- package/dist/astgrep.js.map +1 -0
- package/dist/directives.d.ts +32 -4
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +241 -95
- package/dist/directives.js.map +1 -1
- package/dist/imports.d.ts +36 -0
- package/dist/imports.d.ts.map +1 -0
- package/dist/imports.js +119 -0
- package/dist/imports.js.map +1 -0
- package/dist/remove.d.ts +14 -7
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +254 -81
- package/dist/remove.js.map +1 -1
- package/dist/scanner.d.ts +6 -11
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +79 -23
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +19 -9
- package/dist/splice.js.map +1 -1
- package/package.json +1 -1
- package/src/astgrep.ts +115 -0
- package/src/directives.ts +275 -99
- package/src/imports.ts +145 -0
- package/src/remove.ts +318 -86
- package/src/scanner.ts +87 -24
- package/src/splice.ts +19 -9
package/src/scanner.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import {
|
|
2
|
+
import { isFileWidePosition, type PrecedingNode } from "./astgrep.js";
|
|
3
|
+
import { bunMarkerOffset, carriesAstGrepSuppression, detectDirective, type DirectivePlacement } from "./directives.js";
|
|
4
|
+
import { importGroupGaps, type ImportGroupGap } from "./imports.js";
|
|
3
5
|
import { jsxLayout, type JsxLayout } from "./jsx.js";
|
|
4
|
-
import {
|
|
5
|
-
import { firstIndexAtOrAfter } from "./ranges.js";
|
|
6
|
+
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
6
7
|
import type { Comment, CommentKind } from "./types.js";
|
|
7
8
|
|
|
8
9
|
export interface ScanOptions {
|
|
@@ -17,6 +18,10 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
|
|
|
17
18
|
export interface SourceAnalysis {
|
|
18
19
|
comments: Comment[];
|
|
19
20
|
jsx: JsxLayout;
|
|
21
|
+
/** What stands in front of each comment carrying an ast-grep suppression (see precedingNode). */
|
|
22
|
+
preceding: ReadonlyMap<Comment, PrecedingNode>;
|
|
23
|
+
/** The gaps in front of the import and export declarations the import sorters group (see importGroupGaps). */
|
|
24
|
+
importGroups: ImportGroupGap[];
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
export function analyzeSource(source: string, options: ScanOptions = {}): SourceAnalysis {
|
|
@@ -31,6 +36,14 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
31
36
|
const seen = new Set<number>();
|
|
32
37
|
/** Every token, in document order. */
|
|
33
38
|
const tokens: ts.Node[] = [];
|
|
39
|
+
/**
|
|
40
|
+
* The insides of the blocks with no statements and the switches with no
|
|
41
|
+
* clauses (see DirectivePlacement.emptyBlock), from just past the opening
|
|
42
|
+
* brace to the block's end — from the `static` keyword on for a class
|
|
43
|
+
* static block — in document order. Nothing but comments sits in such a
|
|
44
|
+
* block, so no two of them overlap.
|
|
45
|
+
*/
|
|
46
|
+
const emptyBlocks: Range[] = [];
|
|
34
47
|
|
|
35
48
|
const collect = (found: readonly ts.CommentRange[] | undefined): void => {
|
|
36
49
|
if (!found) return;
|
|
@@ -48,6 +61,17 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
48
61
|
while (pending.length > 0) {
|
|
49
62
|
const node = pending.pop() as ts.Node;
|
|
50
63
|
if (ts.isJsxElement(node) || ts.isJsxFragment(node)) jsxParents.push(node);
|
|
64
|
+
if ((ts.isBlock(node) && node.statements.length === 0) || (ts.isCaseBlock(node) && node.clauses.length === 0)) {
|
|
65
|
+
// A block the parser had to make up, with no opening brace, holds nothing.
|
|
66
|
+
const start = node.getStart(sourceFile);
|
|
67
|
+
if (source[start] === "{") {
|
|
68
|
+
// oxlint's no-empty-static-block counts the comments of the whole
|
|
69
|
+
// static block, `static /* c */ {}` included (its span starts at the
|
|
70
|
+
// keyword); no comment starts inside the keyword itself.
|
|
71
|
+
const from = ts.isClassStaticBlockDeclaration(node.parent) ? node.parent.getStart(sourceFile) : start + 1;
|
|
72
|
+
emptyBlocks.push({ start: from, end: node.end });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
51
75
|
// JSDoc nodes start inside the comment text itself; descending into them
|
|
52
76
|
// would hide the comment (e.g. a trailing JSDoc attached to the
|
|
53
77
|
// end-of-file token). Treat them as trivia like any other comment.
|
|
@@ -96,6 +120,7 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
96
120
|
const shebangEnd = ts.getShebang(source)?.length ?? 0;
|
|
97
121
|
const firstCommentPos = sorted.length > 0 ? (sorted[0] as ts.CommentRange).pos : -1;
|
|
98
122
|
const bunMarkerPos = bunMarkerOffset(source);
|
|
123
|
+
const emptyFile = holdsNoCode(sourceFile);
|
|
99
124
|
// The token after a comment, past any further comments: no token overlaps
|
|
100
125
|
// a comment, so it is the first one ending at or past the comment's end
|
|
101
126
|
// (the end-of-file token ends at the text's end). Token ends never
|
|
@@ -113,7 +138,11 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
113
138
|
header: range.pos < firstTokenStart,
|
|
114
139
|
firstComment: firstComment && /^\s*$/.test(source.slice(shebangEnd, range.pos)),
|
|
115
140
|
fileStart: range.pos === bunMarkerPos,
|
|
141
|
+
// A comment opening the first row has only whitespace in front of it,
|
|
142
|
+
// so only the first comment needs the check.
|
|
116
143
|
fileWide: firstComment && isFileWidePosition(source, range.pos, range.end),
|
|
144
|
+
emptyBlock: rangeAt(emptyBlocks, range.pos) !== undefined,
|
|
145
|
+
emptyFile,
|
|
117
146
|
get fallthroughSlot() {
|
|
118
147
|
return slotsOf().fallthroughSlot;
|
|
119
148
|
},
|
|
@@ -142,31 +171,47 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
142
171
|
};
|
|
143
172
|
});
|
|
144
173
|
|
|
145
|
-
|
|
174
|
+
const preceding = new Map<Comment, PrecedingNode>();
|
|
175
|
+
for (const [index, comment] of comments.entries()) {
|
|
176
|
+
if (carriesAstGrepSuppression(comment.text)) {
|
|
177
|
+
preceding.set(comment, precedingNode(sourceFile, tokens, comments[index - 1], comment.start));
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return {
|
|
182
|
+
comments,
|
|
183
|
+
jsx: jsxLayout(source, sourceFile, jsxParents, comments),
|
|
184
|
+
preceding,
|
|
185
|
+
importGroups: importGroupGaps(source, sourceFile, comments),
|
|
186
|
+
};
|
|
146
187
|
}
|
|
147
188
|
|
|
148
189
|
/**
|
|
149
|
-
*
|
|
150
|
-
* comment
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
190
|
+
* What stands in front of the comment at `offset` in tree-sitter's tree.
|
|
191
|
+
* tree-sitter puts a comment in the innermost node with code on both sides
|
|
192
|
+
* of it, so the comment's previous sibling is the comment right before it
|
|
193
|
+
* when no token stands between them, and otherwise the outermost node that
|
|
194
|
+
* ends with the token before it. TypeScript's nodes end where tree-sitter's
|
|
195
|
+
* do, except that tree-sitter's automatic semicolon can take a trailing
|
|
196
|
+
* comment into its statement, which makes the sibling smaller and starting
|
|
197
|
+
* no earlier: the start read off here is never on a later row.
|
|
155
198
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
199
|
+
function precedingNode(
|
|
200
|
+
sourceFile: ts.SourceFile,
|
|
201
|
+
tokens: readonly ts.Node[],
|
|
202
|
+
previous: Comment | undefined,
|
|
203
|
+
offset: number,
|
|
204
|
+
): PrecedingNode {
|
|
205
|
+
// Empty lists are leaves too, as are missing tokens; neither holds text.
|
|
206
|
+
let index = firstIndexAtOrAfter(tokens, offset + 1, (token) => token.end) - 1;
|
|
207
|
+
while (index >= 0 && (tokens[index] as ts.Node).getStart(sourceFile) >= (tokens[index] as ts.Node).end) index -= 1;
|
|
208
|
+
const token = tokens[index];
|
|
209
|
+
const tokenEnd = token?.end ?? 0;
|
|
210
|
+
if (previous !== undefined && previous.end > tokenEnd) return { start: previous.start, tokenEnd };
|
|
211
|
+
if (token === undefined) return { tokenEnd };
|
|
212
|
+
let node = token;
|
|
213
|
+
while (!ts.isSourceFile(node.parent) && node.parent.end === token.end) node = node.parent;
|
|
214
|
+
return { start: node.getStart(sourceFile), tokenEnd };
|
|
170
215
|
}
|
|
171
216
|
|
|
172
217
|
/**
|
|
@@ -218,3 +263,21 @@ function switchSlots(token: ts.Node): { fallthroughSlot: boolean; noDefaultSlot:
|
|
|
218
263
|
function lacksDefaultClause(caseBlock: ts.CaseBlock): boolean {
|
|
219
264
|
return caseBlock.clauses.length > 0 && !caseBlock.clauses.some(ts.isDefaultClause);
|
|
220
265
|
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Whether the file holds no code, as the no-empty-file rules of oxlint and
|
|
269
|
+
* eslint-plugin-unicorn read it (see DirectivePlacement.emptyFile): every
|
|
270
|
+
* statement is an empty one or a block of nothing else, past the directive
|
|
271
|
+
* prologue — the string-literal statements that open the file.
|
|
272
|
+
*/
|
|
273
|
+
function holdsNoCode(sourceFile: ts.SourceFile): boolean {
|
|
274
|
+
let prologue = true;
|
|
275
|
+
return sourceFile.statements.every((statement) => {
|
|
276
|
+
prologue &&= ts.isExpressionStatement(statement) && ts.isStringLiteral(statement.expression);
|
|
277
|
+
return prologue || isEmptyStatement(statement);
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function isEmptyStatement(statement: ts.Statement): boolean {
|
|
282
|
+
return ts.isEmptyStatement(statement) || (ts.isBlock(statement) && statement.statements.every(isEmptyStatement));
|
|
283
|
+
}
|
package/src/splice.ts
CHANGED
|
@@ -60,8 +60,12 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
60
60
|
lastChar = chunk.slice(-1);
|
|
61
61
|
};
|
|
62
62
|
|
|
63
|
-
/**
|
|
64
|
-
|
|
63
|
+
/**
|
|
64
|
+
* Deletes everything the output holds after its last line break, and that
|
|
65
|
+
* break too when `takeBreak` asks for it and the break holds no line feed;
|
|
66
|
+
* returns whether it went.
|
|
67
|
+
*/
|
|
68
|
+
const dropCurrentLine = (takeBreak: boolean): boolean => {
|
|
65
69
|
while (segments.length > 0) {
|
|
66
70
|
const last = segments[segments.length - 1] as string;
|
|
67
71
|
const breakEnd = lastLineBreakEnd(last);
|
|
@@ -69,13 +73,15 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
69
73
|
segments.pop();
|
|
70
74
|
continue;
|
|
71
75
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
const tookBreak = takeBreak && last.charAt(breakEnd - 1) !== "\n";
|
|
77
|
+
segments[segments.length - 1] = last.slice(0, tookBreak ? breakEnd - 1 : breakEnd);
|
|
78
|
+
lineIsBlank = !tookBreak;
|
|
79
|
+
lastChar = last.charAt(breakEnd - (tookBreak ? 2 : 1));
|
|
80
|
+
return tookBreak;
|
|
76
81
|
}
|
|
77
82
|
lineIsBlank = true;
|
|
78
83
|
lastChar = "";
|
|
84
|
+
return false;
|
|
79
85
|
};
|
|
80
86
|
|
|
81
87
|
/**
|
|
@@ -119,9 +125,13 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
119
125
|
const deleted = source.slice(edit.start, edit.end);
|
|
120
126
|
|
|
121
127
|
if (lineIsBlank && isBlank(restContent)) {
|
|
122
|
-
// The range occupied whole line(s): drop them entirely
|
|
123
|
-
|
|
124
|
-
|
|
128
|
+
// The range occupied whole line(s): drop them entirely, with the
|
|
129
|
+
// break that ends them — or, when that one is a line feed and the
|
|
130
|
+
// break in front of them is not, with that one instead. tree-sitter
|
|
131
|
+
// (and so ast-grep) starts a row at a line feed alone, so the rows
|
|
132
|
+
// around the dropped lines stay apart, while tsc sees one break either
|
|
133
|
+
// way.
|
|
134
|
+
cursor = dropCurrentLine(lineBreak?.[0].endsWith("\n") === true) ? contentEnd : lineEnd;
|
|
125
135
|
} else if (isBlank(restContent)) {
|
|
126
136
|
// Trailing after code: trim the gap, keep the line break.
|
|
127
137
|
trimLineEnd();
|