@kongyo2/ts-comment-scanner 1.5.0 → 1.7.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/src/remove.ts CHANGED
@@ -1,4 +1,5 @@
1
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
 
@@ -275,20 +276,6 @@ function vanishingLineGroup(
275
276
  return group;
276
277
  }
277
278
 
278
- /** Start offset of every line, numbered the way the scanner numbers them. */
279
- function lineStartOffsets(source: string): number[] {
280
- const starts = [0];
281
- for (let index = 0; index < source.length; index += 1) {
282
- const char = source[index] as string;
283
- if (char === "\r") {
284
- starts.push(source[index + 1] === "\n" ? (index += 1) + 1 : index + 1);
285
- } else if (char === "\n" || char === "\u2028" || char === "\u2029") {
286
- starts.push(index + 1);
287
- }
288
- }
289
- return starts;
290
- }
291
-
292
279
  // These suppressions only apply to the following node/line when they stand on
293
280
  // their own line — trailing after code they target (at most) their own line.
294
281
  // Other next-line directives (`@ts-ignore`, `no-dd-sa`, ...) target the next
@@ -303,65 +290,65 @@ const TRAILING_TARGETS_OWN_LINE = new Set([
303
290
  "nosemgrep",
304
291
  ]);
305
292
 
306
- // File- and range-scoped pragmas (`c8 ignore start`, `istanbul ignore file`,
307
- // ...) do not target the following line, so they never shield it.
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
+
308
350
  function isNextLineDirective(name: string): boolean {
309
- return (
310
- name.endsWith("-next-line") ||
311
- [
312
- "@ts-ignore",
313
- "@ts-expect-error",
314
- "biome-ignore",
315
- "deno-lint-ignore",
316
- "deno-fmt-ignore",
317
- "deno-coverage-ignore",
318
- "prettier-ignore",
319
- "oxfmt-ignore",
320
- "dprint-ignore",
321
- "rome-ignore",
322
- "istanbul-ignore",
323
- "istanbul-ignore-next",
324
- "istanbul-ignore-if",
325
- "istanbul-ignore-else",
326
- "c8-ignore",
327
- "c8-ignore-next",
328
- "c8-ignore-if",
329
- "c8-ignore-else",
330
- "v8-ignore",
331
- "v8-ignore-next",
332
- "v8-ignore-if",
333
- "v8-ignore-else",
334
- "node:coverage-ignore",
335
- // Directives that hang off the node or line directly below them: a
336
- // whole-line comment between them and their target would change what
337
- // they apply to, so the line below stays shielded.
338
- "ts-prune-ignore-next",
339
- "million-ignore",
340
- "@million-skip",
341
- "@million-jsx-skip",
342
- "@deno-types",
343
- "@ts-types",
344
- "deepcode-ignore",
345
- "skipcq",
346
- "lgtm",
347
- "codeql",
348
- "nosemgrep",
349
- "no-dd-sa",
350
- "datadog-disable",
351
- "noinspection",
352
- "language-injection",
353
- "@next-codemod-error",
354
- "@next-codemod-ignore",
355
- "$FlowFixMe",
356
- "$FlowExpectedError",
357
- "$FlowIssue",
358
- "$FlowIgnore",
359
- "pragma-allowlist-nextline-secret",
360
- "cspell:disable-next",
361
- "GraphQL",
362
- "HTML",
363
- ].includes(name)
364
- );
351
+ return name.endsWith("-next-line") || NEXT_LINE_DIRECTIVES.has(name);
365
352
  }
366
353
 
367
354
  // The coverage pragmas that actually parse a line count after `ignore next`
@@ -397,15 +384,10 @@ function occupiesWholeLines(source: string, comment: Comment): boolean {
397
384
  return true;
398
385
  }
399
386
 
400
- const isBlank = (text: string): boolean => /^\s*$/.test(text);
401
-
402
387
  const isHorizontalSpace = (char: string | undefined): boolean => char === " " || char === "\t";
403
388
 
404
- const LINE_TERMINATOR = /[\n\r\u2028\u2029]/;
405
-
406
- // Every ECMAScript line break the way the scanner counts them: CRLF as one
407
- // break, then lone LF / CR and the U+2028/U+2029 separators.
408
- const NEXT_LINE_BREAK = /\r\n|[\n\r\u2028\u2029]/g;
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");
409
391
 
410
392
  /** End offset (exclusive) of the last line break in the chunk, or -1 when it has none. */
411
393
  function lastLineBreakEnd(chunk: string): number {
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. Removal keeps directives and license headers
57
- unless explicitly requested, so builds and linters keep working.
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,
@@ -142,31 +146,27 @@ async function collectTargets(options: CliOptions, collectOptions: CollectOption
142
146
  // alias may point at the changed file), and either spelling names the same
143
147
  // content. Broken links (a symlink re-pointed at nothing) only match by
144
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`.
145
152
  const changed = new Set<string>();
146
153
  for (const path of await changedFiles(options.diff, anchor)) {
147
- changed.add(caseFold(path));
154
+ changed.add(foldPathCase(path));
148
155
  const real = tryRealpath(path);
149
- if (real !== undefined) changed.add(caseFold(real));
156
+ if (real !== undefined) changed.add(foldPathCase(real));
150
157
  }
151
158
  // Realpath only the directory part first: spellings through symlinked
152
159
  // directories then compare equal while a tracked symlink still matches the
153
- // path git reports it at; the fully dereferenced path is the fallback.
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.
154
162
  return files.filter((file) => {
155
- if (changed.has(caseFold(join(realpathSync.native(dirname(file)), basename(file))))) return true;
163
+ const directory = tryRealpath(dirname(file)) ?? dirname(file);
164
+ if (changed.has(foldPathCase(join(directory, basename(file))))) return true;
156
165
  const real = tryRealpath(file);
157
- return real !== undefined && changed.has(caseFold(real));
166
+ return real !== undefined && changed.has(foldPathCase(real));
158
167
  });
159
168
  }
160
169
 
161
- /**
162
- * Windows paths are case-insensitive, but git reports the case a file was
163
- * tracked with; folding both sides keeps `Foo.ts` in the --diff scope when
164
- * the scan found it as `foo.ts`.
165
- */
166
- function caseFold(path: string): string {
167
- return process.platform === "win32" ? path.toLowerCase() : path;
168
- }
169
-
170
170
  /**
171
171
  * Native realpath — changedFiles resolves the repository root with the
172
172
  * (native) promises realpath, and on Windows only the native calls expand