refactor(extension): resolve remaining type narrowing diagnostics
Narrow `unknown` types with explicit casts before `in` operator checks
to avoid "Type '{}' may represent a primitive value" errors. Also fix
getActiveTools() which returns string[] (not object[]), removing the
unnecessary .map((t) => t.name). Brings index.ts to zero diagnostics.
This commit is contained in:
31
index.ts
31
index.ts
@@ -26,11 +26,12 @@ function formatHover(result: unknown): string {
|
|||||||
if (!hover.contents) return "(empty)";
|
if (!hover.contents) return "(empty)";
|
||||||
|
|
||||||
// MarkupContent
|
// MarkupContent
|
||||||
|
const contents = hover.contents as Record<string, unknown>;
|
||||||
if (
|
if (
|
||||||
"value" in hover.contents &&
|
"value" in contents &&
|
||||||
typeof (hover.contents as any).value === "string"
|
typeof contents.value === "string"
|
||||||
) {
|
) {
|
||||||
return (hover.contents as any).value;
|
return contents.value;
|
||||||
}
|
}
|
||||||
// MarkedString | MarkedString[]
|
// MarkedString | MarkedString[]
|
||||||
if (Array.isArray(hover.contents)) {
|
if (Array.isArray(hover.contents)) {
|
||||||
@@ -39,11 +40,10 @@ function formatHover(result: unknown): string {
|
|||||||
.join("\n");
|
.join("\n");
|
||||||
}
|
}
|
||||||
if (
|
if (
|
||||||
"value" in hover.contents &&
|
"value" in contents &&
|
||||||
typeof (hover.contents as any).language === "string"
|
typeof contents.language === "string"
|
||||||
) {
|
) {
|
||||||
const ms = hover.contents as any;
|
return `\`\`\`${contents.language}\n${contents.value}\n\`\`\``;
|
||||||
return `\`\`\`${ms.language}\n${ms.value}\n\`\`\``;
|
|
||||||
}
|
}
|
||||||
return JSON.stringify(result, null, 2);
|
return JSON.stringify(result, null, 2);
|
||||||
}
|
}
|
||||||
@@ -85,7 +85,12 @@ function formatCompletions(result: unknown): string {
|
|||||||
let items: any[];
|
let items: any[];
|
||||||
if (Array.isArray(result)) {
|
if (Array.isArray(result)) {
|
||||||
items = result;
|
items = result;
|
||||||
} else if ("items" in result && Array.isArray((result as any).items)) {
|
} else if (
|
||||||
|
result &&
|
||||||
|
typeof result === "object" &&
|
||||||
|
"items" in result &&
|
||||||
|
Array.isArray((result as any).items)
|
||||||
|
) {
|
||||||
items = (result as any).items;
|
items = (result as any).items;
|
||||||
} else {
|
} else {
|
||||||
return JSON.stringify(result, null, 2);
|
return JSON.stringify(result, null, 2);
|
||||||
@@ -190,7 +195,13 @@ function formatDocumentSymbols(result: unknown): string {
|
|||||||
|
|
||||||
// Format Diagnostics - Turn diagnostic messages into readable text.
|
// Format Diagnostics - Turn diagnostic messages into readable text.
|
||||||
function formatDiagnostics(result: unknown): string {
|
function formatDiagnostics(result: unknown): string {
|
||||||
if (!result || !("diagnostics" in result)) return "(no diagnostics)";
|
if (
|
||||||
|
!result ||
|
||||||
|
typeof result !== "object" ||
|
||||||
|
!("diagnostics" in result)
|
||||||
|
) {
|
||||||
|
return "(no diagnostics)";
|
||||||
|
}
|
||||||
const diags = (result as any).diagnostics;
|
const diags = (result as any).diagnostics;
|
||||||
if (!Array.isArray(diags) || diags.length === 0) return "(no diagnostics)";
|
if (!Array.isArray(diags) || diags.length === 0) return "(no diagnostics)";
|
||||||
|
|
||||||
@@ -585,7 +596,7 @@ export default function (pi: ExtensionAPI) {
|
|||||||
// from the active set so the LLM won't attempt them. When any is enabled,
|
// from the active set so the LLM won't attempt them. When any is enabled,
|
||||||
// restore them. Captures current active tools at toggle time.
|
// restore them. Captures current active tools at toggle time.
|
||||||
function updateToolVisibility(): void {
|
function updateToolVisibility(): void {
|
||||||
const current = pi.getActiveTools().map((t) => t.name);
|
const current = pi.getActiveTools();
|
||||||
if (servers.every((s) => disabledServers.has(s.id))) {
|
if (servers.every((s) => disabledServers.has(s.id))) {
|
||||||
// All disabled — strip LSP tools
|
// All disabled — strip LSP tools
|
||||||
pi.setActiveTools(current.filter((name) => !lspToolNames.includes(name)));
|
pi.setActiveTools(current.filter((name) => !lspToolNames.includes(name)));
|
||||||
|
|||||||
Reference in New Issue
Block a user