pi-lens 2.2.9 → 3.0.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/CHANGELOG.md +198 -0
- package/README.md +709 -519
- package/clients/__tests__/file-time.test.js +216 -0
- package/clients/__tests__/file-time.test.ts +276 -0
- package/clients/__tests__/format-service.test.js +245 -0
- package/clients/__tests__/format-service.test.ts +339 -0
- package/clients/__tests__/formatters.test.js +271 -0
- package/clients/__tests__/formatters.test.ts +401 -0
- package/clients/amain-types.js +164 -0
- package/clients/amain-types.ts +165 -0
- package/clients/architect-client.js +56 -12
- package/clients/architect-client.ts +81 -16
- package/clients/ast-grep-client.js +2 -2
- package/clients/ast-grep-client.ts +14 -39
- package/clients/ast-grep-parser.ts +1 -1
- package/clients/ast-grep-rule-manager.js +8 -0
- package/clients/ast-grep-rule-manager.ts +10 -1
- package/clients/ast-grep-types.js +9 -0
- package/clients/ast-grep-types.ts +106 -0
- package/clients/auto-loop.js +10 -0
- package/clients/auto-loop.ts +14 -1
- package/clients/biome-client.js +81 -19
- package/clients/biome-client.ts +103 -22
- package/clients/bus/bus.js +191 -0
- package/clients/bus/bus.ts +251 -0
- package/clients/bus/events.js +214 -0
- package/clients/bus/events.ts +279 -0
- package/clients/bus/index.js +8 -0
- package/clients/bus/index.ts +9 -0
- package/clients/bus/integration.js +158 -0
- package/clients/bus/integration.ts +214 -0
- package/clients/complexity-client.js +13 -7
- package/clients/complexity-client.ts +13 -7
- package/clients/config-validator.js +465 -0
- package/clients/config-validator.ts +558 -0
- package/clients/dependency-checker.js +4 -10
- package/clients/dependency-checker.ts +4 -10
- package/clients/dispatch/__tests__/autofix-integration.test.js +245 -0
- package/clients/dispatch/__tests__/autofix-integration.test.ts +300 -0
- package/clients/dispatch/__tests__/runner-registration.test.js +236 -0
- package/clients/dispatch/__tests__/runner-registration.test.ts +282 -0
- package/clients/dispatch/bus-dispatcher.js +177 -0
- package/clients/dispatch/bus-dispatcher.ts +251 -0
- package/clients/dispatch/dispatcher.edge.test.js +82 -0
- package/clients/dispatch/dispatcher.edge.test.ts +100 -0
- package/clients/dispatch/dispatcher.format.test.js +46 -0
- package/clients/dispatch/dispatcher.format.test.ts +58 -0
- package/clients/dispatch/dispatcher.inline.test.js +74 -0
- package/clients/dispatch/dispatcher.inline.test.ts +93 -0
- package/clients/dispatch/dispatcher.js +19 -53
- package/clients/dispatch/dispatcher.ts +20 -67
- package/clients/dispatch/plan.js +9 -4
- package/clients/dispatch/plan.ts +9 -4
- package/clients/dispatch/runners/architect.js +21 -7
- package/clients/dispatch/runners/architect.test.js +138 -0
- package/clients/dispatch/runners/architect.test.ts +162 -0
- package/clients/dispatch/runners/architect.ts +22 -7
- package/clients/dispatch/runners/ast-grep-napi.js +462 -0
- package/clients/dispatch/runners/ast-grep-napi.test.js +111 -0
- package/clients/dispatch/runners/ast-grep-napi.test.ts +133 -0
- package/clients/dispatch/runners/ast-grep-napi.ts +506 -0
- package/clients/dispatch/runners/ast-grep.js +62 -19
- package/clients/dispatch/runners/ast-grep.ts +70 -18
- package/clients/dispatch/runners/biome.js +29 -53
- package/clients/dispatch/runners/biome.ts +29 -63
- package/clients/dispatch/runners/config-validation.js +67 -0
- package/clients/dispatch/runners/config-validation.ts +82 -0
- package/clients/dispatch/runners/go-vet.js +4 -28
- package/clients/dispatch/runners/go-vet.ts +4 -32
- package/clients/dispatch/runners/index.js +30 -10
- package/clients/dispatch/runners/index.ts +30 -10
- package/clients/dispatch/runners/oxlint.js +141 -0
- package/clients/dispatch/runners/oxlint.test.js +230 -0
- package/clients/dispatch/runners/oxlint.test.ts +303 -0
- package/clients/dispatch/runners/oxlint.ts +175 -0
- package/clients/dispatch/runners/pyright.js +40 -70
- package/clients/dispatch/runners/pyright.test.js +16 -2
- package/clients/dispatch/runners/pyright.test.ts +14 -2
- package/clients/dispatch/runners/pyright.ts +48 -91
- package/clients/dispatch/runners/python-slop.js +97 -0
- package/clients/dispatch/runners/python-slop.test.js +203 -0
- package/clients/dispatch/runners/python-slop.test.ts +298 -0
- package/clients/dispatch/runners/python-slop.ts +124 -0
- package/clients/dispatch/runners/ruff.js +18 -71
- package/clients/dispatch/runners/ruff.ts +19 -79
- package/clients/dispatch/runners/rust-clippy.js +28 -32
- package/clients/dispatch/runners/rust-clippy.ts +29 -31
- package/clients/dispatch/runners/scan_codebase.test.js +89 -0
- package/clients/dispatch/runners/scan_codebase.test.ts +105 -0
- package/clients/dispatch/runners/shellcheck.js +147 -0
- package/clients/dispatch/runners/shellcheck.test.js +98 -0
- package/clients/dispatch/runners/shellcheck.test.ts +129 -0
- package/clients/dispatch/runners/shellcheck.ts +188 -0
- package/clients/dispatch/runners/similarity.js +230 -0
- package/clients/dispatch/runners/similarity.ts +339 -0
- package/clients/dispatch/runners/spellcheck.js +106 -0
- package/clients/dispatch/runners/spellcheck.test.js +158 -0
- package/clients/dispatch/runners/spellcheck.test.ts +214 -0
- package/clients/dispatch/runners/spellcheck.ts +136 -0
- package/clients/dispatch/runners/tree-sitter.js +107 -0
- package/clients/dispatch/runners/tree-sitter.ts +135 -0
- package/clients/dispatch/runners/ts-lsp.js +104 -33
- package/clients/dispatch/runners/ts-lsp.ts +120 -38
- package/clients/dispatch/runners/ts-slop.js +113 -0
- package/clients/dispatch/runners/ts-slop.test.js +180 -0
- package/clients/dispatch/runners/ts-slop.test.ts +230 -0
- package/clients/dispatch/runners/ts-slop.ts +142 -0
- package/clients/dispatch/runners/utils/diagnostic-parsers.js +134 -0
- package/clients/dispatch/runners/utils/diagnostic-parsers.ts +186 -0
- package/clients/dispatch/runners/utils/runner-helpers.js +115 -0
- package/clients/dispatch/runners/utils/runner-helpers.ts +167 -0
- package/clients/dispatch/runners/utils.js +2 -4
- package/clients/dispatch/runners/utils.ts +2 -4
- package/clients/dispatch/types.ts +1 -1
- package/clients/dispatch/utils/format-utils.js +49 -0
- package/clients/dispatch/utils/format-utils.ts +60 -0
- package/clients/dogfood.test.js +201 -0
- package/clients/dogfood.test.ts +269 -0
- package/clients/file-time.js +152 -0
- package/clients/file-time.ts +208 -0
- package/clients/file-utils.js +40 -0
- package/clients/file-utils.ts +44 -0
- package/clients/fix-scanners.js +10 -20
- package/clients/fix-scanners.ts +10 -22
- package/clients/format-service.js +172 -0
- package/clients/format-service.ts +254 -0
- package/clients/formatters.js +435 -0
- package/clients/formatters.ts +508 -0
- package/clients/go-client.js +5 -14
- package/clients/go-client.ts +5 -13
- package/clients/installer/index.js +356 -0
- package/clients/installer/index.ts +426 -0
- package/clients/jscpd-client.js +11 -9
- package/clients/jscpd-client.ts +12 -8
- package/clients/knip-client.js +3 -7
- package/clients/knip-client.ts +3 -6
- package/clients/lsp/__tests__/client.test.js +325 -0
- package/clients/lsp/__tests__/client.test.ts +434 -0
- package/clients/lsp/__tests__/config.test.js +166 -0
- package/clients/lsp/__tests__/config.test.ts +209 -0
- package/clients/lsp/__tests__/error-recovery.test.js +213 -0
- package/clients/lsp/__tests__/error-recovery.test.ts +279 -0
- package/clients/lsp/__tests__/integration.test.js +127 -0
- package/clients/lsp/__tests__/integration.test.ts +160 -0
- package/clients/lsp/__tests__/launch.test.js +260 -0
- package/clients/lsp/__tests__/launch.test.ts +329 -0
- package/clients/lsp/__tests__/server.test.js +259 -0
- package/clients/lsp/__tests__/server.test.ts +332 -0
- package/clients/lsp/__tests__/service.test.js +417 -0
- package/clients/lsp/__tests__/service.test.ts +499 -0
- package/clients/lsp/client.js +235 -0
- package/clients/lsp/client.ts +328 -0
- package/clients/lsp/config.js +115 -0
- package/clients/lsp/config.ts +149 -0
- package/clients/lsp/index.js +222 -0
- package/clients/lsp/index.ts +280 -0
- package/clients/lsp/installer/index.js +391 -0
- package/clients/lsp/interactive-install.js +210 -0
- package/clients/lsp/interactive-install.ts +251 -0
- package/clients/lsp/language.js +170 -0
- package/clients/lsp/language.ts +216 -0
- package/clients/lsp/launch.js +174 -0
- package/clients/lsp/launch.ts +240 -0
- package/clients/lsp/lsp/launch.js +116 -0
- package/clients/lsp/lsp/server.js +532 -0
- package/clients/lsp/lsp-index.js +10 -0
- package/clients/lsp/lsp-index.ts +11 -0
- package/clients/lsp/path-utils.js +48 -0
- package/clients/lsp/path-utils.ts +52 -0
- package/clients/lsp/server.js +615 -0
- package/clients/lsp/server.ts +800 -0
- package/clients/lsp/test-py-spawn/requirements.txt +1 -0
- package/clients/lsp/test-py-spawn/test.py +3 -0
- package/clients/lsp/test-py-svc/requirements.txt +1 -0
- package/clients/lsp/test-py-svc/test.py +3 -0
- package/clients/lsp/test-python-project/requirements.txt +1 -0
- package/clients/lsp/test-python-project/test.py +5 -0
- package/clients/metrics-history.js +2 -2
- package/clients/metrics-history.ts +2 -2
- package/clients/production-readiness.js +522 -0
- package/clients/production-readiness.ts +556 -0
- package/clients/project-index.js +255 -0
- package/clients/project-index.ts +383 -0
- package/clients/project-metadata.js +531 -0
- package/clients/project-metadata.ts +624 -0
- package/clients/ruff-client.js +56 -16
- package/clients/ruff-client.ts +72 -15
- package/clients/runner-tracker.js +152 -0
- package/clients/runner-tracker.ts +213 -0
- package/clients/rust-client.js +4 -11
- package/clients/rust-client.ts +5 -11
- package/clients/safe-spawn.js +96 -0
- package/clients/safe-spawn.ts +128 -0
- package/clients/scan-architectural-debt.js +3 -6
- package/clients/scan-architectural-debt.ts +3 -6
- package/clients/scan-utils.js +5 -20
- package/clients/scan-utils.ts +5 -29
- package/clients/secrets-scanner.js +3 -17
- package/clients/secrets-scanner.ts +4 -20
- package/clients/services/__tests__/effect-integration.test.js +86 -0
- package/clients/services/__tests__/effect-integration.test.ts +111 -0
- package/clients/services/effect-integration.js +194 -0
- package/clients/services/effect-integration.ts +268 -0
- package/clients/services/index.js +7 -0
- package/clients/services/index.ts +8 -0
- package/clients/services/runner-service.js +105 -0
- package/clients/services/runner-service.ts +179 -0
- package/clients/sg-runner.js +87 -13
- package/clients/sg-runner.ts +97 -13
- package/clients/state-matrix.js +160 -0
- package/clients/state-matrix.ts +202 -0
- package/clients/subprocess-client.js +10 -9
- package/clients/subprocess-client.ts +10 -8
- package/clients/test-runner-client.js +3 -7
- package/clients/test-runner-client.ts +3 -6
- package/clients/tool-availability.js +4 -10
- package/clients/tool-availability.ts +4 -9
- package/clients/tree-sitter-client.js +564 -0
- package/clients/tree-sitter-client.ts +797 -0
- package/clients/tree-sitter-query-loader.js +355 -0
- package/clients/tree-sitter-query-loader.ts +425 -0
- package/clients/type-coverage-client.js +3 -7
- package/clients/type-coverage-client.ts +3 -6
- package/clients/typescript-client.codefix.test.js +157 -0
- package/clients/typescript-client.codefix.test.ts +186 -0
- package/clients/typescript-client.js +43 -0
- package/clients/typescript-client.ts +98 -0
- package/commands/booboo.js +799 -219
- package/commands/booboo.ts +1004 -225
- package/commands/clients/ast-grep-client.js +250 -0
- package/commands/clients/ast-grep-parser.js +86 -0
- package/commands/clients/ast-grep-rule-manager.js +91 -0
- package/commands/clients/ast-grep-types.js +9 -0
- package/commands/clients/biome-client.js +380 -0
- package/commands/clients/complexity-client.js +667 -0
- package/commands/clients/file-kinds.js +177 -0
- package/commands/clients/file-utils.js +40 -0
- package/commands/clients/jscpd-client.js +169 -0
- package/commands/clients/knip-client.js +211 -0
- package/commands/clients/ruff-client.js +297 -0
- package/commands/clients/safe-spawn.js +88 -0
- package/commands/clients/scan-utils.js +83 -0
- package/commands/clients/sg-runner.js +190 -0
- package/commands/clients/types.js +11 -0
- package/commands/clients/typescript-client.js +505 -0
- package/commands/fix-from-booboo.js +398 -0
- package/commands/fix-from-booboo.ts +485 -0
- package/commands/fix-simplified.js +618 -0
- package/commands/fix-simplified.ts +768 -0
- package/commands/rate.js +10 -14
- package/commands/rate.ts +9 -16
- package/default-architect.yaml +59 -15
- package/index.ts +342 -429
- package/package.json +16 -3
- package/rules/ast-grep-rules/rules/empty-catch.yml +38 -13
- package/rules/ast-grep-rules/rules/no-array-constructor.yml +1 -0
- package/rules/ast-grep-rules/rules/no-debugger.yml +2 -0
- package/rules/python-slop-rules/.sgconfig.yml +4 -0
- package/rules/python-slop-rules/rules/slop-rules.yml +647 -0
- package/rules/tree-sitter-queries/python/bare-except.yml +54 -0
- package/rules/tree-sitter-queries/python/eval-exec.yml +50 -0
- package/rules/tree-sitter-queries/python/is-vs-equals.yml +60 -0
- package/rules/tree-sitter-queries/python/mutable-default-arg.yml +57 -0
- package/rules/tree-sitter-queries/python/unreachable-except.yml +60 -0
- package/rules/tree-sitter-queries/python/wildcard-import.yml +46 -0
- package/rules/tree-sitter-queries/tsx/dangerously-set-inner-html.yml +63 -0
- package/rules/tree-sitter-queries/typescript/await-in-loop.yml +56 -0
- package/rules/tree-sitter-queries/typescript/console-statement.yml +47 -0
- package/rules/tree-sitter-queries/typescript/debugger.yml +47 -0
- package/rules/tree-sitter-queries/typescript/deep-nesting.yml +117 -0
- package/rules/tree-sitter-queries/typescript/deep-promise-chain.yml +73 -0
- package/rules/tree-sitter-queries/typescript/empty-catch.yml +64 -0
- package/rules/tree-sitter-queries/typescript/eval.yml +48 -0
- package/rules/tree-sitter-queries/typescript/hardcoded-secrets.yml +78 -0
- package/rules/tree-sitter-queries/typescript/long-parameter-list.yml +62 -0
- package/rules/tree-sitter-queries/typescript/mixed-async-styles.yml +49 -0
- package/rules/tree-sitter-queries/typescript/nested-ternary.yml +45 -0
- package/rules/ts-slop-rules/.sgconfig.yml +4 -0
- package/rules/ts-slop-rules/rules/in-correct-optional-input-type.yml +10 -0
- package/rules/ts-slop-rules/rules/jwt-no-verify.yml +13 -0
- package/rules/ts-slop-rules/rules/no-architecture-violation.yml +10 -0
- package/rules/ts-slop-rules/rules/no-case-declarations.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dangerously-set-inner-html.yml +10 -0
- package/rules/ts-slop-rules/rules/no-debugger.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-args.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-class-members.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-keys.yml +10 -0
- package/rules/ts-slop-rules/rules/no-eval.yml +13 -0
- package/rules/ts-slop-rules/rules/no-hardcoded-secrets.yml +12 -0
- package/rules/ts-slop-rules/rules/no-implied-eval.yml +12 -0
- package/rules/ts-slop-rules/rules/no-inner-html.yml +13 -0
- package/rules/ts-slop-rules/rules/no-javascript-url.yml +10 -0
- package/rules/ts-slop-rules/rules/no-mutable-default.yml +10 -0
- package/rules/ts-slop-rules/rules/no-nested-links.yml +12 -0
- package/rules/ts-slop-rules/rules/no-new-symbol.yml +10 -0
- package/rules/ts-slop-rules/rules/no-new-wrappers.yml +13 -0
- package/rules/ts-slop-rules/rules/no-open-redirect.yml +16 -0
- package/rules/ts-slop-rules/rules/slop-rules.yml +455 -0
- package/rules/ts-slop-rules/rules/weak-rsa-key.yml +12 -0
- package/skills/ast-grep/SKILL.md +182 -0
- package/clients/dispatch/runners/secrets.js +0 -109
- package/commands/fix.js +0 -244
- package/commands/fix.ts +0 -373
- package/rules/ast-grep-rules/rules/no-lonely-if.yml +0 -13
|
@@ -0,0 +1,505 @@
|
|
|
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
|
+
fileVersions = new Map();
|
|
72
|
+
fileContents = new Map();
|
|
73
|
+
languageService = null;
|
|
74
|
+
compilerOptions = DEFAULT_COMPILER_OPTIONS;
|
|
75
|
+
lastTsconfigDir = null;
|
|
76
|
+
constructor() {
|
|
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
|
+
getAllCodeFixes(filePath) {
|
|
466
|
+
const fixesByLine = new Map();
|
|
467
|
+
const diagnostics = this.getDiagnostics(filePath);
|
|
468
|
+
for (const diag of diagnostics) {
|
|
469
|
+
if (diag.severity !== 1 || diag.code === undefined)
|
|
470
|
+
continue;
|
|
471
|
+
const fixes = this.getCodeFixes(filePath, diag.range.start.line, diag.range.start.character, [diag.code]);
|
|
472
|
+
if (fixes.length > 0) {
|
|
473
|
+
fixesByLine.set(diag.range.start.line, fixes);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
return fixesByLine;
|
|
477
|
+
}
|
|
478
|
+
symbolKind(kind) {
|
|
479
|
+
const map = {
|
|
480
|
+
script: "file",
|
|
481
|
+
class: "class",
|
|
482
|
+
interface: "interface",
|
|
483
|
+
function: "function",
|
|
484
|
+
method: "method",
|
|
485
|
+
property: "property",
|
|
486
|
+
variable: "variable",
|
|
487
|
+
enum: "enum",
|
|
488
|
+
module: "module",
|
|
489
|
+
};
|
|
490
|
+
return map[kind] || "unknown";
|
|
491
|
+
}
|
|
492
|
+
completionKind(kind) {
|
|
493
|
+
const map = {
|
|
494
|
+
property: "property",
|
|
495
|
+
method: "method",
|
|
496
|
+
class: "class",
|
|
497
|
+
interface: "interface",
|
|
498
|
+
enum: "enum",
|
|
499
|
+
variable: "variable",
|
|
500
|
+
function: "function",
|
|
501
|
+
keyword: "keyword",
|
|
502
|
+
};
|
|
503
|
+
return map[kind] || "text";
|
|
504
|
+
}
|
|
505
|
+
}
|