@kongyo2/ts-comment-scanner 1.20.0 → 1.22.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.
Files changed (78) hide show
  1. package/README.md +240 -52
  2. package/dist/args.d.ts.map +1 -1
  3. package/dist/args.js +23 -19
  4. package/dist/args.js.map +1 -1
  5. package/dist/astgrep.d.ts +5 -3
  6. package/dist/astgrep.d.ts.map +1 -1
  7. package/dist/astgrep.js +10 -12
  8. package/dist/astgrep.js.map +1 -1
  9. package/dist/containers.d.ts +48 -0
  10. package/dist/containers.d.ts.map +1 -0
  11. package/dist/containers.js +82 -0
  12. package/dist/containers.js.map +1 -0
  13. package/dist/coverage.d.ts +45 -0
  14. package/dist/coverage.d.ts.map +1 -0
  15. package/dist/coverage.js +92 -0
  16. package/dist/coverage.js.map +1 -0
  17. package/dist/directives.d.ts +21 -3
  18. package/dist/directives.d.ts.map +1 -1
  19. package/dist/directives.js +31 -23
  20. package/dist/directives.js.map +1 -1
  21. package/dist/files.d.ts +7 -3
  22. package/dist/files.d.ts.map +1 -1
  23. package/dist/files.js +129 -82
  24. package/dist/files.js.map +1 -1
  25. package/dist/imports.d.ts.map +1 -1
  26. package/dist/imports.js +14 -8
  27. package/dist/imports.js.map +1 -1
  28. package/dist/index.d.ts +1 -1
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +1 -1
  31. package/dist/index.js.map +1 -1
  32. package/dist/jsx.d.ts.map +1 -1
  33. package/dist/jsx.js +5 -6
  34. package/dist/jsx.js.map +1 -1
  35. package/dist/lines.d.ts +24 -5
  36. package/dist/lines.d.ts.map +1 -1
  37. package/dist/lines.js +50 -16
  38. package/dist/lines.js.map +1 -1
  39. package/dist/ranges.d.ts +8 -0
  40. package/dist/ranges.d.ts.map +1 -1
  41. package/dist/ranges.js +12 -0
  42. package/dist/ranges.js.map +1 -1
  43. package/dist/remove.d.ts +18 -14
  44. package/dist/remove.d.ts.map +1 -1
  45. package/dist/remove.js +69 -630
  46. package/dist/remove.js.map +1 -1
  47. package/dist/run.d.ts.map +1 -1
  48. package/dist/run.js +21 -28
  49. package/dist/run.js.map +1 -1
  50. package/dist/scanner.d.ts +34 -0
  51. package/dist/scanner.d.ts.map +1 -1
  52. package/dist/scanner.js +137 -31
  53. package/dist/scanner.js.map +1 -1
  54. package/dist/shields.d.ts +72 -0
  55. package/dist/shields.d.ts.map +1 -0
  56. package/dist/shields.js +624 -0
  57. package/dist/shields.js.map +1 -0
  58. package/dist/splice.d.ts +42 -11
  59. package/dist/splice.d.ts.map +1 -1
  60. package/dist/splice.js +189 -56
  61. package/dist/splice.js.map +1 -1
  62. package/package.json +2 -1
  63. package/src/args.ts +21 -21
  64. package/src/astgrep.ts +11 -13
  65. package/src/containers.ts +128 -0
  66. package/src/coverage.ts +110 -0
  67. package/src/directives.ts +38 -20
  68. package/src/files.ts +169 -91
  69. package/src/imports.ts +15 -8
  70. package/src/index.ts +1 -0
  71. package/src/jsx.ts +5 -6
  72. package/src/lines.ts +55 -14
  73. package/src/ranges.ts +17 -0
  74. package/src/remove.ts +82 -730
  75. package/src/run.ts +22 -34
  76. package/src/scanner.ts +180 -32
  77. package/src/shields.ts +701 -0
  78. package/src/splice.ts +224 -60
package/src/files.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import type { Dirent, Stats } from "node:fs";
3
2
  import { chmod, open, readFile, readdir, realpath, rename, rm, stat } from "node:fs/promises";
4
3
  import { basename, dirname, isAbsolute, join, normalize, relative, resolve, sep } from "node:path";
5
4
  import picomatch from "picomatch";
6
- import { scanComments } from "./scanner.js";
5
+ import { scanComments, type ScanOptions } from "./scanner.js";
7
6
  import type { FileScanResult } from "./types.js";
8
7
 
9
8
  const DEFAULT_EXTENSIONS = [".ts", ".tsx", ".mts", ".cts"];
@@ -30,23 +29,28 @@ export interface CollectOptions {
30
29
  type IgnoreMatcher = (path: string, root: string, isDirectory: boolean) => boolean;
31
30
 
32
31
  export async function collectFiles(inputs: string[], options: CollectOptions = {}): Promise<string[]> {
33
- const extensions = normalizeExtensions(options.extensions ?? DEFAULT_EXTENSIONS);
34
- const ignoreDirs = new Set((options.ignoreDirs ?? DEFAULT_IGNORE_DIRS).map(foldPathCase));
35
- const isIgnored = buildIgnoreMatcher(options.ignore ?? []);
32
+ const patterns = options.ignore ?? [];
33
+ const context: WalkContext = {
34
+ extensions: normalizeExtensions(options.extensions ?? DEFAULT_EXTENSIONS),
35
+ ignoreDirs: new Set((options.ignoreDirs ?? DEFAULT_IGNORE_DIRS).map(foldPathCase)),
36
+ isIgnored: buildIgnoreMatcher(patterns),
37
+ pathBlind: patterns.length === 0,
38
+ };
36
39
  // Keyed by the real path so different spellings of one file (relative vs
37
40
  // absolute, `.` segments, case on Windows, symlink aliases) collapse into a
38
41
  // single entry; the first spelling seen is the one reported.
39
42
  const found = new Map<string, string>();
40
- const remember = (path: string, realPath: string): void => {
41
- const key = canonicalKey(realPath);
42
- if (!found.has(key)) found.set(key, path);
43
+ const remember = (file: FoundFile): void => {
44
+ const key = canonicalKey(file.real);
45
+ if (!found.has(key)) found.set(key, file.path);
43
46
  };
44
47
 
45
- // Walks remember their discoveries mid-traversal; explicitly listed files
46
- // are resolved concurrently but remembered afterwards in input order, so
47
- // which spelling of an aliased file gets reported never depends on
48
- // filesystem timing.
49
- const explicitFiles = new Array<{ path: string; real: string } | undefined>(inputs.length);
48
+ // The inputs are looked up concurrently, and the walks and the explicitly
49
+ // listed files are then remembered in a fixed order — the walks' finds
50
+ // first, the listed files after them in input order — so which spelling of
51
+ // an aliased file gets reported never depends on filesystem timing.
52
+ const roots = new Array<Directory | undefined>(inputs.length);
53
+ const explicitFiles = new Array<FoundFile | undefined>(inputs.length);
50
54
  await Promise.all(
51
55
  inputs.map(async (rawInput, index) => {
52
56
  if (rawInput === "") {
@@ -54,21 +58,26 @@ export async function collectFiles(inputs: string[], options: CollectOptions = {
54
58
  throw new Error("empty path is not a valid input");
55
59
  }
56
60
  const input = normalize(rawInput);
57
- const info = await statInput(input);
61
+ const info = await withFriendlyNotFound(input, () => stat(input));
58
62
  if (info.isDirectory()) {
59
63
  // Directory inputs are themselves subject to the ignored directory
60
64
  // names and the ignore patterns; only explicitly listed files bypass
61
65
  // them.
62
- if (!ignoreDirs.has(foldPathCase(basename(input))) && !isIgnored(input, input, true)) {
63
- await walk(input, { extensions, ignoreDirs, isIgnored, root: input, visited: new Set() }, remember);
66
+ if (entersDirectory(context, input, basename(input), input)) {
67
+ roots[index] = { path: input, root: input, above: undefined };
64
68
  }
65
69
  } else {
66
- explicitFiles[index] = { path: input, real: await realpathInput(input) };
70
+ explicitFiles[index] = { path: input, real: await withFriendlyNotFound(input, () => realpath(input)) };
67
71
  }
68
72
  }),
69
73
  );
74
+ await walk(
75
+ roots.filter((root) => root !== undefined),
76
+ context,
77
+ remember,
78
+ );
70
79
  for (const file of explicitFiles) {
71
- if (file !== undefined) remember(file.path, file.real);
80
+ if (file !== undefined) remember(file);
72
81
  }
73
82
 
74
83
  return [...found.values()].sort();
@@ -88,16 +97,7 @@ export function foldPathCase(path: string): string {
88
97
  return process.platform === "win32" ? path.toLowerCase() : path;
89
98
  }
90
99
 
91
- async function statInput(input: string): Promise<Stats> {
92
- return withFriendlyNotFound(input, () => stat(input));
93
- }
94
-
95
- /** Real path of an explicit input, with the same friendly error statInput gives. */
96
- async function realpathInput(input: string): Promise<string> {
97
- return withFriendlyNotFound(input, () => realpath(input));
98
- }
99
-
100
- /** Runs the lookup, reporting a missing path under the name the user spelled it with. */
100
+ /** Runs the lookup of an explicit input, reporting a missing path under the name the user spelled it with. */
101
101
  async function withFriendlyNotFound<T>(input: string, lookup: () => Promise<T>): Promise<T> {
102
102
  try {
103
103
  return await lookup();
@@ -110,88 +110,148 @@ async function withFriendlyNotFound<T>(input: string, lookup: () => Promise<T>):
110
110
  }
111
111
 
112
112
  /**
113
- * True for the errors a walk may step over: entries that vanished mid-walk
114
- * (ENOENT, ENOTDIR after a directory was replaced) and symlinks that do not
115
- * resolve (broken targets, ELOOP cycles). Anything else — permissions, I/O
116
- * failures — propagates, so a partial scan cannot masquerade as a clean one.
113
+ * Runs a lookup of the walk, undefined when it fails with an error the walk
114
+ * may step over: entries that vanished mid-walk (ENOENT, ENOTDIR after a
115
+ * directory was replaced) and symlinks that do not resolve (broken targets,
116
+ * ELOOP cycles). Anything else — permissions, I/O failures — propagates, so
117
+ * a partial scan cannot masquerade as a clean one.
117
118
  */
118
- function isSkippableWalkError(error: unknown): boolean {
119
- const code = (error as NodeJS.ErrnoException).code;
120
- return code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP";
119
+ async function unlessSkippable<T>(lookup: () => Promise<T>): Promise<T | undefined> {
120
+ try {
121
+ return await lookup();
122
+ } catch (error) {
123
+ const code = (error as NodeJS.ErrnoException).code;
124
+ if (code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP") return undefined;
125
+ throw error;
126
+ }
121
127
  }
122
128
 
123
129
  interface WalkContext {
124
130
  extensions: string[];
125
131
  ignoreDirs: Set<string>;
126
132
  isIgnored: IgnoreMatcher;
133
+ /**
134
+ * No ignore pattern is set, so what a walk finds in a directory is the same
135
+ * whatever path led to it: the extensions and the ignored directory names
136
+ * go by the entries' own names. With patterns, a file one spelling excludes
137
+ * can be in scope under another (`--ignore "a-link/*.ts"` and a second link
138
+ * to the same directory).
139
+ */
140
+ pathBlind: boolean;
141
+ }
142
+
143
+ /** A directory a walk is to list, under the path the walk reached it by. */
144
+ interface Directory {
145
+ path: string;
127
146
  /** Top-level directory this walk started from; ignore globs also match relative to it. */
128
147
  root: string;
129
- /** Real paths of directories already walked, so symlink cycles and diamonds terminate. */
130
- visited: Set<string>;
148
+ /** The real paths of the directories the walk came down through. */
149
+ above: Ancestors | undefined;
131
150
  }
132
151
 
152
+ interface Ancestors {
153
+ real: string;
154
+ above: Ancestors | undefined;
155
+ }
156
+
157
+ /** A file in scope, under the path a walk or an input spelled it with, and its real path. */
158
+ interface FoundFile {
159
+ path: string;
160
+ real: string;
161
+ }
162
+
163
+ /** Whether a walk enters the directory: neither its name nor an ignore pattern excludes it. */
164
+ function entersDirectory(context: WalkContext, path: string, name: string, root: string): boolean {
165
+ return !context.ignoreDirs.has(foldPathCase(name)) && !context.isIgnored(path, root, true);
166
+ }
167
+
168
+ /**
169
+ * Walks the directories level by level, handing every file in scope to
170
+ * `remember`. A level's lookups run concurrently, but what they find is taken
171
+ * in the order of the level — the roots in input order, the entries below
172
+ * them by name — so the result, and the spelling a file reached by several
173
+ * paths is remembered under, are the same on every run.
174
+ *
175
+ * Symlinks are followed. A directory a walk came down through is not entered
176
+ * again, which ends the cycles. Where no path can change what a directory
177
+ * holds (see WalkContext.pathBlind), a directory already listed under another
178
+ * path is not listed again either; with ignore patterns, every path to it is
179
+ * walked, and the files found twice collapse in `remember`.
180
+ */
133
181
  async function walk(
134
- dir: string,
182
+ level: readonly Directory[],
135
183
  context: WalkContext,
136
- remember: (path: string, realPath: string) => void,
184
+ remember: (file: FoundFile) => void,
185
+ listed: Set<string> = new Set(),
137
186
  ): Promise<void> {
138
- let real: string;
139
- try {
140
- real = await realpath(dir);
141
- } catch (error) {
142
- if (isSkippableWalkError(error)) return; // the directory vanished mid-walk
143
- throw error;
187
+ if (level.length === 0) return;
188
+ const reals = await Promise.all(level.map((directory) => unlessSkippable(() => realpath(directory.path))));
189
+ const entered: Array<[Directory, string]> = [];
190
+ for (const [index, directory] of level.entries()) {
191
+ const real = reals[index];
192
+ if (real === undefined) continue; // the directory vanished mid-walk
193
+ if (context.pathBlind ? listed.has(real) : isAbove(directory.above, real)) continue;
194
+ listed.add(real);
195
+ entered.push([directory, real]);
144
196
  }
145
- if (context.visited.has(real)) return;
146
- context.visited.add(real);
197
+ const listings = await Promise.all(entered.map(([directory, real]) => list(directory, real, context)));
198
+ const next: Directory[] = [];
199
+ for (const listing of listings) {
200
+ for (const entry of listing) {
201
+ if ("real" in entry) remember(entry);
202
+ else next.push(entry);
203
+ }
204
+ }
205
+ await walk(next, context, remember, listed);
206
+ }
147
207
 
148
- let entries: Dirent[];
149
- try {
150
- entries = await readdir(dir, { withFileTypes: true });
151
- } catch (error) {
152
- if (isSkippableWalkError(error)) return; // the directory vanished after it resolved
153
- throw error;
208
+ /** Whether the real path is among those of the directories a walk came down through. */
209
+ function isAbove(ancestors: Ancestors | undefined, real: string): boolean {
210
+ for (let ancestor = ancestors; ancestor !== undefined; ancestor = ancestor.above) {
211
+ if (ancestor.real === real) return true;
154
212
  }
213
+ return false;
214
+ }
155
215
 
156
- await Promise.all(
157
- entries.map(async (entry) => {
158
- const full = join(dir, entry.name);
159
- let isDirectory = entry.isDirectory();
160
- let isFile = entry.isFile();
161
- if (entry.isSymbolicLink()) {
162
- // Symlinks are followed, matching how explicitly listed inputs are
163
- // stat()ed; the visited set above keeps cycles from recursing forever.
164
- try {
165
- const info = await stat(full);
216
+ /** The files in scope and the directories to enter among the directory's entries, by name. */
217
+ async function list(directory: Directory, real: string, context: WalkContext): Promise<Array<FoundFile | Directory>> {
218
+ const entries = await unlessSkippable(() => readdir(directory.path, { withFileTypes: true }));
219
+ if (entries === undefined) return []; // the directory vanished after it resolved
220
+ const above = { real, above: directory.above };
221
+ const listing = await Promise.all(
222
+ entries
223
+ .sort((a, b) => (a.name < b.name ? -1 : 1))
224
+ .map(async (entry): Promise<FoundFile | Directory | undefined> => {
225
+ const full = join(directory.path, entry.name);
226
+ let isDirectory = entry.isDirectory();
227
+ let isFile = entry.isFile();
228
+ if (entry.isSymbolicLink()) {
229
+ // Symlinks are followed, matching how explicitly listed inputs are
230
+ // stat()ed.
231
+ const info = await unlessSkippable(() => stat(full));
232
+ if (info === undefined) return undefined; // broken symlink
166
233
  isDirectory = info.isDirectory();
167
234
  isFile = info.isFile();
168
- } catch (error) {
169
- if (isSkippableWalkError(error)) return; // broken symlink
170
- throw error;
171
235
  }
172
- }
173
- if (isDirectory) {
174
- if (!context.ignoreDirs.has(foldPathCase(entry.name)) && !context.isIgnored(full, context.root, true)) {
175
- await walk(full, context, remember);
236
+ if (isDirectory) {
237
+ return entersDirectory(context, full, entry.name, directory.root)
238
+ ? { path: full, root: directory.root, above }
239
+ : undefined;
240
+ }
241
+ if (
242
+ !isFile ||
243
+ !hasExtension(entry.name, context.extensions) ||
244
+ context.isIgnored(full, directory.root, false)
245
+ ) {
246
+ return undefined;
176
247
  }
177
- } else if (
178
- isFile &&
179
- hasExtension(entry.name, context.extensions) &&
180
- !context.isIgnored(full, context.root, false)
181
- ) {
182
248
  // Remembered under its real path, so several symlinks to one target
183
249
  // (or a link plus the target itself) count — and get rewritten — once.
184
- let realFile: string;
185
- try {
186
- realFile = await realpath(full);
187
- } catch (error) {
188
- if (isSkippableWalkError(error)) return; // the file vanished mid-walk
189
- throw error;
190
- }
191
- remember(full, realFile);
192
- }
193
- }),
250
+ const realFile = await unlessSkippable(() => realpath(full));
251
+ return realFile === undefined ? undefined : { path: full, real: realFile }; // the file vanished mid-walk
252
+ }),
194
253
  );
254
+ return listing.filter((entry) => entry !== undefined);
195
255
  }
196
256
 
197
257
  function normalizeExtensions(extensions: string[]): string[] {
@@ -272,16 +332,29 @@ function toPosix(path: string): string {
272
332
  return sep === "/" ? path : path.split(sep).join("/");
273
333
  }
274
334
 
335
+ // The extensions tsc reads as JavaScript, with types in JSDoc.
336
+ const JAVASCRIPT_EXTENSIONS = [".js", ".jsx", ".mjs", ".cjs"];
337
+
275
338
  // TypeScript parses plain JavaScript with the JSX language variant, so `.js`
276
339
  // belongs here: read as TS instead, `<div>http://x</div>` in a React file
277
340
  // becomes a type assertion followed by a phantom `//x` comment.
278
- const JSX_EXTENSIONS = [".tsx", ".jsx", ".js", ".mjs", ".cjs"];
341
+ const JSX_EXTENSIONS = [".tsx", ...JAVASCRIPT_EXTENSIONS];
279
342
 
280
343
  /** True for files whose syntax admits JSX (`.tsx`, `.jsx` and plain JavaScript), case-insensitively. */
281
344
  export function isJsxFile(file: string): boolean {
282
345
  return hasExtension(file, JSX_EXTENSIONS);
283
346
  }
284
347
 
348
+ /** True for JavaScript files (`.js`, `.jsx`, `.mjs`, `.cjs`), case-insensitively. */
349
+ export function isJavaScriptFile(file: string): boolean {
350
+ return hasExtension(file, JAVASCRIPT_EXTENSIONS);
351
+ }
352
+
353
+ /** The scan options a file's extension calls for. */
354
+ export function scanOptionsFor(file: string): ScanOptions {
355
+ return { jsx: isJsxFile(file), javascript: isJavaScriptFile(file) };
356
+ }
357
+
285
358
  export type FileEncoding = "utf8" | "utf16le" | "utf16be";
286
359
 
287
360
  export interface FileText {
@@ -381,18 +454,23 @@ export async function writeFileAtomic(file: string, data: Buffer): Promise<void>
381
454
 
382
455
  export async function scanFile(file: string): Promise<FileScanResult> {
383
456
  const { text } = await readFileText(file);
384
- return { file, comments: scanComments(text, { jsx: isJsxFile(file) }) };
457
+ return { file, comments: scanComments(text, scanOptionsFor(file)) };
385
458
  }
386
459
 
387
460
  export async function scanPaths(inputs: string[], options: CollectOptions = {}): Promise<FileScanResult[]> {
388
- const files = await collectFiles(inputs, options);
389
- return mapLimit(files, FILE_CONCURRENCY, scanFile);
461
+ return mapFiles(await collectFiles(inputs, options), scanFile);
390
462
  }
391
463
 
392
- export const FILE_CONCURRENCY = 16;
464
+ /** How many files are read and processed at once. */
465
+ const FILE_CONCURRENCY = 16;
466
+
467
+ /** Maps the files through `fn`, FILE_CONCURRENCY at a time, with the results in the files' order. */
468
+ export function mapFiles<R>(files: readonly string[], fn: (file: string) => Promise<R>): Promise<R[]> {
469
+ return mapLimit(files, FILE_CONCURRENCY, fn);
470
+ }
393
471
 
394
472
  /** Concurrency-limited map that keeps input order and starts work as slots free up. */
395
- export async function mapLimit<T, R>(items: readonly T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
473
+ async function mapLimit<T, R>(items: readonly T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
396
474
  const results = new Array<R>(items.length);
397
475
  let nextIndex = 0;
398
476
 
package/src/imports.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import ts from "typescript";
2
2
  import { LINE_BREAK } from "./lines.js";
3
- import { firstIndexAtOrAfter, type Range } from "./ranges.js";
3
+ import { byStart, firstIndexAtOrAfter, type Range } from "./ranges.js";
4
4
 
5
5
  type ImportSorter = "typescript" | "dprint";
6
6
 
@@ -94,7 +94,6 @@ export function importGroupGaps(
94
94
  for (const statement of statementsAfterImports(statements, false)) addReader(linters, statement, "eslint");
95
95
  }
96
96
  return [...new Set([...sorters.keys(), ...linters.keys()])]
97
- .sort((a, b) => a.pos - b.pos)
98
97
  .map((statement) => {
99
98
  const gap = { start: statement.pos, end: gapEnd(statement, sourceFile) };
100
99
  const splits = [...(sorters.get(statement) ?? [])].filter((sorter) =>
@@ -102,7 +101,8 @@ export function importGroupGaps(
102
101
  );
103
102
  const spaced = [...(linters.get(statement) ?? [])].filter((linter) => FINDS_EMPTY_LINE[linter](source, gap));
104
103
  return { ...gap, splits, spaced };
105
- });
104
+ })
105
+ .sort(byStart);
106
106
  }
107
107
 
108
108
  /** Records that the tool reads the gap in front of the statement. */
@@ -117,16 +117,23 @@ const STARTS_GROUP: Readonly<
117
117
  Record<ImportSorter, (source: string, gap: Range, comments: readonly Range[]) => boolean>
118
118
  > = {
119
119
  typescript: (source, gap, comments) => newLineTrivia(source, gap, comments) >= 2,
120
- dprint: (source, gap) => source.slice(gap.start, gap.end).split("\n").length > 2,
120
+ dprint: holdsTwoBreaks("\n"),
121
121
  };
122
122
 
123
- // Whether the linter finds its empty line in the gap: two line breaks or
124
- // more, those inside comments included.
123
+ // Whether the linter finds its empty line in the gap.
125
124
  const FINDS_EMPTY_LINE: Readonly<Record<ImportLinter, (source: string, gap: Range) => boolean>> = {
126
- eslint: (source, gap) => source.slice(gap.start, gap.end).split(LINE_BREAK).length > 2,
127
- oxlint: (source, gap) => source.slice(gap.start, gap.end).split("\n").length > 2,
125
+ eslint: holdsTwoBreaks(LINE_BREAK),
126
+ oxlint: holdsTwoBreaks("\n"),
128
127
  };
129
128
 
129
+ /**
130
+ * Whether a gap holds two line breaks or more, those inside comments
131
+ * included, to a tool that counts what `lineBreak` matches as one.
132
+ */
133
+ function holdsTwoBreaks(lineBreak: string | RegExp): (source: string, gap: Range) => boolean {
134
+ return (source, gap) => source.slice(gap.start, gap.end).split(lineBreak).length > 2;
135
+ }
136
+
130
137
  /**
131
138
  * Where the gap in front of the statement ends: at its first token, or at the
132
139
  * first decorator of a class, which the linters measure the class from —
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export {
3
3
  collectFiles,
4
4
  decodeFileText,
5
5
  encodeFileText,
6
+ isJavaScriptFile,
6
7
  isJsxFile,
7
8
  readFileText,
8
9
  scanFile,
package/src/jsx.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import ts from "typescript";
2
2
  import { LINE_TERMINATOR, isBlank } from "./lines.js";
3
- import { firstIndexAtOrAfter, type Range } from "./ranges.js";
3
+ import { byStart, rangesWithin, type Range } from "./ranges.js";
4
4
 
5
5
  /**
6
6
  * JSX layout for comment removal.
@@ -102,7 +102,7 @@ export function jsxLayout(
102
102
  segments.push(renderParts(parts));
103
103
  }
104
104
 
105
- containers.sort((a, b) => a.start - b.start);
105
+ containers.sort(byStart);
106
106
  return { segments, containers, openingTagEnds, closingTagStarts };
107
107
  }
108
108
 
@@ -142,16 +142,15 @@ function commentOnlyContainer(
142
142
  // merge-conflict marker, which is no comment.
143
143
  const start = child.getStart(sourceFile);
144
144
  const end = child.end;
145
+ const inside = rangesWithin(comments, { start, end });
145
146
  let cursor = start + 1;
146
147
  let uncommented = "";
147
- let index = firstIndexAtOrAfter(comments, cursor, (comment) => comment.start);
148
- for (let comment = comments[index]; comment !== undefined && comment.end < end; comment = comments[index]) {
148
+ for (const comment of inside) {
149
149
  uncommented += source.slice(cursor, comment.start);
150
150
  cursor = comment.end;
151
- index += 1;
152
151
  }
153
152
  uncommented += source.slice(cursor, end - 1);
154
- return cursor > start + 1 && isBlank(uncommented)
153
+ return inside.length > 0 && isBlank(uncommented)
155
154
  ? { start, end, segment: -1, suspect: false, next: undefined }
156
155
  : undefined;
157
156
  }
package/src/lines.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import { firstIndexAtOrAfter, type Range } from "./ranges.js";
2
+
1
3
  /**
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.
4
+ * Line handling shared by the scanner's consumers. Unless given another line
5
+ * break, everything here counts lines the way the TypeScript scanner does:
6
+ * LF, CR, U+2028 and U+2029 each end a line, and CRLF is a single break.
5
7
  */
6
8
 
7
9
  /** Matches one ECMAScript line terminator character. */
@@ -15,6 +17,11 @@ export function isBlank(text: string): boolean {
15
17
  return /^\s*$/.test(text);
16
18
  }
17
19
 
20
+ /** True for a space or a tab, the blanks that hold the tokens on a line apart. */
21
+ export function isHorizontalSpace(char: string | undefined): boolean {
22
+ return char === " " || char === "\t";
23
+ }
24
+
18
25
  /** Splits on every line break, so exotic terminators never leak into single-line output. */
19
26
  export function splitLines(text: string): string[] {
20
27
  return text.split(LINE_BREAK);
@@ -26,18 +33,52 @@ export function lastLine(text: string): string {
26
33
  return lines[lines.length - 1] as string;
27
34
  }
28
35
 
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[] {
36
+ /**
37
+ * Start offset of every line, numbered the way the scanner numbers them
38
+ * (line 1 starts at index 0) — or, given another `lineBreak`, the way that
39
+ * pattern breaks lines.
40
+ */
41
+ export function lineStartOffsets(source: string, lineBreak: RegExp = LINE_BREAK): number[] {
31
42
  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
- }
43
+ for (const match of source.matchAll(new RegExp(lineBreak, "g"))) {
44
+ starts.push(match.index + match[0].length);
41
45
  }
42
46
  return starts;
43
47
  }
48
+
49
+ /** Index of the line holding the offset, given every line's start offset (see lineStartOffsets). */
50
+ export function lineIndexAt(lineStarts: readonly number[], offset: number): number {
51
+ return firstIndexAtOrAfter(lineStarts, offset + 1, (start) => start) - 1;
52
+ }
53
+
54
+ /**
55
+ * True when nothing but whitespace shares the range's first and last lines.
56
+ * Lines are delimited by any ECMAScript line terminator, matching how the
57
+ * scanner numbers them (a lone `\r`, U+2028 and U+2029 count too).
58
+ */
59
+ export function occupiesWholeLines(source: string, range: Range): boolean {
60
+ return opensItsLine(source, range) && closesItsLine(source, range);
61
+ }
62
+
63
+ /** True when nothing but whitespace follows the range on its last line. */
64
+ export function closesItsLine(source: string, range: Range): boolean {
65
+ return blankToLineEdge(source, range.end, 1);
66
+ }
67
+
68
+ /** True when nothing but whitespace precedes the range on its first line. */
69
+ export function opensItsLine(source: string, range: Range): boolean {
70
+ return blankToLineEdge(source, range.start - 1, -1);
71
+ }
72
+
73
+ /**
74
+ * True when nothing but whitespace lies from the offset on, in the direction
75
+ * of `step`, up to the line break or the edge of the source.
76
+ */
77
+ function blankToLineEdge(source: string, from: number, step: 1 | -1): boolean {
78
+ for (let index = from; index >= 0 && index < source.length; index += step) {
79
+ const char = source[index] as string;
80
+ if (LINE_TERMINATOR.test(char)) return true;
81
+ if (!isBlank(char)) return false;
82
+ }
83
+ return true;
84
+ }
package/src/ranges.ts CHANGED
@@ -4,6 +4,11 @@ export interface Range {
4
4
  end: number;
5
5
  }
6
6
 
7
+ /** Sort comparator for document order: ranges are ordered by where they start. */
8
+ export function byStart(a: Range, b: Range): number {
9
+ return a.start - b.start;
10
+ }
11
+
7
12
  /** Index of the first item whose key is at least `value`, by binary search over ascending keys. */
8
13
  export function firstIndexAtOrAfter<T>(items: readonly T[], value: number, key: (item: T) => number): number {
9
14
  let low = 0;
@@ -21,3 +26,15 @@ export function rangeAt<T extends Range>(ranges: readonly T[], offset: number):
21
26
  const range = ranges[firstIndexAtOrAfter(ranges, offset + 1, (candidate) => candidate.start) - 1];
22
27
  return range !== undefined && offset < range.end ? range : undefined;
23
28
  }
29
+
30
+ /**
31
+ * The ranges among sorted, non-overlapping ones that lie within the span.
32
+ * Their ends ascend as their starts do, so they are the ones from the first
33
+ * starting at or after the span's start up to the first ending past its end.
34
+ */
35
+ export function rangesWithin<T extends Range>(ranges: readonly T[], span: Range): T[] {
36
+ return ranges.slice(
37
+ firstIndexAtOrAfter(ranges, span.start, (range) => range.start),
38
+ firstIndexAtOrAfter(ranges, span.end + 1, (range) => range.end),
39
+ );
40
+ }