Diagnostic collection for source analysis.
Provides structured error/warning collection during TypeScript and Svelte
analysis, replacing silent catch blocks with actionable diagnostics.
Error Handling Contract
The pipeline accumulates failures into the diagnostics array and keeps
going — analysis continues, declarations/members reach the output with
partial: true when extraction failed mid-flight, and the caller decides
how to react. Producers include type resolution failures, individual
member/prop extraction failures, svelte2tsx transform failures, source-map
construction failures, import lex / resolver failures, and duplicate
declarations.
A small set of conditions still throws from public entry points — these are
setup-level, not per-file: missing tsconfig.json (loadTsconfig), Svelte
<5 detected (transformSvelteSource), or strict discovery: 'exports'
mode with no resolvable exports (discoverSourceFiles). Wrap the top-level
analyze / analyzeFromFiles call if you want to handle these.
Usage Pattern
const {modules, diagnostics} = await analyze({sourceFiles, sourceOptions});
if (hasErrors(diagnostics)) {
for (const err of errorsOf(diagnostics)) {
console.error(formatDiagnostic(err));
}
}
for (const warning of warningsOf(diagnostics)) {
console.warn(formatDiagnostic(warning));
}Schema-Validated Plain Data
AnalyzeResultJson.diagnostics is Array<Diagnostic> — no wrapper object,
no methods, no private fields. Round-trip-safe through JSON.stringify /
z.array(Diagnostic).parse and symmetric with AnalyzeResultJson.modules
(also Zod-validated). The full envelope is itself a Zod schema
(AnalyzeResultJson in analyze-core.ts) with both fields defaulting to
[], so the whole {modules, diagnostics} shape round-trips through
JSON.stringify(result, compactReplacer) / AnalyzeResultJson.parse even
when one or both arrays are empty (the wire form becomes {} and .parse()
restores the defaults). Use the free helpers hasErrors, errorsOf,
byKind for queries; mutate the array with native Array.push.
File Path Contract
Diagnostic.file is always project-root-relative — no leading slash,
no ./ prefix. analyze / analyzeFromFiles normalize paths from all
sources (extraction, discovery, dependency resolution) before returning.
Consumers that need an absolute path can rejoin with projectRoot.