@kongyo2/ts-comment-scanner 1.19.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.
- package/README.md +2 -2
- package/dist/args.d.ts.map +1 -1
- package/dist/args.js +23 -19
- package/dist/args.js.map +1 -1
- package/dist/astgrep.d.ts +1 -1
- package/dist/astgrep.d.ts.map +1 -1
- package/dist/astgrep.js +3 -8
- package/dist/astgrep.js.map +1 -1
- package/dist/directives.d.ts +8 -0
- package/dist/directives.d.ts.map +1 -1
- package/dist/directives.js +27 -20
- package/dist/directives.js.map +1 -1
- package/dist/files.d.ts +2 -3
- package/dist/files.d.ts.map +1 -1
- package/dist/files.js +45 -60
- package/dist/files.js.map +1 -1
- package/dist/imports.d.ts +34 -10
- package/dist/imports.d.ts.map +1 -1
- package/dist/imports.js +202 -28
- package/dist/imports.js.map +1 -1
- package/dist/jsx.d.ts.map +1 -1
- package/dist/jsx.js +5 -6
- package/dist/jsx.js.map +1 -1
- package/dist/lines.d.ts +13 -5
- package/dist/lines.d.ts.map +1 -1
- package/dist/lines.js +20 -16
- package/dist/lines.js.map +1 -1
- package/dist/ranges.d.ts +8 -0
- package/dist/ranges.d.ts.map +1 -1
- package/dist/ranges.js +12 -0
- package/dist/ranges.js.map +1 -1
- package/dist/remove.d.ts +10 -7
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +254 -219
- package/dist/remove.js.map +1 -1
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +11 -19
- package/dist/run.js.map +1 -1
- package/dist/scanner.d.ts +10 -1
- package/dist/scanner.d.ts.map +1 -1
- package/dist/scanner.js +15 -26
- package/dist/scanner.js.map +1 -1
- package/dist/splice.d.ts +6 -0
- package/dist/splice.d.ts.map +1 -1
- package/dist/splice.js +22 -5
- package/dist/splice.js.map +1 -1
- package/package.json +1 -1
- package/src/args.ts +21 -21
- package/src/astgrep.ts +4 -8
- package/src/directives.ts +24 -17
- package/src/files.ts +43 -59
- package/src/imports.ts +229 -28
- package/src/jsx.ts +5 -6
- package/src/lines.ts +23 -14
- package/src/ranges.ts +17 -0
- package/src/remove.ts +287 -235
- package/src/run.ts +11 -24
- package/src/scanner.ts +25 -28
- package/src/splice.ts +29 -6
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 {
|
|
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(
|
|
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
|
-
|
|
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
|
|
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 } from "./ranges.js";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* Line handling shared by the scanner's consumers.
|
|
3
|
-
* lines the way the TypeScript scanner does:
|
|
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,20 @@ export function lastLine(text: string): string {
|
|
|
26
33
|
return lines[lines.length - 1] as string;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
/**
|
|
30
|
-
|
|
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 (
|
|
33
|
-
|
|
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
|
+
}
|
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
|
+
}
|