@kongyo2/ts-comment-scanner 1.20.0 → 1.21.0

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