@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.
- 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.map +1 -1
- package/dist/imports.js +14 -8
- 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 +6 -6
- package/dist/remove.d.ts.map +1 -1
- package/dist/remove.js +230 -204
- 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 +5 -0
- 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 +15 -8
- package/src/jsx.ts +5 -6
- package/src/lines.ts +23 -14
- package/src/ranges.ts +17 -0
- package/src/remove.ts +262 -220
- package/src/run.ts +11 -24
- package/src/scanner.ts +20 -27
- package/src/splice.ts +29 -6
package/src/args.ts
CHANGED
|
@@ -29,6 +29,8 @@ export class UsageError extends Error {
|
|
|
29
29
|
|
|
30
30
|
const FORMATS: readonly OutputFormat[] = ["text", "json", "github"];
|
|
31
31
|
const DIRECTIVE_CONFLICT = "--skip-directives and --only-directives cannot be combined";
|
|
32
|
+
const EXT_REQUIRED = "option --ext requires at least one extension";
|
|
33
|
+
const DIFF_REQUIRED = "option --diff requires a git revision range (e.g. HEAD or main..HEAD)";
|
|
32
34
|
|
|
33
35
|
export function parseArgs(argv: string[]): CliOptions {
|
|
34
36
|
const paths: string[] = [];
|
|
@@ -56,13 +58,24 @@ export function parseArgs(argv: string[]): CliOptions {
|
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
const [flag, inlineValue] = splitFlag(arg);
|
|
59
|
-
|
|
61
|
+
/**
|
|
62
|
+
* The option's value, given inline or as the next argument. A forgotten
|
|
63
|
+
* value must not swallow the next flag: `--ignore --dry-run` would turn a
|
|
64
|
+
* preview into an in-place removal. So a next argument starting with `-`
|
|
65
|
+
* is refused with `refusal`; a value that does start with `-` can still
|
|
66
|
+
* be given inline, as `--keep=-x`.
|
|
67
|
+
*/
|
|
68
|
+
const readValue = (refusal = `option ${flag} requires a value`): string => {
|
|
60
69
|
if (inlineValue !== undefined) return inlineValue;
|
|
61
70
|
const next = argv[index + 1];
|
|
62
71
|
if (next === undefined) throw new UsageError(`option ${flag} requires a value`);
|
|
72
|
+
if (next.startsWith("-")) throw new UsageError(refusal);
|
|
63
73
|
index += 1;
|
|
64
74
|
return next;
|
|
65
75
|
};
|
|
76
|
+
/** The refusal for an option whose value may start with `-`, which then goes inline. */
|
|
77
|
+
const inlineForDash = (value: string, placeholder: string): string =>
|
|
78
|
+
`option ${flag} requires ${value} (use ${flag}=${placeholder} for one starting with "-")`;
|
|
66
79
|
const rejectValue = (): void => {
|
|
67
80
|
if (inlineValue !== undefined) throw new UsageError(`option ${flag} does not take a value`);
|
|
68
81
|
};
|
|
@@ -85,27 +98,23 @@ export function parseArgs(argv: string[]): CliOptions {
|
|
|
85
98
|
format = "json";
|
|
86
99
|
break;
|
|
87
100
|
case "--ignore": {
|
|
88
|
-
const value = readValue();
|
|
101
|
+
const value = readValue(inlineForDash("a glob pattern", "<glob>"));
|
|
89
102
|
if (value === "") throw new UsageError("option --ignore requires a glob pattern");
|
|
90
103
|
ignore.push(value);
|
|
91
104
|
break;
|
|
92
105
|
}
|
|
93
106
|
case "--ext": {
|
|
94
|
-
const parsed = readValue()
|
|
107
|
+
const parsed = readValue(EXT_REQUIRED)
|
|
95
108
|
.split(",")
|
|
96
109
|
.map((extension) => extension.trim())
|
|
97
110
|
.filter((extension) => extension !== "");
|
|
98
|
-
if (parsed.length === 0)
|
|
99
|
-
throw new UsageError("option --ext requires at least one extension");
|
|
100
|
-
}
|
|
111
|
+
if (parsed.length === 0) throw new UsageError(EXT_REQUIRED);
|
|
101
112
|
extensions.push(...parsed);
|
|
102
113
|
break;
|
|
103
114
|
}
|
|
104
115
|
case "--diff": {
|
|
105
|
-
const value = readValue();
|
|
106
|
-
if (value === "" || value.startsWith("-"))
|
|
107
|
-
throw new UsageError("option --diff requires a git revision range (e.g. HEAD or main..HEAD)");
|
|
108
|
-
}
|
|
116
|
+
const value = readValue(DIFF_REQUIRED);
|
|
117
|
+
if (value === "" || value.startsWith("-")) throw new UsageError(DIFF_REQUIRED);
|
|
109
118
|
diff = value;
|
|
110
119
|
break;
|
|
111
120
|
}
|
|
@@ -135,18 +144,9 @@ export function parseArgs(argv: string[]): CliOptions {
|
|
|
135
144
|
rejectValue();
|
|
136
145
|
removeLegal = true;
|
|
137
146
|
break;
|
|
138
|
-
case "--keep":
|
|
139
|
-
|
|
140
|
-
// would turn a preview into an in-place removal. A pattern that does
|
|
141
|
-
// start with `-` can still be given inline, as `--keep=-x`.
|
|
142
|
-
if (inlineValue === undefined && argv[index + 1]?.startsWith("-") === true) {
|
|
143
|
-
throw new UsageError(
|
|
144
|
-
`option ${flag} requires a regular expression (use ${flag}=<regex> for one starting with "-")`,
|
|
145
|
-
);
|
|
146
|
-
}
|
|
147
|
-
keep.push(parsePattern(flag, readValue()));
|
|
147
|
+
case "--keep":
|
|
148
|
+
keep.push(parsePattern(flag, readValue(inlineForDash("a regular expression", "<regex>"))));
|
|
148
149
|
break;
|
|
149
|
-
}
|
|
150
150
|
case "--dry-run":
|
|
151
151
|
rejectValue();
|
|
152
152
|
dryRun = true;
|
package/src/astgrep.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LINE_TERMINATOR } from "./lines.js";
|
|
2
|
-
import {
|
|
1
|
+
import { LINE_TERMINATOR, lineIndexAt, lineStartOffsets } from "./lines.js";
|
|
2
|
+
import type { Range } from "./ranges.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Where ast-grep's suppression comments apply (`get_suppression_kind` in its
|
|
@@ -35,11 +35,7 @@ function lineFeedCount(text: string): number {
|
|
|
35
35
|
|
|
36
36
|
/** Start offset of every tree-sitter row: the text's start and each offset just past a line feed. */
|
|
37
37
|
export function rowStartOffsets(text: string): number[] {
|
|
38
|
-
|
|
39
|
-
for (let index = text.indexOf("\n"); index !== -1; index = text.indexOf("\n", index + 1)) {
|
|
40
|
-
starts.push(index + 1);
|
|
41
|
-
}
|
|
42
|
-
return starts;
|
|
38
|
+
return lineStartOffsets(text, /\n/);
|
|
43
39
|
}
|
|
44
40
|
|
|
45
41
|
/** What stands in front of a comment in tree-sitter's tree, which ast-grep reads a suppression's reach from. */
|
|
@@ -89,7 +85,7 @@ export function astGrepReach(
|
|
|
89
85
|
comment: Range,
|
|
90
86
|
preceding: PrecedingNode,
|
|
91
87
|
): AstGrepReach {
|
|
92
|
-
const rowOf = (offset: number): number =>
|
|
88
|
+
const rowOf = (offset: number): number => lineIndexAt(rowStarts, offset);
|
|
93
89
|
const rowStart = (row: number): number => rowStarts[row] ?? text.length;
|
|
94
90
|
const breaks = (unit: Range): boolean => LINE_TERMINATOR.test(text.slice(unit.start, unit.end));
|
|
95
91
|
const row = rowOf(comment.start);
|
package/src/directives.ts
CHANGED
|
@@ -910,39 +910,46 @@ const FLOW_BLOCK_SUPPRESSION = /^\/\*[ \t\r\n*]*@ts-(ignore|expect-error)(?![\w-
|
|
|
910
910
|
* code.
|
|
911
911
|
*/
|
|
912
912
|
export function detectDirective(kind: CommentKind, text: string, placement?: DirectivePlacement): string | undefined {
|
|
913
|
+
return directiveNames(kind, text, placement)[0];
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
/**
|
|
917
|
+
* Every directive the comment carries that is live at the placement, the
|
|
918
|
+
* name detectDirective reports first. A comment can carry several: what a
|
|
919
|
+
* removal has to leave in place for each of them (`// cspell:ignore foo --
|
|
920
|
+
* falls through`, read as a fall-through comment where it is the last
|
|
921
|
+
* comment before a `case`) must not hide behind the name.
|
|
922
|
+
*/
|
|
923
|
+
export function directiveNames(kind: CommentKind, text: string, placement?: DirectivePlacement): string[] {
|
|
913
924
|
const at = placement ?? ANY_PLACEMENT;
|
|
925
|
+
const names: string[] = [];
|
|
914
926
|
if (kind === "line") {
|
|
915
927
|
const tripleSlash = tripleSlashDirective(text);
|
|
916
|
-
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at))
|
|
928
|
+
if (tripleSlash !== undefined && isActiveAt(tripleSlash, at)) names.push(tripleSlash);
|
|
917
929
|
const suppression = TS_LINE_SUPPRESSION.exec(text) ?? FLOW_LINE_SUPPRESSION.exec(text);
|
|
918
|
-
if (suppression) {
|
|
919
|
-
return `@ts-${suppression[1]}`;
|
|
920
|
-
}
|
|
930
|
+
if (suppression) names.push(`@ts-${suppression[1]}`);
|
|
921
931
|
const checkPragma = TS6_LINE_CHECK_PRAGMA.exec(text) ?? TS7_LINE_CHECK_PRAGMA.exec(text);
|
|
922
932
|
if (checkPragma) {
|
|
923
933
|
const name = `@ts-${checkPragma[1]?.toLowerCase()}`;
|
|
924
|
-
if (isActiveAt(name, at))
|
|
934
|
+
if (isActiveAt(name, at)) names.push(name);
|
|
925
935
|
}
|
|
926
936
|
} else {
|
|
927
937
|
const suppression = TS_BLOCK_SUPPRESSION.exec(lastLine(text)) ?? FLOW_BLOCK_SUPPRESSION.exec(text);
|
|
928
|
-
if (suppression) {
|
|
929
|
-
return `@ts-${suppression[1]}`;
|
|
930
|
-
}
|
|
938
|
+
if (suppression) names.push(`@ts-${suppression[1]}`);
|
|
931
939
|
}
|
|
932
940
|
|
|
933
941
|
// A directive that opens the comment names it, however the comment goes on
|
|
934
942
|
// to mention other tools' markers (`// nosemgrep: ... @jsx h` stays a
|
|
935
|
-
// Semgrep suppression
|
|
936
|
-
//
|
|
937
|
-
|
|
943
|
+
// Semgrep suppression); only without one does a marker found after other
|
|
944
|
+
// text name the comment.
|
|
945
|
+
const mentions: string[] = [];
|
|
938
946
|
for (const match of ruleMatches(kind, text)) {
|
|
939
|
-
if (
|
|
940
|
-
if (match.opening) return match.name;
|
|
941
|
-
mention ??= match.name;
|
|
947
|
+
if (isMatchActiveAt(match, at)) (match.opening ? names : mentions).push(match.name);
|
|
942
948
|
}
|
|
943
|
-
|
|
944
|
-
if (
|
|
945
|
-
|
|
949
|
+
names.push(...mentions);
|
|
950
|
+
if (names.length > 0) return [...new Set(names)];
|
|
951
|
+
if (at.emptyBlock === true) return [EMPTY_BLOCK];
|
|
952
|
+
return at.emptyFile === true && kind === "line" && text.startsWith("///") ? [EMPTY_FILE] : [];
|
|
946
953
|
}
|
|
947
954
|
|
|
948
955
|
/**
|
package/src/files.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
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";
|
|
@@ -54,16 +53,15 @@ export async function collectFiles(inputs: string[], options: CollectOptions = {
|
|
|
54
53
|
throw new Error("empty path is not a valid input");
|
|
55
54
|
}
|
|
56
55
|
const input = normalize(rawInput);
|
|
57
|
-
const info = await
|
|
56
|
+
const info = await withFriendlyNotFound(input, () => stat(input));
|
|
58
57
|
if (info.isDirectory()) {
|
|
59
58
|
// Directory inputs are themselves subject to the ignored directory
|
|
60
59
|
// names and the ignore patterns; only explicitly listed files bypass
|
|
61
60
|
// them.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
61
|
+
const context: WalkContext = { extensions, ignoreDirs, isIgnored, root: input, visited: new Set() };
|
|
62
|
+
if (entersDirectory(context, input, basename(input))) await walk(input, context, remember);
|
|
65
63
|
} else {
|
|
66
|
-
explicitFiles[index] = { path: input, real: await
|
|
64
|
+
explicitFiles[index] = { path: input, real: await withFriendlyNotFound(input, () => realpath(input)) };
|
|
67
65
|
}
|
|
68
66
|
}),
|
|
69
67
|
);
|
|
@@ -88,16 +86,7 @@ export function foldPathCase(path: string): string {
|
|
|
88
86
|
return process.platform === "win32" ? path.toLowerCase() : path;
|
|
89
87
|
}
|
|
90
88
|
|
|
91
|
-
|
|
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. */
|
|
89
|
+
/** Runs the lookup of an explicit input, reporting a missing path under the name the user spelled it with. */
|
|
101
90
|
async function withFriendlyNotFound<T>(input: string, lookup: () => Promise<T>): Promise<T> {
|
|
102
91
|
try {
|
|
103
92
|
return await lookup();
|
|
@@ -110,14 +99,20 @@ async function withFriendlyNotFound<T>(input: string, lookup: () => Promise<T>):
|
|
|
110
99
|
}
|
|
111
100
|
|
|
112
101
|
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
102
|
+
* Runs a lookup of the walk, undefined when it fails with an error the walk
|
|
103
|
+
* may step over: entries that vanished mid-walk (ENOENT, ENOTDIR after a
|
|
104
|
+
* directory was replaced) and symlinks that do not resolve (broken targets,
|
|
105
|
+
* ELOOP cycles). Anything else — permissions, I/O failures — propagates, so
|
|
106
|
+
* a partial scan cannot masquerade as a clean one.
|
|
117
107
|
*/
|
|
118
|
-
function
|
|
119
|
-
|
|
120
|
-
|
|
108
|
+
async function unlessSkippable<T>(lookup: () => Promise<T>): Promise<T | undefined> {
|
|
109
|
+
try {
|
|
110
|
+
return await lookup();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
113
|
+
if (code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP") return undefined;
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
121
116
|
}
|
|
122
117
|
|
|
123
118
|
interface WalkContext {
|
|
@@ -130,28 +125,23 @@ interface WalkContext {
|
|
|
130
125
|
visited: Set<string>;
|
|
131
126
|
}
|
|
132
127
|
|
|
128
|
+
/** Whether a walk enters the directory: neither its name nor an ignore pattern excludes it. */
|
|
129
|
+
function entersDirectory(context: WalkContext, path: string, name: string): boolean {
|
|
130
|
+
return !context.ignoreDirs.has(foldPathCase(name)) && !context.isIgnored(path, context.root, true);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
133
|
async function walk(
|
|
134
134
|
dir: string,
|
|
135
135
|
context: WalkContext,
|
|
136
136
|
remember: (path: string, realPath: string) => void,
|
|
137
137
|
): Promise<void> {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
real = await realpath(dir);
|
|
141
|
-
} catch (error) {
|
|
142
|
-
if (isSkippableWalkError(error)) return; // the directory vanished mid-walk
|
|
143
|
-
throw error;
|
|
144
|
-
}
|
|
138
|
+
const real = await unlessSkippable(() => realpath(dir));
|
|
139
|
+
if (real === undefined) return; // the directory vanished mid-walk
|
|
145
140
|
if (context.visited.has(real)) return;
|
|
146
141
|
context.visited.add(real);
|
|
147
142
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
entries = await readdir(dir, { withFileTypes: true });
|
|
151
|
-
} catch (error) {
|
|
152
|
-
if (isSkippableWalkError(error)) return; // the directory vanished after it resolved
|
|
153
|
-
throw error;
|
|
154
|
-
}
|
|
143
|
+
const entries = await unlessSkippable(() => readdir(dir, { withFileTypes: true }));
|
|
144
|
+
if (entries === undefined) return; // the directory vanished after it resolved
|
|
155
145
|
|
|
156
146
|
await Promise.all(
|
|
157
147
|
entries.map(async (entry) => {
|
|
@@ -161,19 +151,13 @@ async function walk(
|
|
|
161
151
|
if (entry.isSymbolicLink()) {
|
|
162
152
|
// Symlinks are followed, matching how explicitly listed inputs are
|
|
163
153
|
// stat()ed; the visited set above keeps cycles from recursing forever.
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
} catch (error) {
|
|
169
|
-
if (isSkippableWalkError(error)) return; // broken symlink
|
|
170
|
-
throw error;
|
|
171
|
-
}
|
|
154
|
+
const info = await unlessSkippable(() => stat(full));
|
|
155
|
+
if (info === undefined) return; // broken symlink
|
|
156
|
+
isDirectory = info.isDirectory();
|
|
157
|
+
isFile = info.isFile();
|
|
172
158
|
}
|
|
173
159
|
if (isDirectory) {
|
|
174
|
-
if (
|
|
175
|
-
await walk(full, context, remember);
|
|
176
|
-
}
|
|
160
|
+
if (entersDirectory(context, full, entry.name)) await walk(full, context, remember);
|
|
177
161
|
} else if (
|
|
178
162
|
isFile &&
|
|
179
163
|
hasExtension(entry.name, context.extensions) &&
|
|
@@ -181,13 +165,8 @@ async function walk(
|
|
|
181
165
|
) {
|
|
182
166
|
// Remembered under its real path, so several symlinks to one target
|
|
183
167
|
// (or a link plus the target itself) count — and get rewritten — once.
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
realFile = await realpath(full);
|
|
187
|
-
} catch (error) {
|
|
188
|
-
if (isSkippableWalkError(error)) return; // the file vanished mid-walk
|
|
189
|
-
throw error;
|
|
190
|
-
}
|
|
168
|
+
const realFile = await unlessSkippable(() => realpath(full));
|
|
169
|
+
if (realFile === undefined) return; // the file vanished mid-walk
|
|
191
170
|
remember(full, realFile);
|
|
192
171
|
}
|
|
193
172
|
}),
|
|
@@ -385,14 +364,19 @@ export async function scanFile(file: string): Promise<FileScanResult> {
|
|
|
385
364
|
}
|
|
386
365
|
|
|
387
366
|
export async function scanPaths(inputs: string[], options: CollectOptions = {}): Promise<FileScanResult[]> {
|
|
388
|
-
|
|
389
|
-
return mapLimit(files, FILE_CONCURRENCY, scanFile);
|
|
367
|
+
return mapFiles(await collectFiles(inputs, options), scanFile);
|
|
390
368
|
}
|
|
391
369
|
|
|
392
|
-
|
|
370
|
+
/** How many files are read and processed at once. */
|
|
371
|
+
const FILE_CONCURRENCY = 16;
|
|
372
|
+
|
|
373
|
+
/** Maps the files through `fn`, FILE_CONCURRENCY at a time, with the results in the files' order. */
|
|
374
|
+
export function mapFiles<R>(files: readonly string[], fn: (file: string) => Promise<R>): Promise<R[]> {
|
|
375
|
+
return mapLimit(files, FILE_CONCURRENCY, fn);
|
|
376
|
+
}
|
|
393
377
|
|
|
394
378
|
/** Concurrency-limited map that keeps input order and starts work as slots free up. */
|
|
395
|
-
|
|
379
|
+
async function mapLimit<T, R>(items: readonly T[], limit: number, fn: (item: T) => Promise<R>): Promise<R[]> {
|
|
396
380
|
const results = new Array<R>(items.length);
|
|
397
381
|
let nextIndex = 0;
|
|
398
382
|
|
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: (
|
|
120
|
+
dprint: holdsTwoBreaks("\n"),
|
|
121
121
|
};
|
|
122
122
|
|
|
123
|
-
// Whether the linter finds its empty line in the gap
|
|
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: (
|
|
127
|
-
oxlint: (
|
|
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/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
|
+
}
|