sveld 0.32.3 → 0.32.5

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.
@@ -135,26 +135,6 @@ interface ComponentSlot {
135
135
  /** Source range for the slot/snippet declaration or documentation tag, when available */
136
136
  source?: SourceRange;
137
137
  }
138
- /**
139
- * Event that is forwarded from a child component or element.
140
- *
141
- * Forwarded events are those that use `on:eventname` syntax without
142
- * a handler, passing the event through to the parent.
143
- */
144
- interface ForwardedEvent {
145
- /** Always "forwarded" for forwarded events */
146
- type: "forwarded";
147
- /** The event name (e.g., "click", "change") */
148
- name: string;
149
- /** The element or component that forwards this event */
150
- element: ComponentInlineElement | ComponentElement;
151
- /** Description extracted from JSDoc `@event` tags */
152
- description?: string;
153
- /** The detail type if explicitly specified in `@event` tag */
154
- detail?: string;
155
- /** Source range for the forwarded event declaration or documentation tag, when available */
156
- source?: SourceRange;
157
- }
158
138
  /**
159
139
  * Event that is dispatched by the component.
160
140
  *
@@ -173,7 +153,40 @@ interface DispatchedEvent {
173
153
  /** Source range for the dispatched event call or documentation tag, when available */
174
154
  source?: SourceRange;
175
155
  }
176
- type ComponentEvent = ForwardedEvent | DispatchedEvent;
156
+ /**
157
+ * Serialized version of {@link ForwardedEvent} for JSON output.
158
+ *
159
+ * This interface maintains backward compatibility by serializing the `element`
160
+ * property as a string instead of an object. The element name is extracted
161
+ * from the {@link ComponentInlineElement} or {@link ComponentElement} object.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * // ForwardedEvent with element object:
166
+ * { type: "forwarded", name: "click", element: { type: "Element", name: "button" } }
167
+ *
168
+ * // SerializedForwardedEvent for JSON:
169
+ * { type: "forwarded", name: "click", element: "button" }
170
+ * ```
171
+ */
172
+ export interface SerializedForwardedEvent {
173
+ /** Always "forwarded" for forwarded events */
174
+ type: "forwarded";
175
+ /** The event name (e.g., "click", "change") */
176
+ name: string;
177
+ /**
178
+ * Serialized as string for JSON backward compatibility.
179
+ * In the internal API, this is an object, but for JSON output it's a string.
180
+ */
181
+ element: string;
182
+ /** Description extracted from JSDoc `@event` tags */
183
+ description?: string;
184
+ /** The detail type if explicitly specified in `@event` tag */
185
+ detail?: string;
186
+ /** Source range for the forwarded event declaration or documentation tag, when available */
187
+ source?: SourceRange;
188
+ }
189
+ export type SerializedComponentEvent = SerializedForwardedEvent | DispatchedEvent;
177
190
  /**
178
191
  * Type definition extracted from JSDoc `@typedef` tags.
179
192
  *
@@ -306,8 +319,8 @@ export interface ParsedComponent {
306
319
  moduleExports: ComponentProp[];
307
320
  /** Slots available in the component template */
308
321
  slots: ComponentSlot[];
309
- /** Events that the component can dispatch or forward */
310
- events: ComponentEvent[];
322
+ /** Events that the component can dispatch or forward (serialized for JSON/API output) */
323
+ events: SerializedComponentEvent[];
311
324
  /** Custom type definitions from JSDoc `@typedef` tags */
312
325
  typedefs: TypeDef[];
313
326
  /** Generic type parameters (e.g., `[name: "T", type: "string"]`) or null */
@@ -350,6 +363,8 @@ export default class ComponentParser {
350
363
  private readonly reactive_vars;
351
364
  /** Set of all variable declarations found in the component script */
352
365
  private readonly vars;
366
+ /** Function declarations in the component script, by name */
367
+ private readonly funcDecls;
353
368
  /** Map of component props keyed by prop name */
354
369
  private readonly props;
355
370
  /** Map of module exports (functions/variables exported from script) keyed by name */
@@ -440,7 +455,6 @@ export default class ComponentParser {
440
455
  private buildScopeDeclarations;
441
456
  private createRestPropsFromParent;
442
457
  private maybeSetRestProps;
443
- private isCallExpressionNamed;
444
458
  private getPropertyName;
445
459
  private logUnsupportedRunesPattern;
446
460
  private logParserWarning;
@@ -506,23 +520,14 @@ export default class ComponentParser {
506
520
  * ```ts
507
521
  * // Input JSDoc:
508
522
  * /**
509
- * * @type {string}
510
- * * @param {number} x - The x coordinate
511
- * * @param {number} y - The y coordinate
512
- * * @returns {void}
513
- * * Description text
514
- * *\/
515
- *
523
+ * * @type {string}
524
+ * * @param {number} x - The x coordinate
525
+ * * @param {number} y - The y coordinate
526
+ * * @returns {void}
527
+ * * Description text
528
+ * *\/
516
529
  * // Output:
517
- * {
518
- * type: "string",
519
- * params: [
520
- * { name: "x", type: "number", description: "The x coordinate", optional: false },
521
- * { name: "y", type: "number", description: "The y coordinate", optional: false }
522
- * ],
523
- * returnType: "void",
524
- * description: "Description text"
525
- * }
530
+ * { type: "string", params: [ { name: "x", type: "number", description: "The x coordinate", optional: false }, { name: "y", type: "number", description: "The y coordinate", optional: false } ], returnType: "void", description: "Description text" }
526
531
  * ```
527
532
  */
528
533
  private processJSDocComment;
@@ -597,6 +602,44 @@ export default class ComponentParser {
597
602
  * Returns the init node if found, or undefined.
598
603
  */
599
604
  private resolveLocalVarInitializer;
605
+ /**
606
+ * Look up JSDoc on a local variable declaration by name.
607
+ */
608
+ private resolveLocalVarJSDoc;
609
+ /**
610
+ * Build a function type from `@param`/`@returns` when `@type` is missing.
611
+ * If JSDoc has no params or return either, try the function `node`, then
612
+ * `(...args: any[]) => any`.
613
+ */
614
+ private buildFunctionTypeFromParts;
615
+ /**
616
+ * Guess arity and return type for a function default with no JSDoc `@type`.
617
+ * Explicit `@type`/`@param`/`@returns` on the prop beat this every time.
618
+ * A default's body is a weak signal for the prop contract. We only read
619
+ * named params and literal returns. Everything else becomes `any`.
620
+ */
621
+ private inferFunctionTypeFromNode;
622
+ /**
623
+ * Turn params into `name: any`, or use `...args: any[]` when arity is unclear:
624
+ * no params, destructuring, rest, or defaults.
625
+ */
626
+ private inferParamsFromNode;
627
+ /**
628
+ * Infer return type from literal returns only. Every `return` must agree on
629
+ * the same primitive. Bare `return;`, no returns, identifiers, calls,
630
+ * objects, ternaries, async, or generators all become `any`.
631
+ */
632
+ private inferReturnTypeFromNode;
633
+ /**
634
+ * Walk a block body and collect each `return`'s argument, skipping nested
635
+ * functions. Bare `return;` becomes `null`.
636
+ */
637
+ private collectReturnArguments;
638
+ /**
639
+ * Map one return expression to `string`, `number`, or `boolean`, or `null`
640
+ * if it isn't a literal, template literal, or `String`/`Number`/`Boolean` call.
641
+ */
642
+ private inferReturnPrimitive;
600
643
  /**
601
644
  * Unwraps `$bindable(...)` calls so defaults are documented as their underlying values.
602
645
  */
@@ -794,11 +837,7 @@ export default class ComponentParser {
794
837
  * @example
795
838
  * ```ts
796
839
  * // Input:
797
- * [
798
- * { name: "value", type: "string", description: "The new value" },
799
- * { name: "count", type: "number", optional: true, default: "0" }
800
- * ]
801
- *
840
+ * [ { name: "value", type: "string", description: "The new value" }, { name: "count", type: "number", optional: true, default: "0" } ]
802
841
  * // Output:
803
842
  * "{ /** The new value *\/ value: string; /** @default 0 *\/ count?: number; }"
804
843
  * ```
@@ -0,0 +1,12 @@
1
+ import type { ArrowFunctionExpression, CallExpression, FunctionDeclaration, FunctionExpression, Identifier, Literal, MemberExpression, ObjectExpression, VariableDeclaration } from "estree";
2
+ export declare function isObject(value: unknown): value is Record<string, unknown>;
3
+ export declare function isVariableDeclaration(node: unknown): node is VariableDeclaration;
4
+ export declare function isLiteral(node: unknown): node is Literal;
5
+ export declare function isIdentifier(node: unknown): node is Identifier;
6
+ export declare function isMemberExpression(node: unknown): node is MemberExpression;
7
+ export declare function isObjectExpression(node: unknown): node is ObjectExpression;
8
+ export declare function isCallExpression(node: unknown): node is CallExpression;
9
+ export declare function isCallExpressionNamed(node: unknown, calleeName: string): node is CallExpression;
10
+ export declare function isFunctionDeclaration(node: unknown): node is FunctionDeclaration;
11
+ export declare function isFunctionExpression(node: unknown): node is FunctionExpression;
12
+ export declare function isArrowFunctionExpression(node: unknown): node is ArrowFunctionExpression;
@@ -0,0 +1,11 @@
1
+ declare const brand: unique symbol;
2
+ export type Brand<TBase extends string, TBrand extends string> = TBase & {
3
+ readonly [brand]: TBrand;
4
+ };
5
+ export type SvelteEntryPoint = Brand<string, "SvelteEntryPoint">;
6
+ export declare function asSvelteEntryPoint(path: string): SvelteEntryPoint;
7
+ export type NormalizedPath = Brand<string, "NormalizedPath">;
8
+ export declare function asNormalizedPath(path: string): NormalizedPath;
9
+ export type RelativeSourcePath = Brand<string, "RelativeSourcePath">;
10
+ export declare function asRelativeSourcePath(path: string): RelativeSourcePath;
11
+ export {};
package/lib/cli.d.ts CHANGED
@@ -1,10 +1,5 @@
1
1
  /**
2
- * Command-line interface for sveld.
3
- *
4
- * Parses command-line arguments, runs Rollup to process the entry point,
5
- * generates component documentation, and writes output files.
6
- *
7
- * @param process - Node.js process object containing command-line arguments
2
+ * CLI entry point: parse flags, run Rollup, generate docs, write outputs.
8
3
  *
9
4
  * @example
10
5
  * ```ts
@@ -1,13 +1,6 @@
1
1
  import type { ParsedExports } from "./parse-exports";
2
2
  /**
3
- * Creates export statements from parsed export information.
4
- *
5
- * Groups exports by source file and generates optimized export statements.
6
- * Handles special cases like Svelte component exports, default exports,
7
- * and mixed exports from module context.
8
- *
9
- * @param parsed_exports - Map of export names to their source and metadata
10
- * @returns A string containing all export statements
3
+ * Builds export statements from parsed export metadata.
11
4
  *
12
5
  * @example
13
6
  * ```ts
@@ -20,10 +13,7 @@ import type { ParsedExports } from "./parse-exports";
20
13
  */
21
14
  export declare function createExports(parsed_exports: ParsedExports): string;
22
15
  /**
23
- * Removes the `.svelte` extension from a file path.
24
- *
25
- * @param filePath - The file path to process
26
- * @returns The path without the .svelte extension
16
+ * Strips the `.svelte` extension from a path.
27
17
  *
28
18
  * @example
29
19
  * ```ts
@@ -32,10 +22,7 @@ export declare function createExports(parsed_exports: ParsedExports): string;
32
22
  */
33
23
  export declare function removeSvelteExt(filePath: string): string;
34
24
  /**
35
- * Converts a `.svelte` file path to a `.svelte.d.ts` TypeScript definition path.
36
- *
37
- * @param filePath - The Svelte file path to convert
38
- * @returns The path with .svelte.d.ts extension
25
+ * Maps a `.svelte` path to its `.svelte.d.ts` definition path.
39
26
  *
40
27
  * @example
41
28
  * ```ts
@@ -1,9 +1,5 @@
1
1
  /**
2
- * Element tag map adapted from TypeScript's `lib.dom.d.ts`.
3
- *
4
- * Maps HTML element tag names to their corresponding TypeScript element types.
5
- * Used for generating proper TypeScript types for element bindings and rest props.
6
- * See the TypeScript lib.dom.d.ts for the original source.
2
+ * Tag-to-DOM-type map from TypeScript `lib.dom.d.ts`. Unknown tags fall back to `HTMLElement`.
7
3
  *
8
4
  * @example
9
5
  * ```ts
@@ -135,14 +131,9 @@ declare const tag_map: {
135
131
  wbr: string;
136
132
  };
137
133
  type ElementTag = keyof typeof tag_map;
134
+ export declare function isElementTag(element: string): element is ElementTag;
138
135
  /**
139
- * Gets the TypeScript element type for a given HTML tag name.
140
- *
141
- * Returns the specific element type (e.g., `HTMLButtonElement`) if the tag
142
- * is in the map, otherwise returns the generic `HTMLElement` type.
143
- *
144
- * @param element - The HTML tag name (e.g., "button", "div", "input")
145
- * @returns The corresponding TypeScript element type name
136
+ * Returns the DOM interface for a tag, or `HTMLElement` when unknown.
146
137
  *
147
138
  * @example
148
139
  * ```ts
@@ -151,5 +142,5 @@ type ElementTag = keyof typeof tag_map;
151
142
  * getElementByTag("custom") // Returns: "HTMLElement" (fallback)
152
143
  * ```
153
144
  */
154
- export declare function getElementByTag(element: ElementTag | string): string;
145
+ export declare function getElementByTag(element: string): string;
155
146
  export {};
@@ -1,6 +1,4 @@
1
- export type SvelteEntryPoint = string;
2
- /**
3
- * Get the file path entry point for uncompiled Svelte source code
4
- * Expects a "svelte" field in the consumer's `package.json`
5
- */
6
- export declare function getSvelteEntry(entryPoint?: SvelteEntryPoint): SvelteEntryPoint | null;
1
+ import { type SvelteEntryPoint } from "./brands";
2
+ export type { SvelteEntryPoint };
3
+ /** Resolve the Svelte entry from `entryPoint` or `package.json#svelte`. */
4
+ export declare function getSvelteEntry(entryPoint?: string): SvelteEntryPoint | null;
package/lib/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { default as ComponentParser } from "./ComponentParser";
1
+ export { default as ComponentParser, type SerializedComponentEvent } from "./ComponentParser";
2
2
  export { cli } from "./cli";
3
+ export type { SvelteEntryPoint } from "./get-svelte-entry";
3
4
  export { default } from "./plugin";
4
5
  export { sveld } from "./sveld";