@kongyo2/ts-comment-scanner 1.3.0 → 1.5.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.en.md +23 -8
- package/README.md +21 -6
- package/dist/directives.d.ts +31 -1
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +127 -34
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +43 -1
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +215 -26
- package/dist/files.js.map +1 -1
- package/dist/git.js +6 -6
- package/dist/git.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/remove.d.ts +11 -2
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +226 -45
- package/dist/remove.js.map +1 -1
- package/dist/report.d.ts.map +1 -1
- package/dist/report.js +24 -2
- package/dist/report.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +86 -16
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +43 -43
- package/dist/scanner.js.map +1 -1
- package/package.json +11 -5
- package/src/args.ts +198 -0
- package/src/cli.ts +12 -0
- package/src/directives.ts +516 -0
- package/src/files.ts +370 -0
- package/src/git.ts +72 -0
- package/src/index.ts +19 -0
- package/src/remove.ts +515 -0
- package/src/report.ts +106 -0
- package/src/run.ts +317 -0
- package/src/scanner.ts +104 -0
- package/src/types.ts +19 -0
- package/src/version.ts +7 -0
package/dist/run.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { realpathSync } from "node:fs";
|
|
2
|
-
import {
|
|
2
|
+
import { stat } from "node:fs/promises";
|
|
3
3
|
import { basename, dirname, join, resolve } from "node:path";
|
|
4
4
|
import { parseArgs, UsageError } from "./args.js";
|
|
5
|
-
import { collectFiles, isJsxFile, mapLimit, scanFile, FILE_CONCURRENCY } from "./files.js";
|
|
5
|
+
import { collectFiles, encodeFileText, isJsxFile, mapLimit, readFileText, scanFile, writeFileAtomic, FILE_CONCURRENCY, } from "./files.js";
|
|
6
6
|
import { changedFiles } from "./git.js";
|
|
7
7
|
import { removeComments } from "./remove.js";
|
|
8
8
|
import { count, formatGitHub, formatJson, formatText } from "./report.js";
|
|
@@ -70,6 +70,12 @@ export async function run(argv, io) {
|
|
|
70
70
|
}
|
|
71
71
|
throw error;
|
|
72
72
|
}
|
|
73
|
+
// A -h/--help the early scan above could not see (e.g. consumed-looking
|
|
74
|
+
// spots like `--ignore -- --help`) still means help once parsed as a flag.
|
|
75
|
+
if (options.help) {
|
|
76
|
+
io.out(HELP_TEXT);
|
|
77
|
+
return 0;
|
|
78
|
+
}
|
|
73
79
|
try {
|
|
74
80
|
if (options.version) {
|
|
75
81
|
io.out(`${await getVersion()}\n`);
|
|
@@ -106,11 +112,50 @@ async function collectTargets(options, collectOptions) {
|
|
|
106
112
|
return files;
|
|
107
113
|
const first = resolve(options.paths[0]);
|
|
108
114
|
const anchor = (await stat(first)).isDirectory() ? first : dirname(first);
|
|
109
|
-
|
|
110
|
-
//
|
|
111
|
-
//
|
|
112
|
-
//
|
|
113
|
-
|
|
115
|
+
// Both the spelling git reports and the real file it refers to count as
|
|
116
|
+
// changed: the collector keeps one spelling per real file, so a changed
|
|
117
|
+
// symlink may have been remembered under its target's path (or a scanned
|
|
118
|
+
// alias may point at the changed file), and either spelling names the same
|
|
119
|
+
// content. Broken links (a symlink re-pointed at nothing) only match by
|
|
120
|
+
// their reported spelling.
|
|
121
|
+
const changed = new Set();
|
|
122
|
+
for (const path of await changedFiles(options.diff, anchor)) {
|
|
123
|
+
changed.add(caseFold(path));
|
|
124
|
+
const real = tryRealpath(path);
|
|
125
|
+
if (real !== undefined)
|
|
126
|
+
changed.add(caseFold(real));
|
|
127
|
+
}
|
|
128
|
+
// Realpath only the directory part first: spellings through symlinked
|
|
129
|
+
// directories then compare equal while a tracked symlink still matches the
|
|
130
|
+
// path git reports it at; the fully dereferenced path is the fallback.
|
|
131
|
+
return files.filter((file) => {
|
|
132
|
+
if (changed.has(caseFold(join(realpathSync.native(dirname(file)), basename(file)))))
|
|
133
|
+
return true;
|
|
134
|
+
const real = tryRealpath(file);
|
|
135
|
+
return real !== undefined && changed.has(caseFold(real));
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Windows paths are case-insensitive, but git reports the case a file was
|
|
140
|
+
* tracked with; folding both sides keeps `Foo.ts` in the --diff scope when
|
|
141
|
+
* the scan found it as `foo.ts`.
|
|
142
|
+
*/
|
|
143
|
+
function caseFold(path) {
|
|
144
|
+
return process.platform === "win32" ? path.toLowerCase() : path;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Native realpath — changedFiles resolves the repository root with the
|
|
148
|
+
* (native) promises realpath, and on Windows only the native calls expand
|
|
149
|
+
* 8.3 short names like RUNNER~1, so every path compared against it must be
|
|
150
|
+
* expanded the same way. Undefined when the path cannot be resolved.
|
|
151
|
+
*/
|
|
152
|
+
function tryRealpath(path) {
|
|
153
|
+
try {
|
|
154
|
+
return realpathSync.native(path);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
114
159
|
}
|
|
115
160
|
/** True when -h/--help appears before any `--` separator. */
|
|
116
161
|
function wantsHelp(argv) {
|
|
@@ -148,17 +193,26 @@ async function runRemove(options, collectOptions, io) {
|
|
|
148
193
|
const files = await collectTargets(options, collectOptions);
|
|
149
194
|
const outcomes = await mapLimit(files, FILE_CONCURRENCY, async (file) => {
|
|
150
195
|
try {
|
|
151
|
-
const
|
|
152
|
-
const result = removeComments(
|
|
196
|
+
const { text, encoding, bom, lossless } = await readFileText(file);
|
|
197
|
+
const result = removeComments(text, {
|
|
153
198
|
jsx: isJsxFile(file),
|
|
154
199
|
removeDirectives: options.removeDirectives,
|
|
155
200
|
removeLegal: options.removeLegal,
|
|
156
201
|
shouldRemove: (comment) => inScope(comment, options.directives),
|
|
157
202
|
});
|
|
158
|
-
if (result.changed
|
|
159
|
-
|
|
203
|
+
if (result.changed) {
|
|
204
|
+
if (!lossless) {
|
|
205
|
+
// Re-encoding would not reproduce the original bytes (invalid
|
|
206
|
+
// UTF-8, truncated UTF-16, ...): rewriting the file would corrupt
|
|
207
|
+
// content outside the comments. Reported for --dry-run too, so the
|
|
208
|
+
// preflight fails the same way the real removal would.
|
|
209
|
+
throw new Error("file is not valid UTF-8 or UTF-16; refusing to modify it");
|
|
210
|
+
}
|
|
211
|
+
if (!options.dryRun) {
|
|
212
|
+
await writeFileAtomic(file, encodeFileText(result.code, { encoding, bom }));
|
|
213
|
+
}
|
|
160
214
|
}
|
|
161
|
-
return { file, removed: result.removed, kept: result.kept };
|
|
215
|
+
return { file, removed: result.removed, kept: result.kept, skipped: result.skipped };
|
|
162
216
|
}
|
|
163
217
|
catch (error) {
|
|
164
218
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -171,7 +225,7 @@ async function runRemove(options, collectOptions, io) {
|
|
|
171
225
|
if ("failure" in outcome) {
|
|
172
226
|
failures.push(outcome.failure);
|
|
173
227
|
}
|
|
174
|
-
else if (outcome.removed.length > 0 || outcome.kept.length > 0) {
|
|
228
|
+
else if (outcome.removed.length > 0 || outcome.kept.length > 0 || outcome.skipped.length > 0) {
|
|
175
229
|
removals.push(outcome);
|
|
176
230
|
}
|
|
177
231
|
}
|
|
@@ -184,16 +238,32 @@ async function runRemove(options, collectOptions, io) {
|
|
|
184
238
|
function renderRemoval(removals, options) {
|
|
185
239
|
const totalRemoved = removals.reduce((sum, entry) => sum + entry.removed.length, 0);
|
|
186
240
|
const totalKept = removals.reduce((sum, entry) => sum + entry.kept.length, 0);
|
|
241
|
+
const totalSkipped = removals.reduce((sum, entry) => sum + entry.skipped.length, 0);
|
|
187
242
|
const changedEntries = removals.filter((entry) => entry.removed.length > 0);
|
|
188
243
|
const keptNote = totalKept > 0 ? ` Kept ${count(totalKept, "protected comment")}.` : "";
|
|
244
|
+
// Comments held out of scope by --skip-directives / --only-directives are
|
|
245
|
+
// invisible in the removal counts; say how many were left untouched, naming
|
|
246
|
+
// the flag actually responsible, so "removed N" is not mistaken for "the
|
|
247
|
+
// files are now comment-free".
|
|
248
|
+
const skippedReason = options.directives === "only" ? "outside --only-directives" : "--skip-directives";
|
|
249
|
+
const skippedNote = totalSkipped > 0 ? ` Skipped ${count(totalSkipped, "comment")} (${skippedReason}).` : "";
|
|
189
250
|
if (options.format === "json") {
|
|
190
251
|
return JSON.stringify({
|
|
191
|
-
summary: {
|
|
252
|
+
summary: {
|
|
253
|
+
// `files` counts the entries of `files` below, like the scan JSON;
|
|
254
|
+
// `changedFiles` is how many of them actually had comments removed.
|
|
255
|
+
files: removals.length,
|
|
256
|
+
changedFiles: changedEntries.length,
|
|
257
|
+
removed: totalRemoved,
|
|
258
|
+
kept: totalKept,
|
|
259
|
+
skipped: totalSkipped,
|
|
260
|
+
dryRun: options.dryRun,
|
|
261
|
+
},
|
|
192
262
|
files: removals,
|
|
193
263
|
}, null, 2);
|
|
194
264
|
}
|
|
195
265
|
if (totalRemoved === 0) {
|
|
196
|
-
return `No removable comments found.${keptNote}`;
|
|
266
|
+
return `No removable comments found.${keptNote}${skippedNote}`;
|
|
197
267
|
}
|
|
198
268
|
const verb = options.dryRun ? "would remove" : "removed";
|
|
199
269
|
const lines = changedEntries.map((entry) => {
|
|
@@ -201,7 +271,7 @@ function renderRemoval(removals, options) {
|
|
|
201
271
|
return `${entry.file}: ${verb} ${entry.removed.length}${kept}`;
|
|
202
272
|
});
|
|
203
273
|
const sentenceVerb = verb.charAt(0).toUpperCase() + verb.slice(1);
|
|
204
|
-
lines.push("", `${sentenceVerb} ${count(totalRemoved, "comment")} across ${count(changedEntries.length, "file")}.${keptNote}`);
|
|
274
|
+
lines.push("", `${sentenceVerb} ${count(totalRemoved, "comment")} across ${count(changedEntries.length, "file")}.${keptNote}${skippedNote}`);
|
|
205
275
|
return lines.join("\n");
|
|
206
276
|
}
|
|
207
277
|
//# sourceMappingURL=run.js.map
|
package/dist/run.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"run.js","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,UAAU,EAAuC,MAAM,WAAW,CAAC;AACvF,OAAO,EACL,YAAY,EACZ,cAAc,EACd,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,eAAe,EACf,gBAAgB,GAEjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AACxC,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC1E,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAQ1C,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA6CxB,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,GAAG,CAAC,IAAc,EAAE,EAAS;IACjD,+DAA+D;IAC/D,IAAI,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC;QACpB,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClB,OAAO,CAAC,CAAC;IACX,CAAC;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WAAW,EAAE,CAAC;IACjE,CAAC;IAED,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,SAAS,CAAC;IACzD,MAAM,KAAK,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QACzC,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QACzE,OAAO,GAAG,KAAK,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,GAAG,IAAI,EAAE,CAAC;IACjE,CAAC,CAAC,CAAC;IAEH,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAClE,KAAK,CAAC,IAAI,CACR,EAAE,EACF,GAAG,YAAY,IAAI,KAAK,CAAC,YAAY,EAAE,SAAS,CAAC,WAAW,KAAK,CAAC,cAAc,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI,QAAQ,GAAG,WAAW,EAAE,CAC7H,CAAC;IACF,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,CAAC"}
|
package/dist/scanner.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scanner.d.ts","sourceRoot":"","sources":["../src/scanner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAe,MAAM,YAAY,CAAC;AAEvD,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;
|
|
1
|
+
{"version":3,"file":"scanner.d.ts","sourceRoot":"","sources":["../src/scanner.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAe,MAAM,YAAY,CAAC;AAEvD,MAAM,WAAW,WAAW;IAC1B,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,GAAG,OAAO,EAAE,CA+FjF"}
|
package/dist/scanner.js
CHANGED
|
@@ -1,22 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { detectDirective } from "./directives.js";
|
|
3
|
-
// dprint-ignore-file is deliberately absent: a stray one below the header no
|
|
4
|
-
// longer skips the file, but dprint's node pragma still matches inside it, so
|
|
5
|
-
// the comment stays an active `dprint-ignore` and must keep its tag.
|
|
6
|
-
const HEADER_ONLY_DIRECTIVES = new Set([
|
|
7
|
-
"@ts-nocheck",
|
|
8
|
-
"@ts-check",
|
|
9
|
-
"@format",
|
|
10
|
-
"@noformat",
|
|
11
|
-
"@prettier",
|
|
12
|
-
"@noprettier",
|
|
13
|
-
"@flow",
|
|
14
|
-
"@noflow",
|
|
15
|
-
"@bun",
|
|
16
|
-
"@ts-self-types",
|
|
17
|
-
"jslint",
|
|
18
|
-
"property",
|
|
19
|
-
]);
|
|
20
3
|
export function scanComments(source, options = {}) {
|
|
21
4
|
const jsx = options.jsx === true;
|
|
22
5
|
const fileName = jsx ? "module.tsx" : "module.ts";
|
|
@@ -35,7 +18,12 @@ export function scanComments(source, options = {}) {
|
|
|
35
18
|
ranges.push(range);
|
|
36
19
|
}
|
|
37
20
|
};
|
|
38
|
-
|
|
21
|
+
// Iterative depth-first walk: generated or minified sources nest thousands
|
|
22
|
+
// of expressions deep, where a recursive visitor overflows the call stack
|
|
23
|
+
// even though the parse itself succeeded.
|
|
24
|
+
const pending = [sourceFile];
|
|
25
|
+
while (pending.length > 0) {
|
|
26
|
+
const node = pending.pop();
|
|
39
27
|
// JSDoc nodes start inside the comment text itself; descending into them
|
|
40
28
|
// would hide the comment (e.g. a trailing JSDoc attached to the
|
|
41
29
|
// end-of-file token). Treat them as trivia like any other comment.
|
|
@@ -46,39 +34,51 @@ export function scanComments(source, options = {}) {
|
|
|
46
34
|
}
|
|
47
35
|
collect(ts.getLeadingCommentRanges(source, node.getFullStart()));
|
|
48
36
|
collect(ts.getTrailingCommentRanges(source, node.getEnd()));
|
|
49
|
-
|
|
37
|
+
continue;
|
|
50
38
|
}
|
|
51
|
-
for (
|
|
52
|
-
|
|
39
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
40
|
+
pending.push(children[index]);
|
|
53
41
|
}
|
|
42
|
+
}
|
|
43
|
+
// JsxText spans never overlap, so a position is inside one exactly when it
|
|
44
|
+
// is inside the closest span starting at or before it — found by binary
|
|
45
|
+
// search, keeping comment-dense TSX files out of quadratic time.
|
|
46
|
+
jsxTextSpans.sort((a, b) => a[0] - b[0]);
|
|
47
|
+
const insideJsxText = (pos) => {
|
|
48
|
+
let low = 0;
|
|
49
|
+
let high = jsxTextSpans.length - 1;
|
|
50
|
+
let candidate = -1;
|
|
51
|
+
while (low <= high) {
|
|
52
|
+
const middle = (low + high) >> 1;
|
|
53
|
+
if (jsxTextSpans[middle][0] <= pos) {
|
|
54
|
+
candidate = middle;
|
|
55
|
+
low = middle + 1;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
high = middle - 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return candidate >= 0 && pos < jsxTextSpans[candidate][1];
|
|
54
62
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
//
|
|
58
|
-
//
|
|
59
|
-
//
|
|
60
|
-
// token; anywhere later the tools treat them as ordinary text. Coverage
|
|
61
|
-
// file pragmas (`istanbul ignore file`, `c8/v8 ignore file`) are NOT
|
|
62
|
-
// gated: istanbul-lib-instrument and Vitest's v8 provider
|
|
63
|
-
// (ast-v8-to-istanbul) honour them on any comment in the file. Test
|
|
64
|
-
// environment pragmas are not gated either: Vitest matches them (including
|
|
65
|
-
// the @jest- spellings) with a regex over the whole file, and neither are
|
|
66
|
-
// @ts-strict pragmas (typescript-strict-plugin scans every line).
|
|
63
|
+
const sorted = ranges.filter((range) => !insideJsxText(range.pos)).sort((a, b) => a.pos - b.pos);
|
|
64
|
+
// Directive rules with positional requirements (header pragmas, prettier's
|
|
65
|
+
// first-comment docblock, Bun's file-start marker) get the comment's actual
|
|
66
|
+
// placement; a shebang does not count as content before the first comment,
|
|
67
|
+
// matching jest-docblock.
|
|
67
68
|
const firstTokenStart = sourceFile.getStart(sourceFile);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
69
|
+
const shebangEnd = ts.getShebang(source)?.length ?? 0;
|
|
70
|
+
const firstCommentPos = sorted.length > 0 ? sorted[0].pos : -1;
|
|
71
|
+
const placementOf = (pos) => ({
|
|
72
|
+
header: pos < firstTokenStart,
|
|
73
|
+
firstComment: pos === firstCommentPos && /^\s*$/.test(source.slice(shebangEnd, pos)),
|
|
74
|
+
fileStart: pos === 0,
|
|
75
|
+
});
|
|
76
|
+
return sorted.map((range) => {
|
|
76
77
|
const kind = range.kind === ts.SyntaxKind.SingleLineCommentTrivia ? "line" : "block";
|
|
77
78
|
const text = source.slice(range.pos, range.end);
|
|
78
79
|
const startPosition = sourceFile.getLineAndCharacterOfPosition(range.pos);
|
|
79
80
|
const endPosition = sourceFile.getLineAndCharacterOfPosition(range.end);
|
|
80
|
-
const
|
|
81
|
-
const directive = detected !== undefined && isActiveDirective(detected, range.pos) ? detected : undefined;
|
|
81
|
+
const directive = detectDirective(kind, text, placementOf(range.pos));
|
|
82
82
|
return {
|
|
83
83
|
kind,
|
|
84
84
|
text,
|
package/dist/scanner.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scanner.js","sourceRoot":"","sources":["../src/scanner.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,eAAe,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,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kongyo2/ts-comment-scanner",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "CLI to detect, report and safely remove comments across a TypeScript project.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"author": "kongyo2",
|
|
20
|
+
"homepage": "https://kongyo2.github.io/ts-comment-scanner/",
|
|
20
21
|
"repository": {
|
|
21
22
|
"type": "git",
|
|
22
23
|
"url": "git+https://github.com/kongyo2/ts-comment-scanner.git"
|
|
@@ -39,22 +40,26 @@
|
|
|
39
40
|
},
|
|
40
41
|
"sideEffects": false,
|
|
41
42
|
"files": [
|
|
42
|
-
"dist"
|
|
43
|
+
"dist",
|
|
44
|
+
"src",
|
|
45
|
+
"!src/*.test.ts"
|
|
43
46
|
],
|
|
44
47
|
"scripts": {
|
|
45
|
-
"
|
|
48
|
+
"clean": "node --eval \"require('node:fs').rmSync('dist', { recursive: true, force: true })\"",
|
|
49
|
+
"build": "npm run clean && tsc",
|
|
46
50
|
"build:watch": "tsc --watch",
|
|
47
|
-
"typecheck": "tsc --noEmit",
|
|
51
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.tests.json",
|
|
48
52
|
"typecheck:watch": "tsc --noEmit --watch",
|
|
49
53
|
"start": "node dist/cli.js",
|
|
50
54
|
"test": "vitest run",
|
|
55
|
+
"test:coverage": "vitest run --coverage",
|
|
51
56
|
"test:watch": "vitest",
|
|
52
57
|
"lint": "oxlint --format agent",
|
|
53
58
|
"lint:fix": "oxlint --fix",
|
|
54
59
|
"lint:strict": "oxlint --deny-warnings",
|
|
55
60
|
"format": "prettier --write .",
|
|
56
61
|
"format:check": "prettier --check .",
|
|
57
|
-
"
|
|
62
|
+
"prepare": "npm run build"
|
|
58
63
|
},
|
|
59
64
|
"publishConfig": {
|
|
60
65
|
"access": "public"
|
|
@@ -66,6 +71,7 @@
|
|
|
66
71
|
"devDependencies": {
|
|
67
72
|
"@types/node": "^25.9.3",
|
|
68
73
|
"@types/picomatch": "^4.0.3",
|
|
74
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
69
75
|
"oxlint": "^1.70.0",
|
|
70
76
|
"prettier": "^3.8.4",
|
|
71
77
|
"vitest": "^4.1.9"
|
package/src/args.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
export type OutputFormat = "text" | "json" | "github";
|
|
2
|
+
export type DirectiveMode = "include" | "skip" | "only";
|
|
3
|
+
|
|
4
|
+
export interface CliOptions {
|
|
5
|
+
paths: string[];
|
|
6
|
+
format: OutputFormat;
|
|
7
|
+
ignore: string[];
|
|
8
|
+
extensions: string[] | undefined;
|
|
9
|
+
diff: string | undefined;
|
|
10
|
+
directives: DirectiveMode;
|
|
11
|
+
failOnComment: boolean;
|
|
12
|
+
remove: boolean;
|
|
13
|
+
removeDirectives: boolean;
|
|
14
|
+
removeLegal: boolean;
|
|
15
|
+
dryRun: boolean;
|
|
16
|
+
help: boolean;
|
|
17
|
+
version: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Invalid command line input; the CLI reports the message and exits with code 2. */
|
|
21
|
+
export class UsageError extends Error {
|
|
22
|
+
constructor(message: string) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "UsageError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const FORMATS: readonly OutputFormat[] = ["text", "json", "github"];
|
|
29
|
+
const DIRECTIVE_CONFLICT = "--skip-directives and --only-directives cannot be combined";
|
|
30
|
+
|
|
31
|
+
export function parseArgs(argv: string[]): CliOptions {
|
|
32
|
+
const paths: string[] = [];
|
|
33
|
+
const ignore: string[] = [];
|
|
34
|
+
const extensions: string[] = [];
|
|
35
|
+
let format: OutputFormat = "text";
|
|
36
|
+
let diff: string | undefined;
|
|
37
|
+
let directives: DirectiveMode = "include";
|
|
38
|
+
let failOnComment = false;
|
|
39
|
+
let remove = false;
|
|
40
|
+
let removeDirectives = false;
|
|
41
|
+
let removeLegal = false;
|
|
42
|
+
let dryRun = false;
|
|
43
|
+
let help = false;
|
|
44
|
+
let version = false;
|
|
45
|
+
let pathsOnly = false;
|
|
46
|
+
|
|
47
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
48
|
+
const arg = argv[index] as string;
|
|
49
|
+
|
|
50
|
+
if (pathsOnly || !arg.startsWith("-") || arg === "-") {
|
|
51
|
+
paths.push(arg);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const [flag, inlineValue] = splitFlag(arg);
|
|
56
|
+
const readValue = (): string => {
|
|
57
|
+
if (inlineValue !== undefined) return inlineValue;
|
|
58
|
+
const next = argv[index + 1];
|
|
59
|
+
if (next === undefined) throw new UsageError(`option ${flag} requires a value`);
|
|
60
|
+
index += 1;
|
|
61
|
+
return next;
|
|
62
|
+
};
|
|
63
|
+
const rejectValue = (): void => {
|
|
64
|
+
if (inlineValue !== undefined) throw new UsageError(`option ${flag} does not take a value`);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
switch (flag) {
|
|
68
|
+
case "--":
|
|
69
|
+
rejectValue();
|
|
70
|
+
pathsOnly = true;
|
|
71
|
+
break;
|
|
72
|
+
case "--format": {
|
|
73
|
+
const value = readValue();
|
|
74
|
+
if (!FORMATS.includes(value as OutputFormat)) {
|
|
75
|
+
throw new UsageError(`unknown format: ${value} (expected ${FORMATS.join(", ")})`);
|
|
76
|
+
}
|
|
77
|
+
format = value as OutputFormat;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case "--json":
|
|
81
|
+
rejectValue();
|
|
82
|
+
format = "json";
|
|
83
|
+
break;
|
|
84
|
+
case "--ignore":
|
|
85
|
+
ignore.push(readValue());
|
|
86
|
+
break;
|
|
87
|
+
case "--ext": {
|
|
88
|
+
const parsed = readValue()
|
|
89
|
+
.split(",")
|
|
90
|
+
.map((extension) => extension.trim())
|
|
91
|
+
.filter((extension) => extension !== "");
|
|
92
|
+
if (parsed.length === 0) {
|
|
93
|
+
throw new UsageError("option --ext requires at least one extension");
|
|
94
|
+
}
|
|
95
|
+
extensions.push(...parsed);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case "--diff": {
|
|
99
|
+
const value = readValue();
|
|
100
|
+
if (value === "" || value.startsWith("-")) {
|
|
101
|
+
throw new UsageError("option --diff requires a git revision range (e.g. HEAD or main..HEAD)");
|
|
102
|
+
}
|
|
103
|
+
diff = value;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "--skip-directives":
|
|
107
|
+
rejectValue();
|
|
108
|
+
if (directives === "only") throw new UsageError(DIRECTIVE_CONFLICT);
|
|
109
|
+
directives = "skip";
|
|
110
|
+
break;
|
|
111
|
+
case "--only-directives":
|
|
112
|
+
rejectValue();
|
|
113
|
+
if (directives === "skip") throw new UsageError(DIRECTIVE_CONFLICT);
|
|
114
|
+
directives = "only";
|
|
115
|
+
break;
|
|
116
|
+
case "--fail-on-comment":
|
|
117
|
+
rejectValue();
|
|
118
|
+
failOnComment = true;
|
|
119
|
+
break;
|
|
120
|
+
case "--remove":
|
|
121
|
+
rejectValue();
|
|
122
|
+
remove = true;
|
|
123
|
+
break;
|
|
124
|
+
case "--remove-directives":
|
|
125
|
+
rejectValue();
|
|
126
|
+
removeDirectives = true;
|
|
127
|
+
break;
|
|
128
|
+
case "--remove-legal":
|
|
129
|
+
rejectValue();
|
|
130
|
+
removeLegal = true;
|
|
131
|
+
break;
|
|
132
|
+
case "--dry-run":
|
|
133
|
+
rejectValue();
|
|
134
|
+
dryRun = true;
|
|
135
|
+
break;
|
|
136
|
+
case "-h":
|
|
137
|
+
case "--help":
|
|
138
|
+
rejectValue();
|
|
139
|
+
help = true;
|
|
140
|
+
break;
|
|
141
|
+
case "-v":
|
|
142
|
+
case "--version":
|
|
143
|
+
rejectValue();
|
|
144
|
+
version = true;
|
|
145
|
+
break;
|
|
146
|
+
default:
|
|
147
|
+
throw new UsageError(`unknown option: ${flag}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (!remove) {
|
|
152
|
+
for (const [set, flag] of [
|
|
153
|
+
[dryRun, "--dry-run"],
|
|
154
|
+
[removeDirectives, "--remove-directives"],
|
|
155
|
+
[removeLegal, "--remove-legal"],
|
|
156
|
+
] as const) {
|
|
157
|
+
if (set) throw new UsageError(`${flag} requires --remove`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (remove && failOnComment) {
|
|
161
|
+
throw new UsageError("--fail-on-comment cannot be combined with --remove");
|
|
162
|
+
}
|
|
163
|
+
if (remove && format === "github") {
|
|
164
|
+
throw new UsageError("--format github cannot be combined with --remove");
|
|
165
|
+
}
|
|
166
|
+
if (remove && directives === "only" && !removeDirectives) {
|
|
167
|
+
throw new UsageError(
|
|
168
|
+
"--remove --only-directives would remove nothing; add --remove-directives to delete directives",
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
if (remove && directives === "skip" && removeDirectives) {
|
|
172
|
+
throw new UsageError("--remove-directives cannot be combined with --skip-directives");
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
paths: paths.length > 0 ? paths : ["."],
|
|
177
|
+
format,
|
|
178
|
+
ignore,
|
|
179
|
+
extensions: extensions.length > 0 ? extensions : undefined,
|
|
180
|
+
diff,
|
|
181
|
+
directives,
|
|
182
|
+
failOnComment,
|
|
183
|
+
remove,
|
|
184
|
+
removeDirectives,
|
|
185
|
+
removeLegal,
|
|
186
|
+
dryRun,
|
|
187
|
+
help,
|
|
188
|
+
version,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function splitFlag(arg: string): [string, string | undefined] {
|
|
193
|
+
const equalsIndex = arg.indexOf("=");
|
|
194
|
+
if (arg.startsWith("--") && equalsIndex > 2) {
|
|
195
|
+
return [arg.slice(0, equalsIndex), arg.slice(equalsIndex + 1)];
|
|
196
|
+
}
|
|
197
|
+
return [arg, undefined];
|
|
198
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { run } from "./run.js";
|
|
4
|
+
|
|
5
|
+
process.exitCode = await run(process.argv.slice(2), {
|
|
6
|
+
out: (text) => {
|
|
7
|
+
process.stdout.write(text);
|
|
8
|
+
},
|
|
9
|
+
err: (text) => {
|
|
10
|
+
process.stderr.write(text);
|
|
11
|
+
},
|
|
12
|
+
});
|