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
@@ -1,48 +1,48 @@
1
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
1
+ import { afterEach, beforeEach, describe, expect, it } from "vitest";
2
2
  import { JscpdClient } from "./jscpd-client.js";
3
3
  import { createTempFile, setupTestEnvironment } from "./test-utils.js";
4
4
 
5
5
  describe("JscpdClient", () => {
6
- let client: JscpdClient;
7
- let tmpDir: string;
8
- let cleanup: () => void;
9
-
10
- beforeEach(() => {
11
- client = new JscpdClient();
12
- ({ tmpDir, cleanup } = setupTestEnvironment("pi-lens-jscpd-test-"));
13
- });
14
-
15
- afterEach(() => {
16
- cleanup();
17
- });
18
-
19
- afterEach(() => {
20
- cleanup();
21
- });
22
-
23
- describe("isAvailable", () => {
24
- it("should check jscpd availability", () => {
25
- const available = client.isAvailable();
26
- expect(typeof available).toBe("boolean");
27
- });
28
- });
29
-
30
- describe("scan", () => {
31
- it("should return success=false when not available", () => {
32
- // Create a mock that returns false
33
- const mockClient = new JscpdClient();
34
- if (mockClient.isAvailable()) return; // Skip if available
35
-
36
- const result = mockClient.scan(tmpDir);
37
- expect(result.success).toBe(false);
38
- expect(result.clones).toEqual([]);
39
- });
40
-
41
- it("should detect duplicate code blocks", () => {
42
- if (!client.isAvailable()) return;
43
-
44
- // Create identical code blocks in different files
45
- const duplicateCode = `
6
+ let client: JscpdClient;
7
+ let tmpDir: string;
8
+ let cleanup: () => void;
9
+
10
+ beforeEach(() => {
11
+ client = new JscpdClient();
12
+ ({ tmpDir, cleanup } = setupTestEnvironment("pi-lens-jscpd-test-"));
13
+ });
14
+
15
+ afterEach(() => {
16
+ cleanup();
17
+ });
18
+
19
+ afterEach(() => {
20
+ cleanup();
21
+ });
22
+
23
+ describe("isAvailable", () => {
24
+ it("should check jscpd availability", () => {
25
+ const available = client.isAvailable();
26
+ expect(typeof available).toBe("boolean");
27
+ });
28
+ });
29
+
30
+ describe("scan", () => {
31
+ it("should return success=false when not available", () => {
32
+ // Create a mock that returns false
33
+ const mockClient = new JscpdClient();
34
+ if (mockClient.isAvailable()) return; // Skip if available
35
+
36
+ const result = mockClient.scan(tmpDir);
37
+ expect(result.success).toBe(false);
38
+ expect(result.clones).toEqual([]);
39
+ });
40
+
41
+ it("should detect duplicate code blocks", () => {
42
+ if (!client.isAvailable()) return;
43
+
44
+ // Create identical code blocks in different files
45
+ const duplicateCode = `
46
46
  function processData(data: number[]): number {
47
47
  let sum = 0;
48
48
  for (let i = 0; i < data.length; i++) {
@@ -51,95 +51,95 @@ function processData(data: number[]): number {
51
51
  return sum;
52
52
  }
53
53
  `;
54
- createTempFile(tmpDir, "file1.ts", duplicateCode);
55
- createTempFile(tmpDir, "file2.ts", duplicateCode);
56
-
57
- const result = client.scan(tmpDir, 3, 20); // Lower thresholds for test
58
-
59
- expect(result.success).toBe(true);
60
- // May or may not detect clones depending on jscpd behavior
61
- });
62
- });
63
-
64
- describe("formatResult", () => {
65
- it("should return empty string for no success", () => {
66
- const result = {
67
- success: false,
68
- clones: [],
69
- duplicatedLines: 0,
70
- totalLines: 100,
71
- percentage: 0,
72
- };
73
-
74
- expect(client.formatResult(result)).toBe("");
75
- });
76
-
77
- it("should return empty string for no clones", () => {
78
- const result = {
79
- success: true,
80
- clones: [],
81
- duplicatedLines: 0,
82
- totalLines: 100,
83
- percentage: 0,
84
- };
85
-
86
- expect(client.formatResult(result)).toBe("");
87
- });
88
-
89
- it("should format clones for display", () => {
90
- const result = {
91
- success: true,
92
- clones: [
93
- {
94
- fileA: "src/file1.ts",
95
- startA: 10,
96
- fileB: "src/file2.ts",
97
- startB: 20,
98
- lines: 15,
99
- tokens: 50,
100
- },
101
- {
102
- fileA: "src/file3.ts",
103
- startA: 5,
104
- fileB: "src/file4.ts",
105
- startB: 12,
106
- lines: 8,
107
- tokens: 30,
108
- },
109
- ],
110
- duplicatedLines: 23,
111
- totalLines: 500,
112
- percentage: 4.6,
113
- };
114
-
115
- const formatted = client.formatResult(result);
116
- expect(formatted).toContain("jscpd");
117
- expect(formatted).toContain("2 duplicate block(s)");
118
- expect(formatted).toContain("4.6%");
119
- expect(formatted).toContain("15 lines");
120
- });
121
-
122
- it("should truncate long clone lists", () => {
123
- const clones = Array.from({ length: 10 }, (_, i) => ({
124
- fileA: `file${i}a.ts`,
125
- startA: 1,
126
- fileB: `file${i}b.ts`,
127
- startB: 1,
128
- lines: 5,
129
- tokens: 20,
130
- }));
131
-
132
- const result = {
133
- success: true,
134
- clones,
135
- duplicatedLines: 50,
136
- totalLines: 1000,
137
- percentage: 5,
138
- };
139
-
140
- const formatted = client.formatResult(result, 8);
141
- expect(formatted).toContain("...");
142
- expect(formatted).toContain("2 more");
143
- });
144
- });
54
+ createTempFile(tmpDir, "file1.ts", duplicateCode);
55
+ createTempFile(tmpDir, "file2.ts", duplicateCode);
56
+
57
+ const result = client.scan(tmpDir, 3, 20); // Lower thresholds for test
58
+
59
+ expect(result.success).toBe(true);
60
+ // May or may not detect clones depending on jscpd behavior
61
+ });
62
+ });
63
+
64
+ describe("formatResult", () => {
65
+ it("should return empty string for no success", () => {
66
+ const result = {
67
+ success: false,
68
+ clones: [],
69
+ duplicatedLines: 0,
70
+ totalLines: 100,
71
+ percentage: 0,
72
+ };
73
+
74
+ expect(client.formatResult(result)).toBe("");
75
+ });
76
+
77
+ it("should return empty string for no clones", () => {
78
+ const result = {
79
+ success: true,
80
+ clones: [],
81
+ duplicatedLines: 0,
82
+ totalLines: 100,
83
+ percentage: 0,
84
+ };
85
+
86
+ expect(client.formatResult(result)).toBe("");
87
+ });
88
+
89
+ it("should format clones for display", () => {
90
+ const result = {
91
+ success: true,
92
+ clones: [
93
+ {
94
+ fileA: "src/file1.ts",
95
+ startA: 10,
96
+ fileB: "src/file2.ts",
97
+ startB: 20,
98
+ lines: 15,
99
+ tokens: 50,
100
+ },
101
+ {
102
+ fileA: "src/file3.ts",
103
+ startA: 5,
104
+ fileB: "src/file4.ts",
105
+ startB: 12,
106
+ lines: 8,
107
+ tokens: 30,
108
+ },
109
+ ],
110
+ duplicatedLines: 23,
111
+ totalLines: 500,
112
+ percentage: 4.6,
113
+ };
114
+
115
+ const formatted = client.formatResult(result);
116
+ expect(formatted).toContain("jscpd");
117
+ expect(formatted).toContain("2 duplicate block(s)");
118
+ expect(formatted).toContain("4.6%");
119
+ expect(formatted).toContain("15 lines");
120
+ });
121
+
122
+ it("should truncate long clone lists", () => {
123
+ const clones = Array.from({ length: 10 }, (_, i) => ({
124
+ fileA: `file${i}a.ts`,
125
+ startA: 1,
126
+ fileB: `file${i}b.ts`,
127
+ startB: 1,
128
+ lines: 5,
129
+ tokens: 20,
130
+ }));
131
+
132
+ const result = {
133
+ success: true,
134
+ clones,
135
+ duplicatedLines: 50,
136
+ totalLines: 1000,
137
+ percentage: 5,
138
+ };
139
+
140
+ const formatted = client.formatResult(result, 8);
141
+ expect(formatted).toContain("...");
142
+ expect(formatted).toContain("2 more");
143
+ });
144
+ });
145
145
  });
@@ -9,136 +9,173 @@
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";
14
13
  import * as os from "node:os";
14
+ import * as path from "node:path";
15
15
 
16
16
  // --- Types ---
17
17
 
18
18
  export interface DuplicateClone {
19
- fileA: string;
20
- startA: number;
21
- fileB: string;
22
- startB: number;
23
- lines: number;
24
- tokens: number;
19
+ fileA: string;
20
+ startA: number;
21
+ fileB: string;
22
+ startB: number;
23
+ lines: number;
24
+ tokens: number;
25
25
  }
26
26
 
27
27
  export interface JscpdResult {
28
- success: boolean;
29
- clones: DuplicateClone[];
30
- duplicatedLines: number;
31
- totalLines: number;
32
- percentage: number;
28
+ success: boolean;
29
+ clones: DuplicateClone[];
30
+ duplicatedLines: number;
31
+ totalLines: number;
32
+ percentage: number;
33
33
  }
34
34
 
35
35
  // --- Client ---
36
36
 
37
37
  export class JscpdClient {
38
- private available: boolean | null = null;
39
- private log: (msg: string) => void;
40
-
41
- constructor(verbose = false) {
42
- this.log = verbose ? (msg) => console.log(`[jscpd] ${msg}`) : () => {};
43
- }
44
-
45
- isAvailable(): boolean {
46
- if (this.available !== null) return this.available;
47
- const result = spawnSync("npx", ["jscpd", "--version"], {
48
- encoding: "utf-8",
49
- timeout: 10000,
50
- shell: true,
51
- });
52
- this.available = !result.error && result.status === 0;
53
- return this.available;
54
- }
55
-
56
- /**
57
- * Scan a directory for duplicate code blocks.
58
- * Uses a temp output dir to capture JSON report.
59
- */
60
- scan(cwd: string, minLines = 5, minTokens = 50): JscpdResult {
61
- if (!this.isAvailable()) {
62
- return { success: false, clones: [], duplicatedLines: 0, totalLines: 0, percentage: 0 };
63
- }
64
-
65
- const outDir = path.join(os.tmpdir(), `pi-lens-jscpd-${Date.now()}`);
66
- fs.mkdirSync(outDir, { recursive: true });
67
-
68
- try {
69
- spawnSync("npx", [
70
- "jscpd",
71
- ".",
72
- "--min-lines", String(minLines),
73
- "--min-tokens", String(minTokens),
74
- "--reporters", "json",
75
- "--output", outDir,
76
- "--ignore", "**/node_modules/**,**/dist/**,**/build/**,**/.git/**,**/.pi-lens/**,**/*.md,**/*.txt,**/*.json,**/*.yaml,**/*.yml,**/*.toml,**/*.lock",
77
- ], {
78
- encoding: "utf-8",
79
- timeout: 30000,
80
- cwd,
81
- shell: true,
82
- });
83
-
84
- const reportPath = path.join(outDir, "jscpd-report.json");
85
- if (!fs.existsSync(reportPath)) {
86
- return { success: true, clones: [], duplicatedLines: 0, totalLines: 0, percentage: 0 };
87
- }
88
-
89
- return this.parseReport(reportPath);
90
- } catch (err: any) {
91
- this.log(`Scan error: ${err.message}`);
92
- return { success: false, clones: [], duplicatedLines: 0, totalLines: 0, percentage: 0 };
93
- } finally {
94
- try { fs.rmSync(outDir, { recursive: true, force: true }); } catch {}
95
- }
96
- }
97
-
98
- formatResult(result: JscpdResult, maxClones = 8): string {
99
- if (!result.success || result.clones.length === 0) return "";
100
-
101
- const pct = result.percentage.toFixed(1);
102
- let output = `[jscpd] ${result.clones.length} duplicate block(s) — ${pct}% of codebase (${result.duplicatedLines}/${result.totalLines} lines):\n`;
103
-
104
- for (const clone of result.clones.slice(0, maxClones)) {
105
- const a = `${path.basename(clone.fileA)}:${clone.startA}`;
106
- const b = `${path.basename(clone.fileB)}:${clone.startB}`;
107
- output += ` ${clone.lines} lines — ${a} ↔ ${b}\n`;
108
- }
109
-
110
- if (result.clones.length > maxClones) {
111
- output += ` ... and ${result.clones.length - maxClones} more\n`;
112
- }
113
-
114
- return output;
115
- }
116
-
117
- // --- Internal ---
118
-
119
- private parseReport(reportPath: string): JscpdResult {
120
- try {
121
- const data = JSON.parse(fs.readFileSync(reportPath, "utf-8"));
122
- // Stats live in statistics.total, not statistics.clones
123
- const total = data.statistics?.total ?? {};
124
-
125
- const duplicatedLines: number = total.duplicatedLines ?? 0;
126
- const totalLines: number = total.lines ?? 0;
127
- const percentage: number = total.percentage ?? (totalLines > 0 ? (duplicatedLines / totalLines) * 100 : 0);
128
-
129
- const rawClones: any[] = data.duplicates ?? [];
130
- const clones: DuplicateClone[] = rawClones.map((c: any) => ({
131
- fileA: c.firstFile?.name ?? "",
132
- startA: c.firstFile?.start ?? 0,
133
- fileB: c.secondFile?.name ?? "",
134
- startB: c.secondFile?.start ?? 0,
135
- lines: c.lines ?? 0,
136
- tokens: c.tokens ?? 0,
137
- }));
138
-
139
- return { success: true, clones, duplicatedLines, totalLines, percentage };
140
- } catch {
141
- return { success: false, clones: [], duplicatedLines: 0, totalLines: 0, percentage: 0 };
142
- }
143
- }
38
+ private available: boolean | null = null;
39
+ private log: (msg: string) => void;
40
+
41
+ constructor(verbose = false) {
42
+ this.log = verbose ? (msg) => console.log(`[jscpd] ${msg}`) : () => {};
43
+ }
44
+
45
+ isAvailable(): boolean {
46
+ if (this.available !== null) return this.available;
47
+ const result = spawnSync("npx", ["jscpd", "--version"], {
48
+ encoding: "utf-8",
49
+ timeout: 10000,
50
+ shell: true,
51
+ });
52
+ this.available = !result.error && result.status === 0;
53
+ return this.available;
54
+ }
55
+
56
+ /**
57
+ * Scan a directory for duplicate code blocks.
58
+ * Uses a temp output dir to capture JSON report.
59
+ */
60
+ scan(cwd: string, minLines = 5, minTokens = 50): JscpdResult {
61
+ if (!this.isAvailable()) {
62
+ return {
63
+ success: false,
64
+ clones: [],
65
+ duplicatedLines: 0,
66
+ totalLines: 0,
67
+ percentage: 0,
68
+ };
69
+ }
70
+
71
+ const outDir = path.join(os.tmpdir(), `pi-lens-jscpd-${Date.now()}`);
72
+ fs.mkdirSync(outDir, { recursive: true });
73
+
74
+ try {
75
+ spawnSync(
76
+ "npx",
77
+ [
78
+ "jscpd",
79
+ ".",
80
+ "--min-lines",
81
+ String(minLines),
82
+ "--min-tokens",
83
+ String(minTokens),
84
+ "--reporters",
85
+ "json",
86
+ "--output",
87
+ outDir,
88
+ "--ignore",
89
+ "**/node_modules/**,**/dist/**,**/build/**,**/.git/**,**/.pi-lens/**,**/*.md,**/*.txt,**/*.json,**/*.yaml,**/*.yml,**/*.toml,**/*.lock",
90
+ ],
91
+ {
92
+ encoding: "utf-8",
93
+ timeout: 30000,
94
+ cwd,
95
+ shell: true,
96
+ },
97
+ );
98
+
99
+ const reportPath = path.join(outDir, "jscpd-report.json");
100
+ if (!fs.existsSync(reportPath)) {
101
+ return {
102
+ success: true,
103
+ clones: [],
104
+ duplicatedLines: 0,
105
+ totalLines: 0,
106
+ percentage: 0,
107
+ };
108
+ }
109
+
110
+ return this.parseReport(reportPath);
111
+ } catch (err: any) {
112
+ this.log(`Scan error: ${err.message}`);
113
+ return {
114
+ success: false,
115
+ clones: [],
116
+ duplicatedLines: 0,
117
+ totalLines: 0,
118
+ percentage: 0,
119
+ };
120
+ } finally {
121
+ try {
122
+ fs.rmSync(outDir, { recursive: true, force: true });
123
+ } catch (err) { void err; }
124
+ }
125
+ }
126
+
127
+ formatResult(result: JscpdResult, maxClones = 8): string {
128
+ if (!result.success || result.clones.length === 0) return "";
129
+
130
+ const pct = result.percentage.toFixed(1);
131
+ let output = `[jscpd] ${result.clones.length} duplicate block(s) — ${pct}% of codebase (${result.duplicatedLines}/${result.totalLines} lines):\n`;
132
+
133
+ for (const clone of result.clones.slice(0, maxClones)) {
134
+ const a = `${path.basename(clone.fileA)}:${clone.startA}`;
135
+ const b = `${path.basename(clone.fileB)}:${clone.startB}`;
136
+ output += ` ${clone.lines} lines — ${a} ↔ ${b}\n`;
137
+ }
138
+
139
+ if (result.clones.length > maxClones) {
140
+ output += ` ... and ${result.clones.length - maxClones} more\n`;
141
+ }
142
+
143
+ return output;
144
+ }
145
+
146
+ // --- Internal ---
147
+
148
+ private parseReport(reportPath: string): JscpdResult {
149
+ try {
150
+ const data = JSON.parse(fs.readFileSync(reportPath, "utf-8"));
151
+ // Stats live in statistics.total, not statistics.clones
152
+ const total = data.statistics?.total ?? {};
153
+
154
+ const duplicatedLines: number = total.duplicatedLines ?? 0;
155
+ const totalLines: number = total.lines ?? 0;
156
+ const percentage: number =
157
+ total.percentage ??
158
+ (totalLines > 0 ? (duplicatedLines / totalLines) * 100 : 0);
159
+
160
+ const rawClones: any[] = data.duplicates ?? [];
161
+ const clones: DuplicateClone[] = rawClones.map((c: any) => ({
162
+ fileA: c.firstFile?.name ?? "",
163
+ startA: c.firstFile?.start ?? 0,
164
+ fileB: c.secondFile?.name ?? "",
165
+ startB: c.secondFile?.start ?? 0,
166
+ lines: c.lines ?? 0,
167
+ tokens: c.tokens ?? 0,
168
+ }));
169
+
170
+ return { success: true, clones, duplicatedLines, totalLines, percentage };
171
+ } catch {
172
+ return {
173
+ success: false,
174
+ clones: [],
175
+ duplicatedLines: 0,
176
+ totalLines: 0,
177
+ percentage: 0,
178
+ };
179
+ }
180
+ }
144
181
  }