@kongyo2/ts-comment-scanner 1.4.0 → 1.6.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 +8 -3
- package/README.md +8 -3
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +6 -2
- package/dist/args.js.map +1 -1
- package/dist/directives.d.ts +11 -0
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +60 -23
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +20 -4
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +150 -70
- package/dist/files.js.map +1 -1
- package/dist/git.d.ts.map +1 -1
- package/dist/git.js +7 -2
- package/dist/git.js.map +1 -1
- package/dist/lines.d.ts +18 -0
- package/dist/lines.d.ts.map +1 -0
- package/dist/lines.js +40 -0
- package/dist/lines.js.map +1 -0
- package/dist/remove.d.ts +11 -2
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +185 -104
- package/dist/remove.js.map +1 -1
- package/dist/report.d.ts.map +1 -1
- package/dist/report.js +2 -2
- package/dist/report.js.map +1 -1
- package/dist/run.d.ts +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +54 -18
- package/dist/run.js.map +1 -1
- package/package.json +1 -1
- package/src/args.ts +5 -2
- package/src/directives.ts +57 -24
- package/src/files.ts +177 -72
- package/src/git.ts +6 -2
- package/src/lines.ts +43 -0
- package/src/remove.ts +194 -107
- package/src/report.ts +2 -2
- package/src/run.ts +51 -17
package/src/lines.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Line handling shared by the scanner's consumers. Everything here counts
|
|
3
|
+
* lines the way the TypeScript scanner does: LF, CR, U+2028 and U+2029 each
|
|
4
|
+
* end a line, and CRLF is a single break.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** Matches one ECMAScript line terminator character. */
|
|
8
|
+
export const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
|
|
9
|
+
|
|
10
|
+
/** Matches one line break: CRLF as a unit, else any single terminator. */
|
|
11
|
+
export const LINE_BREAK = /\r\n|[\n\r\u2028\u2029]/;
|
|
12
|
+
|
|
13
|
+
/** True when the text holds nothing but whitespace (a byte-order mark included). */
|
|
14
|
+
export function isBlank(text: string): boolean {
|
|
15
|
+
return /^\s*$/.test(text);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Splits on every line break, so exotic terminators never leak into single-line output. */
|
|
19
|
+
export function splitLines(text: string): string[] {
|
|
20
|
+
return text.split(LINE_BREAK);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The literal last line of the text, exactly as tsc slices a block comment's closing line. */
|
|
24
|
+
export function lastLine(text: string): string {
|
|
25
|
+
const lines = splitLines(text);
|
|
26
|
+
return lines[lines.length - 1] as string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Start offset of every line, numbered the way the scanner numbers them (line 1 starts at index 0). */
|
|
30
|
+
export function lineStartOffsets(source: string): number[] {
|
|
31
|
+
const starts = [0];
|
|
32
|
+
for (let index = 0; index < source.length; index += 1) {
|
|
33
|
+
const char = source[index] as string;
|
|
34
|
+
if (char === "\r") {
|
|
35
|
+
// A CRLF pair is one break: skip the LF so it does not open a second line.
|
|
36
|
+
if (source[index + 1] === "\n") index += 1;
|
|
37
|
+
starts.push(index + 1);
|
|
38
|
+
} else if (char === "\n" || char === "\u2028" || char === "\u2029") {
|
|
39
|
+
starts.push(index + 1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return starts;
|
|
43
|
+
}
|
package/src/remove.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isLegalComment } from "./directives.js";
|
|
1
|
+
import { activePositionalDirectives, isLegalComment, type DirectivePlacement } from "./directives.js";
|
|
2
|
+
import { LINE_BREAK, LINE_TERMINATOR, isBlank, lineStartOffsets } from "./lines.js";
|
|
2
3
|
import { scanComments } from "./scanner.js";
|
|
3
4
|
import type { Comment } from "./types.js";
|
|
4
5
|
|
|
@@ -18,7 +19,9 @@ export interface RemoveResult {
|
|
|
18
19
|
removed: Comment[];
|
|
19
20
|
/**
|
|
20
21
|
* Comments kept because they are protected: directives, license headers,
|
|
21
|
-
*
|
|
22
|
+
* comments whose removal would shift code under a next-line directive, and
|
|
23
|
+
* comments whose removal would move a position-dependent directive
|
|
24
|
+
* (prettier's first-comment pragma, Bun's file-start marker) into effect.
|
|
22
25
|
*/
|
|
23
26
|
kept: Comment[];
|
|
24
27
|
/** Comments excluded from removal by the `shouldRemove` predicate. */
|
|
@@ -33,17 +36,22 @@ export interface RemoveResult {
|
|
|
33
36
|
* - AST-based ranges: strings, template literals and regexes are never affected.
|
|
34
37
|
* - Directives (`@ts-expect-error`, `eslint-disable`, ...) and license headers are
|
|
35
38
|
* kept by default, since deleting them changes build/lint behaviour.
|
|
39
|
+
* - Surviving comments keep their directive semantics: when deleting a leading
|
|
40
|
+
* comment would newly activate a position-dependent directive in a kept
|
|
41
|
+
* comment (prettier's `@format` docblock pragma counts only as the file's
|
|
42
|
+
* first comment, Bun's `// @bun` only at the very start), the leading
|
|
43
|
+
* comment is kept instead.
|
|
36
44
|
* - A space is inserted where removing a block comment would merge two tokens,
|
|
37
45
|
* and a line break is kept where the comment acted as one (ASI stays intact).
|
|
38
|
-
* - A leading byte-order mark is preserved.
|
|
46
|
+
* - A leading byte-order mark is preserved. Reported positions index into the
|
|
47
|
+
* `source` string exactly as `scanComments(source)` reports them (a BOM
|
|
48
|
+
* counts as its first character).
|
|
39
49
|
* - The result is re-scanned; if the remaining comments do not match the kept
|
|
40
50
|
* set, an error is thrown instead of returning corrupted output.
|
|
41
51
|
*/
|
|
42
52
|
export function removeComments(source: string, options: RemoveOptions = {}): RemoveResult {
|
|
43
53
|
const jsx = options.jsx === true;
|
|
44
|
-
const
|
|
45
|
-
const body = bom === "" ? source : source.slice(bom.length);
|
|
46
|
-
const comments = scanComments(body, { jsx });
|
|
54
|
+
const comments = scanComments(source, { jsx });
|
|
47
55
|
|
|
48
56
|
let removed: Comment[] = [];
|
|
49
57
|
const kept: Comment[] = [];
|
|
@@ -58,30 +66,116 @@ export function removeComments(source: string, options: RemoveOptions = {}): Rem
|
|
|
58
66
|
}
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
for (;;) {
|
|
70
|
+
removed = shieldNextLineDirectives(source, removed, kept, skipped);
|
|
62
71
|
|
|
63
|
-
|
|
64
|
-
|
|
72
|
+
if (removed.length === 0) {
|
|
73
|
+
return { code: source, removed, kept, skipped, changed: false };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const code = spliceKeepingBom(source, removed);
|
|
77
|
+
const survivor = firstChangedSurvivor(source, code, scanComments(code, { jsx }), kept, skipped);
|
|
78
|
+
if (survivor === undefined) {
|
|
79
|
+
return { code, removed, kept, skipped, changed: true };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// A surviving comment changed meaning: with everything before it deleted
|
|
83
|
+
// it became the file's first comment (or its very first bytes), turning a
|
|
84
|
+
// previously inert positional directive live. Re-protect the removal
|
|
85
|
+
// candidate closest before it and try again: that comment then keeps
|
|
86
|
+
// holding the survivor away from the activating position, exactly as the
|
|
87
|
+
// input did. (Looping, because the re-protected comment can itself be a
|
|
88
|
+
// next-line directive that shields further lines.)
|
|
89
|
+
const blocker = closestRemovalBefore(removed, survivor);
|
|
90
|
+
if (blocker === undefined) {
|
|
91
|
+
throw unexpectedResult();
|
|
92
|
+
}
|
|
93
|
+
removed = removed.filter((comment) => comment !== blocker);
|
|
94
|
+
kept.push(blocker);
|
|
95
|
+
kept.sort((a, b) => a.start - b.start);
|
|
65
96
|
}
|
|
97
|
+
}
|
|
66
98
|
|
|
67
|
-
|
|
99
|
+
function unexpectedResult(): Error {
|
|
100
|
+
return new Error("comment removal produced an unexpected result; refusing to continue");
|
|
101
|
+
}
|
|
68
102
|
|
|
69
|
-
|
|
70
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Compares the spliced output's comments against the comments meant to
|
|
105
|
+
* survive, in document order (removal never reorders them). Throws when the
|
|
106
|
+
* texts themselves differ (the output would be corrupted) and returns the
|
|
107
|
+
* first surviving comment whose directive semantics changed, or undefined
|
|
108
|
+
* when the output is exactly as expected. Besides the canonical directive
|
|
109
|
+
* name, the position-dependent pragmas are compared on their own: a comment
|
|
110
|
+
* can carry an inert `@format` underneath the directive an earlier rule
|
|
111
|
+
* already named (an eslint config block, say), and that pragma going live
|
|
112
|
+
* must not hide behind the unchanged name.
|
|
113
|
+
*/
|
|
114
|
+
function firstChangedSurvivor(
|
|
115
|
+
source: string,
|
|
116
|
+
code: string,
|
|
117
|
+
actual: Comment[],
|
|
118
|
+
kept: Comment[],
|
|
119
|
+
skipped: Comment[],
|
|
120
|
+
): Comment | undefined {
|
|
121
|
+
const expected = [...kept, ...skipped].sort((a, b) => a.start - b.start);
|
|
122
|
+
if (actual.length !== expected.length) {
|
|
123
|
+
throw unexpectedResult();
|
|
124
|
+
}
|
|
125
|
+
for (const [index, comment] of expected.entries()) {
|
|
126
|
+
const survivor = actual[index] as Comment;
|
|
127
|
+
if (survivor.text !== comment.text) {
|
|
128
|
+
throw unexpectedResult();
|
|
129
|
+
}
|
|
130
|
+
if (survivor.directive !== comment.directive) return comment;
|
|
131
|
+
const before = activePositionalDirectives(comment.kind, comment.text, positionalPlacement(source, comment));
|
|
132
|
+
const after = activePositionalDirectives(survivor.kind, survivor.text, positionalPlacement(code, survivor));
|
|
133
|
+
if (before.join("\n") !== after.join("\n")) return comment;
|
|
71
134
|
}
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* The placement bits the position-dependent directives care about, derived
|
|
140
|
+
* without a parse: the comment is the file's first comment when nothing but
|
|
141
|
+
* whitespace (or a shebang) precedes it — any earlier comment's text keeps
|
|
142
|
+
* the slice non-blank — and it sits at the file start when it begins at
|
|
143
|
+
* offset zero. Header status needs the token structure, but removing
|
|
144
|
+
* comments never moves a comment across the first token, so the positional
|
|
145
|
+
* check does not consult it.
|
|
146
|
+
*/
|
|
147
|
+
function positionalPlacement(text: string, comment: Comment): DirectivePlacement {
|
|
148
|
+
const shebangLength = /^#!.*/.exec(text)?.[0].length ?? 0;
|
|
149
|
+
return {
|
|
150
|
+
header: false,
|
|
151
|
+
firstComment: isBlank(text.slice(shebangLength, comment.start)),
|
|
152
|
+
fileStart: comment.start === 0,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
72
155
|
|
|
73
|
-
|
|
156
|
+
/** The removal candidate that starts closest before the comment, if any. */
|
|
157
|
+
function closestRemovalBefore(removals: readonly Comment[], comment: Comment): Comment | undefined {
|
|
158
|
+
let closest: Comment | undefined;
|
|
159
|
+
for (const removal of removals) {
|
|
160
|
+
if (removal.start < comment.start) closest = removal;
|
|
161
|
+
}
|
|
162
|
+
return closest;
|
|
74
163
|
}
|
|
75
164
|
|
|
76
165
|
/**
|
|
77
|
-
*
|
|
78
|
-
*
|
|
166
|
+
* Splices the removals out of the source, restoring the byte-order mark when
|
|
167
|
+
* dropping a comment-only first line took it along: the mark is an encoding
|
|
168
|
+
* artifact, not line content.
|
|
79
169
|
*/
|
|
80
|
-
function
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
170
|
+
function spliceKeepingBom(source: string, removals: readonly Comment[]): string {
|
|
171
|
+
if (!source.startsWith("\uFEFF")) return splice(source, removals);
|
|
172
|
+
// The splicing itself works on the BOM-less body (with ranges shifted to
|
|
173
|
+
// match) so its line handling never sees the mark, then the mark is
|
|
174
|
+
// re-attached; this keeps `\uFEFF// gone\n...` from losing the mark with
|
|
175
|
+
// the dropped line.
|
|
176
|
+
const body = source.slice(1);
|
|
177
|
+
const shifted = removals.map((comment) => ({ ...comment, start: comment.start - 1, end: comment.end - 1 }));
|
|
178
|
+
return `\uFEFF${splice(body, shifted)}`;
|
|
85
179
|
}
|
|
86
180
|
|
|
87
181
|
function isProtected(comment: Comment, options: RemoveOptions): boolean {
|
|
@@ -182,20 +276,6 @@ function vanishingLineGroup(
|
|
|
182
276
|
return group;
|
|
183
277
|
}
|
|
184
278
|
|
|
185
|
-
/** Start offset of every line, numbered the way the scanner numbers them. */
|
|
186
|
-
function lineStartOffsets(source: string): number[] {
|
|
187
|
-
const starts = [0];
|
|
188
|
-
for (let index = 0; index < source.length; index += 1) {
|
|
189
|
-
const char = source[index] as string;
|
|
190
|
-
if (char === "\r") {
|
|
191
|
-
starts.push(source[index + 1] === "\n" ? (index += 1) + 1 : index + 1);
|
|
192
|
-
} else if (char === "\n" || char === "\u2028" || char === "\u2029") {
|
|
193
|
-
starts.push(index + 1);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return starts;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
279
|
// These suppressions only apply to the following node/line when they stand on
|
|
200
280
|
// their own line — trailing after code they target (at most) their own line.
|
|
201
281
|
// Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
|
|
@@ -210,65 +290,65 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
|
|
|
210
290
|
"nosemgrep",
|
|
211
291
|
]);
|
|
212
292
|
|
|
213
|
-
//
|
|
214
|
-
//
|
|
293
|
+
// Directives that target the line or node directly below them. File- and
|
|
294
|
+
// range-scoped pragmas (`c8 ignore start`, `istanbul ignore file`, ...) are
|
|
295
|
+
// absent: they do not target the following line, so they never shield it.
|
|
296
|
+
const NEXT_LINE_DIRECTIVES = new Set([
|
|
297
|
+
"@ts-ignore",
|
|
298
|
+
"@ts-expect-error",
|
|
299
|
+
"biome-ignore",
|
|
300
|
+
"deno-lint-ignore",
|
|
301
|
+
"deno-fmt-ignore",
|
|
302
|
+
"deno-coverage-ignore",
|
|
303
|
+
"prettier-ignore",
|
|
304
|
+
"oxfmt-ignore",
|
|
305
|
+
"dprint-ignore",
|
|
306
|
+
"rome-ignore",
|
|
307
|
+
"istanbul-ignore",
|
|
308
|
+
"istanbul-ignore-next",
|
|
309
|
+
"istanbul-ignore-if",
|
|
310
|
+
"istanbul-ignore-else",
|
|
311
|
+
"c8-ignore",
|
|
312
|
+
"c8-ignore-next",
|
|
313
|
+
"c8-ignore-if",
|
|
314
|
+
"c8-ignore-else",
|
|
315
|
+
"v8-ignore",
|
|
316
|
+
"v8-ignore-next",
|
|
317
|
+
"v8-ignore-if",
|
|
318
|
+
"v8-ignore-else",
|
|
319
|
+
"node:coverage-ignore",
|
|
320
|
+
// Directives that hang off the node or line directly below them: a
|
|
321
|
+
// whole-line comment between them and their target would change what
|
|
322
|
+
// they apply to, so the line below stays shielded.
|
|
323
|
+
"ts-prune-ignore-next",
|
|
324
|
+
"million-ignore",
|
|
325
|
+
"@million-skip",
|
|
326
|
+
"@million-jsx-skip",
|
|
327
|
+
"@deno-types",
|
|
328
|
+
"@ts-types",
|
|
329
|
+
"deepcode-ignore",
|
|
330
|
+
"skipcq",
|
|
331
|
+
"lgtm",
|
|
332
|
+
"codeql",
|
|
333
|
+
"nosemgrep",
|
|
334
|
+
"no-dd-sa",
|
|
335
|
+
"datadog-disable",
|
|
336
|
+
"noinspection",
|
|
337
|
+
"language-injection",
|
|
338
|
+
"@next-codemod-error",
|
|
339
|
+
"@next-codemod-ignore",
|
|
340
|
+
"$FlowFixMe",
|
|
341
|
+
"$FlowExpectedError",
|
|
342
|
+
"$FlowIssue",
|
|
343
|
+
"$FlowIgnore",
|
|
344
|
+
"pragma-allowlist-nextline-secret",
|
|
345
|
+
"cspell:disable-next",
|
|
346
|
+
"GraphQL",
|
|
347
|
+
"HTML",
|
|
348
|
+
]);
|
|
349
|
+
|
|
215
350
|
function isNextLineDirective(name: string): boolean {
|
|
216
|
-
return (
|
|
217
|
-
name.endsWith("-next-line") ||
|
|
218
|
-
[
|
|
219
|
-
"@ts-ignore",
|
|
220
|
-
"@ts-expect-error",
|
|
221
|
-
"biome-ignore",
|
|
222
|
-
"deno-lint-ignore",
|
|
223
|
-
"deno-fmt-ignore",
|
|
224
|
-
"deno-coverage-ignore",
|
|
225
|
-
"prettier-ignore",
|
|
226
|
-
"oxfmt-ignore",
|
|
227
|
-
"dprint-ignore",
|
|
228
|
-
"rome-ignore",
|
|
229
|
-
"istanbul-ignore",
|
|
230
|
-
"istanbul-ignore-next",
|
|
231
|
-
"istanbul-ignore-if",
|
|
232
|
-
"istanbul-ignore-else",
|
|
233
|
-
"c8-ignore",
|
|
234
|
-
"c8-ignore-next",
|
|
235
|
-
"c8-ignore-if",
|
|
236
|
-
"c8-ignore-else",
|
|
237
|
-
"v8-ignore",
|
|
238
|
-
"v8-ignore-next",
|
|
239
|
-
"v8-ignore-if",
|
|
240
|
-
"v8-ignore-else",
|
|
241
|
-
"node:coverage-ignore",
|
|
242
|
-
// Directives that hang off the node or line directly below them: a
|
|
243
|
-
// whole-line comment between them and their target would change what
|
|
244
|
-
// they apply to, so the line below stays shielded.
|
|
245
|
-
"ts-prune-ignore-next",
|
|
246
|
-
"million-ignore",
|
|
247
|
-
"@million-skip",
|
|
248
|
-
"@million-jsx-skip",
|
|
249
|
-
"@deno-types",
|
|
250
|
-
"@ts-types",
|
|
251
|
-
"deepcode-ignore",
|
|
252
|
-
"skipcq",
|
|
253
|
-
"lgtm",
|
|
254
|
-
"codeql",
|
|
255
|
-
"nosemgrep",
|
|
256
|
-
"no-dd-sa",
|
|
257
|
-
"datadog-disable",
|
|
258
|
-
"noinspection",
|
|
259
|
-
"language-injection",
|
|
260
|
-
"@next-codemod-error",
|
|
261
|
-
"@next-codemod-ignore",
|
|
262
|
-
"$FlowFixMe",
|
|
263
|
-
"$FlowExpectedError",
|
|
264
|
-
"$FlowIssue",
|
|
265
|
-
"$FlowIgnore",
|
|
266
|
-
"pragma-allowlist-nextline-secret",
|
|
267
|
-
"cspell:disable-next",
|
|
268
|
-
"GraphQL",
|
|
269
|
-
"HTML",
|
|
270
|
-
].includes(name)
|
|
271
|
-
);
|
|
351
|
+
return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
|
|
272
352
|
}
|
|
273
353
|
|
|
274
354
|
// The coverage pragmas that actually parse a line count after `ignore next`
|
|
@@ -304,11 +384,18 @@ function occupiesWholeLines(source: string, comment: Comment): boolean {
|
|
|
304
384
|
return true;
|
|
305
385
|
}
|
|
306
386
|
|
|
307
|
-
const isBlank = (text: string): boolean => /^\s*$/.test(text);
|
|
308
|
-
|
|
309
387
|
const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
|
|
310
388
|
|
|
311
|
-
|
|
389
|
+
// A global copy of the line-break matcher, so exec() can resume from a removal's end offset.
|
|
390
|
+
const NEXT_LINE_BREAK = new RegExp(LINE_BREAK.source, "g");
|
|
391
|
+
|
|
392
|
+
/** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
|
|
393
|
+
function lastLineBreakEnd(chunk: string): number {
|
|
394
|
+
for (let index = chunk.length - 1; index >= 0; index -= 1) {
|
|
395
|
+
if (LINE_TERMINATOR.test(chunk[index] as string)) return index + 1;
|
|
396
|
+
}
|
|
397
|
+
return -1;
|
|
398
|
+
}
|
|
312
399
|
|
|
313
400
|
/**
|
|
314
401
|
* Splices sorted, non-overlapping comment ranges out of the source, tidying lines.
|
|
@@ -326,8 +413,8 @@ function splice(source: string, removals: readonly Comment[]): string {
|
|
|
326
413
|
const push = (chunk: string): void => {
|
|
327
414
|
if (chunk === "") return;
|
|
328
415
|
segments.push(chunk);
|
|
329
|
-
const
|
|
330
|
-
lineIsBlank =
|
|
416
|
+
const breakEnd = lastLineBreakEnd(chunk);
|
|
417
|
+
lineIsBlank = breakEnd === -1 ? lineIsBlank && isBlank(chunk) : isBlank(chunk.slice(breakEnd));
|
|
331
418
|
lastChar = chunk.slice(-1);
|
|
332
419
|
};
|
|
333
420
|
|
|
@@ -335,14 +422,14 @@ function splice(source: string, removals: readonly Comment[]): string {
|
|
|
335
422
|
const dropCurrentLine = (): void => {
|
|
336
423
|
while (segments.length > 0) {
|
|
337
424
|
const last = segments[segments.length - 1] as string;
|
|
338
|
-
const
|
|
339
|
-
if (
|
|
425
|
+
const breakEnd = lastLineBreakEnd(last);
|
|
426
|
+
if (breakEnd === -1) {
|
|
340
427
|
segments.pop();
|
|
341
428
|
continue;
|
|
342
429
|
}
|
|
343
|
-
segments[segments.length - 1] = last.slice(0,
|
|
430
|
+
segments[segments.length - 1] = last.slice(0, breakEnd);
|
|
344
431
|
lineIsBlank = true;
|
|
345
|
-
lastChar =
|
|
432
|
+
lastChar = last.charAt(breakEnd - 1);
|
|
346
433
|
return;
|
|
347
434
|
}
|
|
348
435
|
lineIsBlank = true;
|
|
@@ -370,11 +457,11 @@ function splice(source: string, removals: readonly Comment[]): string {
|
|
|
370
457
|
push(source.slice(cursor, comment.start));
|
|
371
458
|
cursor = comment.end;
|
|
372
459
|
|
|
373
|
-
|
|
374
|
-
const
|
|
375
|
-
const
|
|
376
|
-
const
|
|
377
|
-
const restContent =
|
|
460
|
+
NEXT_LINE_BREAK.lastIndex = cursor;
|
|
461
|
+
const lineBreak = NEXT_LINE_BREAK.exec(source);
|
|
462
|
+
const contentEnd = lineBreak === null ? source.length : lineBreak.index;
|
|
463
|
+
const lineEnd = lineBreak === null ? source.length : lineBreak.index + lineBreak[0].length;
|
|
464
|
+
const restContent = source.slice(cursor, contentEnd);
|
|
378
465
|
|
|
379
466
|
if (lineIsBlank && isBlank(restContent)) {
|
|
380
467
|
// The comment occupied whole line(s): drop them entirely.
|
|
@@ -383,7 +470,7 @@ function splice(source: string, removals: readonly Comment[]): string {
|
|
|
383
470
|
} else if (isBlank(restContent)) {
|
|
384
471
|
// Trailing comment after code: trim the gap, keep the line break.
|
|
385
472
|
trimLineEnd();
|
|
386
|
-
push(
|
|
473
|
+
push(source.slice(contentEnd, lineEnd));
|
|
387
474
|
cursor = lineEnd;
|
|
388
475
|
} else if (!lineIsBlank && LINE_TERMINATOR.test(comment.text)) {
|
|
389
476
|
// Code on both sides of a block comment that spans lines. Per ECMA-262
|
package/src/report.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Buffer } from "node:buffer";
|
|
2
|
+
import { splitLines } from "./lines.js";
|
|
2
3
|
import type { Comment, FileScanResult } from "./types.js";
|
|
3
4
|
|
|
4
5
|
/** Formats `3 comments`, `1 file`, ... with naive pluralization. */
|
|
@@ -38,8 +39,7 @@ export function formatText(results: FileScanResult[]): string {
|
|
|
38
39
|
function preview(text: string): string {
|
|
39
40
|
// Split on every ECMAScript line terminator so exotic breaks (lone \r,
|
|
40
41
|
// U+2028/U+2029) cannot leak raw control characters into terminal output.
|
|
41
|
-
return text
|
|
42
|
-
.split(/\r\n|[\n\r\u2028\u2029]/)
|
|
42
|
+
return splitLines(text)
|
|
43
43
|
.map((line) => line.trim())
|
|
44
44
|
.join(" ")
|
|
45
45
|
.trim();
|
package/src/run.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { parseArgs, UsageError, type CliOptions, type DirectiveMode } from "./ar
|
|
|
5
5
|
import {
|
|
6
6
|
collectFiles,
|
|
7
7
|
encodeFileText,
|
|
8
|
+
foldPathCase,
|
|
8
9
|
isJsxFile,
|
|
9
10
|
mapLimit,
|
|
10
11
|
readFileText,
|
|
@@ -53,8 +54,11 @@ General:
|
|
|
53
54
|
-v, --version Print the version number
|
|
54
55
|
|
|
55
56
|
Paths default to the current directory. Directories are scanned recursively,
|
|
56
|
-
skipping node_modules and .git.
|
|
57
|
-
|
|
57
|
+
skipping node_modules and .git. Ignore globs without a slash match file names,
|
|
58
|
+
and a trailing slash (dist/) restricts a glob to directories. JavaScript files
|
|
59
|
+
(.js, .jsx, .mjs, .cjs) added with --ext are parsed with JSX enabled. Removal
|
|
60
|
+
keeps directives and license headers unless explicitly requested, so builds
|
|
61
|
+
and linters keep working.
|
|
58
62
|
|
|
59
63
|
--diff narrows any scan or removal to files changed in git: a single revision
|
|
60
64
|
compares the working tree against it (HEAD covers all uncommitted work,
|
|
@@ -136,20 +140,45 @@ async function collectTargets(options: CliOptions, collectOptions: CollectOption
|
|
|
136
140
|
|
|
137
141
|
const first = resolve(options.paths[0] as string);
|
|
138
142
|
const anchor = (await stat(first)).isDirectory() ? first : dirname(first);
|
|
139
|
-
|
|
140
|
-
//
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
|
|
143
|
+
// Both the spelling git reports and the real file it refers to count as
|
|
144
|
+
// changed: the collector keeps one spelling per real file, so a changed
|
|
145
|
+
// symlink may have been remembered under its target's path (or a scanned
|
|
146
|
+
// alias may point at the changed file), and either spelling names the same
|
|
147
|
+
// content. Broken links (a symlink re-pointed at nothing) only match by
|
|
148
|
+
// their reported spelling.
|
|
149
|
+
// Paths are folded the way the platform compares them: git reports the case
|
|
150
|
+
// a file was tracked with, so on Windows `Foo.ts` must stay in scope when
|
|
151
|
+
// the scan found it as `foo.ts`.
|
|
152
|
+
const changed = new Set<string>();
|
|
153
|
+
for (const path of await changedFiles(options.diff, anchor)) {
|
|
154
|
+
changed.add(foldPathCase(path));
|
|
155
|
+
const real = tryRealpath(path);
|
|
156
|
+
if (real !== undefined) changed.add(foldPathCase(real));
|
|
157
|
+
}
|
|
158
|
+
// Realpath only the directory part first: spellings through symlinked
|
|
159
|
+
// directories then compare equal while a tracked symlink still matches the
|
|
160
|
+
// path git reports it at; the fully dereferenced path is the fallback. A
|
|
161
|
+
// directory that vanished since the walk is compared as spelled.
|
|
162
|
+
return files.filter((file) => {
|
|
163
|
+
const directory = tryRealpath(dirname(file)) ?? dirname(file);
|
|
164
|
+
if (changed.has(foldPathCase(join(directory, basename(file))))) return true;
|
|
165
|
+
const real = tryRealpath(file);
|
|
166
|
+
return real !== undefined && changed.has(foldPathCase(real));
|
|
167
|
+
});
|
|
144
168
|
}
|
|
145
169
|
|
|
146
170
|
/**
|
|
147
|
-
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
171
|
+
* Native realpath — changedFiles resolves the repository root with the
|
|
172
|
+
* (native) promises realpath, and on Windows only the native calls expand
|
|
173
|
+
* 8.3 short names like RUNNER~1, so every path compared against it must be
|
|
174
|
+
* expanded the same way. Undefined when the path cannot be resolved.
|
|
150
175
|
*/
|
|
151
|
-
function
|
|
152
|
-
|
|
176
|
+
function tryRealpath(path: string): string | undefined {
|
|
177
|
+
try {
|
|
178
|
+
return realpathSync.native(path);
|
|
179
|
+
} catch {
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
153
182
|
}
|
|
154
183
|
|
|
155
184
|
/** True when -h/--help appears before any `--` separator. */
|
|
@@ -242,16 +271,21 @@ function renderRemoval(removals: FileRemoval[], options: CliOptions): string {
|
|
|
242
271
|
const totalSkipped = removals.reduce((sum, entry) => sum + entry.skipped.length, 0);
|
|
243
272
|
const changedEntries = removals.filter((entry) => entry.removed.length > 0);
|
|
244
273
|
const keptNote = totalKept > 0 ? ` Kept ${count(totalKept, "protected comment")}.` : "";
|
|
245
|
-
//
|
|
246
|
-
// counts; say how many were left untouched
|
|
247
|
-
//
|
|
248
|
-
|
|
274
|
+
// Comments held out of scope by --skip-directives / --only-directives are
|
|
275
|
+
// invisible in the removal counts; say how many were left untouched, naming
|
|
276
|
+
// the flag actually responsible, so "removed N" is not mistaken for "the
|
|
277
|
+
// files are now comment-free".
|
|
278
|
+
const skippedReason = options.directives === "only" ? "outside --only-directives" : "--skip-directives";
|
|
279
|
+
const skippedNote = totalSkipped > 0 ? ` Skipped ${count(totalSkipped, "comment")} (${skippedReason}).` : "";
|
|
249
280
|
|
|
250
281
|
if (options.format === "json") {
|
|
251
282
|
return JSON.stringify(
|
|
252
283
|
{
|
|
253
284
|
summary: {
|
|
254
|
-
files
|
|
285
|
+
// `files` counts the entries of `files` below, like the scan JSON;
|
|
286
|
+
// `changedFiles` is how many of them actually had comments removed.
|
|
287
|
+
files: removals.length,
|
|
288
|
+
changedFiles: changedEntries.length,
|
|
255
289
|
removed: totalRemoved,
|
|
256
290
|
kept: totalKept,
|
|
257
291
|
skipped: totalSkipped,
|