sveld 0.32.8 → 0.34.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.
@@ -0,0 +1,156 @@
1
+ import { type NormalizedPath } from "./brands";
2
+ import { type ParsedComponent } from "./ComponentParser";
3
+ import { type SveldDiagnostic } from "./diagnostics";
4
+ import { ParseCache } from "./parse-cache";
5
+ import { type EntryExports } from "./parse-entry-exports";
6
+ import { type ParsedExports } from "./parse-exports";
7
+ export interface ComponentDocApi extends ParsedComponent {
8
+ filePath: NormalizedPath;
9
+ moduleName: string;
10
+ }
11
+ export type ComponentDocs = Map<string, ComponentDocApi>;
12
+ /**
13
+ * A parse failure for a single component, captured so the rest of the run
14
+ * can continue. Surfaced via {@link GenerateBundleResult.errors}.
15
+ */
16
+ export interface ComponentParseError {
17
+ filePath: string;
18
+ moduleName: string;
19
+ message: string;
20
+ stack?: string;
21
+ }
22
+ export interface GenerateBundleResult {
23
+ exports: ParsedExports;
24
+ /** Entry-barrel exports other than components. Empty when `documentExports` is off. */
25
+ entryExports: EntryExports;
26
+ components: ComponentDocs;
27
+ allComponentsForTypes: ComponentDocs;
28
+ /**
29
+ * Components that failed to parse. Empty unless `failFast` is disabled and
30
+ * one or more components threw during parsing.
31
+ */
32
+ errors: ComponentParseError[];
33
+ /** Unknown props, `any` contexts, and orphan `@event` tags across the bundle. */
34
+ diagnostics: SveldDiagnostic[];
35
+ }
36
+ export interface GenerateBundleOptions {
37
+ /**
38
+ * Throw on the first component that fails to parse instead of collecting
39
+ * the failure and continuing with the remaining components.
40
+ */
41
+ failFast?: boolean;
42
+ /**
43
+ * Load the TypeScript program to expand opaque imported whole-object `$props()`
44
+ * types into JSON/Markdown props. Off by default; requires `typescript`.
45
+ */
46
+ resolveTypes?: boolean;
47
+ /** Record consts, functions, and types from the entry barrel. Off by default. */
48
+ documentExports?: boolean;
49
+ /**
50
+ * Cache parsed component output to disk. Unchanged files skip re-parsing on
51
+ * later runs. `true` uses `node_modules/.cache/sveld/parse-cache.json`; a
52
+ * string sets a custom path. Off by default.
53
+ */
54
+ cache?: boolean | string;
55
+ /**
56
+ * Run plain TS/JS `@example` blocks on props, module exports, slots, and
57
+ * events through the TypeScript program. Broken examples become
58
+ * `example-compile-error` diagnostics. Svelte/HTML markup is skipped.
59
+ * Off by default. Requires `typescript`.
60
+ */
61
+ checkExamples?: boolean;
62
+ }
63
+ export declare function toGenerateBundleOptions(opts?: Pick<GenerateBundleOptions, "failFast" | "resolveTypes" | "documentExports" | "cache" | "checkExamples">): GenerateBundleOptions;
64
+ /** A function that resolves a (possibly relative) component path to an absolute path. */
65
+ export type ResolveComponentFilePath = (filePath: string) => string;
66
+ /** Options controlling how a single component parse failure is handled. */
67
+ export interface ProcessComponentOptions {
68
+ /** Rethrow on parse failure instead of reporting it via `onParseError`. */
69
+ failFast?: boolean;
70
+ /** Invoked with a diagnostic when a component fails to parse (and `failFast` is off). */
71
+ onParseError?: (error: ComponentParseError) => void;
72
+ /** When set, reuse a component's previous parse if its content hash is unchanged. */
73
+ cache?: ParseCache;
74
+ }
75
+ /**
76
+ * Discovered component sources for an entry point, before parsing.
77
+ *
78
+ * `exports` holds explicitly exported components (used for JSON/Markdown);
79
+ * `allComponents` additionally includes glob-discovered components (used for
80
+ * `.d.ts` generation). `resolveComponentFilePath` maps a component `source`
81
+ * to its absolute path on disk.
82
+ */
83
+ export interface CollectedComponents {
84
+ exports: ParsedExports;
85
+ allComponents: ParsedExports;
86
+ rootDir: string;
87
+ resolveComponentFilePath: ResolveComponentFilePath;
88
+ }
89
+ /**
90
+ * Discovers component sources for an entry point without parsing them.
91
+ *
92
+ * Parses the entry's exports (when `input` is a file) and, when `glob` is set,
93
+ * augments the set with every `.svelte` file under the entry directory.
94
+ *
95
+ * @param documentExports - When `true`, log and continue if the entry file fails
96
+ * the acorn component-export parse (TypeScript-only syntax is common).
97
+ */
98
+ export declare function collectComponents(input: string, glob: boolean, documentExports?: boolean): CollectedComponents;
99
+ /**
100
+ * Reads the given component file paths into a map of path -> contents.
101
+ *
102
+ * Failed reads are recorded as `null` (and logged) so callers can skip them
103
+ * gracefully rather than aborting the whole bundle.
104
+ */
105
+ export declare function readFileMap(filePaths: Iterable<string>): Promise<Map<string, string | null>>;
106
+ /**
107
+ * Parses a single component entry into its documentation API.
108
+ *
109
+ * Reads the component contents from `fileMap`, removes top-level styles for
110
+ * metadata parsing, and parses it to extract component metadata. Returns `null`
111
+ * for non-Svelte entries or files that could not be read.
112
+ *
113
+ * A component that throws while parsing is captured via `options.onParseError`
114
+ * (and `null` is returned) so callers can continue with the rest, unless
115
+ * `options.failFast` is set, in which case the error is rethrown.
116
+ *
117
+ * @param entry - Export entry tuple `[exportName, exportInfo]`
118
+ * @param entries - All sibling entries, used to resolve the module name
119
+ * @param fileMap - Map of resolved file paths to their contents
120
+ * @param resolveComponentFilePath - Resolves a component `source` to its absolute path
121
+ * @param options - Parse-failure handling (`failFast` / `onParseError`)
122
+ */
123
+ export declare function processComponent([exportName, entry]: [string, ParsedExports[string]], entries: Array<[string, ParsedExports[string]]>, fileMap: Map<string, string | null>, resolveComponentFilePath: ResolveComponentFilePath, options?: ProcessComponentOptions): ComponentDocApi | null;
124
+ /** Collects the resolved, absolute paths of all `.svelte` entries. */
125
+ export declare function collectSvelteFilePaths(entriesList: Array<Array<[string, ParsedExports[string]]>>, resolveComponentFilePath: ResolveComponentFilePath): Set<string>;
126
+ /** Reports collected component parse errors to stderr. No-op when empty. */
127
+ export declare function reportParseErrors(errors: ComponentParseError[]): void;
128
+ /**
129
+ * Generates component documentation bundle from Svelte source files.
130
+ *
131
+ * Parses exports, discovers components (optionally via glob), and processes
132
+ * all Svelte files to extract component metadata. Returns both exported
133
+ * components (for JSON/Markdown) and all components (for TypeScript definitions).
134
+ *
135
+ * A single component that fails to parse is captured as a diagnostic (see
136
+ * {@link GenerateBundleResult.errors}) so the remaining components still emit
137
+ * output. Pass `{ failFast: true }` to restore abort-on-first-error behavior.
138
+ *
139
+ * @param input - Entry point file or directory containing Svelte components
140
+ * @param glob - Whether to glob for all .svelte files in the directory
141
+ * @param options - Bundle options (e.g. `failFast`, `resolveTypes`, `documentExports`)
142
+ * @returns Bundle result containing exports, entryExports, components, allComponentsForTypes, and errors
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * // Generate from single file:
147
+ * const result = await generateBundle("./src/App.svelte", false);
148
+ *
149
+ * // Generate from directory with glob:
150
+ * const result = await generateBundle("./src", true);
151
+ *
152
+ * // Abort on the first parse failure:
153
+ * const result = await generateBundle("./src", true, { failFast: true });
154
+ * ```
155
+ */
156
+ export declare function generateBundle(input: string, glob: boolean, options?: GenerateBundleOptions): Promise<GenerateBundleResult>;
package/lib/check.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ import type { ComponentDocs } from "./bundle";
2
+ import type { EntryExports } from "./parse-entry-exports";
3
+ import { type ComponentApiDocument } from "./writer/document-model";
4
+ export type SemverBump = "major" | "minor" | "patch" | "none";
5
+ /** One API diff between the committed snapshot and the current parse. */
6
+ export interface ApiChange {
7
+ /** Component `moduleName` this change belongs to, or `"*"` for document-wide notices. */
8
+ component: string;
9
+ kind: "component" | "prop" | "moduleExport" | "event" | "slot" | "shape";
10
+ /** Prop, event, slot, or shape-field name, when applicable. */
11
+ name?: string;
12
+ bump: SemverBump;
13
+ message: string;
14
+ }
15
+ export interface CheckResult {
16
+ /** `false` when there was nothing on disk to diff against (e.g. first run). */
17
+ snapshotExists: boolean;
18
+ snapshotFile: string;
19
+ changes: ApiChange[];
20
+ /** Highest bump across all changes. */
21
+ bump: SemverBump;
22
+ }
23
+ /** Diffs two `COMPONENT_API.json` documents and assigns a semver bump to each change. */
24
+ export declare function diffApiDocuments(previous: ComponentApiDocument, next: ComponentApiDocument): ApiChange[];
25
+ export interface RunCheckOptions {
26
+ /** Entry-barrel exports when `documentExports` is on. */
27
+ entryExports?: EntryExports;
28
+ }
29
+ /**
30
+ * Diffs the current component set against a committed `COMPONENT_API.json`
31
+ * snapshot and assigns a semver bump to each change. Returns
32
+ * `snapshotExists: false` when no snapshot file exists yet, so the first
33
+ * run does not fail CI.
34
+ */
35
+ export declare function runCheck(components: ComponentDocs, snapshotFile: string, options?: RunCheckOptions): Promise<CheckResult>;
36
+ /** Groups changes by component for CLI output. */
37
+ export declare function formatCheckReport(result: CheckResult): string;
package/lib/cli.d.ts CHANGED
@@ -1,5 +1,20 @@
1
+ import { type PluginSveldOptions } from "./plugin";
2
+ /** CLI options layered on top of the shared plugin options. */
3
+ interface CliOptions extends PluginSveldOptions {
4
+ /** Set exit code 1 when diagnostics are present. */
5
+ strict?: boolean;
6
+ /**
7
+ * Diff the parsed component API against a committed snapshot (default:
8
+ * the `json` writer's `outFile`, or `COMPONENT_API.json`) and assign a
9
+ * semver bump to each change. Exits `1` on a breaking change. Pass a
10
+ * string for a custom snapshot path.
11
+ */
12
+ check?: boolean | string;
13
+ }
14
+ export declare function parseCliOptions(argv: string[]): CliOptions;
1
15
  /**
2
- * CLI entry point: parse flags, run Rollup, generate docs, write outputs.
16
+ * CLI entry point: parse flags, load any config file, run Rollup, generate
17
+ * docs, write outputs.
3
18
  *
4
19
  * @example
5
20
  * ```ts
@@ -8,3 +23,4 @@
8
23
  * ```
9
24
  */
10
25
  export declare function cli(process: NodeJS.Process): Promise<void>;
26
+ export {};
@@ -0,0 +1,20 @@
1
+ import type { ComponentDocApi, ComponentDocs, ResolveComponentFilePath } from "./bundle";
2
+ /**
3
+ * Resolves a component's `@extendProps` / `@extends` target to an absolute path,
4
+ * or `null` when it doesn't point at a local `.svelte` file (e.g. it references
5
+ * an external package interface like `carbon-components-svelte`).
6
+ */
7
+ export declare function resolveExtendsDependency(api: ComponentDocApi, componentPath: string): string | null;
8
+ /**
9
+ * Builds a reverse-dependency map: `dependencyPath -> set of dependent paths`.
10
+ *
11
+ * A component is a dependent of `X` when it extends `X` via `@extendProps` /
12
+ * `@extends`. When `X` changes, every dependent must be re-parsed.
13
+ */
14
+ export declare function buildReverseDeps(components: ComponentDocs, resolveComponentFilePath: ResolveComponentFilePath): Map<string, Set<string>>;
15
+ /**
16
+ * Expands the set of changed paths to include every transitive dependent via
17
+ * the reverse-dependency map (e.g. editing `Button.svelte` also marks the
18
+ * `SecondaryButton.svelte` that `@extendProps`-es it).
19
+ */
20
+ export declare function expandAffected(changed: Iterable<string>, reverseDeps: Map<string, Set<string>>): Set<string>;
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Why sveld could not pin a type during parsing.
3
+ *
4
+ * - `prop-unknown-type`: prop `typeSource` is `"unknown"`.
5
+ * - `context-any-type`: `setContext` value inferred as `any`.
6
+ * - `event-no-source`: `@event` with no dispatch, forward, or callback prop.
7
+ * - `example-compile-error`: an `@example` block failed to type-check (opt-in, `checkExamples`).
8
+ */
9
+ export type SveldDiagnosticKind = "prop-unknown-type" | "context-any-type" | "event-no-source" | "example-compile-error";
10
+ /**
11
+ * One place sveld had to guess a type instead of inferring it.
12
+ */
13
+ export interface SveldDiagnostic {
14
+ /** File this came from, e.g. `"./Button.svelte"`. */
15
+ component: string;
16
+ kind: SveldDiagnosticKind;
17
+ /** Prop, context field, or event name. */
18
+ name: string;
19
+ /** What went wrong and what type sveld used. */
20
+ message: string;
21
+ }
22
+ /**
23
+ * Drop duplicates (same component, kind, and name). Each file is parsed twice
24
+ * when building exports and `.d.ts`, so without this the CLI summary doubles.
25
+ */
26
+ export declare function dedupeDiagnostics(diagnostics: SveldDiagnostic[]): SveldDiagnostic[];
27
+ /**
28
+ * Group diagnostics by kind and component for CLI output.
29
+ */
30
+ export declare function formatDiagnosticsSummary(diagnostics: SveldDiagnostic[]): string;
@@ -0,0 +1,22 @@
1
+ import type { ParsedComponent } from "./ComponentParser";
2
+ /**
3
+ * One `@example` block worth type-checking, reduced to what `resolve-types.ts`
4
+ * needs: a declaration for the documented symbol and the example body itself.
5
+ */
6
+ export interface ExampleCheckSource {
7
+ /** Stable id for diagnostics, e.g. `"prop:variant"` or `"prop:variant#1"` for a second example. */
8
+ id: string;
9
+ /** Human-readable name shown in diagnostics, e.g. `"variant"` or `"variant (example 2)"`. */
10
+ name: string;
11
+ /** TypeScript type to bind the documented symbol to before running `code`. */
12
+ type: string;
13
+ /** The `@example` body, stripped of any surrounding code fence. */
14
+ code: string;
15
+ }
16
+ /**
17
+ * Collects every `@example` block sveld can type-check for a parsed component:
18
+ * plain TS/JS bodies on props, module exports, slots, and events. Svelte/HTML
19
+ * markup examples (most slot/event examples, many prop examples) are skipped.
20
+ * Checking those needs `svelte2tsx` or similar; sveld stays AST-only.
21
+ */
22
+ export declare function collectExampleSources(component: ParsedComponent): ExampleCheckSource[];
package/lib/index.d.ts CHANGED
@@ -1,5 +1,10 @@
1
1
  export { default as ComponentParser, type SerializedComponentEvent } from "./ComponentParser";
2
+ export { type ApiChange, type CheckResult, diffApiDocuments, formatCheckReport, runCheck, type SemverBump, } from "./check";
2
3
  export { cli } from "./cli";
4
+ export type { SveldDiagnostic, SveldDiagnosticKind } from "./diagnostics";
3
5
  export type { SvelteEntryPoint } from "./get-svelte-entry";
6
+ export { defineConfig, type SveldConfig } from "./load-config";
4
7
  export { default } from "./plugin";
5
8
  export { sveld } from "./sveld";
9
+ export { buildComponentApiDocument, type ComponentApiDocument } from "./writer/document-model";
10
+ export { getWriter, listWriters, type OutputWriter, registerWriter } from "./writer/registry";