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.
Files changed (34) hide show
  1. package/clients/ast-grep-client.test.ts +146 -116
  2. package/clients/ast-grep-client.ts +645 -551
  3. package/clients/biome-client.test.ts +154 -137
  4. package/clients/biome-client.ts +397 -337
  5. package/clients/complexity-client.test.ts +188 -200
  6. package/clients/complexity-client.ts +815 -667
  7. package/clients/dependency-checker.test.ts +55 -55
  8. package/clients/dependency-checker.ts +358 -333
  9. package/clients/go-client.test.ts +121 -111
  10. package/clients/go-client.ts +218 -216
  11. package/clients/jscpd-client.test.ts +132 -132
  12. package/clients/jscpd-client.ts +155 -118
  13. package/clients/knip-client.test.ts +123 -133
  14. package/clients/knip-client.ts +231 -218
  15. package/clients/metrics-client.test.ts +171 -167
  16. package/clients/metrics-client.ts +283 -252
  17. package/clients/ruff-client.test.ts +128 -117
  18. package/clients/ruff-client.ts +300 -269
  19. package/clients/rust-client.test.ts +104 -85
  20. package/clients/rust-client.ts +241 -234
  21. package/clients/subprocess-client.ts +1 -1
  22. package/clients/test-runner-client.test.ts +248 -215
  23. package/clients/test-runner-client.ts +728 -608
  24. package/clients/test-utils.ts +10 -3
  25. package/clients/todo-scanner.test.ts +288 -202
  26. package/clients/todo-scanner.ts +225 -187
  27. package/clients/type-coverage-client.test.ts +119 -119
  28. package/clients/type-coverage-client.ts +142 -115
  29. package/clients/types.ts +28 -28
  30. package/clients/typescript-client.test.ts +99 -93
  31. package/clients/typescript-client.ts +527 -502
  32. package/index.ts +662 -212
  33. package/package.json +1 -1
  34. package/tsconfig.json +12 -12
@@ -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
- 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;
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
- 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;
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
- 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("ruff", [
92
- "check",
93
- "--output-format", "json",
94
- "--target-version", "py310",
95
- absolutePath,
96
- ], {
97
- encoding: "utf-8",
98
- timeout: 10000,
99
- shell: true,
100
- });
101
-
102
- // ruff exits 1 when it finds issues (normal)
103
- const output = result.stdout || "";
104
- if (!output.trim()) return [];
105
-
106
- return this.parseOutput(output, absolutePath);
107
- } catch (err: any) {
108
- this.log(`Check error: ${err.message}`);
109
- return [];
110
- }
111
- }
112
-
113
- /**
114
- * Check if file has formatting issues (ruff format --check)
115
- */
116
- checkFormatting(filePath: string): string {
117
- if (!this.isAvailable()) return "";
118
-
119
- const absolutePath = path.resolve(filePath);
120
- if (!fs.existsSync(absolutePath)) return "";
121
-
122
- try {
123
- const result = spawnSync("ruff", [
124
- "format",
125
- "--check",
126
- "--diff",
127
- absolutePath,
128
- ], {
129
- encoding: "utf-8",
130
- timeout: 10000,
131
- shell: true,
132
- });
133
-
134
- // ruff format --check exits 1 when changes needed
135
- if (result.status === 0) return "";
136
-
137
- const diff = result.stdout || "";
138
- if (!diff.trim()) return "";
139
-
140
- // Count lines that would change
141
- const diffLines = diff.split("\n").filter(l => l.startsWith("+") || l.startsWith("-")).length;
142
- return `[Ruff Format] ${diffLines} line(s) would change — run 'ruff format ${path.basename(filePath)}' to fix`;
143
- } catch {
144
- return "";
145
- }
146
- }
147
-
148
- /**
149
- * Auto-fix linting issues (writes to disk)
150
- */
151
- fixFile(filePath: string): { success: boolean; changed: boolean; fixed: number; error?: string } {
152
- if (!this.isAvailable()) return { success: false, changed: false, fixed: 0, error: "Ruff not available" };
153
-
154
- const absolutePath = path.resolve(filePath);
155
- if (!fs.existsSync(absolutePath)) return { success: false, changed: false, fixed: 0, error: "File not found" };
156
-
157
- const content = fs.readFileSync(absolutePath, "utf-8");
158
-
159
- try {
160
- const beforeDiags = this.checkFile(filePath);
161
- const fixableCount = beforeDiags.filter(d => d.fixable).length;
162
-
163
- const result = spawnSync("ruff", [
164
- "check",
165
- "--fix",
166
- absolutePath,
167
- ], {
168
- encoding: "utf-8",
169
- timeout: 15000,
170
- shell: true,
171
- });
172
-
173
- if (result.error) {
174
- return { success: false, changed: false, fixed: 0, error: result.error.message };
175
- }
176
-
177
- const fixed = fs.readFileSync(absolutePath, "utf-8");
178
- const changed = content !== fixed;
179
-
180
- if (changed) {
181
- this.log(`Fixed ${fixableCount} issue(s) in ${path.basename(filePath)}`);
182
- }
183
-
184
- return { success: true, changed, fixed: fixableCount };
185
- } catch (err: any) {
186
- return { success: false, changed: false, fixed: 0, error: err.message };
187
- }
188
- }
189
-
190
- /**
191
- * Format a Python file (writes to disk)
192
- */
193
- formatFile(filePath: string): { success: boolean; changed: boolean; error?: string } {
194
- if (!this.isAvailable()) return { success: false, changed: false, error: "Ruff not available" };
195
-
196
- const absolutePath = path.resolve(filePath);
197
- if (!fs.existsSync(absolutePath)) return { success: false, changed: false, error: "File not found" };
198
-
199
- const content = fs.readFileSync(absolutePath, "utf-8");
200
-
201
- try {
202
- const result = spawnSync("ruff", [
203
- "format",
204
- absolutePath,
205
- ], {
206
- encoding: "utf-8",
207
- timeout: 10000,
208
- shell: true,
209
- });
210
-
211
- if (result.error) {
212
- return { success: false, changed: false, error: result.error.message };
213
- }
214
-
215
- const formatted = fs.readFileSync(absolutePath, "utf-8");
216
- const changed = content !== formatted;
217
-
218
- if (changed) {
219
- this.log(`Formatted ${path.basename(filePath)}`);
220
- }
221
-
222
- return { success: true, changed };
223
- } catch (err: any) {
224
- return { success: false, changed: false, error: err.message };
225
- }
226
- }
227
-
228
- /**
229
- * Format diagnostics for LLM consumption
230
- */
231
- formatDiagnostics(diags: RuffDiagnostic[]): string {
232
- if (diags.length === 0) return "";
233
-
234
- const errors = diags.filter((d) => d.severity === "error");
235
- const warnings = diags.filter((d) => d.severity === "warning");
236
- const fixable = diags.filter((d) => d.fixable);
237
-
238
- let result = `[Ruff] ${diags.length} issue(s)`;
239
- if (errors.length) result += ` — ${errors.length} error(s)`;
240
- if (warnings.length) result += ` — ${warnings.length} warning(s)`;
241
- if (fixable.length) result += ` — ${fixable.length} auto-fixable`;
242
- result += ":\n";
243
-
244
- for (const d of diags.slice(0, 15)) {
245
- const loc = d.line === d.endLine
246
- ? `L${d.line}:${d.column}-${d.endColumn}`
247
- : `L${d.line}:${d.column}-L${d.endLine}:${d.endColumn}`;
248
- const fix = d.fixable ? " [fixable]" : "";
249
- result += ` [${d.rule}] ${loc} ${d.message}${fix}\n`;
250
- }
251
-
252
- if (diags.length > 15) {
253
- result += ` ... and ${diags.length - 15} more\n`;
254
- }
255
-
256
- if (fixable.length > 0) {
257
- result += `\n Run 'ruff check --fix ${path.basename(diags[0].file)}' to auto-fix ${fixable.length} issue(s)\n`;
258
- }
259
-
260
- return result;
261
- }
262
-
263
- // --- Internal ---
264
-
265
- private parseOutput(output: string, filterFile?: string): RuffDiagnostic[] {
266
- if (!output.trim()) return [];
267
-
268
- try {
269
- const items: RuffJsonDiagnostic[] = JSON.parse(output);
270
- const diagnostics: RuffDiagnostic[] = [];
271
-
272
- for (const item of items) {
273
- // Filter to single file if requested
274
- if (filterFile && path.resolve(item.filename) !== filterFile) continue;
275
-
276
- diagnostics.push({
277
- line: item.location.row - 1, // ruff is 1-indexed
278
- column: item.location.column - 1,
279
- endLine: item.end_location.row - 1,
280
- endColumn: item.end_location.column - 1,
281
- severity: item.code?.startsWith("E") ? "error" : "warning",
282
- message: item.message,
283
- rule: item.code || "unknown",
284
- file: item.filename,
285
- fixable: item.fix !== null,
286
- });
287
- }
288
-
289
- return diagnostics;
290
- } catch {
291
- this.log("Failed to parse ruff JSON output");
292
- return [];
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
  }