sveld 0.35.1 → 0.36.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.
Files changed (74) hide show
  1. package/README.md +33 -10
  2. package/cli.js +1 -1
  3. package/lib/browser.d.ts +660 -39
  4. package/lib/browser.js +205 -227
  5. package/lib/chunk-0fqdg2xa.js +20 -0
  6. package/lib/chunk-2vwf7nck.js +1 -0
  7. package/lib/chunk-8myyzjb1.js +4 -0
  8. package/lib/chunk-as5hgj7e.js +62 -0
  9. package/lib/chunk-denfbw92.js +13 -0
  10. package/lib/chunk-h1r040pg.js +6 -0
  11. package/lib/chunk-m538qnhp.js +230 -0
  12. package/lib/chunk-r4rrncmd.js +1 -0
  13. package/lib/chunk-t9d5k3p7.js +2 -0
  14. package/lib/chunk-tmj7vnjr.js +10 -0
  15. package/lib/chunk-xryt7ad2.js +3 -0
  16. package/lib/cli-entry.js +1 -0
  17. package/lib/index.d.ts +767 -10
  18. package/lib/index.js +1 -343
  19. package/package.json +1 -1
  20. package/lib/ComponentParser.d.ts +0 -822
  21. package/lib/ast-guards.d.ts +0 -18
  22. package/lib/brands.d.ts +0 -11
  23. package/lib/bundle.d.ts +0 -176
  24. package/lib/check.d.ts +0 -43
  25. package/lib/cli.d.ts +0 -28
  26. package/lib/create-exports.d.ts +0 -32
  27. package/lib/dependency-graph.d.ts +0 -20
  28. package/lib/diagnostics.d.ts +0 -34
  29. package/lib/element-tag-map.d.ts +0 -146
  30. package/lib/example-check.d.ts +0 -24
  31. package/lib/get-svelte-entry.d.ts +0 -4
  32. package/lib/load-config.d.ts +0 -62
  33. package/lib/parse-cache.d.ts +0 -28
  34. package/lib/parse-entry-exports.d.ts +0 -30
  35. package/lib/parse-exports.d.ts +0 -25
  36. package/lib/parser/bindings.d.ts +0 -6
  37. package/lib/parser/context.d.ts +0 -114
  38. package/lib/parser/contexts.d.ts +0 -19
  39. package/lib/parser/diagnostics.d.ts +0 -8
  40. package/lib/parser/events.d.ts +0 -23
  41. package/lib/parser/generics.d.ts +0 -24
  42. package/lib/parser/jsdoc.d.ts +0 -59
  43. package/lib/parser/props.d.ts +0 -73
  44. package/lib/parser/rest-props.d.ts +0 -6
  45. package/lib/parser/runes-detection.d.ts +0 -16
  46. package/lib/parser/runes-props.d.ts +0 -18
  47. package/lib/parser/scopes.d.ts +0 -58
  48. package/lib/parser/slots.d.ts +0 -29
  49. package/lib/parser/source-position.d.ts +0 -30
  50. package/lib/parser/type-resolution.d.ts +0 -32
  51. package/lib/parser/typescript-casts.d.ts +0 -2
  52. package/lib/path.d.ts +0 -3
  53. package/lib/plugin.d.ts +0 -85
  54. package/lib/resolve-alias.d.ts +0 -30
  55. package/lib/resolve-types.d.ts +0 -52
  56. package/lib/sveld.d.ts +0 -29
  57. package/lib/validate.d.ts +0 -13
  58. package/lib/watch.d.ts +0 -34
  59. package/lib/writer/MarkdownWriterBase.d.ts +0 -26
  60. package/lib/writer/Writer.d.ts +0 -53
  61. package/lib/writer/WriterMarkdown.d.ts +0 -33
  62. package/lib/writer/built-in-writers.d.ts +0 -1
  63. package/lib/writer/document-model.d.ts +0 -34
  64. package/lib/writer/format-generated-ts.d.ts +0 -8
  65. package/lib/writer/markdown-format-utils.d.ts +0 -104
  66. package/lib/writer/markdown-render-utils.d.ts +0 -10
  67. package/lib/writer/registry.d.ts +0 -17
  68. package/lib/writer/writer-custom-elements-core.d.ts +0 -86
  69. package/lib/writer/writer-custom-elements.d.ts +0 -15
  70. package/lib/writer/writer-json.d.ts +0 -37
  71. package/lib/writer/writer-markdown-core.d.ts +0 -32
  72. package/lib/writer/writer-markdown.d.ts +0 -25
  73. package/lib/writer/writer-ts-definitions-core.d.ts +0 -38
  74. package/lib/writer/writer-ts-definitions.d.ts +0 -70
@@ -1,822 +0,0 @@
1
- import type { Property } from "estree";
2
- import type { Node } from "estree-walker";
3
- import type { SveldDiagnostic } from "./diagnostics";
4
- /** Returns `value`, or `undefined` when it's `undefined` or the empty string. */
5
- export declare function assignValue(value?: "" | string): string | undefined;
6
- /** Structured JSDoc tag (e.g. `{ name: "since", body: "1.2.0" }`). */
7
- export interface JsDocPassthroughTag {
8
- name: string;
9
- body: string;
10
- }
11
- /**
12
- * From `@deprecated` JSDoc: a message string, or `true` when the tag has no message.
13
- */
14
- export type DeprecatedValue = string | true;
15
- export interface LegacyAstRoot {
16
- module?: Node;
17
- html?: Node;
18
- instance?: Node;
19
- }
20
- export interface SourcePosition {
21
- /** 1-based source line number */
22
- line: number;
23
- /** 0-based source column number */
24
- column: number;
25
- }
26
- export interface SourceRange {
27
- start: SourcePosition;
28
- end: SourcePosition;
29
- }
30
- export interface RunesPropTypeMetadata {
31
- optional: boolean;
32
- source?: SourceRange;
33
- type: string;
34
- }
35
- export interface RunesPropsDeclarationMetadata {
36
- canonicalType?: string;
37
- props: Map<string, RunesPropTypeMetadata>;
38
- referencedImportedTypes: Set<string>;
39
- referencedLocalTypes: Set<string>;
40
- }
41
- export type ModernRunesTypeNode = {
42
- type?: string;
43
- id?: {
44
- name?: string;
45
- };
46
- start?: number;
47
- end?: number;
48
- body?: {
49
- body?: ModernRunesTypeMember[];
50
- };
51
- typeAnnotation?: ModernRunesTypeNode;
52
- typeName?: unknown;
53
- types?: ModernRunesTypeNode[];
54
- members?: ModernRunesTypeMember[];
55
- /** Declaration-side `<T, U = Default>` (interfaces/type aliases). */
56
- typeParameters?: {
57
- params?: Array<{
58
- name?: string;
59
- }>;
60
- };
61
- /** Reference-side `<string, number>` concrete arguments (`TSTypeReference`). */
62
- typeArguments?: {
63
- params?: ModernRunesTypeNode[];
64
- };
65
- };
66
- export type ModernRunesTypeMember = {
67
- type?: string;
68
- computed?: boolean;
69
- optional?: boolean;
70
- start?: number;
71
- end?: number;
72
- key?: Property["key"];
73
- typeAnnotation?: {
74
- start?: number;
75
- end?: number;
76
- typeAnnotation?: ModernRunesTypeNode;
77
- };
78
- };
79
- export interface TypeImportBinding {
80
- importedName?: string;
81
- localName: string;
82
- source: string;
83
- specifierType: "default" | "named" | "namespace";
84
- }
85
- export interface LocalTypeDeclaration {
86
- code: string;
87
- node: ModernRunesTypeNode;
88
- start: number;
89
- }
90
- export interface ParsedComponentTypeScriptMetadata {
91
- canonicalPropsType?: string;
92
- canonicalPropNames: string[];
93
- localTypeDeclarations: string[];
94
- typeImportStatements: string[];
95
- /**
96
- * Whether `canonicalPropsType` mentions one of the component's own
97
- * `<script generics="...">` parameters (e.g. `Props<T>`). The semantic
98
- * resolver has no binding for `T`, so `resolveTypes` must leave this
99
- * component's props as their AST-derived text rather than expand them.
100
- */
101
- referencesComponentGenerics?: boolean;
102
- }
103
- export declare const PARSED_COMPONENT_TYPE_SCRIPT_METADATA: unique symbol;
104
- export declare function getParsedComponentTypeScriptMetadata(component: {
105
- [PARSED_COMPONENT_TYPE_SCRIPT_METADATA]?: ParsedComponentTypeScriptMetadata;
106
- }): ParsedComponentTypeScriptMetadata | undefined;
107
- /** One prop returned by the TypeScript checker during `resolveTypes`. */
108
- export interface ResolvedComponentProp {
109
- name: string;
110
- type: string;
111
- isRequired: boolean;
112
- description?: string;
113
- }
114
- /** Append checker-resolved props. Names the AST walker already found are left alone. */
115
- export declare function applyResolvedProps(component: ParsedComponent, resolved: ResolvedComponentProp[]): void;
116
- export type SyntaxMode = "legacy" | "runes";
117
- export type ScriptLanguage = "js" | "ts";
118
- export type ScopeBindingKind = "prop" | "local";
119
- export type ScopeBinding = {
120
- kind: ScopeBindingKind;
121
- publicPropName?: string;
122
- };
123
- export type LexicalScope = Map<string, ScopeBinding>;
124
- export type ComponentPropTypeSource = "typescript" | "jsdoc" | "default" | "inferred" | "unknown";
125
- export type ComponentPropDefaultValueKind = "literal" | "array" | "object" | "expression" | "function" | "unknown";
126
- export interface ComponentPropDefaultValue {
127
- raw: string;
128
- kind: ComponentPropDefaultValueKind;
129
- value?: unknown;
130
- }
131
- export interface ProcessedInitializer {
132
- value?: string;
133
- type?: string;
134
- isFunction: boolean;
135
- defaultValue?: ComponentPropDefaultValue;
136
- /** JSDoc from identifier default when the prop has none. */
137
- resolvedType?: string;
138
- resolvedDescription?: string;
139
- resolvedParams?: ComponentPropParam[];
140
- resolvedReturnType?: string;
141
- }
142
- type ModernScriptAttribute = {
143
- name?: string;
144
- value?: Array<{
145
- data?: string;
146
- raw?: string;
147
- }> | boolean;
148
- start?: number;
149
- end?: number;
150
- };
151
- export type ModernScriptNode = {
152
- attributes?: ModernScriptAttribute[];
153
- };
154
- /**
155
- * Diagnostic information for component parsing.
156
- *
157
- * Used to provide context about the component being parsed for error
158
- * messages and logging.
159
- */
160
- interface ComponentParserDiagnostics {
161
- /** The module/component name (e.g., "Button", "App") */
162
- moduleName: string;
163
- /** The file path to the component (e.g., "./Button.svelte") */
164
- filePath: string;
165
- }
166
- export type ComponentPropBinding = "readonly" | "writable";
167
- /**
168
- * Parameter information for function props.
169
- *
170
- * Extracted from JSDoc `@param` tags to provide detailed function signatures.
171
- */
172
- export interface ComponentPropParam {
173
- /** The parameter name */
174
- name: string;
175
- /** The parameter type (e.g., "string", "number", "CustomType") */
176
- type: string;
177
- /** Optional description from JSDoc */
178
- description?: string;
179
- /** Whether the parameter is optional */
180
- optional?: boolean;
181
- }
182
- /**
183
- * Component prop definition extracted from Svelte component.
184
- *
185
- * Represents a single prop that can be passed to the component, including
186
- * its type, default value, description, and metadata about whether it's
187
- * required, reactive, or a function.
188
- */
189
- export interface ComponentProp {
190
- /** The prop name as declared in the component */
191
- name: string;
192
- /** The declaration kind: "let" (required), "const" (optional with default), or "function" */
193
- kind: "let" | "const" | "function";
194
- /** Whether this prop is declared as `const` (has a default value) */
195
- constant: boolean;
196
- /** The TypeScript type of the prop (e.g., "string", "number | string") */
197
- type?: string;
198
- /** Conservative provenance for the prop type */
199
- typeSource?: ComponentPropTypeSource;
200
- /** Local variable name, emitted when it differs from the public prop name */
201
- localName?: string;
202
- /** The default value as a string representation of the source code */
203
- value?: string;
204
- /** Structured default value metadata for docs UIs */
205
- defaultValue?: ComponentPropDefaultValue;
206
- /** Description extracted from JSDoc comments */
207
- description?: string;
208
- /** Function parameters (for function props) extracted from `@param` tags */
209
- params?: ComponentPropParam[];
210
- /** Return type (for function props) extracted from `@returns` tag */
211
- returnType?: string;
212
- /** Whether this prop is a function (arrow function or function expression) */
213
- isFunction: boolean;
214
- /** Whether this prop is a function declaration (not an expression) */
215
- isFunctionDeclaration: boolean;
216
- /** Whether this prop is required (no default value, declared with `let`) */
217
- isRequired: boolean;
218
- /** Whether this prop is reactive (can change and trigger reactivity) */
219
- reactive: boolean;
220
- /** Explicit author-documented binding direction from `@bindable` JSDoc */
221
- binding?: ComponentPropBinding;
222
- /** True when the prop is explicitly declared with Svelte 5 `$bindable()` */
223
- bindable?: true;
224
- /** From `@deprecated` JSDoc. */
225
- deprecated?: DeprecatedValue;
226
- /** Structured `@since` / `@example` tags, in source order. */
227
- tags?: JsDocPassthroughTag[];
228
- /** Source range for the prop declaration, when available */
229
- source?: SourceRange;
230
- }
231
- /**
232
- * Component slot definition.
233
- *
234
- * Represents a slot that can be used to pass content into the component.
235
- * Includes information about slot props, fallback content, and descriptions.
236
- */
237
- export interface ComponentSlot {
238
- /** The slot name (null or undefined for default slot) */
239
- name?: string | null;
240
- /** Whether this is the default slot */
241
- default: boolean;
242
- /** Fallback content to display when slot is not provided */
243
- fallback?: string;
244
- /** TypeScript type definition for slot props (e.g., "{ title: string }") */
245
- slot_props?: string;
246
- /** Description extracted from JSDoc `@slot` or `@snippet` tags */
247
- description?: string;
248
- /** From `@deprecated` JSDoc. */
249
- deprecated?: DeprecatedValue;
250
- /**
251
- * JSDoc tags that appeared after the prose description and before `@slot` / `@snippet`
252
- * (e.g. `@example`), in source order.
253
- */
254
- tags?: JsDocPassthroughTag[];
255
- /** Source range for the slot/snippet declaration or documentation tag, when available */
256
- source?: SourceRange;
257
- }
258
- /**
259
- * Slot prop value definition.
260
- *
261
- * Used internally to track slot prop types and whether they should be
262
- * replaced with prop type references.
263
- */
264
- export interface SlotPropValue {
265
- /** The prop type value or reference */
266
- value?: string;
267
- /** Whether this value should be replaced with a prop type reference */
268
- replace: boolean;
269
- }
270
- export type SlotProps = Record<string, SlotPropValue>;
271
- /**
272
- * Internal representation of {@link ComponentSlot} used while parsing.
273
- *
274
- * `slot_props` is either raw TS type text (from a JSDoc `@slot`/`@snippet` tag,
275
- * used as-is) or a structured {@link SlotProps} map (from template parsing,
276
- * formatted into TS type text once at the end of the parse). Keeping it
277
- * structured until then avoids a JSON.stringify/JSON.parse round-trip per slot.
278
- */
279
- export type InternalComponentSlot = Omit<ComponentSlot, "slot_props"> & {
280
- slot_props?: string | SlotProps;
281
- };
282
- /**
283
- * Event that is forwarded from a child component or element.
284
- *
285
- * Forwarded events are those that use `on:eventname` syntax without
286
- * a handler, passing the event through to the parent.
287
- */
288
- export interface ForwardedEvent {
289
- /** Always "forwarded" for forwarded events */
290
- type: "forwarded";
291
- /** The event name (e.g., "click", "change") */
292
- name: string;
293
- /** The element or component that forwards this event */
294
- element: ComponentInlineElement | ComponentElement;
295
- /** Description extracted from JSDoc `@event` tags */
296
- description?: string;
297
- /** From `@deprecated` JSDoc. */
298
- deprecated?: DeprecatedValue;
299
- /** The detail type if explicitly specified in `@event` tag */
300
- detail?: string;
301
- /** Structured `@since` / `@example` tags, in source order. */
302
- tags?: JsDocPassthroughTag[];
303
- /** Source range for the forwarded event declaration or documentation tag, when available */
304
- source?: SourceRange;
305
- }
306
- /**
307
- * Event that is dispatched by the component.
308
- *
309
- * Dispatched events are those created with `createEventDispatcher()` and
310
- * dispatched via `dispatch("eventname", detail)`, or dispatched from a custom
311
- * element via `$host().dispatchEvent(new CustomEvent("eventname", { detail }))`.
312
- */
313
- export interface DispatchedEvent {
314
- /** Always "dispatched" for dispatched events */
315
- type: "dispatched";
316
- /** The event name (e.g., "click", "change") */
317
- name: string;
318
- /** The detail type (e.g., "{ value: string }", "null", "CustomEvent<...>") */
319
- detail?: string;
320
- /** Description extracted from JSDoc `@event` tags */
321
- description?: string;
322
- /** From `@deprecated` JSDoc. */
323
- deprecated?: DeprecatedValue;
324
- /** Structured `@since` / `@example` tags, in source order. */
325
- tags?: JsDocPassthroughTag[];
326
- /** Source range for the dispatched event call or documentation tag, when available */
327
- source?: SourceRange;
328
- }
329
- export type ComponentEvent = ForwardedEvent | DispatchedEvent;
330
- /**
331
- * Serialized version of {@link ForwardedEvent} for JSON output.
332
- *
333
- * This interface maintains backward compatibility by serializing the `element`
334
- * property as a string instead of an object. The element name is extracted
335
- * from the {@link ComponentInlineElement} or {@link ComponentElement} object.
336
- *
337
- * @example
338
- * ```ts
339
- * // ForwardedEvent with element object:
340
- * { type: "forwarded", name: "click", element: { type: "Element", name: "button" } }
341
- *
342
- * // SerializedForwardedEvent for JSON:
343
- * { type: "forwarded", name: "click", element: "button" }
344
- * ```
345
- */
346
- export interface SerializedForwardedEvent {
347
- /** Always "forwarded" for forwarded events */
348
- type: "forwarded";
349
- /** The event name (e.g., "click", "change") */
350
- name: string;
351
- /**
352
- * Serialized as string for JSON backward compatibility.
353
- * In the internal API, this is an object, but for JSON output it's a string.
354
- */
355
- element: string;
356
- /** Description extracted from JSDoc `@event` tags */
357
- description?: string;
358
- /** From `@deprecated` JSDoc. */
359
- deprecated?: DeprecatedValue;
360
- /** The detail type if explicitly specified in `@event` tag */
361
- detail?: string;
362
- /** Structured `@since` / `@example` tags, in source order. */
363
- tags?: JsDocPassthroughTag[];
364
- /** Source range for the forwarded event declaration or documentation tag, when available */
365
- source?: SourceRange;
366
- }
367
- export type SerializedComponentEvent = SerializedForwardedEvent | DispatchedEvent;
368
- /**
369
- * Type definition extracted from JSDoc `@typedef` tags.
370
- *
371
- * Represents custom types defined in component comments that can be
372
- * referenced by props, events, and other type annotations.
373
- */
374
- export interface TypeDef {
375
- /** The type string representation (e.g., "{ x: number; y: number }") */
376
- type: string;
377
- /** The type name (e.g., "Point", "User") */
378
- name: string;
379
- /** Description extracted from JSDoc comments */
380
- description?: string;
381
- /** The full TypeScript type definition string (e.g., "type Point = { x: number; y: number }") */
382
- ts: string;
383
- }
384
- export type ComponentGenerics = [name: string, type: string] | null;
385
- /**
386
- * Represents an inline Svelte component element.
387
- *
388
- * Used to identify which component forwards an event or accepts rest props.
389
- */
390
- export interface ComponentInlineElement {
391
- /** Always "InlineComponent" for component elements */
392
- type: "InlineComponent";
393
- /** The component name (e.g., "Button", "Modal") */
394
- name: string;
395
- }
396
- export interface ComponentElement {
397
- type: "Element";
398
- name: string;
399
- /**
400
- * For `svelte:element`, stores the hardcoded element tag if `this` is a literal string.
401
- *
402
- * When `svelte:element` is used with a static tag (e.g., `svelte:element this="div"`),
403
- * this property contains the tag name. If the tag is dynamic, this property is undefined.
404
- *
405
- * @example
406
- * ```svelte
407
- * <!-- Static tag -->
408
- * <svelte:element this="div" bind:this={elementRef} />
409
- * // thisValue: "div"
410
- *
411
- * <!-- Dynamic tag -->
412
- * <svelte:element this={tagName} bind:this={elementRef} />
413
- * // thisValue: undefined
414
- * ```
415
- */
416
- thisValue?: string;
417
- /** Inline or block description from the `@restProps` JSDoc tag */
418
- description?: string;
419
- }
420
- export type RestProps = undefined | ComponentInlineElement | ComponentElement;
421
- /**
422
- * Interface extension information from JSDoc `@extends` tag.
423
- *
424
- * Allows components to extend external TypeScript interfaces for
425
- * better type safety and code reuse.
426
- */
427
- export interface Extends {
428
- /** The interface name to extend (e.g., "ButtonProps") */
429
- interface: string;
430
- /** The import path for the interface (e.g., "./types" or "carbon-components-svelte") */
431
- import: string;
432
- }
433
- export interface ComponentPropBindings {
434
- elements: string[];
435
- }
436
- /**
437
- * Property definition for a component context.
438
- *
439
- * Represents a single property in a context object created with `setContext`.
440
- */
441
- export interface ComponentContextProp {
442
- /** The property name */
443
- name: string;
444
- /** The property type (inferred from JSDoc or variable types) */
445
- type: string;
446
- /** Description extracted from JSDoc comments on the variable */
447
- description?: string;
448
- /** Whether the property is optional */
449
- optional: boolean;
450
- }
451
- /**
452
- * Component context definition.
453
- *
454
- * Represents a context created with `setContext(key, value)` that can be
455
- * accessed by child components via `getContext(key)`.
456
- */
457
- export interface ComponentContext {
458
- /** The context key (e.g., "modal", "tabs") */
459
- key: string;
460
- /** The generated TypeScript type name (e.g., "ModalContext", "TabsContext") */
461
- typeName: string;
462
- /** Description extracted from JSDoc comments */
463
- description?: string;
464
- /** Properties available in this context */
465
- properties: ComponentContextProp[];
466
- }
467
- /**
468
- * Complete parsed component metadata.
469
- *
470
- * This is the main return type from {@link ComponentParser.parseSvelteComponent}.
471
- * Contains all extracted information about a Svelte component including props,
472
- * slots, events, types, and more.
473
- *
474
- * @example
475
- * ```ts
476
- * const parser = new ComponentParser();
477
- * const parsed = parser.parseSvelteComponent(source, {
478
- * moduleName: "Button",
479
- * filePath: "./Button.svelte"
480
- * });
481
- *
482
- * // Access component metadata:
483
- * parsed.props // Array of component props
484
- * parsed.slots // Array of component slots
485
- * parsed.events // Array of component events
486
- * parsed.typedefs // Array of custom type definitions
487
- * parsed.contexts // Array of context definitions
488
- * ```
489
- */
490
- export interface ParsedComponent {
491
- /** Source range for the component source that was parsed */
492
- source?: SourceRange;
493
- /** Whether the component uses legacy or runes syntax according to compiler metadata */
494
- syntaxMode: SyntaxMode;
495
- /** Language used by the instance or module script when it can be determined */
496
- scriptLanguage?: ScriptLanguage;
497
- /** Component props that can be passed to the component */
498
- props: ComponentProp[];
499
- /** Exports from `<script context="module">` block */
500
- moduleExports: ComponentProp[];
501
- /** Slots available in the component template */
502
- slots: ComponentSlot[];
503
- /** Events that the component can dispatch or forward (serialized for JSON/API output) */
504
- events: SerializedComponentEvent[];
505
- /** Custom type definitions from JSDoc `@typedef` tags */
506
- typedefs: TypeDef[];
507
- /** Generic type parameters (e.g., `[name: "T", type: "string"]`) or null */
508
- generics: null | ComponentGenerics;
509
- /** Rest props configuration (which elements/components accept rest props) */
510
- rest_props: RestProps;
511
- /** Interface extension from JSDoc `@extends` tag */
512
- extends?: Extends;
513
- /** Component-level description from `@component` HTML comment */
514
- componentComment?: string;
515
- /** Source range for the `@component` HTML comment, when available */
516
- componentCommentSource?: SourceRange;
517
- /** Contexts created with `setContext` in the component */
518
- contexts?: ComponentContext[];
519
- /** Custom-element tag name from `<svelte:options customElement="x-foo" />` (or the object form's `tag`), when present */
520
- customElementTag?: string;
521
- /**
522
- * Type guesses from this parse (unknown props, `any` contexts, orphan `@event` tags).
523
- * Set on every {@link ComponentParser.parseSvelteComponent} call.
524
- */
525
- diagnostics?: SveldDiagnostic[];
526
- /** Internal writer-only TypeScript metadata. Not serialized to JSON. */
527
- [PARSED_COMPONENT_TYPE_SCRIPT_METADATA]?: ParsedComponentTypeScriptMetadata;
528
- }
529
- export default class ComponentParser {
530
- /**
531
- * All per-parse mutable state (props, slots, events, scopes, source, etc.).
532
- * See {@link ParserContext} for field-by-field documentation. Replaced
533
- * wholesale by `cleanup()` between parses.
534
- */
535
- private ctx;
536
- private static mapToArray;
537
- private static getStaticAttributeValue;
538
- resolveScriptLanguage(parsed: {
539
- instance?: ModernScriptNode;
540
- module?: ModernScriptNode;
541
- }): ScriptLanguage | undefined;
542
- /**
543
- * Reads the `generics` attribute off the instance script (Svelte only allows
544
- * it there, and only alongside `lang="ts"`). Returns the raw value for later
545
- * precedence resolution against `@generics`/`@template` JSDoc tags, or
546
- * `undefined` if absent. Records a `syntax-skipped` diagnostic and returns
547
- * `undefined` if the attribute is present without `lang="ts"`, since sveld
548
- * can't safely guess how to parse it as plain JavaScript.
549
- */
550
- resolveScriptGenericsAttribute(parsed: {
551
- instance?: ModernScriptNode;
552
- }): {
553
- value: string;
554
- source?: SourceRange;
555
- } | undefined;
556
- private static assignValue;
557
- private resolvePublicPropName;
558
- trackPropLocalName(propName: string, localName?: string): void;
559
- private getPropByLocalOrPublic;
560
- getPropTypeByLocalOrPublic(name: string): string | undefined;
561
- getExplicitPropType(name: string): string | undefined;
562
- getPropertyName(node: Property["key"]): string | undefined;
563
- /**
564
- * Checks if a MemberExpression represents a well-known numeric constant.
565
- *
566
- * Identifies constants from the Number and Math objects that should be
567
- * typed as `number` rather than their literal values.
568
- *
569
- * @param memberExpr - The AST node to check
570
- * @returns True if the expression is a recognized numeric constant
571
- *
572
- * @example
573
- * ```ts
574
- * // Recognized constants:
575
- * Number.POSITIVE_INFINITY // true
576
- * Number.MAX_VALUE // true
577
- * Math.PI // true
578
- * Math.E // true
579
- *
580
- * // Not recognized:
581
- * Custom.CONSTANT // false
582
- * Number.UNKNOWN // false
583
- * ```
584
- */
585
- isNumericConstant(memberExpr: unknown): boolean;
586
- resolveTypeSource({ hasTypeScriptType, hasJSDocType, inferredType, finalType, }: {
587
- hasTypeScriptType?: boolean;
588
- hasJSDocType?: boolean;
589
- inferredType?: string;
590
- finalType?: string;
591
- }): ComponentPropTypeSource;
592
- /**
593
- * Look up JSDoc on a local variable declaration by name.
594
- */
595
- resolveLocalVarJSDoc(name: string): {
596
- type?: string;
597
- params?: ComponentPropParam[];
598
- returnType?: string;
599
- description?: string;
600
- binding?: ComponentPropBinding;
601
- deprecated?: DeprecatedValue;
602
- tags?: JsDocPassthroughTag[];
603
- } | undefined;
604
- /**
605
- * Adds or merges a module export to the moduleExports map.
606
- *
607
- * Similar to {@link addProp}, but for exported values from the module script.
608
- * If an export with the same name already exists, the new data is merged
609
- * with the existing export.
610
- *
611
- * @param prop_name - The name of the exported value
612
- * @param data - The export data to add or merge
613
- *
614
- * @example
615
- * ```ts
616
- * // For: export const API_URL = "https://api.example.com"
617
- * addModuleExport("API_URL", {
618
- * name: "API_URL",
619
- * kind: "const",
620
- * type: "string",
621
- * value: '"https://api.example.com"'
622
- * })
623
- * ```
624
- */
625
- private addModuleExport;
626
- /**
627
- * Normalizes type strings by aliasing common patterns.
628
- *
629
- * Converts `*` to `any` (common JSDoc wildcard) and trims whitespace
630
- * from type annotations.
631
- *
632
- * @param type - The type string to normalize
633
- * @returns The normalized type string
634
- *
635
- * @example
636
- * ```ts
637
- * aliasType("*") // Returns: "any"
638
- * aliasType(" string ") // Returns: "string"
639
- * aliasType("number") // Returns: "number"
640
- * ```
641
- */
642
- aliasType(type: string): string;
643
- /**
644
- * Builds a cache of variable type information from JSDoc comments.
645
- *
646
- * Scans the source code for variable declarations and extracts type information
647
- * from preceding JSDoc comments. This cache is used to infer types for context
648
- * properties and other variable references.
649
- *
650
- * @example
651
- * ```ts
652
- * // Source code:
653
- * /**
654
- * * @type {string}
655
- * * The user's name
656
- * *\/
657
- * const userName = "John";
658
- *
659
- * // Cache entry:
660
- * // { "userName": { type: "string", description: "The user's name" } }
661
- * ```
662
- */
663
- private buildVariableInfoCache;
664
- /**
665
- * Cache for compiled regex patterns for variable name matching.
666
- *
667
- * Stores three regex patterns (const, let, function) per variable name
668
- * to avoid recreating them on each lookup. Improves performance when
669
- * searching for the same variable multiple times.
670
- */
671
- private static readonly VAR_NAME_REGEX_CACHE;
672
- /**
673
- * Gets or creates cached regex patterns for matching variable declarations.
674
- *
675
- * Creates three regex patterns for matching const, let, and function declarations
676
- * of a specific variable name. The patterns are cached to avoid recreating them
677
- * for the same variable name.
678
- *
679
- * @param varName - The variable name to create regex patterns for
680
- * @returns A tuple of three RegExp objects for const, let, and function patterns
681
- *
682
- * @example
683
- * ```ts
684
- * getVarNameRegexes("count")
685
- * // Returns:
686
- * // [
687
- * // /\bconst\s+count\s*=/,
688
- * // /\blet\s+count\s*=/,
689
- * // /\bfunction\s+count\s*\(/
690
- * // ]
691
- * ```
692
- */
693
- private static getVarNameRegexes;
694
- /**
695
- * Finds the type and description for a variable by searching for its JSDoc comment.
696
- *
697
- * First checks the cache built by {@link buildVariableInfoCache}. If not found,
698
- * searches the source code directly for the variable declaration and its
699
- * preceding JSDoc comment.
700
- *
701
- * @param varName - The variable name to look up
702
- * @returns The type and description if found, null otherwise
703
- *
704
- * @example
705
- * ```ts
706
- * // Source:
707
- * /**
708
- * * @type {number}
709
- * * The count value
710
- * *\/
711
- * const count = 0;
712
- *
713
- * findVariableTypeAndDescription("count")
714
- * // Returns: { type: "number", description: "The count value" }
715
- * ```
716
- */
717
- findVariableTypeAndDescription(varName: string): {
718
- type: string;
719
- description?: string;
720
- } | null;
721
- /**
722
- * Cleans up all parser state, resetting the instance for reuse.
723
- *
724
- * Clears all maps, caches, and resets all state variables to their initial
725
- * values. Should be called before parsing a new component or when the
726
- * parser instance needs to be reset.
727
- *
728
- * @example
729
- * ```ts
730
- * parser.parseSvelteComponent(source1, diagnostics1);
731
- * parser.cleanup(); // Reset state
732
- * parser.parseSvelteComponent(source2, diagnostics2); // Fresh parse
733
- * ```
734
- */
735
- accumulateGeneric(name: string, constraint: string): void;
736
- cleanup(): void;
737
- /**
738
- * Pre-compiled regex for matching script blocks in Svelte components.
739
- *
740
- * Matches `<script>` tags and their content, capturing the opening tag,
741
- * script content, and closing tag. Global and case-insensitive flags
742
- * allow matching multiple script blocks.
743
- *
744
- * @example
745
- * ```ts
746
- * // Matches:
747
- * // "<script>const x = 1;</script>"
748
- * // "<script lang='ts'>...</script>"
749
- * ```
750
- */
751
- private static readonly SCRIPT_BLOCK_REGEX;
752
- /**
753
- * Pre-compiled regex for matching TypeScript directive comments.
754
- *
755
- * Matches TypeScript directive comments like ts-ignore, ts-expect-error,
756
- * etc. Used to remove these directives from script blocks before JSDoc parsing.
757
- *
758
- * @example
759
- * ```ts
760
- * // Matches:
761
- * // "// ts-ignore"
762
- * // "// ts-expect-error: reason"
763
- * // "// ts-nocheck"
764
- * ```
765
- */
766
- private static readonly TS_DIRECTIVE_REGEX;
767
- /**
768
- * Strips TypeScript directive comments from script blocks only.
769
- *
770
- * Removes TypeScript directive comments (e.g., ts-ignore, ts-expect-error directives)
771
- * from within `<script>` blocks to prevent them from interfering with JSDoc parsing.
772
- * Directives outside script blocks are left untouched.
773
- *
774
- * @param source - The Svelte component source code
775
- * @returns The source code with TypeScript directives removed from script blocks
776
- *
777
- * @example
778
- * ```ts
779
- * // Input (with TypeScript directive):
780
- * <script>
781
- * const x: string = 123; // directive removed
782
- * </script>
783
- *
784
- * // Output (directive stripped):
785
- * <script>
786
- * const x: string = 123;
787
- * </script>
788
- * ```
789
- */
790
- private static stripTypeScriptDirectivesFromScripts;
791
- /**
792
- * Parses a Svelte component and extracts all component metadata.
793
- *
794
- * This is the main entry point that orchestrates the entire parsing process:
795
- * 1. Cleans up previous state
796
- * 2. Strips TypeScript directives that might interfere with JSDoc
797
- * 3. Compiles the component to get the AST
798
- * 4. Collects reactive variables
799
- * 5. Builds variable type cache
800
- * 6. Parses custom types from JSDoc comments
801
- * 7. Walks the AST to extract props, slots, events, bindings, and contexts
802
- * 8. Post-processes events to distinguish dispatched vs forwarded
803
- * 9. Processes props with bindings and slots with prop references
804
- * 10. Returns the complete parsed component structure
805
- *
806
- * @param source - The Svelte component source code
807
- * @param diagnostics - Diagnostic information (module name and file path)
808
- * @returns A ParsedComponent object containing all extracted metadata
809
- *
810
- * @example
811
- * ```ts
812
- * const parser = new ComponentParser();
813
- * const result = parser.parseSvelteComponent(source, {
814
- * moduleName: "Button",
815
- * filePath: "./Button.svelte"
816
- * });
817
- * // Returns: { props: [...], slots: [...], events: [...], ... }
818
- * ```
819
- */
820
- parseSvelteComponent(source: string, diagnostics: ComponentParserDiagnostics): ParsedComponent;
821
- }
822
- export {};