sveld 0.30.0 → 0.30.1
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.
- package/README.md +83 -0
- package/lib/ComponentParser.d.ts +5 -0
- package/lib/index.js +36 -36
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -129,6 +129,7 @@ export default class Button extends SvelteComponentTyped<
|
|
|
129
129
|
- [Available Options](#available-options)
|
|
130
130
|
- [API Reference](#api-reference)
|
|
131
131
|
- [@type](#type)
|
|
132
|
+
- [@default](#default)
|
|
132
133
|
- [@typedef](#typedef)
|
|
133
134
|
- [@callback](#callback)
|
|
134
135
|
- [@slot / @snippet](#slot--snippet)
|
|
@@ -419,6 +420,88 @@ For runes components with multiple destructured props, place JSDoc on the indivi
|
|
|
419
420
|
</script>
|
|
420
421
|
```
|
|
421
422
|
|
|
423
|
+
### `@default`
|
|
424
|
+
|
|
425
|
+
By default, `sveld` infers the `@default` value from the prop's initializer and includes it in the generated TypeScript definitions:
|
|
426
|
+
|
|
427
|
+
```svelte
|
|
428
|
+
<script>
|
|
429
|
+
export let open = false;
|
|
430
|
+
</script>
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
```ts
|
|
434
|
+
/**
|
|
435
|
+
* @default false
|
|
436
|
+
*/
|
|
437
|
+
open?: boolean;
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
Use the `@default` tag to explicitly document the default value. When an explicit `@default` annotation is provided, `sveld` uses it instead of the inferred value, avoiding duplicate `@default` tags in the output.
|
|
441
|
+
|
|
442
|
+
This is useful when the initializer references a variable or expression that is not meaningful to consumers:
|
|
443
|
+
|
|
444
|
+
```svelte
|
|
445
|
+
<script>
|
|
446
|
+
const defaultFilter = () => true;
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* @default () => true
|
|
450
|
+
* @type {(item: string, value: string) => boolean}
|
|
451
|
+
*/
|
|
452
|
+
export let shouldFilter = defaultFilter;
|
|
453
|
+
</script>
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
```ts
|
|
457
|
+
/**
|
|
458
|
+
* @default () => true
|
|
459
|
+
*/
|
|
460
|
+
shouldFilter?: (item: string, value: string) => boolean;
|
|
461
|
+
```
|
|
462
|
+
|
|
463
|
+
#### Identifier resolution
|
|
464
|
+
|
|
465
|
+
When a prop's initializer is a variable reference, `sveld` resolves it to the actual value automatically:
|
|
466
|
+
|
|
467
|
+
```svelte
|
|
468
|
+
<script>
|
|
469
|
+
const DEFAULT_SIZE = "md";
|
|
470
|
+
|
|
471
|
+
/** @type {"sm" | "md" | "lg"} */
|
|
472
|
+
export let size = DEFAULT_SIZE;
|
|
473
|
+
</script>
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
```ts
|
|
477
|
+
/**
|
|
478
|
+
* @default "md"
|
|
479
|
+
*/
|
|
480
|
+
size?: "sm" | "md" | "lg";
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
Chained references are also resolved:
|
|
484
|
+
|
|
485
|
+
```svelte
|
|
486
|
+
<script>
|
|
487
|
+
const ACTUAL_VALUE = 42;
|
|
488
|
+
const ALIAS = ACTUAL_VALUE;
|
|
489
|
+
|
|
490
|
+
export let count = ALIAS;
|
|
491
|
+
</script>
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
```ts
|
|
495
|
+
/**
|
|
496
|
+
* @default 42
|
|
497
|
+
*/
|
|
498
|
+
count?: number;
|
|
499
|
+
```
|
|
500
|
+
|
|
501
|
+
Resolution follows up to 5 levels of indirection. Beyond that, the last resolved identifier name is used as the default value. If the identifier cannot be resolved (e.g., it is imported from another module), the variable name is used as-is.
|
|
502
|
+
|
|
503
|
+
When an explicit `@default` annotation is provided, it always takes precedence over the resolved value.
|
|
504
|
+
|
|
422
505
|
### `@typedef`
|
|
423
506
|
|
|
424
507
|
The `@typedef` tag can be used to define a common type that is used multiple times within a component. All typedefs defined in a component will be exported from the generated TypeScript definition file.
|
package/lib/ComponentParser.d.ts
CHANGED
|
@@ -520,6 +520,11 @@ export default class ComponentParser {
|
|
|
520
520
|
* ```
|
|
521
521
|
*/
|
|
522
522
|
private processInitializer;
|
|
523
|
+
/**
|
|
524
|
+
* Look up a local variable's initializer AST node by name.
|
|
525
|
+
* Returns the init node if found, or undefined.
|
|
526
|
+
*/
|
|
527
|
+
private resolveLocalVarInitializer;
|
|
523
528
|
/**
|
|
524
529
|
* Unwraps `$bindable(...)` calls so defaults are documented as their underlying values.
|
|
525
530
|
*/
|