@kongyo2/ts-comment-scanner 1.19.0 → 1.21.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 +2 -2
- 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 +1 -1
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +3 -8
- package/dist/astgrep.js.map +1 -1
- package/dist/directives.d.ts +8 -0
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +27 -20
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +2 -3
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +45 -60
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts +34 -10
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +202 -28
- package/dist/imports.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 +13 -5
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +20 -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 +10 -7
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +254 -219
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +11 -19
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +10 -1
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +15 -26
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts +6 -0
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +22 -5
- package/dist/splice.js.map +1 -1
- package/package.json +1 -1
- package/src/args.ts +21 -21
- package/src/astgrep.ts +4 -8
- package/src/directives.ts +24 -17
- package/src/files.ts +43 -59
- package/src/imports.ts +229 -28
- package/src/jsx.ts +5 -6
- package/src/lines.ts +23 -14
- package/src/ranges.ts +17 -0
- package/src/remove.ts +287 -235
- package/src/run.ts +11 -24
- package/src/scanner.ts +25 -28
- package/src/splice.ts +29 -6
package/src/run.ts
CHANGED
|
@@ -7,12 +7,10 @@ import {
|
|
|
7
7
|
encodeFileText,
|
|
8
8
|
foldPathCase,
|
|
9
9
|
isJsxFile,
|
|
10
|
-
|
|
10
|
+
mapFiles,
|
|
11
11
|
readFileText,
|
|
12
12
|
scanFile,
|
|
13
13
|
writeFileAtomic,
|
|
14
|
-
FILE_CONCURRENCY,
|
|
15
|
-
type CollectOptions,
|
|
16
14
|
} from "./files.js";
|
|
17
15
|
import { changedFiles } from "./git.js";
|
|
18
16
|
import { removeComments, type RemoveResult } from "./remove.js";
|
|
@@ -96,30 +94,18 @@ export async function run(argv: string[], io: CliIO): Promise<number> {
|
|
|
96
94
|
throw error;
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
// A -h/--help the early scan above could not see (e.g. consumed-looking
|
|
100
|
-
// spots like `--ignore -- --help`) still means help once parsed as a flag.
|
|
101
|
-
if (options.help) {
|
|
102
|
-
io.out(HELP_TEXT);
|
|
103
|
-
return 0;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
97
|
try {
|
|
107
98
|
if (options.version) {
|
|
108
99
|
io.out(`${await getVersion()}\n`);
|
|
109
100
|
return 0;
|
|
110
101
|
}
|
|
111
102
|
|
|
112
|
-
const
|
|
113
|
-
ignore: options.ignore,
|
|
114
|
-
...(options.extensions === undefined ? {} : { extensions: options.extensions }),
|
|
115
|
-
};
|
|
116
|
-
|
|
103
|
+
const files = await collectTargets(options);
|
|
117
104
|
if (options.remove) {
|
|
118
|
-
return await runRemove(
|
|
105
|
+
return await runRemove(files, options, io);
|
|
119
106
|
}
|
|
120
107
|
|
|
121
|
-
const
|
|
122
|
-
const results = filterDirectives(await mapLimit(files, FILE_CONCURRENCY, scanFile), options.directives);
|
|
108
|
+
const results = filterDirectives(await mapFiles(files, scanFile), options.directives);
|
|
123
109
|
io.out(`${render(results, options.format)}\n`);
|
|
124
110
|
|
|
125
111
|
const total = results.reduce((sum, result) => sum + result.comments.length, 0);
|
|
@@ -141,8 +127,11 @@ function errorMessage(error: unknown): string {
|
|
|
141
127
|
* (not the collected files, which could sit in a nested repository), so
|
|
142
128
|
* scanning another repository's checkout works from anywhere.
|
|
143
129
|
*/
|
|
144
|
-
async function collectTargets(options: CliOptions
|
|
145
|
-
const files = await collectFiles(options.paths,
|
|
130
|
+
async function collectTargets(options: CliOptions): Promise<string[]> {
|
|
131
|
+
const files = await collectFiles(options.paths, {
|
|
132
|
+
ignore: options.ignore,
|
|
133
|
+
...(options.extensions === undefined ? {} : { extensions: options.extensions }),
|
|
134
|
+
});
|
|
146
135
|
if (options.diff === undefined) return files;
|
|
147
136
|
|
|
148
137
|
const first = resolve(options.paths[0] as string);
|
|
@@ -222,10 +211,8 @@ interface FileRemoval extends Pick<RemoveResult, "removed" | "kept" | "skipped">
|
|
|
222
211
|
file: string;
|
|
223
212
|
}
|
|
224
213
|
|
|
225
|
-
async function runRemove(
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
const outcomes = await mapLimit(files, FILE_CONCURRENCY, async (file) => {
|
|
214
|
+
async function runRemove(files: readonly string[], options: CliOptions, io: CliIO): Promise<number> {
|
|
215
|
+
const outcomes = await mapFiles(files, async (file) => {
|
|
229
216
|
try {
|
|
230
217
|
const { text, encoding, bom, lossless } = await readFileText(file);
|
|
231
218
|
const result = removeComments(text, {
|
package/src/scanner.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { isFileWidePosition, type PrecedingNode } from "./astgrep.js";
|
|
3
|
-
import { bunMarkerOffset, carriesAstGrepSuppression,
|
|
3
|
+
import { bunMarkerOffset, carriesAstGrepSuppression, directiveNames, type DirectivePlacement } from "./directives.js";
|
|
4
4
|
import { importGroupGaps, type ImportGroupGap } from "./imports.js";
|
|
5
5
|
import { jsxLayout, type JsxLayout } from "./jsx.js";
|
|
6
|
-
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
6
|
+
import { byStart, firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
7
7
|
import type { Comment, CommentKind } from "./types.js";
|
|
8
8
|
|
|
9
9
|
export interface ScanOptions {
|
|
@@ -17,10 +17,19 @@ export function scanComments(source: string, options: ScanOptions = {}): Comment
|
|
|
17
17
|
/** A source's comments together with the layout of its JSX children, from a single parse. */
|
|
18
18
|
export interface SourceAnalysis {
|
|
19
19
|
comments: Comment[];
|
|
20
|
+
/**
|
|
21
|
+
* Every directive each directive comment carries, its `directive` first
|
|
22
|
+
* (see directiveNames).
|
|
23
|
+
*/
|
|
24
|
+
directives: ReadonlyMap<Comment, readonly string[]>;
|
|
20
25
|
jsx: JsxLayout;
|
|
21
26
|
/** What stands in front of each comment carrying an ast-grep suppression (see precedingNode). */
|
|
22
27
|
preceding: ReadonlyMap<Comment, PrecedingNode>;
|
|
23
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* The gaps in front of the import and export declarations the import
|
|
30
|
+
* sorters group, and in front of the statements after the imports, where
|
|
31
|
+
* the linters want an empty line (see importGroupGaps).
|
|
32
|
+
*/
|
|
24
33
|
importGroups: ImportGroupGap[];
|
|
25
34
|
}
|
|
26
35
|
|
|
@@ -30,7 +39,7 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
30
39
|
const scriptKind = jsx ? ts.ScriptKind.TSX : ts.ScriptKind.TS;
|
|
31
40
|
const sourceFile = ts.createSourceFile(fileName, source, ts.ScriptTarget.Latest, true, scriptKind);
|
|
32
41
|
|
|
33
|
-
const jsxTextSpans:
|
|
42
|
+
const jsxTextSpans: Range[] = [];
|
|
34
43
|
const jsxParents: Array<ts.JsxElement | ts.JsxFragment> = [];
|
|
35
44
|
const ranges: ts.CommentRange[] = [];
|
|
36
45
|
const seen = new Set<number>();
|
|
@@ -79,7 +88,7 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
79
88
|
if (children.length === 0) {
|
|
80
89
|
tokens.push(node);
|
|
81
90
|
if (node.kind === ts.SyntaxKind.JsxText) {
|
|
82
|
-
jsxTextSpans.push(
|
|
91
|
+
jsxTextSpans.push({ start: node.getFullStart(), end: node.getEnd() });
|
|
83
92
|
}
|
|
84
93
|
collect(ts.getLeadingCommentRanges(source, node.getFullStart()));
|
|
85
94
|
collect(ts.getTrailingCommentRanges(source, node.getEnd()));
|
|
@@ -90,27 +99,10 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
90
99
|
}
|
|
91
100
|
}
|
|
92
101
|
|
|
93
|
-
// JsxText spans never overlap, so a position is inside
|
|
94
|
-
//
|
|
95
|
-
|
|
96
|
-
jsxTextSpans.sort((a, b) => a
|
|
97
|
-
const insideJsxText = (pos: number): boolean => {
|
|
98
|
-
let low = 0;
|
|
99
|
-
let high = jsxTextSpans.length - 1;
|
|
100
|
-
let candidate = -1;
|
|
101
|
-
while (low <= high) {
|
|
102
|
-
const middle = (low + high) >> 1;
|
|
103
|
-
if ((jsxTextSpans[middle] as [number, number])[0] <= pos) {
|
|
104
|
-
candidate = middle;
|
|
105
|
-
low = middle + 1;
|
|
106
|
-
} else {
|
|
107
|
-
high = middle - 1;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return candidate >= 0 && pos < (jsxTextSpans[candidate] as [number, number])[1];
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const sorted = ranges.filter((range) => !insideJsxText(range.pos)).sort((a, b) => a.pos - b.pos);
|
|
102
|
+
// JsxText spans never overlap, so the one a position is inside is found by
|
|
103
|
+
// binary search, keeping comment-dense TSX files out of quadratic time.
|
|
104
|
+
jsxTextSpans.sort(byStart);
|
|
105
|
+
const sorted = ranges.filter((range) => rangeAt(jsxTextSpans, range.pos) === undefined).sort((a, b) => a.pos - b.pos);
|
|
114
106
|
|
|
115
107
|
// Directive rules with positional requirements (header pragmas, prettier's
|
|
116
108
|
// first-comment docblock, Bun's file-start marker) get the comment's actual
|
|
@@ -152,13 +144,15 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
152
144
|
};
|
|
153
145
|
};
|
|
154
146
|
|
|
147
|
+
const directives = new Map<Comment, readonly string[]>();
|
|
155
148
|
const comments = sorted.map((range): Comment => {
|
|
156
149
|
const kind: CommentKind = range.kind === ts.SyntaxKind.SingleLineCommentTrivia ? "line" : "block";
|
|
157
150
|
const text = source.slice(range.pos, range.end);
|
|
158
151
|
const startPosition = sourceFile.getLineAndCharacterOfPosition(range.pos);
|
|
159
152
|
const endPosition = sourceFile.getLineAndCharacterOfPosition(range.end);
|
|
160
|
-
const
|
|
161
|
-
|
|
153
|
+
const names = directiveNames(kind, text, placementOf(range));
|
|
154
|
+
const directive = names[0];
|
|
155
|
+
const comment: Comment = {
|
|
162
156
|
kind,
|
|
163
157
|
text,
|
|
164
158
|
start: range.pos,
|
|
@@ -169,6 +163,8 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
169
163
|
endColumn: endPosition.character + 1,
|
|
170
164
|
...(directive === undefined ? {} : { directive }),
|
|
171
165
|
};
|
|
166
|
+
if (directive !== undefined) directives.set(comment, names);
|
|
167
|
+
return comment;
|
|
172
168
|
});
|
|
173
169
|
|
|
174
170
|
const preceding = new Map<Comment, PrecedingNode>();
|
|
@@ -180,6 +176,7 @@ export function analyzeSource(source: string, options: ScanOptions = {}): Source
|
|
|
180
176
|
|
|
181
177
|
return {
|
|
182
178
|
comments,
|
|
179
|
+
directives,
|
|
183
180
|
jsx: jsxLayout(source, sourceFile, jsxParents, comments),
|
|
184
181
|
preceding,
|
|
185
182
|
importGroups: importGroupGaps(source, sourceFile, comments),
|
package/src/splice.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LINE_BREAK, LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
1
|
+
import { LINE_BREAK, LINE_TERMINATOR, isBlank, isHorizontalSpace, lineStartOffsets } from "./lines.js";
|
|
2
2
|
import { firstIndexAtOrAfter, rangeAt, type Range } from "./ranges.js";
|
|
3
3
|
|
|
4
4
|
/** One change to the source: a range to delete (tidying its line) or to replace verbatim. */
|
|
@@ -18,10 +18,14 @@ export interface Edit extends Range {
|
|
|
18
18
|
* the empty line.
|
|
19
19
|
*/
|
|
20
20
|
blankLine?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* With code on both sides of a range that spans lines, one of its own line
|
|
23
|
+
* breaks takes its place, wherever the range stands (see ownLineBreak), so
|
|
24
|
+
* the code on either side stays on lines and rows apart.
|
|
25
|
+
*/
|
|
26
|
+
lineBreak?: boolean;
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
|
|
24
|
-
|
|
25
29
|
// Brackets never merge with what follows them, nor punctuation with what
|
|
26
30
|
// precedes it, so removing a comment between them needs no space to keep
|
|
27
31
|
// the tokens apart, and the whitespace that only held the comment off them
|
|
@@ -34,6 +38,20 @@ function needsSpace(before: string, after: string): boolean {
|
|
|
34
38
|
return !OPENING_BRACKETS.includes(before) && !CLOSING_PUNCTUATION.includes(after);
|
|
35
39
|
}
|
|
36
40
|
|
|
41
|
+
/**
|
|
42
|
+
* The line break that keeps the code on both sides of a deleted range apart
|
|
43
|
+
* as the range did: a line feed, with the CR in front of it when the range
|
|
44
|
+
* has a CRLF, for a range holding one, which ends a line for every tool, and
|
|
45
|
+
* otherwise the range's first line terminator, which ends one for tsc and
|
|
46
|
+
* ESLint but none for the tools that only count line feeds (tree-sitter,
|
|
47
|
+
* oxlint, Semgrep, c8).
|
|
48
|
+
*/
|
|
49
|
+
function ownLineBreak(deleted: string): string {
|
|
50
|
+
if (deleted.includes("\r\n")) return "\r\n";
|
|
51
|
+
if (deleted.includes("\n")) return "\n";
|
|
52
|
+
return (LINE_TERMINATOR.exec(deleted) as RegExpExecArray)[0];
|
|
53
|
+
}
|
|
54
|
+
|
|
37
55
|
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
38
56
|
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
39
57
|
|
|
@@ -161,15 +179,20 @@ export function applyEdits(source: string, edits: readonly Edit[]): string {
|
|
|
161
179
|
trimLineEnd();
|
|
162
180
|
push(source.slice(contentEnd, lineEnd));
|
|
163
181
|
cursor = lineEnd;
|
|
164
|
-
} else if (
|
|
182
|
+
} else if (
|
|
183
|
+
!lineIsBlank &&
|
|
184
|
+
LINE_TERMINATOR.test(deleted) &&
|
|
185
|
+
(edit.lineBreak === true || needsSpace(lastChar, source.charAt(cursor)))
|
|
186
|
+
) {
|
|
165
187
|
// Code on both sides of a range that spans lines. Per ECMA-262 a
|
|
166
188
|
// multi-line comment acts as a line terminator for semicolon
|
|
167
189
|
// insertion, so a real line break (matching the file's style) takes
|
|
168
190
|
// its place instead of merging the lines. (Right after an opening
|
|
169
191
|
// bracket or before closing punctuation a line terminator changes
|
|
170
|
-
// nothing, so a multi-line comment there goes without one
|
|
192
|
+
// nothing, so a multi-line comment there goes without one, unless the
|
|
193
|
+
// lines are to stay apart.)
|
|
171
194
|
trimLineEnd();
|
|
172
|
-
push(deleted.includes("\r\n") ? "\r\n" : "\n");
|
|
195
|
+
push(edit.lineBreak === true ? ownLineBreak(deleted) : deleted.includes("\r\n") ? "\r\n" : "\n");
|
|
173
196
|
} else if (lastChar !== "" && !isBlank(lastChar)) {
|
|
174
197
|
// Code right before the range, more on the same line after it.
|
|
175
198
|
if (edit.jsx === true) continue;
|