pi-lens 3.6.2 → 3.6.4

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 (207) hide show
  1. package/CHANGELOG.md +10 -2
  2. package/package.json +4 -4
  3. package/tsconfig.json +1 -1
  4. package/clients/__tests__/file-time.test.js +0 -216
  5. package/clients/__tests__/file-time.test.ts +0 -276
  6. package/clients/__tests__/format-service.test.js +0 -245
  7. package/clients/__tests__/format-service.test.ts +0 -339
  8. package/clients/__tests__/formatters.test.js +0 -271
  9. package/clients/__tests__/formatters.test.ts +0 -401
  10. package/clients/agent-behavior-client.js +0 -110
  11. package/clients/agent-behavior-client.test.js +0 -94
  12. package/clients/agent-behavior-client.test.ts +0 -116
  13. package/clients/amain-types.js +0 -164
  14. package/clients/architect-client.js +0 -291
  15. package/clients/ast-grep-client.js +0 -253
  16. package/clients/ast-grep-parser.js +0 -84
  17. package/clients/ast-grep-rule-manager.js +0 -89
  18. package/clients/ast-grep-types.js +0 -9
  19. package/clients/auto-loop.js +0 -131
  20. package/clients/biome-client.js +0 -420
  21. package/clients/biome-client.test.js +0 -144
  22. package/clients/biome-client.test.ts +0 -163
  23. package/clients/cache/rule-cache.js +0 -72
  24. package/clients/cache-manager.js +0 -245
  25. package/clients/cache-manager.test.js +0 -197
  26. package/clients/cache-manager.test.ts +0 -299
  27. package/clients/complexity-client.js +0 -675
  28. package/clients/complexity-client.test.js +0 -234
  29. package/clients/complexity-client.test.ts +0 -255
  30. package/clients/config-validator.js +0 -465
  31. package/clients/dependency-checker.js +0 -325
  32. package/clients/dependency-checker.test.js +0 -60
  33. package/clients/dependency-checker.test.ts +0 -71
  34. package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
  35. package/clients/dispatch/__tests__/autofix-integration.test.ts +0 -300
  36. package/clients/dispatch/__tests__/runner-registration.test.js +0 -234
  37. package/clients/dispatch/__tests__/runner-registration.test.ts +0 -286
  38. package/clients/dispatch/debug.log +0 -1
  39. package/clients/dispatch/dispatcher.edge.test.js +0 -82
  40. package/clients/dispatch/dispatcher.edge.test.ts +0 -100
  41. package/clients/dispatch/dispatcher.format.test.js +0 -46
  42. package/clients/dispatch/dispatcher.format.test.ts +0 -58
  43. package/clients/dispatch/dispatcher.inline.test.js +0 -74
  44. package/clients/dispatch/dispatcher.inline.test.ts +0 -93
  45. package/clients/dispatch/dispatcher.js +0 -381
  46. package/clients/dispatch/dispatcher.test.js +0 -116
  47. package/clients/dispatch/dispatcher.test.ts +0 -149
  48. package/clients/dispatch/integration.js +0 -108
  49. package/clients/dispatch/plan.js +0 -183
  50. package/clients/dispatch/runners/architect.js +0 -83
  51. package/clients/dispatch/runners/architect.test.js +0 -138
  52. package/clients/dispatch/runners/architect.test.ts +0 -162
  53. package/clients/dispatch/runners/ast-grep-napi.js +0 -405
  54. package/clients/dispatch/runners/ast-grep-napi.test.js +0 -107
  55. package/clients/dispatch/runners/ast-grep-napi.test.ts +0 -129
  56. package/clients/dispatch/runners/ast-grep.js +0 -157
  57. package/clients/dispatch/runners/biome.js +0 -55
  58. package/clients/dispatch/runners/config-validation.js +0 -67
  59. package/clients/dispatch/runners/go-vet.js +0 -48
  60. package/clients/dispatch/runners/index.js +0 -47
  61. package/clients/dispatch/runners/lsp.js +0 -102
  62. package/clients/dispatch/runners/oxlint.js +0 -67
  63. package/clients/dispatch/runners/oxlint.test.js +0 -230
  64. package/clients/dispatch/runners/oxlint.test.ts +0 -303
  65. package/clients/dispatch/runners/pyright.js +0 -100
  66. package/clients/dispatch/runners/pyright.test.js +0 -98
  67. package/clients/dispatch/runners/pyright.test.ts +0 -121
  68. package/clients/dispatch/runners/python-slop.js +0 -97
  69. package/clients/dispatch/runners/python-slop.test.js +0 -203
  70. package/clients/dispatch/runners/python-slop.test.ts +0 -298
  71. package/clients/dispatch/runners/ruff.js +0 -48
  72. package/clients/dispatch/runners/rust-clippy.js +0 -102
  73. package/clients/dispatch/runners/scan_codebase.test.js +0 -89
  74. package/clients/dispatch/runners/scan_codebase.test.ts +0 -105
  75. package/clients/dispatch/runners/shellcheck.js +0 -147
  76. package/clients/dispatch/runners/shellcheck.test.js +0 -98
  77. package/clients/dispatch/runners/shellcheck.test.ts +0 -129
  78. package/clients/dispatch/runners/similarity.js +0 -230
  79. package/clients/dispatch/runners/spellcheck.js +0 -106
  80. package/clients/dispatch/runners/spellcheck.test.js +0 -158
  81. package/clients/dispatch/runners/spellcheck.test.ts +0 -214
  82. package/clients/dispatch/runners/tree-sitter.js +0 -246
  83. package/clients/dispatch/runners/ts-lsp.js +0 -125
  84. package/clients/dispatch/runners/ts-slop.js +0 -113
  85. package/clients/dispatch/runners/type-safety.js +0 -142
  86. package/clients/dispatch/runners/utils/diagnostic-parsers.js +0 -134
  87. package/clients/dispatch/runners/utils/runner-helpers.js +0 -115
  88. package/clients/dispatch/runners/utils.js +0 -51
  89. package/clients/dispatch/runners/yaml-rule-parser.js +0 -360
  90. package/clients/dispatch/types.js +0 -16
  91. package/clients/dispatch/utils/format-utils.js +0 -44
  92. package/clients/dogfood.test.js +0 -201
  93. package/clients/dogfood.test.ts +0 -269
  94. package/clients/file-kinds.js +0 -177
  95. package/clients/file-kinds.test.js +0 -169
  96. package/clients/file-kinds.test.ts +0 -210
  97. package/clients/file-time.js +0 -152
  98. package/clients/file-utils.js +0 -40
  99. package/clients/fix-scanners.js +0 -204
  100. package/clients/format-service.js +0 -184
  101. package/clients/formatters.js +0 -488
  102. package/clients/go-client.js +0 -203
  103. package/clients/go-client.test.js +0 -127
  104. package/clients/go-client.test.ts +0 -143
  105. package/clients/installer/index.js +0 -403
  106. package/clients/interviewer-templates.js +0 -75
  107. package/clients/interviewer.js +0 -173
  108. package/clients/jscpd-client.js +0 -196
  109. package/clients/jscpd-client.test.js +0 -127
  110. package/clients/jscpd-client.test.ts +0 -145
  111. package/clients/knip-client.js +0 -239
  112. package/clients/knip-client.test.js +0 -112
  113. package/clients/knip-client.test.ts +0 -128
  114. package/clients/latency-logger.js +0 -40
  115. package/clients/lsp/__tests__/client.test.js +0 -310
  116. package/clients/lsp/__tests__/client.test.ts +0 -412
  117. package/clients/lsp/__tests__/config.test.js +0 -167
  118. package/clients/lsp/__tests__/config.test.ts +0 -217
  119. package/clients/lsp/__tests__/error-recovery.test.js +0 -213
  120. package/clients/lsp/__tests__/error-recovery.test.ts +0 -279
  121. package/clients/lsp/__tests__/integration.test.js +0 -127
  122. package/clients/lsp/__tests__/integration.test.ts +0 -160
  123. package/clients/lsp/__tests__/launch.test.js +0 -313
  124. package/clients/lsp/__tests__/launch.test.ts +0 -394
  125. package/clients/lsp/__tests__/server.test.js +0 -259
  126. package/clients/lsp/__tests__/server.test.ts +0 -332
  127. package/clients/lsp/__tests__/service.test.js +0 -438
  128. package/clients/lsp/__tests__/service.test.ts +0 -530
  129. package/clients/lsp/client.js +0 -350
  130. package/clients/lsp/config.js +0 -112
  131. package/clients/lsp/index.js +0 -318
  132. package/clients/lsp/installer/index.js +0 -391
  133. package/clients/lsp/interactive-install.js +0 -221
  134. package/clients/lsp/language.js +0 -170
  135. package/clients/lsp/launch.js +0 -329
  136. package/clients/lsp/lsp/launch.js +0 -116
  137. package/clients/lsp/lsp/server.js +0 -532
  138. package/clients/lsp/lsp-index.js +0 -10
  139. package/clients/lsp/path-utils.js +0 -5
  140. package/clients/lsp/server.js +0 -725
  141. package/clients/lsp/test-py-spawn/requirements.txt +0 -1
  142. package/clients/lsp/test-py-spawn/test.py +0 -3
  143. package/clients/lsp/test-py-svc/requirements.txt +0 -1
  144. package/clients/lsp/test-py-svc/test.py +0 -3
  145. package/clients/lsp/test-python-project/requirements.txt +0 -1
  146. package/clients/lsp/test-python-project/test.py +0 -5
  147. package/clients/metrics-client.js +0 -107
  148. package/clients/metrics-client.test.js +0 -128
  149. package/clients/metrics-client.test.ts +0 -163
  150. package/clients/metrics-history.js +0 -367
  151. package/clients/path-utils.js +0 -142
  152. package/clients/pipeline.js +0 -272
  153. package/clients/production-readiness.js +0 -522
  154. package/clients/project-index.js +0 -255
  155. package/clients/project-metadata.js +0 -531
  156. package/clients/ruff-client.js +0 -325
  157. package/clients/ruff-client.test.js +0 -132
  158. package/clients/ruff-client.test.ts +0 -153
  159. package/clients/rules-scanner.js +0 -97
  160. package/clients/runner-tracker.js +0 -152
  161. package/clients/rust-client.js +0 -205
  162. package/clients/rust-client.test.js +0 -108
  163. package/clients/rust-client.test.ts +0 -130
  164. package/clients/safe-spawn-async.js +0 -163
  165. package/clients/safe-spawn.js +0 -241
  166. package/clients/sanitize.js +0 -291
  167. package/clients/sanitize.test.js +0 -177
  168. package/clients/sanitize.test.ts +0 -223
  169. package/clients/scan-architectural-debt.js +0 -167
  170. package/clients/scan-utils.js +0 -83
  171. package/clients/secrets-scanner.js +0 -119
  172. package/clients/secrets-scanner.test.js +0 -100
  173. package/clients/secrets-scanner.test.ts +0 -113
  174. package/clients/sg-runner.js +0 -292
  175. package/clients/state-matrix.js +0 -160
  176. package/clients/subprocess-client.js +0 -65
  177. package/clients/symbol-types.js +0 -5
  178. package/clients/test-runner-client.js +0 -523
  179. package/clients/test-runner-client.test.js +0 -192
  180. package/clients/test-runner-client.test.ts +0 -253
  181. package/clients/test-utils.js +0 -27
  182. package/clients/test-utils.ts +0 -36
  183. package/clients/todo-scanner.js +0 -200
  184. package/clients/todo-scanner.test.js +0 -301
  185. package/clients/todo-scanner.test.ts +0 -352
  186. package/clients/tool-availability.js +0 -207
  187. package/clients/tree-sitter-client.js +0 -601
  188. package/clients/tree-sitter-query-loader.js +0 -355
  189. package/clients/tree-sitter-symbol-extractor.js +0 -289
  190. package/clients/ts-service.js +0 -129
  191. package/clients/type-coverage-client.js +0 -127
  192. package/clients/type-coverage-client.test.js +0 -105
  193. package/clients/type-coverage-client.test.ts +0 -125
  194. package/clients/type-safety-client.js +0 -138
  195. package/clients/types.js +0 -11
  196. package/clients/typescript-client.codefix.test.js +0 -157
  197. package/clients/typescript-client.codefix.test.ts +0 -186
  198. package/clients/typescript-client.js +0 -509
  199. package/clients/typescript-client.test.js +0 -105
  200. package/clients/typescript-client.test.ts +0 -126
  201. package/commands/booboo.js +0 -1007
  202. package/commands/fix-from-booboo.js +0 -398
  203. package/commands/fix-simplified.js +0 -618
  204. package/commands/rate.js +0 -281
  205. package/commands/rate.test.js +0 -119
  206. package/commands/rate.test.ts +0 -131
  207. package/commands/refactor.js +0 -130
@@ -1,509 +0,0 @@
1
- /**
2
- * TypeScript Language Service Client for pi-local
3
- *
4
- * Uses TypeScript's in-process Language Service API for rich code intelligence.
5
- * This is lighter weight than spawning tsserver and provides the same features.
6
- */
7
- import * as fs from "node:fs";
8
- import * as path from "node:path";
9
- import * as ts from "typescript";
10
- // TypeScript file extensions
11
- const TS_EXTENSIONS = new Set([".ts", ".tsx", ".js", ".jsx"]);
12
- // Default compiler options when no tsconfig is found
13
- /**
14
- * Build default CompilerOptions through TypeScript's own config parser so that
15
- * lib name → file path resolution works correctly in the Language Service.
16
- * Direct assignment of `lib: ["lib.es2020.d.ts"]` doesn't work because the
17
- * Language Service looks up those names relative to cwd, not the TS install dir.
18
- */
19
- function buildDefaultCompilerOptions() {
20
- const fakeConfig = {
21
- compilerOptions: {
22
- target: "ES2020",
23
- module: "ESNext",
24
- moduleResolution: "bundler",
25
- strict: true,
26
- esModuleInterop: true,
27
- skipLibCheck: true,
28
- lib: ["es2020", "dom", "dom.iterable"],
29
- },
30
- };
31
- const parsed = ts.parseJsonConfigFileContent(fakeConfig, ts.sys, process.cwd());
32
- return { ...parsed.options, skipLibCheck: true };
33
- }
34
- const DEFAULT_COMPILER_OPTIONS = buildDefaultCompilerOptions();
35
- /**
36
- * Walk up from startDir until we find a tsconfig.json, or hit the fs root.
37
- */
38
- function findTsConfig(startDir) {
39
- let dir = startDir;
40
- while (true) {
41
- const candidate = path.join(dir, "tsconfig.json");
42
- if (fs.existsSync(candidate))
43
- return candidate;
44
- const parent = path.dirname(dir);
45
- if (parent === dir)
46
- return null; // reached root
47
- dir = parent;
48
- }
49
- }
50
- /**
51
- * Read and parse a tsconfig.json, returning merged CompilerOptions.
52
- * Falls back to DEFAULT_COMPILER_OPTIONS on any error.
53
- */
54
- function loadCompilerOptions(tsconfigPath) {
55
- try {
56
- const configFile = ts.readConfigFile(tsconfigPath, ts.sys.readFile);
57
- if (configFile.error)
58
- return DEFAULT_COMPILER_OPTIONS;
59
- const parsed = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(tsconfigPath));
60
- if (parsed.errors.length)
61
- return DEFAULT_COMPILER_OPTIONS;
62
- // Always set skipLibCheck to avoid noise from node_modules
63
- return { ...parsed.options, skipLibCheck: true };
64
- }
65
- catch (err) {
66
- void err;
67
- return DEFAULT_COMPILER_OPTIONS;
68
- }
69
- }
70
- export class TypeScriptClient {
71
- constructor() {
72
- this.fileVersions = new Map();
73
- this.fileContents = new Map();
74
- this.languageService = null;
75
- this.compilerOptions = DEFAULT_COMPILER_OPTIONS;
76
- this.lastTsconfigDir = null;
77
- this.initialize();
78
- }
79
- /**
80
- * Normalize file path for consistent cross-platform use
81
- */
82
- normalizePath(filePath) {
83
- return path.resolve(filePath).replace(/\\/g, "/");
84
- }
85
- /**
86
- * Check if a file is a TypeScript/JavaScript file
87
- */
88
- isTypeScriptFile(filePath) {
89
- const ext = path.extname(filePath).toLowerCase();
90
- return TS_EXTENSIONS.has(ext);
91
- }
92
- initialize() {
93
- const host = {
94
- getScriptFileNames: () => Array.from(this.fileContents.keys()),
95
- getScriptVersion: (fileName) => {
96
- const normalized = fileName.replace(/\\/g, "/");
97
- return String(this.fileVersions.get(normalized) ?? 0);
98
- },
99
- getScriptSnapshot: (fileName) => {
100
- const normalized = fileName.replace(/\\/g, "/");
101
- const content = this.fileContents.get(normalized);
102
- if (content)
103
- return ts.ScriptSnapshot.fromString(content);
104
- try {
105
- return ts.ScriptSnapshot.fromString(fs.readFileSync(fileName, "utf-8"));
106
- }
107
- catch {
108
- return undefined;
109
- }
110
- },
111
- getCurrentDirectory: () => process.cwd(),
112
- getCompilationSettings: () => this.compilerOptions,
113
- getDefaultLibFileName: (options) => ts.getDefaultLibFilePath(options),
114
- fileExists: (fileName) => ts.sys.fileExists(fileName),
115
- readFile: (fileName) => {
116
- const normalized = fileName.replace(/\\/g, "/");
117
- const cached = this.fileContents.get(normalized);
118
- if (cached !== undefined)
119
- return cached;
120
- return ts.sys.readFile(fileName);
121
- },
122
- directoryExists: (dirName) => ts.sys.directoryExists(dirName),
123
- getDirectories: (dir) => ts.sys.getDirectories(dir),
124
- };
125
- this.languageService = ts.createLanguageService(host, ts.createDocumentRegistry());
126
- }
127
- /**
128
- * Detect tsconfig for the given file and refresh compilerOptions if the
129
- * project root changed (avoids redundant re-parses across edits to the same project).
130
- */
131
- refreshCompilerOptions(filePath) {
132
- const dir = path.dirname(path.resolve(filePath));
133
- const tsconfigPath = findTsConfig(dir);
134
- const key = tsconfigPath ?? dir;
135
- if (key === this.lastTsconfigDir)
136
- return; // same project, no change
137
- this.lastTsconfigDir = key;
138
- this.compilerOptions = tsconfigPath
139
- ? loadCompilerOptions(tsconfigPath)
140
- : DEFAULT_COMPILER_OPTIONS;
141
- }
142
- /**
143
- * Add a file to the language service
144
- */
145
- addFile(filePath, content) {
146
- const normalized = this.normalizePath(filePath);
147
- this.fileContents.set(normalized, content);
148
- this.fileVersions.set(normalized, (this.fileVersions.get(normalized) || 0) + 1);
149
- }
150
- /**
151
- * Update a file's content — also refreshes compilerOptions if project changed
152
- */
153
- updateFile(filePath, content) {
154
- this.refreshCompilerOptions(filePath);
155
- const normalized = this.normalizePath(filePath);
156
- this.fileVersions.set(normalized, (this.fileVersions.get(normalized) ?? 0) + 1);
157
- this.fileContents.set(normalized, content);
158
- }
159
- /**
160
- * Ensure a file is loaded from disk (refreshes cache)
161
- */
162
- ensureFile(filePath) {
163
- const normalized = this.normalizePath(filePath);
164
- try {
165
- const diskContent = fs.readFileSync(filePath, "utf-8");
166
- const cachedContent = this.fileContents.get(normalized);
167
- if (cachedContent !== diskContent) {
168
- this.updateFile(filePath, diskContent);
169
- }
170
- }
171
- catch (err) {
172
- void err;
173
- }
174
- }
175
- /**
176
- * Get all tracked files
177
- */
178
- getTrackedFiles() {
179
- return Array.from(this.fileContents.keys());
180
- }
181
- /**
182
- * Convert line/character to position offset
183
- */
184
- lineCharToPosition(content, line, character) {
185
- const lines = content.split("\n");
186
- let position = 0;
187
- for (let i = 0; i < Math.min(line, lines.length); i++) {
188
- position += lines[i].length + 1;
189
- }
190
- return position + character;
191
- }
192
- /**
193
- * Get diagnostics (errors and warnings) for a file
194
- */
195
- getDiagnostics(filePath) {
196
- this.refreshCompilerOptions(filePath);
197
- const normalized = this.normalizePath(filePath);
198
- this.ensureFile(filePath);
199
- if (!this.languageService)
200
- return [];
201
- const syntactic = this.languageService.getSyntacticDiagnostics(normalized);
202
- const semantic = this.languageService.getSemanticDiagnostics(normalized);
203
- return ([...syntactic, ...semantic]
204
- .filter((diag) => diag.file && diag.start !== undefined)
205
- // Filter cross-file "redeclare" noise — happens when non-module scripts
206
- // share global scope across multiple tracked files (TS2300, TS2451)
207
- .filter((diag) => {
208
- if (diag.code !== 2300 && diag.code !== 2451)
209
- return true;
210
- // Only keep if the related information points back to the same file
211
- const related = diag.relatedInformation ?? [];
212
- return related.every((r) => !r.file || this.normalizePath(r.file.fileName) === normalized);
213
- })
214
- .map((diag) => {
215
- const startPos = diag.file?.getLineAndCharacterOfPosition(diag.start);
216
- const endPos = diag.file?.getLineAndCharacterOfPosition(diag.start + diag.length);
217
- return {
218
- range: {
219
- start: { line: startPos.line, character: startPos.character },
220
- end: { line: endPos.line, character: endPos.character },
221
- },
222
- severity: (diag.category === ts.DiagnosticCategory.Error
223
- ? 1
224
- : 2),
225
- code: diag.code,
226
- message: ts.flattenDiagnosticMessageText(diag.messageText, "\n"),
227
- source: "typescript",
228
- };
229
- }));
230
- }
231
- /**
232
- * Get hover information at a position
233
- */
234
- getHover(filePath, line, character) {
235
- const resolved = this.resolvePosition(filePath, line, character);
236
- if (!resolved)
237
- return null;
238
- const { normalized, position, ls } = resolved;
239
- const info = ls.getQuickInfoAtPosition(normalized, position);
240
- if (!info)
241
- return null;
242
- return {
243
- type: ts.displayPartsToString(info.displayParts),
244
- documentation: info.documentation
245
- ? ts.displayPartsToString(info.documentation)
246
- : undefined,
247
- };
248
- }
249
- /**
250
- * Shared preamble for position-based LSP queries.
251
- * Returns null if prerequisites are not met.
252
- */
253
- resolvePosition(filePath, line, character) {
254
- const normalized = this.normalizePath(filePath);
255
- this.ensureFile(filePath);
256
- if (!this.languageService)
257
- return null;
258
- const content = this.fileContents.get(normalized);
259
- if (!content)
260
- return null;
261
- return {
262
- normalized,
263
- position: this.lineCharToPosition(content, line, character),
264
- ls: this.languageService,
265
- };
266
- }
267
- withPosition(filePath, line, character, cb) {
268
- const resolved = this.resolvePosition(filePath, line, character);
269
- if (!resolved)
270
- return [];
271
- const { normalized, position, ls } = resolved;
272
- return cb(normalized, position, ls) ?? [];
273
- }
274
- /**
275
- * Go to definition
276
- */
277
- getDefinition(filePath, line, character) {
278
- return this.withPosition(filePath, line, character, (normalized, position, ls) => {
279
- const definitions = ls.getDefinitionAtPosition(normalized, position);
280
- if (!definitions)
281
- return undefined;
282
- return definitions.map((def) => {
283
- if (def.textSpan) {
284
- const defFile = def.fileName || normalized;
285
- const defContent = this.fileContents.get(defFile) || "";
286
- if (defContent) {
287
- const lines = defContent
288
- .substring(0, def.textSpan.start)
289
- .split("\n");
290
- return {
291
- file: defFile,
292
- line: lines.length - 1,
293
- character: lines[lines.length - 1].length,
294
- };
295
- }
296
- }
297
- return { file: def.fileName, line: 0, character: 0 };
298
- });
299
- });
300
- }
301
- /**
302
- * Get type definition
303
- */
304
- getTypeDefinition(filePath, line, character) {
305
- return this.withPosition(filePath, line, character, (normalized, position, ls) => {
306
- const defs = ls.getTypeDefinitionAtPosition(normalized, position);
307
- if (!defs)
308
- return undefined;
309
- return this.toLocations(defs, normalized);
310
- });
311
- }
312
- /**
313
- * Find references
314
- */
315
- getReferences(filePath, line, character) {
316
- return this.withPosition(filePath, line, character, (normalized, position, ls) => {
317
- const references = ls.getReferencesAtPosition(normalized, position);
318
- if (!references)
319
- return undefined;
320
- return this.toLocations(references);
321
- });
322
- }
323
- /** Map TS definition/reference entries to Location objects. */
324
- toLocations(entries, fallbackFile) {
325
- return entries.map((e) => ({
326
- file: e.fileName || fallbackFile || "",
327
- line: 0,
328
- character: 0,
329
- }));
330
- }
331
- /**
332
- * Shared preamble for tree-based LSP queries (symbols, folding).
333
- */
334
- resolveTree(filePath) {
335
- const normalized = this.normalizePath(filePath);
336
- this.ensureFile(filePath);
337
- if (!this.languageService)
338
- return null;
339
- const tree = this.languageService.getNavigationTree(normalized);
340
- if (!tree)
341
- return null;
342
- return { normalized, tree };
343
- }
344
- /**
345
- * Get document symbols
346
- */
347
- getSymbols(filePath) {
348
- const resolved = this.resolveTree(filePath);
349
- if (!resolved)
350
- return [];
351
- const { tree } = resolved;
352
- const symbols = [];
353
- const extract = (node, container) => {
354
- if (node.span) {
355
- symbols.push({
356
- name: node.text,
357
- kind: this.symbolKind(node.kind),
358
- line: 0,
359
- containerName: container,
360
- });
361
- }
362
- if (node.childItems) {
363
- for (const child of node.childItems) {
364
- extract(child, node.text);
365
- }
366
- }
367
- };
368
- extract(tree);
369
- return symbols;
370
- }
371
- /**
372
- * Get completions at a position
373
- */
374
- getCompletions(filePath, line, character) {
375
- return this.withPosition(filePath, line, character, (normalized, position, ls) => {
376
- const completions = ls.getCompletionsAtPosition(normalized, position, {});
377
- if (!completions)
378
- return undefined;
379
- return completions.entries.slice(0, 50).map((entry) => ({
380
- name: entry.name,
381
- kind: this.completionKind(entry.kind),
382
- sortText: entry.sortText,
383
- }));
384
- });
385
- }
386
- /**
387
- * Go to implementation
388
- */
389
- getImplementation(filePath, line, character) {
390
- return this.withPosition(filePath, line, character, (normalized, position, ls) => {
391
- const implementations = ls.getImplementationAtPosition(normalized, position);
392
- if (!implementations)
393
- return undefined;
394
- return this.toLocations(implementations);
395
- });
396
- }
397
- /**
398
- * Get folding ranges
399
- */
400
- getFoldingRanges(filePath) {
401
- const resolved = this.resolveTree(filePath);
402
- if (!resolved)
403
- return [];
404
- const { tree } = resolved;
405
- const ranges = [];
406
- const findFolds = (node) => {
407
- if (!node?.span)
408
- return;
409
- if (node.kind === "function" || node.kind === "class") {
410
- ranges.push({
411
- startLine: 0,
412
- endLine: 0,
413
- kind: node.kind,
414
- });
415
- }
416
- if (node.childItems) {
417
- for (const child of node.childItems) {
418
- findFolds(child);
419
- }
420
- }
421
- };
422
- findFolds(tree);
423
- return ranges;
424
- }
425
- /**
426
- * Explain an error at a specific line
427
- */
428
- explainError(filePath, line) {
429
- const diagnostics = this.getDiagnostics(filePath);
430
- const errorAtLine = diagnostics.find((d) => d.range.start.line === line && d.severity === 1);
431
- if (!errorAtLine)
432
- return null;
433
- return { message: errorAtLine.message, code: errorAtLine.code };
434
- }
435
- /**
436
- * Get quick fixes (code actions) for a diagnostic at a position.
437
- * Returns array of fix descriptions with their edit changes.
438
- */
439
- getCodeFixes(filePath, line, character, errorCodes) {
440
- const resolved = this.resolvePosition(filePath, line, character);
441
- if (!resolved)
442
- return [];
443
- const { normalized, position, ls } = resolved;
444
- const formatOpts = {
445
- indentSize: 2,
446
- tabSize: 2,
447
- newLineCharacter: "\n",
448
- convertTabsToSpaces: true,
449
- };
450
- const fixes = ls.getCodeFixesAtPosition(normalized, position, position, errorCodes, formatOpts, {});
451
- if (!fixes)
452
- return [];
453
- return fixes.map((fix) => ({
454
- description: fix.description,
455
- changes: fix.changes?.map((change) => ({
456
- fileName: change.fileName,
457
- textChanges: change.textChanges,
458
- })) || [],
459
- }));
460
- }
461
- /**
462
- * Get all quick fixes for all diagnostics in a file.
463
- * Returns a map of diagnostic line → fixes.
464
- */
465
- /**
466
- * Accept pre-computed diagnostics to avoid a second getSemanticDiagnostics call
467
- * when the caller already has them (saves ~1–2s on large files).
468
- */
469
- getAllCodeFixes(filePath, precomputedDiags) {
470
- const fixesByLine = new Map();
471
- const diagnostics = precomputedDiags ?? this.getDiagnostics(filePath);
472
- for (const diag of diagnostics) {
473
- if (diag.severity !== 1 || diag.code === undefined)
474
- continue;
475
- const fixes = this.getCodeFixes(filePath, diag.range.start.line, diag.range.start.character, [diag.code]);
476
- if (fixes.length > 0) {
477
- fixesByLine.set(diag.range.start.line, fixes);
478
- }
479
- }
480
- return fixesByLine;
481
- }
482
- symbolKind(kind) {
483
- const map = {
484
- script: "file",
485
- class: "class",
486
- interface: "interface",
487
- function: "function",
488
- method: "method",
489
- property: "property",
490
- variable: "variable",
491
- enum: "enum",
492
- module: "module",
493
- };
494
- return map[kind] || "unknown";
495
- }
496
- completionKind(kind) {
497
- const map = {
498
- property: "property",
499
- method: "method",
500
- class: "class",
501
- interface: "interface",
502
- enum: "enum",
503
- variable: "variable",
504
- function: "function",
505
- keyword: "keyword",
506
- };
507
- return map[kind] || "text";
508
- }
509
- }
@@ -1,105 +0,0 @@
1
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
2
- import { createTempFile, setupTestEnvironment } from "./test-utils.js";
3
- import { TypeScriptClient } from "./typescript-client.js";
4
- describe("TypeScriptClient", () => {
5
- let client;
6
- let tmpDir;
7
- let cleanup;
8
- beforeEach(() => {
9
- client = new TypeScriptClient();
10
- ({ tmpDir, cleanup } = setupTestEnvironment("pi-lens-ts-test-"));
11
- });
12
- afterEach(() => {
13
- cleanup();
14
- });
15
- afterEach(() => {
16
- cleanup();
17
- });
18
- describe("isTypeScriptFile", () => {
19
- it("should recognize TypeScript files", () => {
20
- expect(client.isTypeScriptFile("test.ts")).toBe(true);
21
- expect(client.isTypeScriptFile("test.tsx")).toBe(true);
22
- });
23
- it("should also recognize JavaScript files (for type checking)", () => {
24
- expect(client.isTypeScriptFile("test.js")).toBe(true);
25
- expect(client.isTypeScriptFile("test.jsx")).toBe(true);
26
- });
27
- it("should not recognize non-JS/TS files", () => {
28
- expect(client.isTypeScriptFile("test.py")).toBe(false);
29
- expect(client.isTypeScriptFile("test.md")).toBe(false);
30
- });
31
- });
32
- describe("updateFile and getDiagnostics", () => {
33
- it("should detect type errors", () => {
34
- const content = `
35
- const x: number = "string"; // Type error
36
- `;
37
- const filePath = createTempFile(tmpDir, "test.ts", content);
38
- client.updateFile(filePath, content);
39
- const diags = client.getDiagnostics(filePath);
40
- expect(diags.length).toBeGreaterThan(0);
41
- expect(diags.some((d) => d.message.includes("string"))).toBe(true);
42
- });
43
- it("should not report errors for valid code", () => {
44
- const content = `
45
- const x: number = 42;
46
- const y: string = "hello";
47
- `;
48
- const filePath = createTempFile(tmpDir, "test.ts", content);
49
- client.updateFile(filePath, content);
50
- const diags = client.getDiagnostics(filePath);
51
- // May have warnings but no errors for valid code
52
- const errors = diags.filter((d) => d.severity === 1); // Error severity
53
- expect(errors.length).toBe(0);
54
- });
55
- it("should detect undefined variables", () => {
56
- const content = `
57
- function test() {
58
- return undefinedVariable;
59
- }
60
- `;
61
- const filePath = createTempFile(tmpDir, "test.ts", content);
62
- client.updateFile(filePath, content);
63
- const diags = client.getDiagnostics(filePath);
64
- expect(diags.some((d) => d.message.includes("undefined"))).toBe(true);
65
- });
66
- it("should detect missing function arguments", () => {
67
- const content = `
68
- function add(a: number, b: number): number {
69
- return a + b;
70
- }
71
- const result = add(1);
72
- `;
73
- const filePath = createTempFile(tmpDir, "test.ts", content);
74
- client.updateFile(filePath, content);
75
- const diags = client.getDiagnostics(filePath);
76
- expect(diags.some((d) => d.message.includes("Expected"))).toBe(true);
77
- });
78
- });
79
- describe("diagnostic severity", () => {
80
- it("should have correct severity levels", () => {
81
- const diags = [
82
- {
83
- range: {
84
- start: { line: 0, character: 0 },
85
- end: { line: 0, character: 10 },
86
- },
87
- severity: 1, // Error
88
- message: "Error message",
89
- },
90
- {
91
- range: {
92
- start: { line: 1, character: 0 },
93
- end: { line: 1, character: 10 },
94
- },
95
- severity: 2, // Warning
96
- message: "Warning message",
97
- },
98
- ];
99
- // Test that diagnostics have expected structure
100
- expect(diags[0].severity).toBe(1); // Error
101
- expect(diags[1].severity).toBe(2); // Warning
102
- expect(diags[0].message).toContain("Error");
103
- });
104
- });
105
- });