Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions .claude/docs/v1.2.1/sessions/56b-modernize-ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Session log: #56 (part B) — modernize the TypeScript idioms

Date: 2026-11-25. Branch `claude/project-thread-n2s3q6-modernize-ts`, cut from `origin/v1.2.1` at `a2d92672` (after #35 parts A and B, PRs #61/#63, and #56 part A, PR #62). Story: Code-XR-Project/Code-XR#56 (epic #26). Plan: `docs/cleanup/modernization.md` §5, rows 1 (TypeScript part) and 4–10; the measured before/after table is its §7. Part A (Python and browser runtimes) is `56a-modernize-py-js.md`.

## Commits

| Commit | Subject | Plan row | Proof |
|---|---|---|---|
| `3e282b4c` | chore(lint): lock the TypeScript idioms already at zero | 1 | config only |
| `2308d6c0` | refactor(ts): static imports for Node built-ins | 4 | H2 (+802 B: hoisted requires, one `__importStar` helper) |
| `27bfec86` | refactor(ts): slice() instead of substr(), lock includes() and slice() | 5 | H2 (one statement) |
| `9e2506ec` | refactor(ts): optional chaining where it is safe | 6 | H2 (10 sites) |
| `bf60a7a1` | refactor(ts): template literals instead of string concatenation | 7 | H2 (8 sites) + byte-identical SSE script output |
| `55d5c819` | refactor(ts): type the any in code_analysis/views/subsections/active_analyses | 8 | H1 `3838d8e16de4` |
| `9c873f6e` | refactor(ts): type the any in code_analysis/services | 8 | H1 `3838d8e16de4` |
| `d335a157` | refactor(ts): type the any in code_analysis/engine/watchers | 8 | H1 `3838d8e16de4` |
| `552825a0` | refactor(ts): type the any in active_servers (model, registry, registrar) | 8 | H1 `3838d8e16de4` |
| `12f9599b` | refactor(ts): typed metadata.sessionId lookup in serverControl, lock active_servers | 8 + 6 | H2 (one statement) |
| `cd83e65e` | refactor(ts): ?? for object-typed defaults | 9 | H2 (48 `??`, 43 statements) |

Row 10 has no commit: none of the 24 `.then()` qualifies (see below).

## Measurements

| Metric | Before | After |
|---|---:|---:|
| `npm test` | 1053/1053 | 1056/1056 (+3 lint-contract tests) |
| ESLint warning budget (`eslint.maxWarnings`) | 204 | 158 |
| `any.total` (guardrail) | 130 | 84 |
| `complexity.over10` / `over15` | 54 / 9 | 53 / 8 |
| inline `require()` of built-ins | 7 | 0 |
| `substr()` | 1 | 0 |
| typed `prefer-optional-chain` reports | 10 | 0 |
| `prefer-template` reports | 8 | 0 |
| typed `prefer-nullish-coalescing` (`ignorePrimitives` all) | 76 (60 `\|\|`, 15 if-assign, 1 ternary) | 12 `\|\|` kept, in 8 exempted files |
| `dist/extension.js` (production, no source maps) | `b4c1894828cd`, 1,194,230 B | `983cfe36a26d`, 1,194,977 B |

The bundle grows 747 B in total, all of it from row 4 (the hoisted `require`s rename minifier locals and add TypeScript's `__importStar` helper to `SSEManager`); every other row is neutral or smaller.

## How the changes were proved

- **Types-only commits (row 8)**: the production bundle (`CODEXR_NO_SOURCEMAPS=1 npm run package`) is byte-identical to the parent, `3838d8e16de4`, each time. One `any` pair (`serverControl.ts`'s `(server.metadata as any).sessionId`) could not be typed in a dist-identical commit: once typed, `prefer-optional-chain` forces `?.`, so it is its own H2 commit (`12f9599b`).
- **H2 commits**: the minified bundle split on `;` and diffed against the parent; every changed statement is listed in the commit body.
- **Generated HTML (row 7)**: `CreateStructure.getSSEScriptWithNonce` was run from `out/` under the fake vscode, for `xr`, `dom` and `none`, before and after: same length and sha256.
- **`??` (row 9)**: converted only where the left operand is object-typed **and** produced by this code (Map/find over stored objects, internal call results, internal optional parameters). Parsed JSON from WebSocket clients and the analysis bridge, workspace storage and settings updates keep `||`, because a malformed `0`, `''` or `false` must still fall back.
- **Text pins**: every touched file grepped by basename in `test/` and `scripts/`, pins classified in each commit body. Row 9 also checked every `\|\|` / `\?\?` regex of the suites that read the touched files: one S pin found and kept.

## Gates

`npm run typecheck`, `npm run lint` and `npm test` after every commit; `npm run test:analysis` after rows 7 (executePython touched) and 9 (export code touched), both green. `guardrails:update` in every commit that moved a metric.

## Deliberately not done

- **Row 10 (`await` instead of `.then()`)**: 0 of 24. Every `.then()` is a fire-and-forget user prompt (`void vscode.window.showXMessage(…).then(choice => …)`, 18), the HTTP request handler's `routeRequest(…).then().catch()` (converting needs an async handler, and `HttpServer` is §4 territory), promise caches/queues whose promise is stored, not awaited (`gitTimelineBlobIndex` 2, `gitTimelineBlobAnalyzer` 1, `gitAnalysisEligibility` 2), `void job.then(clear, clear)` in `dependencyGraphService`, or `.then(resolve, reject)` inside a `new Promise` (`cloudflaredBinaryManager`, §4).
- **`projectEvolutionService.ts:421`** keeps `|| null`: `historicalComparison.test.cjs:467` pins `source.kind === 'workingCopy') || null` verbatim (S pin). Relaxing it needs a characterization commit first.
- **The 11 other `||` of row 9** (WebSocket/bridge payloads, storage, settings): not provably free of `''`/`0`/`false` at runtime.
- **The 18 remaining concatenations** of the A.1 row: literal + literal line wraps of long messages; `prefer-template` does not report them and they read better as they are.
- **`a && a.b` left by the script (2)**: `serverWatcherIntegration.ts:151` (`server.metadata && server.metadata.sessionId === sessionId` — not equivalent when `sessionId` is undefined) and `sharedChartTypeSettingCore.ts:103` (not reported by the typed rule).
- **Row 8 beyond 4 folders**: `utils/commandBuilder.ts` (generic `TArgs extends any[]`), `utils/jsonFieldAnalyzer.ts` (`unknown` would need narrowing code, not types), `executePython.ts`'s `PythonPayload = any` and the pinned `parsePythonOutput(…): any` signature, and the rest listed in modernization.md §7.
- §4 items: enums, `new Promise` wrappers, sync `fs` in async functions, `HttpServer.stop()` order, the XR field-schema contract, the 8 lazy requires of the tree provider: untouched.

## Design notes

- `ActiveServer.serverInstance` is typed by a structural `ActiveServerRuntime` interface in the model rather than `ServerInstance` from `servers/runtime/serverFactory`: importing the latter (even `import type`) made the model, registry and registrar lose their vscode-free status, and `dependencyDirection.test.cjs`'s floor (109) went red.
- `ActiveServer.metadata` became `ActiveServerMetadata` (the read fields plus `sessionId`, with an `unknown` index signature for what the launcher records). The model used to declare three fields while the registry stored anything.
- The per-folder `no-explicit-any: error` lives in a flat-config block named `codexr/any-free-folders`; the lint contract test excludes that one named block from its "budget rules stay warnings" check and pins it to that single rule over existing `src/` folders.

## Quirks found (record only, for 1.3.0)

- `ActiveAnalysesDataService.getSubsectionItem()` returns a plain object where its caller expects a `CodeAnalysisTreeItem`; it type-checks structurally, but it is not an instance.
- `metadata.sessionId` reaches the registry only through the launcher's `additionalMetadata` spread (`serverLaunchOrchestrator.ts` passes `{ sessionId }`), which `ActiveServer.metadata` did not declare until this session; it is now an optional field of `ActiveServerMetadata`.

## CHANGELOG

No line: nothing a user can notice. Covered by the existing "Internal improvements" summary.

## F5 checks for the maintainer

1. Active Analyses: run a directory analysis (XR and LivePanel); left-click an entry (actions quick pick), Show Details, View in Browser, Export folder, Close Analysis.
2. Stop the analysis server from Active Servers: the analysis is cleaned up and the notification names the port.
3. Edit a file in an analyzed directory: the incremental re-analysis updates the scene/panel (XR and LivePanel), including adding and deleting a file.
4. Active Servers tooltip shows `File: <name>` for a served HTML file; Open in Browser works on HTTP and HTTPS servers.
5. Collaboration: join a room, transfer host, kick a participant, presenter stop (paths with `?? {}` defaults around them).
6. Visualize Data: pick a chart, a JSON file and map dimensions; the field list shows "N values • Samples: …".
73 changes: 73 additions & 0 deletions apps/vscode-extension/eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,79 @@ export default [{
"@typescript-eslint/no-floating-promises": ["error", { checkThenables: true }],
"@typescript-eslint/no-misused-promises": "warn",
},
}, {
// Modernized idioms (story #56, docs/cleanup/modernization.md §1 and §7).
// Each rule landed in the commit that brought its idiom to zero in src/, so
// the count can only stay there. Exceptions are per-file overrides below,
// never eslint-disable comments (the suppressions guardrail is at 0).
files: ["src/**/*.ts"],
rules: {
"no-var": "error",
"prefer-rest-params": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-unsafe-function-type": "error",
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-optional-chain": "error",
"prefer-template": "error",
"no-restricted-properties": ["error", {
property: "substr",
message: "Use slice() (substr is Annex B legacy; slice(start, start + length) for non-negative arguments).",
}],
// `??` only where the left operand is object-typed: '' || d and '' ?? d
// differ, and settings or user JSON carry real '', 0 and false values,
// so primitive operands, ternaries and if-assignments are not reported.
"@typescript-eslint/prefer-nullish-coalescing": ["error", {
ignorePrimitives: { string: true, number: true, boolean: true, bigint: true },
ignoreTernaryTests: true,
ignoreIfStatements: true,
}],
},
}, {
// Files that keep an object-typed `x || {}` on purpose: the operand is
// parsed JSON from a WebSocket client, the bridge protocol, workspace
// storage or a settings update, where a malformed `0`, `''` or `false`
// must still fall back to the default (modernization.md §7, row 9).
files: [
// historicalComparison.test.cjs pins `source.kind === 'workingCopy') || null` here (S pin).
"src/code_analysis/historical/projectEvolutionService.ts",
"src/servers/runtime/analysis/dependencyGraphBridge.ts",
"src/servers/runtime/analysis/projectEvolutionBridge.ts",
"src/servers/runtime/collaboration/collaborationRoomServer.ts",
"src/servers/runtime/collaboration/roomSharedState.ts",
"src/servers/storage/serverSettingsManager.ts",
"src/visualize_data/state/visualizeDataState.ts",
"src/code_analysis/views/subsections/analysis_settings/dimension_mapping_shared/sharedDimensionMappingSettingCore.ts",
],
rules: {
"@typescript-eslint/prefer-nullish-coalescing": "off",
},
}, {
// The lazy require() calls that stay (modernization.md §4): the eight in
// the tree provider are load-bearing (cleanup skill §4), and the other two
// defer a relative import to break a load-time module cycle.
files: [
"src/views/ModularTreeDataProvider.ts",
"src/code_analysis/engine/watchers/directoryReAnalyzer.ts",
"src/utils/getVisualizationConfiguration.ts",
],
rules: {
"@typescript-eslint/no-require-imports": "off",
},
}, {
// Folders whose `any`s were typed away (story #56, modernization.md §7):
// there no-explicit-any is an error, not a budget warning. A folder joins
// the list in the commit that brings it to zero.
name: "codexr/any-free-folders",
files: [
"src/active_servers/**/*.ts",
"src/code_analysis/engine/watchers/**/*.ts",
"src/code_analysis/services/**/*.ts",
"src/code_analysis/views/subsections/active_analyses/**/*.ts",
],
rules: {
"@typescript-eslint/no-explicit-any": "error",
},
}, {
files: NO_CONSOLE_FILES,
ignores: CONSOLE_ALLOWLIST,
Expand Down
2 changes: 1 addition & 1 deletion apps/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@
"typecheck": "node ../../node_modules/typescript/bin/tsc --noEmit --incremental false -p tsconfig.check.json && node ../../node_modules/typescript/bin/tsc --incremental false -p tsconfig.tooling.json",
"compile-tests": "node -e \"require('fs').rmSync('out',{recursive:true,force:true})\" && node ../../node_modules/typescript/bin/tsc -p . --outDir out",
"watch-tests": "node ../../node_modules/typescript/bin/tsc -p . -w --outDir out",
"lint": "node ../../node_modules/eslint/bin/eslint.js src test scripts templates webpack.config.js eslint.config.mjs --max-warnings 204",
"lint": "node ../../node_modules/eslint/bin/eslint.js src test scripts templates webpack.config.js eslint.config.mjs --max-warnings 158",
"test": "npm run test:basic",
"test:basic": "npm run typecheck && npm run lint && npm run compile-tests && npm run test:unit",
"test:unit": "node test/runners/run-node-tests.cjs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,35 @@ export interface ActiveServer {
customName?: string;

/** Server instance reference for control operations */
serverInstance?: any;
serverInstance?: ActiveServerRuntime;

/** Additional metadata */
metadata?: {
host?: string;
staticRoot?: string;
description?: string;
};
metadata?: ActiveServerMetadata;
}

/**
* The runtime behind a registered server (HttpServer or one of the HTTPS
* servers). Declared structurally, with only what the extension calls on it,
* so this model stays free of the server runtime and of vscode.
*/
export interface ActiveServerRuntime {
stop(): Promise<void>;
forceStop(): void;
createAuthenticatedBrowserUrl(baseUrl: string): string;
}

/**
* Registration metadata, stored as given: the fields the extension reads,
* plus whatever the launcher records (serverType, originalPort, portChanged,
* launcherId, ...) and its callers add.
*/
export interface ActiveServerMetadata {
host?: string;
staticRoot?: string;
description?: string;
/** The analysis session the server serves, when the registration names one. */
sessionId?: string;
[field: string]: unknown;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events';
import { ActiveServer, RegistryEvent, RegistryEventType, LaunchMode, CertMode } from '../model/activeServerModel';
import { ActiveServer, ActiveServerMetadata, ActiveServerRuntime, RegistryEvent, RegistryEventType, LaunchMode, CertMode } from '../model/activeServerModel';
import { RemoteAccessState } from '../../remote_access/model/remoteAccessModel';
import { getLogger } from '../../core/logging/sink';

Expand Down Expand Up @@ -81,8 +81,8 @@ export class ActiveServerRegistry {
timestamp: number;
htmlFile?: string;
customName?: string;
serverInstance?: any;
metadata?: any;
serverInstance?: ActiveServerRuntime;
metadata?: ActiveServerMetadata;
}): ActiveServer {
logger.debug('SERVER: registerServer called with config:', {
port: config.port,
Expand Down Expand Up @@ -343,8 +343,8 @@ export function registerActiveServer(config: {
timestamp: number;
htmlFile?: string;
customName?: string;
serverInstance?: any;
metadata?: any;
serverInstance?: ActiveServerRuntime;
metadata?: ActiveServerMetadata;
}): ActiveServer {
return getActiveServerRegistry().registerServer(config);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import { ActiveServer } from '../model/activeServerModel';
import { ActiveServer, ActiveServerRuntime } from '../model/activeServerModel';
import type { UnifiedAnalysisSession } from '../../code_analysis/engine/core/analysisSession';
import { getActiveServerRegistry } from '../registry/activeServerRegistry';
import { getPanelManager } from '../services/panelManager';
import { fileToServerMap } from '../../utils/fileToServerMap';
Expand Down Expand Up @@ -117,7 +118,7 @@ export class ServerControl {
* Await the instance's graceful stop, falling back to a forced socket close
* if it hangs or fails. Returns false when the shutdown was not clean.
*/
private static async stopInstanceOrForce(serverId: string, instance: any): Promise<boolean> {
private static async stopInstanceOrForce(serverId: string, instance: ActiveServerRuntime): Promise<boolean> {
let timer: NodeJS.Timeout | undefined;
try {
await Promise.race([
Expand Down Expand Up @@ -220,7 +221,7 @@ export class ServerControl {
const allSessions = sessionRegistry.getAllSessions();

logger.debug(() => `DEBUG - Found ${allSessions.length} active analysis sessions`);
allSessions.forEach((session: any, index: number) => {
allSessions.forEach((session: UnifiedAnalysisSession, index: number) => {
logger.debug(() => `DEBUG - Session ${index + 1}:`, {
id: session.id,
targetPath: session.targetPath,
Expand All @@ -233,8 +234,8 @@ export class ServerControl {
let associatedSession = null;

// Try to find by sessionId in server metadata (when implemented)
if (server.metadata && (server.metadata as any).sessionId) {
const sessionId = (server.metadata as any).sessionId;
if (server.metadata?.sessionId) {
const sessionId = server.metadata.sessionId;
logger.debug(() => `DEBUG - Trying to find session by metadata sessionId: ${sessionId}`);
associatedSession = sessionRegistry.getSession(sessionId);
if (associatedSession) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActiveServer, LaunchMode, CertMode } from '../model/activeServerModel';
import { ActiveServer, ActiveServerMetadata, ActiveServerRuntime, LaunchMode, CertMode } from '../model/activeServerModel';
import { registerActiveServer } from '../registry/activeServerRegistry';
import { getLogger } from '../../core/logging/sink';

Expand All @@ -15,8 +15,8 @@ export interface ServerRegistrationConfig {
timestamp: number;
htmlFile?: string;
customName?: string;
serverInstance?: any;
metadata?: any;
serverInstance?: ActiveServerRuntime;
metadata?: ActiveServerMetadata;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function getServerLabel(server: ActiveServer): string {
* Format server details for display
*/
export function formatServerDetails(server: ActiveServer): string {
const metadata = (server.metadata || {}) as Record<string, unknown>;
const metadata = (server.metadata ?? {}) as Record<string, unknown>;
const remote = server.remoteAccess;
const protocol = server.certMode === 'http' ? 'http' : 'https';
const lanUrl = NetworkUtils.getPrimaryExternalUrl(server.port, protocol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class CreateChart {
// Every chart — boats included — comes from the single canonical
// template list; a duplicated boats fallback used to live here and
// drifted from the real one.
const chart = chartTemplates.find((candidate) => candidate.id === chartId) || null;
const chart = chartTemplates.find((candidate) => candidate.id === chartId) ?? null;
if (!chart) {
return {
success: false,
Expand Down Expand Up @@ -216,7 +216,7 @@ export class CreateChart {
* Get chart metadata by ID
*/
public static getChartMetadata(chartId: string): ChartMetadata | null {
return chartTemplates.find(c => c.id === chartId) || null;
return chartTemplates.find(c => c.id === chartId) ?? null;
}

/**
Expand Down
Loading
Loading