@kongyo2/ts-comment-scanner 1.21.0 → 1.23.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 (66) hide show
  1. package/README.md +240 -52
  2. package/dist/astgrep.d.ts +4 -2
  3. package/dist/astgrep.d.ts.map +1 -1
  4. package/dist/astgrep.js +7 -4
  5. package/dist/astgrep.js.map +1 -1
  6. package/dist/containers.d.ts +48 -0
  7. package/dist/containers.d.ts.map +1 -0
  8. package/dist/containers.js +82 -0
  9. package/dist/containers.js.map +1 -0
  10. package/dist/coverage.d.ts +45 -0
  11. package/dist/coverage.d.ts.map +1 -0
  12. package/dist/coverage.js +92 -0
  13. package/dist/coverage.js.map +1 -0
  14. package/dist/directives.d.ts +13 -3
  15. package/dist/directives.d.ts.map +1 -1
  16. package/dist/directives.js +31 -11
  17. package/dist/directives.js.map +1 -1
  18. package/dist/files.d.ts +5 -0
  19. package/dist/files.d.ts.map +1 -1
  20. package/dist/files.js +103 -41
  21. package/dist/files.js.map +1 -1
  22. package/dist/imports.d.ts +12 -5
  23. package/dist/imports.d.ts.map +1 -1
  24. package/dist/imports.js +33 -10
  25. package/dist/imports.js.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/lines.d.ts +11 -0
  31. package/dist/lines.d.ts.map +1 -1
  32. package/dist/lines.js +30 -0
  33. package/dist/lines.js.map +1 -1
  34. package/dist/remove.d.ts +16 -12
  35. package/dist/remove.d.ts.map +1 -1
  36. package/dist/remove.js +51 -638
  37. package/dist/remove.js.map +1 -1
  38. package/dist/run.d.ts.map +1 -1
  39. package/dist/run.js +11 -10
  40. package/dist/run.js.map +1 -1
  41. package/dist/scanner.d.ts +29 -0
  42. package/dist/scanner.d.ts.map +1 -1
  43. package/dist/scanner.js +122 -5
  44. package/dist/scanner.js.map +1 -1
  45. package/dist/shields.d.ts +77 -0
  46. package/dist/shields.d.ts.map +1 -0
  47. package/dist/shields.js +758 -0
  48. package/dist/shields.js.map +1 -0
  49. package/dist/splice.d.ts +40 -15
  50. package/dist/splice.d.ts.map +1 -1
  51. package/dist/splice.js +185 -69
  52. package/dist/splice.js.map +1 -1
  53. package/package.json +6 -5
  54. package/src/astgrep.ts +7 -5
  55. package/src/containers.ts +128 -0
  56. package/src/coverage.ts +110 -0
  57. package/src/directives.ts +45 -11
  58. package/src/files.ts +147 -53
  59. package/src/imports.ts +42 -17
  60. package/src/index.ts +1 -0
  61. package/src/lines.ts +33 -1
  62. package/src/remove.ts +58 -748
  63. package/src/run.ts +11 -10
  64. package/src/scanner.ts +160 -5
  65. package/src/shields.ts +852 -0
  66. package/src/splice.ts +216 -75
package/src/imports.ts CHANGED
@@ -1,16 +1,18 @@
1
1
  import ts from "typescript";
2
2
  import { LINE_BREAK } from "./lines.js";
3
- import { byStart, firstIndexAtOrAfter, type Range } from "./ranges.js";
3
+ import { byStart, rangesWithin, type Range } from "./ranges.js";
4
4
 
5
5
  type ImportSorter = "typescript" | "dprint";
6
6
 
7
7
  /**
8
8
  * The linters whose newline-after-import rule wants an empty line between
9
9
  * the imports and the code after them: eslint-plugin-import ("eslint"),
10
- * whose rule eslint-plugin-import-x and eslint-plugin-import-lite carry
11
- * over, and oxlint, which ports it ("oxlint").
10
+ * whose rule eslint-plugin-import-lite carries over; eslint-plugin-import-x
11
+ * ("import-x"), which measures from the comments trailing the import since
12
+ * 4.18.1 and read the rule as eslint-plugin-import does before; and oxlint,
13
+ * which ports it ("oxlint").
12
14
  */
13
- type ImportLinter = "eslint" | "oxlint";
15
+ type ImportLinter = "eslint" | "import-x" | "oxlint";
14
16
 
15
17
  /**
16
18
  * The gap in front of an import or export declaration that an import sorter
@@ -60,13 +62,18 @@ export interface ImportGroupGap extends Range {
60
62
  * breaks its parsers number lines by, U+2028 and U+2029 included, and reads
61
63
  * the imports in the bodies of modules and namespaces as well (see
62
64
  * moduleBodies); oxlint counts line feeds alone, in the file's own
63
- * statements.
65
+ * statements. eslint-plugin-import-x 4.18.1 measures from the end of the
66
+ * comments trailing the import instead (see trailingCommentsEnd), so a
67
+ * comment that starts on the import's line and ends further down holds no
68
+ * line for it.
64
69
  *
65
70
  * The sorters start a group at an empty line, and the linters find theirs
66
71
  * there, which is what removal leaves where it deletes the comments that held
67
72
  * a gap open. Biome's organizeImports starts a chunk only at a comment that
68
73
  * an empty line follows, never at the empty line alone, so no layout without
69
- * the comment keeps its chunks apart, and they are not read.
74
+ * the comment keeps its chunks apart, and they are not read. Neither are the
75
+ * runs dprint sorts with `module.importGroups` set (0.97.0 on), which end at
76
+ * such a comment alone as well.
70
77
  */
71
78
  export function importGroupGaps(
72
79
  source: string,
@@ -87,11 +94,12 @@ export function importGroupGaps(
87
94
  addReader(sorters, declaration, "dprint");
88
95
  }
89
96
  for (const statement of statementsAfterImports(sourceFile.statements, true)) {
90
- addReader(linters, statement, "eslint");
91
- addReader(linters, statement, "oxlint");
97
+ for (const linter of ["eslint", "import-x", "oxlint"] as const) addReader(linters, statement, linter);
92
98
  }
93
99
  for (const statements of moduleBodies(sourceFile.statements)) {
94
- for (const statement of statementsAfterImports(statements, false)) addReader(linters, statement, "eslint");
100
+ for (const statement of statementsAfterImports(statements, false)) {
101
+ for (const linter of ["eslint", "import-x"] as const) addReader(linters, statement, linter);
102
+ }
95
103
  }
96
104
  return [...new Set([...sorters.keys(), ...linters.keys()])]
97
105
  .map((statement) => {
@@ -99,7 +107,9 @@ export function importGroupGaps(
99
107
  const splits = [...(sorters.get(statement) ?? [])].filter((sorter) =>
100
108
  STARTS_GROUP[sorter](source, gap, comments),
101
109
  );
102
- const spaced = [...(linters.get(statement) ?? [])].filter((linter) => FINDS_EMPTY_LINE[linter](source, gap));
110
+ const spaced = [...(linters.get(statement) ?? [])].filter((linter) =>
111
+ FINDS_EMPTY_LINE[linter](source, gap, comments),
112
+ );
103
113
  return { ...gap, splits, spaced };
104
114
  })
105
115
  .sort(byStart);
@@ -121,11 +131,31 @@ const STARTS_GROUP: Readonly<
121
131
  };
122
132
 
123
133
  // Whether the linter finds its empty line in the gap.
124
- const FINDS_EMPTY_LINE: Readonly<Record<ImportLinter, (source: string, gap: Range) => boolean>> = {
134
+ const FINDS_EMPTY_LINE: Readonly<
135
+ Record<ImportLinter, (source: string, gap: Range, comments: readonly Range[]) => boolean>
136
+ > = {
125
137
  eslint: holdsTwoBreaks(LINE_BREAK),
138
+ "import-x": (source, gap, comments) =>
139
+ holdsTwoBreaks(LINE_BREAK)(source, { start: trailingCommentsEnd(source, gap, comments), end: gap.end }),
126
140
  oxlint: holdsTwoBreaks("\n"),
127
141
  };
128
142
 
143
+ /**
144
+ * Where the comments trailing the statement in front of the gap end, as
145
+ * eslint-plugin-import-x reads them (`getTrailingComments` in its
146
+ * utils/trailing-comments.js): the comments after the statement that each
147
+ * start on the line the statement, or the comment before, ends on. The gap's
148
+ * start when none does.
149
+ */
150
+ function trailingCommentsEnd(source: string, gap: Range, comments: readonly Range[]): number {
151
+ let end = gap.start;
152
+ for (const comment of rangesWithin(comments, gap)) {
153
+ if (LINE_BREAK.test(source.slice(end, comment.start))) break;
154
+ end = comment.end;
155
+ }
156
+ return end;
157
+ }
158
+
129
159
  /**
130
160
  * Whether a gap holds two line breaks or more, those inside comments
131
161
  * included, to a tool that counts what `lineBreak` matches as one.
@@ -347,12 +377,7 @@ function dprintKind(statement: ts.Statement): "imports" | "exports" | undefined
347
377
  function newLineTrivia(source: string, gap: Range, comments: readonly Range[]): number {
348
378
  let count = 0;
349
379
  let from = gap.start;
350
- for (
351
- let index = firstIndexAtOrAfter(comments, gap.start, (comment) => comment.start);
352
- (comments[index]?.start ?? gap.end) < gap.end;
353
- index += 1
354
- ) {
355
- const comment = comments[index] as Range;
380
+ for (const comment of rangesWithin(comments, gap)) {
356
381
  count += lineBreaks(source.slice(from, comment.start));
357
382
  from = comment.end;
358
383
  }
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/lines.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { firstIndexAtOrAfter } from "./ranges.js";
1
+ import { firstIndexAtOrAfter, type Range } from "./ranges.js";
2
2
 
3
3
  /**
4
4
  * Line handling shared by the scanner's consumers. Unless given another line
@@ -50,3 +50,35 @@ export function lineStartOffsets(source: string, lineBreak: RegExp = LINE_BREAK)
50
50
  export function lineIndexAt(lineStarts: readonly number[], offset: number): number {
51
51
  return firstIndexAtOrAfter(lineStarts, offset + 1, (start) => start) - 1;
52
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
+ }