pi-lens 2.0.7 → 2.0.8
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/clients/ast-grep-client.test.ts +146 -116
- package/clients/ast-grep-client.ts +645 -551
- package/clients/biome-client.test.ts +154 -137
- package/clients/biome-client.ts +397 -337
- package/clients/complexity-client.test.ts +188 -200
- package/clients/complexity-client.ts +815 -667
- package/clients/dependency-checker.test.ts +55 -55
- package/clients/dependency-checker.ts +358 -333
- package/clients/go-client.test.ts +121 -111
- package/clients/go-client.ts +218 -216
- package/clients/jscpd-client.test.ts +132 -132
- package/clients/jscpd-client.ts +155 -118
- package/clients/knip-client.test.ts +123 -133
- package/clients/knip-client.ts +231 -218
- package/clients/metrics-client.test.ts +171 -167
- package/clients/metrics-client.ts +283 -252
- package/clients/ruff-client.test.ts +128 -117
- package/clients/ruff-client.ts +300 -269
- package/clients/rust-client.test.ts +104 -85
- package/clients/rust-client.ts +241 -234
- package/clients/subprocess-client.ts +1 -1
- package/clients/test-runner-client.test.ts +248 -215
- package/clients/test-runner-client.ts +728 -608
- package/clients/test-utils.ts +10 -3
- package/clients/todo-scanner.test.ts +288 -202
- package/clients/todo-scanner.ts +225 -187
- package/clients/type-coverage-client.test.ts +119 -119
- package/clients/type-coverage-client.ts +142 -115
- package/clients/types.ts +28 -28
- package/clients/typescript-client.test.ts +99 -93
- package/clients/typescript-client.ts +527 -502
- package/index.ts +662 -212
- package/package.json +1 -1
- package/tsconfig.json +12 -12
package/clients/ruff-client.ts
CHANGED
|
@@ -9,287 +9,318 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
import { spawnSync } from "node:child_process";
|
|
12
|
-
import * as path from "node:path";
|
|
13
12
|
import * as fs from "node:fs";
|
|
13
|
+
import * as path from "node:path";
|
|
14
14
|
|
|
15
15
|
// --- Types ---
|
|
16
16
|
|
|
17
17
|
export interface RuffDiagnostic {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
line: number;
|
|
19
|
+
column: number;
|
|
20
|
+
endLine: number;
|
|
21
|
+
endColumn: number;
|
|
22
|
+
severity: "error" | "warning";
|
|
23
|
+
message: string;
|
|
24
|
+
rule: string;
|
|
25
|
+
file: string;
|
|
26
|
+
fixable: boolean;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
// ruff check --output-format json
|
|
30
30
|
interface RuffJsonDiagnostic {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
code: string | null;
|
|
32
|
+
message: string;
|
|
33
|
+
location: { row: number; column: number };
|
|
34
|
+
end_location: { row: number; column: number };
|
|
35
|
+
fix: { applicability: string } | null;
|
|
36
|
+
filename: string;
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
// --- Client ---
|
|
40
40
|
|
|
41
41
|
export class RuffClient {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
42
|
+
private ruffAvailable: boolean | null = null;
|
|
43
|
+
private log: (msg: string) => void;
|
|
44
|
+
|
|
45
|
+
constructor(verbose = false) {
|
|
46
|
+
this.log = verbose
|
|
47
|
+
? (msg: string) => console.log(`[ruff] ${msg}`)
|
|
48
|
+
: () => {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Check if ruff CLI is available
|
|
53
|
+
*/
|
|
54
|
+
isAvailable(): boolean {
|
|
55
|
+
if (this.ruffAvailable !== null) return this.ruffAvailable;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const result = spawnSync("ruff", ["--version"], {
|
|
59
|
+
encoding: "utf-8",
|
|
60
|
+
timeout: 5000,
|
|
61
|
+
shell: true,
|
|
62
|
+
});
|
|
63
|
+
this.ruffAvailable = !result.error && result.status === 0;
|
|
64
|
+
if (this.ruffAvailable) {
|
|
65
|
+
this.log(`Ruff found: ${result.stdout.trim()}`);
|
|
66
|
+
}
|
|
67
|
+
} catch {
|
|
68
|
+
this.ruffAvailable = false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return this.ruffAvailable;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check if a file is a Python file
|
|
76
|
+
*/
|
|
77
|
+
isPythonFile(filePath: string): boolean {
|
|
78
|
+
return path.extname(filePath).toLowerCase() === ".py";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Lint a Python file
|
|
83
|
+
*/
|
|
84
|
+
checkFile(filePath: string): RuffDiagnostic[] {
|
|
85
|
+
if (!this.isAvailable()) return [];
|
|
86
|
+
|
|
87
|
+
const absolutePath = path.resolve(filePath);
|
|
88
|
+
if (!fs.existsSync(absolutePath)) return [];
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
const result = spawnSync(
|
|
92
|
+
"ruff",
|
|
93
|
+
[
|
|
94
|
+
"check",
|
|
95
|
+
"--output-format",
|
|
96
|
+
"json",
|
|
97
|
+
"--target-version",
|
|
98
|
+
"py310",
|
|
99
|
+
absolutePath,
|
|
100
|
+
],
|
|
101
|
+
{
|
|
102
|
+
encoding: "utf-8",
|
|
103
|
+
timeout: 10000,
|
|
104
|
+
shell: true,
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// ruff exits 1 when it finds issues (normal)
|
|
109
|
+
const output = result.stdout || "";
|
|
110
|
+
if (!output.trim()) return [];
|
|
111
|
+
|
|
112
|
+
return this.parseOutput(output, absolutePath);
|
|
113
|
+
} catch (err: any) {
|
|
114
|
+
this.log(`Check error: ${err.message}`);
|
|
115
|
+
return [];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Check if file has formatting issues (ruff format --check)
|
|
121
|
+
*/
|
|
122
|
+
checkFormatting(filePath: string): string {
|
|
123
|
+
if (!this.isAvailable()) return "";
|
|
124
|
+
|
|
125
|
+
const absolutePath = path.resolve(filePath);
|
|
126
|
+
if (!fs.existsSync(absolutePath)) return "";
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const result = spawnSync(
|
|
130
|
+
"ruff",
|
|
131
|
+
["format", "--check", "--diff", absolutePath],
|
|
132
|
+
{
|
|
133
|
+
encoding: "utf-8",
|
|
134
|
+
timeout: 10000,
|
|
135
|
+
shell: true,
|
|
136
|
+
},
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
// ruff format --check exits 1 when changes needed
|
|
140
|
+
if (result.status === 0) return "";
|
|
141
|
+
|
|
142
|
+
const diff = result.stdout || "";
|
|
143
|
+
if (!diff.trim()) return "";
|
|
144
|
+
|
|
145
|
+
// Count lines that would change
|
|
146
|
+
const diffLines = diff
|
|
147
|
+
.split("\n")
|
|
148
|
+
.filter((l) => l.startsWith("+") || l.startsWith("-")).length;
|
|
149
|
+
return `[Ruff Format] ${diffLines} line(s) would change — run 'ruff format ${path.basename(filePath)}' to fix`;
|
|
150
|
+
} catch {
|
|
151
|
+
return "";
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Auto-fix linting issues (writes to disk)
|
|
157
|
+
*/
|
|
158
|
+
fixFile(filePath: string): {
|
|
159
|
+
success: boolean;
|
|
160
|
+
changed: boolean;
|
|
161
|
+
fixed: number;
|
|
162
|
+
error?: string;
|
|
163
|
+
} {
|
|
164
|
+
if (!this.isAvailable())
|
|
165
|
+
return {
|
|
166
|
+
success: false,
|
|
167
|
+
changed: false,
|
|
168
|
+
fixed: 0,
|
|
169
|
+
error: "Ruff not available",
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const absolutePath = path.resolve(filePath);
|
|
173
|
+
if (!fs.existsSync(absolutePath))
|
|
174
|
+
return {
|
|
175
|
+
success: false,
|
|
176
|
+
changed: false,
|
|
177
|
+
fixed: 0,
|
|
178
|
+
error: "File not found",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const content = fs.readFileSync(absolutePath, "utf-8");
|
|
182
|
+
|
|
183
|
+
try {
|
|
184
|
+
const beforeDiags = this.checkFile(filePath);
|
|
185
|
+
const fixableCount = beforeDiags.filter((d) => d.fixable).length;
|
|
186
|
+
|
|
187
|
+
const result = spawnSync("ruff", ["check", "--fix", absolutePath], {
|
|
188
|
+
encoding: "utf-8",
|
|
189
|
+
timeout: 15000,
|
|
190
|
+
shell: true,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (result.error) {
|
|
194
|
+
return {
|
|
195
|
+
success: false,
|
|
196
|
+
changed: false,
|
|
197
|
+
fixed: 0,
|
|
198
|
+
error: result.error.message,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const fixed = fs.readFileSync(absolutePath, "utf-8");
|
|
203
|
+
const changed = content !== fixed;
|
|
204
|
+
|
|
205
|
+
if (changed) {
|
|
206
|
+
this.log(
|
|
207
|
+
`Fixed ${fixableCount} issue(s) in ${path.basename(filePath)}`,
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return { success: true, changed, fixed: fixableCount };
|
|
212
|
+
} catch (err: any) {
|
|
213
|
+
return { success: false, changed: false, fixed: 0, error: err.message };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Format a Python file (writes to disk)
|
|
219
|
+
*/
|
|
220
|
+
formatFile(filePath: string): {
|
|
221
|
+
success: boolean;
|
|
222
|
+
changed: boolean;
|
|
223
|
+
error?: string;
|
|
224
|
+
} {
|
|
225
|
+
if (!this.isAvailable())
|
|
226
|
+
return { success: false, changed: false, error: "Ruff not available" };
|
|
227
|
+
|
|
228
|
+
const absolutePath = path.resolve(filePath);
|
|
229
|
+
if (!fs.existsSync(absolutePath))
|
|
230
|
+
return { success: false, changed: false, error: "File not found" };
|
|
231
|
+
|
|
232
|
+
const content = fs.readFileSync(absolutePath, "utf-8");
|
|
233
|
+
|
|
234
|
+
try {
|
|
235
|
+
const result = spawnSync("ruff", ["format", absolutePath], {
|
|
236
|
+
encoding: "utf-8",
|
|
237
|
+
timeout: 10000,
|
|
238
|
+
shell: true,
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
if (result.error) {
|
|
242
|
+
return { success: false, changed: false, error: result.error.message };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const formatted = fs.readFileSync(absolutePath, "utf-8");
|
|
246
|
+
const changed = content !== formatted;
|
|
247
|
+
|
|
248
|
+
if (changed) {
|
|
249
|
+
this.log(`Formatted ${path.basename(filePath)}`);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return { success: true, changed };
|
|
253
|
+
} catch (err: any) {
|
|
254
|
+
return { success: false, changed: false, error: err.message };
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Format diagnostics for LLM consumption
|
|
260
|
+
*/
|
|
261
|
+
formatDiagnostics(diags: RuffDiagnostic[]): string {
|
|
262
|
+
if (diags.length === 0) return "";
|
|
263
|
+
|
|
264
|
+
const errors = diags.filter((d) => d.severity === "error");
|
|
265
|
+
const warnings = diags.filter((d) => d.severity === "warning");
|
|
266
|
+
const fixable = diags.filter((d) => d.fixable);
|
|
267
|
+
|
|
268
|
+
let result = `[Ruff] ${diags.length} issue(s)`;
|
|
269
|
+
if (errors.length) result += ` — ${errors.length} error(s)`;
|
|
270
|
+
if (warnings.length) result += ` — ${warnings.length} warning(s)`;
|
|
271
|
+
if (fixable.length) result += ` — ${fixable.length} auto-fixable`;
|
|
272
|
+
result += ":\n";
|
|
273
|
+
|
|
274
|
+
for (const d of diags.slice(0, 15)) {
|
|
275
|
+
const loc =
|
|
276
|
+
d.line === d.endLine
|
|
277
|
+
? `L${d.line}:${d.column}-${d.endColumn}`
|
|
278
|
+
: `L${d.line}:${d.column}-L${d.endLine}:${d.endColumn}`;
|
|
279
|
+
const fix = d.fixable ? " [fixable]" : "";
|
|
280
|
+
result += ` [${d.rule}] ${loc} ${d.message}${fix}\n`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (diags.length > 15) {
|
|
284
|
+
result += ` ... and ${diags.length - 15} more\n`;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
if (fixable.length > 0) {
|
|
288
|
+
result += `\n Run 'ruff check --fix ${path.basename(diags[0].file)}' to auto-fix ${fixable.length} issue(s)\n`;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return result;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// --- Internal ---
|
|
295
|
+
|
|
296
|
+
private parseOutput(output: string, filterFile?: string): RuffDiagnostic[] {
|
|
297
|
+
if (!output.trim()) return [];
|
|
298
|
+
|
|
299
|
+
try {
|
|
300
|
+
const items: RuffJsonDiagnostic[] = JSON.parse(output);
|
|
301
|
+
const diagnostics: RuffDiagnostic[] = [];
|
|
302
|
+
|
|
303
|
+
for (const item of items) {
|
|
304
|
+
// Filter to single file if requested
|
|
305
|
+
if (filterFile && path.resolve(item.filename) !== filterFile) continue;
|
|
306
|
+
|
|
307
|
+
diagnostics.push({
|
|
308
|
+
line: item.location.row - 1, // ruff is 1-indexed
|
|
309
|
+
column: item.location.column - 1,
|
|
310
|
+
endLine: item.end_location.row - 1,
|
|
311
|
+
endColumn: item.end_location.column - 1,
|
|
312
|
+
severity: item.code?.startsWith("E") ? "error" : "warning",
|
|
313
|
+
message: item.message,
|
|
314
|
+
rule: item.code || "unknown",
|
|
315
|
+
file: item.filename,
|
|
316
|
+
fixable: item.fix !== null,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return diagnostics;
|
|
321
|
+
} catch {
|
|
322
|
+
this.log("Failed to parse ruff JSON output");
|
|
323
|
+
return [];
|
|
324
|
+
}
|
|
325
|
+
}
|
|
295
326
|
}
|