svelte-origin 0.0.0 → 1.0.0-next.15
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/LLM.md +754 -0
- package/README.md +412 -1
- package/dist/aliases.d.ts +44 -0
- package/dist/cli.d.ts +19 -0
- package/dist/cli.js +3440 -0
- package/dist/generate-dts.d.ts +31 -0
- package/dist/globals.d.ts +576 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +6685 -0
- package/dist/plugin.d.ts +143 -0
- package/dist/plugin.js +3467 -0
- package/dist/post-process.d.ts +27 -0
- package/dist/post-process.js +3071 -0
- package/dist/preprocess.d.ts +73 -0
- package/dist/preprocess.js +3360 -0
- package/dist/runtime/attrs.d.ts +52 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.js +3665 -0
- package/dist/runtime/origin.d.ts +144 -0
- package/dist/runtime/types.d.ts +173 -0
- package/dist/transform/attrs-for-transform.d.ts +24 -0
- package/dist/transform/attrs-origin-transform.d.ts +62 -0
- package/dist/transform/attrs-schema.d.ts +52 -0
- package/dist/transform/codegen.d.ts +15 -0
- package/dist/transform/core.d.ts +62 -0
- package/dist/transform/declaration-parser.d.ts +77 -0
- package/dist/transform/element-types.d.ts +10 -0
- package/dist/transform/origin-transform.d.ts +51 -0
- package/dist/transform/patterns.d.ts +82 -0
- package/dist/transform/schema.d.ts +116 -0
- package/dist/transform/standalone-attrs-transform.d.ts +36 -0
- package/dist/vite-dts.d.ts +17 -0
- package/dist/vite-dts.js +606 -0
- package/package.json +73 -3
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate .svelte.d.ts declaration files for svelte-origin components
|
|
3
|
+
*
|
|
4
|
+
* This module scans directories for Svelte components that use $attrs.origin()
|
|
5
|
+
* and generates sidecar .svelte.d.ts files. These files enable svelte-check
|
|
6
|
+
* to properly type-check components, working around the limitation that
|
|
7
|
+
* svelte-check doesn't apply preprocessor transformations for type inference.
|
|
8
|
+
*/
|
|
9
|
+
export interface GenerateDtsOptions {
|
|
10
|
+
/** Directory to scan */
|
|
11
|
+
dir: string;
|
|
12
|
+
/** Don't write files, just report */
|
|
13
|
+
dryRun?: boolean;
|
|
14
|
+
/** Print verbose output */
|
|
15
|
+
verbose?: boolean;
|
|
16
|
+
/** Remove generated files instead of creating */
|
|
17
|
+
clean?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface GenerateDtsResult {
|
|
20
|
+
/** Number of files processed */
|
|
21
|
+
filesProcessed: number;
|
|
22
|
+
/** List of files that were generated/removed */
|
|
23
|
+
files: string[];
|
|
24
|
+
/** Any errors encountered */
|
|
25
|
+
errors: string[];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Generate .svelte.d.ts files for all components using $attrs.origin()
|
|
29
|
+
*/
|
|
30
|
+
export declare function generateDtsFiles(options: GenerateDtsOptions): Promise<GenerateDtsResult>;
|
|
31
|
+
export default generateDtsFiles;
|
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global type declarations for svelte-origin macros
|
|
3
|
+
*
|
|
4
|
+
* Add "svelte-origin/globals" to your tsconfig.json compilerOptions.types
|
|
5
|
+
* to enable type-checking for svelte-origin macros.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// Bindable Marker Type
|
|
10
|
+
// ============================================================================
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Marker type for bindable attrs.
|
|
14
|
+
*/
|
|
15
|
+
interface SvelteOriginBindable<T = unknown> {
|
|
16
|
+
readonly __bindable: true
|
|
17
|
+
readonly default: T
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ============================================================================
|
|
21
|
+
// Origin Definition & Factory Types
|
|
22
|
+
// ============================================================================
|
|
23
|
+
|
|
24
|
+
interface SvelteOriginDefinition {
|
|
25
|
+
attrs?: Record<string, any>
|
|
26
|
+
props?: Record<string, any>
|
|
27
|
+
[key: string]: any
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extract attrs from an origin definition.
|
|
32
|
+
* Supports both 'attrs' and 'props' property names.
|
|
33
|
+
* Strips away SvelteOriginAttrsFactory interface if present.
|
|
34
|
+
*/
|
|
35
|
+
type SvelteOriginExtractAttrs<T extends SvelteOriginDefinition> =
|
|
36
|
+
T['props'] extends Record<string, any>
|
|
37
|
+
? SvelteOriginStripFactoryProps<T['props']>
|
|
38
|
+
: T['attrs'] extends Record<string, any>
|
|
39
|
+
? SvelteOriginStripFactoryProps<T['attrs']>
|
|
40
|
+
: {}
|
|
41
|
+
|
|
42
|
+
interface SvelteOriginAttrSchemaEntry {
|
|
43
|
+
default: unknown
|
|
44
|
+
bindable: boolean
|
|
45
|
+
hasDefault: boolean
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface SvelteOriginAttrSchema {
|
|
49
|
+
[key: string]: SvelteOriginAttrSchemaEntry
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Origin factory type.
|
|
54
|
+
*
|
|
55
|
+
* TDef: The origin's own definition
|
|
56
|
+
* TAllAttrs: All attrs including inherited from parents (for component props)
|
|
57
|
+
*/
|
|
58
|
+
interface SvelteOriginFactory<
|
|
59
|
+
TDef extends SvelteOriginDefinition = SvelteOriginDefinition,
|
|
60
|
+
TAllAttrs extends Record<string, any> = TDef['attrs'] extends Record<string, any> ? TDef['attrs'] : {}
|
|
61
|
+
> {
|
|
62
|
+
(inputAttrs: SvelteOriginAllAttrsInput<TAllAttrs>): SvelteOriginInstance<TDef>
|
|
63
|
+
readonly __origin: true
|
|
64
|
+
readonly __attrSchema: SvelteOriginAttrSchema
|
|
65
|
+
readonly __parents: SvelteOriginFactory<any, any>[]
|
|
66
|
+
/** Type-level marker for all attrs (including inherited) */
|
|
67
|
+
readonly __allAttrs: TAllAttrs
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Filter out private keys (those starting with _) from an object type
|
|
72
|
+
*/
|
|
73
|
+
type SvelteOriginPublicKeys<T> = {
|
|
74
|
+
[K in keyof T as K extends `_${string}` ? never : K]: T[K]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Attachments object type - can be spread onto elements to attach behaviors.
|
|
79
|
+
* Uses createAttachmentKey() symbols as keys.
|
|
80
|
+
*/
|
|
81
|
+
type SvelteOriginAttachments = {
|
|
82
|
+
[key: symbol]: (element: Element) => void | (() => void)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Origin instance type - the public interface exposed to components.
|
|
87
|
+
* Properties prefixed with _ are treated as private and not accessible.
|
|
88
|
+
* Props/attrs are unwrapped (SvelteOriginBindable<T> → T).
|
|
89
|
+
*/
|
|
90
|
+
type SvelteOriginInstance<T extends SvelteOriginDefinition> = {
|
|
91
|
+
props: SvelteOriginReactiveAttrs<T['props']>
|
|
92
|
+
attrs: SvelteOriginReactiveAttrs<T['attrs']>
|
|
93
|
+
super?: any
|
|
94
|
+
/**
|
|
95
|
+
* Attachments object that can be spread onto elements.
|
|
96
|
+
* When spread, registers attachment behaviors defined in the origin.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* <div {...counter.$attachments}>...</div>
|
|
100
|
+
*/
|
|
101
|
+
readonly $attachments: SvelteOriginAttachments
|
|
102
|
+
} & SvelteOriginPublicKeys<Omit<T, 'attrs' | 'props'>>
|
|
103
|
+
|
|
104
|
+
/** Extract input type from attrs (unwrap bindables, make optional) */
|
|
105
|
+
type SvelteOriginAllAttrsInput<T> = T extends Record<string, any>
|
|
106
|
+
? { [K in keyof T]?: SvelteOriginUnwrapBindable<T[K]> }
|
|
107
|
+
: Record<string, never>
|
|
108
|
+
|
|
109
|
+
type SvelteOriginAttrsInput<T extends SvelteOriginDefinition> = T['attrs'] extends Record<string, any>
|
|
110
|
+
? { [K in keyof T['attrs']]?: SvelteOriginUnwrapBindable<T['attrs'][K]> }
|
|
111
|
+
: Record<string, never>
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Widen literal types to their base types.
|
|
115
|
+
* E.g. 0 -> number, 'hello' -> string, true -> boolean
|
|
116
|
+
*/
|
|
117
|
+
type SvelteOriginWiden<T> =
|
|
118
|
+
T extends number ? number :
|
|
119
|
+
T extends string ? string :
|
|
120
|
+
T extends boolean ? boolean :
|
|
121
|
+
T extends bigint ? bigint :
|
|
122
|
+
T
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Unwrap bindable types, handling both direct and intersection types.
|
|
126
|
+
*
|
|
127
|
+
* The key insight: `0 & SvelteOriginBindable<0>` DOES extend SvelteOriginBindable<infer U>
|
|
128
|
+
* because the intersection inherits from both sides.
|
|
129
|
+
*
|
|
130
|
+
* Cases:
|
|
131
|
+
* - SvelteOriginBindable<T> -> widen(T)
|
|
132
|
+
* - T & SvelteOriginBindable<T> -> widen(T) (intersection inherits from SvelteOriginBindable)
|
|
133
|
+
* - T (non-bindable) -> widen(T)
|
|
134
|
+
*/
|
|
135
|
+
type SvelteOriginUnwrapBindable<T> =
|
|
136
|
+
T extends SvelteOriginBindable<infer U>
|
|
137
|
+
? SvelteOriginWiden<U>
|
|
138
|
+
: SvelteOriginWiden<T>
|
|
139
|
+
|
|
140
|
+
type SvelteOriginReactiveAttrs<T> = T extends Record<string, any>
|
|
141
|
+
? { [K in keyof T]: SvelteOriginUnwrapBindable<T[K]> }
|
|
142
|
+
: Record<string, never>
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Transform a definition type to have unwrapped props for use as `this` inside the definition.
|
|
146
|
+
* This ensures methods can access `this.props.count` as `number` instead of `SvelteOriginBindable<number>`.
|
|
147
|
+
*/
|
|
148
|
+
type SvelteOriginDefinitionThis<T extends SvelteOriginDefinition> = {
|
|
149
|
+
[K in keyof T]: K extends 'props' | 'attrs'
|
|
150
|
+
? SvelteOriginReactiveAttrs<T[K]>
|
|
151
|
+
: T[K]
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The `this` context available inside an extended origin definition
|
|
156
|
+
*/
|
|
157
|
+
type SvelteOriginExtendedThis<
|
|
158
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
159
|
+
Child extends SvelteOriginDefinition
|
|
160
|
+
> = Child & {
|
|
161
|
+
super: Parents extends [SvelteOriginFactory<infer P, any>]
|
|
162
|
+
? SvelteOriginInstance<P>
|
|
163
|
+
: Parents extends [SvelteOriginFactory<infer P1, any>, ...infer _Rest]
|
|
164
|
+
? SvelteOriginInstance<P1>
|
|
165
|
+
: never
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Merge parent attrs with child attrs (child overrides parent)
|
|
170
|
+
*/
|
|
171
|
+
type SvelteOriginMergeAttrs<
|
|
172
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
173
|
+
ChildAttrs
|
|
174
|
+
> = SvelteOriginExtractParentAttrs<Parents> & SvelteOriginStripFactoryProps<ChildAttrs>
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Recursively extract all attrs from parent factories
|
|
178
|
+
*/
|
|
179
|
+
type SvelteOriginExtractParentAttrs<Parents extends SvelteOriginFactory<any, any>[]> =
|
|
180
|
+
Parents extends [SvelteOriginFactory<any, infer A>, ...infer Rest]
|
|
181
|
+
? Rest extends SvelteOriginFactory<any, any>[]
|
|
182
|
+
? SvelteOriginStripFactoryProps<A> & SvelteOriginExtractParentAttrs<Rest>
|
|
183
|
+
: SvelteOriginStripFactoryProps<A>
|
|
184
|
+
: {}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Extract the instance type from the first parent factory (for this.super)
|
|
188
|
+
*/
|
|
189
|
+
type SvelteOriginExtractParentInstance<Parents extends SvelteOriginFactory<any, any>[]> =
|
|
190
|
+
Parents extends [SvelteOriginFactory<infer P, any>, ...any[]]
|
|
191
|
+
? SvelteOriginInstance<P>
|
|
192
|
+
: never
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Merged origin definition (child + super access)
|
|
196
|
+
*/
|
|
197
|
+
type SvelteOriginMerged<
|
|
198
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
199
|
+
Child extends SvelteOriginDefinition
|
|
200
|
+
> = Child & {
|
|
201
|
+
super: Parents extends [SvelteOriginFactory<infer P, any>]
|
|
202
|
+
? SvelteOriginInstance<P>
|
|
203
|
+
: Parents extends [SvelteOriginFactory<infer P1, any>, ...infer _Rest]
|
|
204
|
+
? SvelteOriginInstance<P1>
|
|
205
|
+
: never
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// ============================================================================
|
|
209
|
+
// Standalone Attrs Factory Types
|
|
210
|
+
// ============================================================================
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Standalone attrs factory type.
|
|
214
|
+
* Created by $attrs({...}) outside of $origin.
|
|
215
|
+
*/
|
|
216
|
+
interface SvelteOriginAttrsFactory<TAttrs extends Record<string, any> = Record<string, any>> {
|
|
217
|
+
readonly __attrs: true
|
|
218
|
+
readonly __attrSchema: SvelteOriginAttrSchema
|
|
219
|
+
readonly __parents: SvelteOriginAttrsFactory<any>[]
|
|
220
|
+
/** Type-level marker for attrs */
|
|
221
|
+
readonly __allAttrs: TAttrs
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Strip AttrsFactory interface properties from a type.
|
|
226
|
+
* This is needed because $attrs({...}) returns SvelteOriginAttrsFactory<T> & T
|
|
227
|
+
* but we only want T when extracting attrs for component props.
|
|
228
|
+
*
|
|
229
|
+
* Handles:
|
|
230
|
+
* - Direct SvelteOriginAttrsFactory<A> → A
|
|
231
|
+
* - Intersection SvelteOriginAttrsFactory<A> & A → A (via extracting from __allAttrs)
|
|
232
|
+
* - Other types → pass through unchanged (identity, no Omit)
|
|
233
|
+
*/
|
|
234
|
+
type SvelteOriginStripFactoryProps<T> =
|
|
235
|
+
T extends { __allAttrs: infer A }
|
|
236
|
+
? A
|
|
237
|
+
: T extends SvelteOriginAttrsFactory<infer A>
|
|
238
|
+
? A
|
|
239
|
+
: T
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Extract attrs from an AttrsFactory
|
|
243
|
+
*/
|
|
244
|
+
type SvelteOriginExtractAttrsFromFactory<T> =
|
|
245
|
+
T extends SvelteOriginAttrsFactory<infer A> ? A : never
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Merge attrs from multiple parent factories
|
|
249
|
+
*/
|
|
250
|
+
type SvelteOriginMergeAttrFactories<Parents extends SvelteOriginAttrsFactory<any>[]> =
|
|
251
|
+
Parents extends [SvelteOriginAttrsFactory<infer A>, ...infer Rest]
|
|
252
|
+
? Rest extends SvelteOriginAttrsFactory<any>[]
|
|
253
|
+
? A & SvelteOriginMergeAttrFactories<Rest>
|
|
254
|
+
: A
|
|
255
|
+
: {}
|
|
256
|
+
|
|
257
|
+
// ============================================================================
|
|
258
|
+
// Origin Init Callback Types
|
|
259
|
+
// ============================================================================
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Cleanup function returned from init callback
|
|
263
|
+
*/
|
|
264
|
+
type SvelteOriginCleanupFn = () => void
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Init callback type - runs on component creation
|
|
268
|
+
* Uses the full definition type (not SvelteOriginInstance) to allow access to private members
|
|
269
|
+
*/
|
|
270
|
+
type SvelteOriginInitCallback<TDef extends SvelteOriginDefinition> = (
|
|
271
|
+
this: SvelteOriginFullInstance<TDef>
|
|
272
|
+
) => void | SvelteOriginCleanupFn
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Full origin instance type - includes ALL members (including private _ prefixed).
|
|
276
|
+
* Used for the init callback `this` context.
|
|
277
|
+
*/
|
|
278
|
+
type SvelteOriginFullInstance<T extends SvelteOriginDefinition> = {
|
|
279
|
+
props: SvelteOriginReactiveAttrs<T['props']>
|
|
280
|
+
attrs: SvelteOriginReactiveAttrs<T['attrs']>
|
|
281
|
+
super?: any
|
|
282
|
+
/**
|
|
283
|
+
* Attachments object that can be spread onto elements.
|
|
284
|
+
*/
|
|
285
|
+
readonly $attachments: SvelteOriginAttachments
|
|
286
|
+
} & Omit<T, 'attrs' | 'props'>
|
|
287
|
+
|
|
288
|
+
// ============================================================================
|
|
289
|
+
// $attrs Global Type
|
|
290
|
+
// ============================================================================
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Extract ALL component props from a factory (including inherited attrs)
|
|
294
|
+
*/
|
|
295
|
+
type SvelteOriginComponentProps<T> =
|
|
296
|
+
T extends SvelteOriginFactory<any, infer AllAttrs>
|
|
297
|
+
? SvelteOriginAllAttrsInput<AllAttrs>
|
|
298
|
+
: Record<string, never>
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Extract attrs input type from a factory (what the component receives as props)
|
|
302
|
+
*/
|
|
303
|
+
type SvelteOriginFactoryAttrs<T> = T extends SvelteOriginFactory<infer D, any>
|
|
304
|
+
? SvelteOriginAttrsInput<D>
|
|
305
|
+
: Record<string, any>
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Combined $attrs interface with all call signatures and methods
|
|
309
|
+
*/
|
|
310
|
+
interface SvelteOriginAttrsGlobal {
|
|
311
|
+
/**
|
|
312
|
+
* Create attrs extending parent(s) - 2 argument version (must come first)
|
|
313
|
+
*/
|
|
314
|
+
<
|
|
315
|
+
Parents extends SvelteOriginAttrsFactory<any>[],
|
|
316
|
+
T extends Record<string, any>
|
|
317
|
+
>(parents: [...Parents], schema: T): SvelteOriginAttrsFactory<SvelteOriginMergeAttrFactories<Parents> & T> & (SvelteOriginMergeAttrFactories<Parents> & T)
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Create attrs - returns AttrsFactory for standalone use, or inline schema
|
|
321
|
+
*/
|
|
322
|
+
<T extends Record<string, any>>(schema: T): SvelteOriginAttrsFactory<T> & T
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Create origin instance from component attrs
|
|
326
|
+
*
|
|
327
|
+
* Expands to $props() destructuring + factory call.
|
|
328
|
+
* When using this macro, the component will accept the factory's attrs as props.
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* // Non-generic origin
|
|
332
|
+
* let counter = $attrs.origin(Counter)
|
|
333
|
+
*
|
|
334
|
+
* // Generic origin (both syntaxes work)
|
|
335
|
+
* let list = $attrs.origin(List<T>) // auto-invoked
|
|
336
|
+
* let list = $attrs.origin(List<T>()) // explicit call
|
|
337
|
+
*/
|
|
338
|
+
origin<T extends SvelteOriginFactory<any, any>>(
|
|
339
|
+
factory: T
|
|
340
|
+
): SvelteOriginInstance<T extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Create origin instance from a generic factory function
|
|
344
|
+
*
|
|
345
|
+
* For generic factories like `const List = <T>() => $origin({...})`,
|
|
346
|
+
* you can pass the type without calling: `$attrs.origin(List<T>)`
|
|
347
|
+
*/
|
|
348
|
+
origin<T extends () => SvelteOriginFactory<any, any>>(
|
|
349
|
+
factoryFn: T
|
|
350
|
+
): SvelteOriginInstance<ReturnType<T> extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Forward attrs for child origin
|
|
354
|
+
*
|
|
355
|
+
* Creates an attrs object containing only the keys needed by the child origin.
|
|
356
|
+
*
|
|
357
|
+
* @example
|
|
358
|
+
* let childAttrs = $attrs.for(ChildOrigin)
|
|
359
|
+
*/
|
|
360
|
+
for<T extends SvelteOriginFactory<any, any>>(
|
|
361
|
+
factory: T,
|
|
362
|
+
mapping?: Partial<Record<string, (source: any) => any>>
|
|
363
|
+
): SvelteOriginComponentProps<T>
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Typed rest attrs for HTML element wrapper
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* let inputAttrs = $attrs.for('input')
|
|
370
|
+
*/
|
|
371
|
+
for<K extends keyof HTMLElementTagNameMap>(element: K): Partial<HTMLElementTagNameMap[K]>
|
|
372
|
+
for(element: string): Record<string, any>
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Type helper to extract component props from an origin factory.
|
|
376
|
+
* Use this to manually declare $$Props when auto-injection is not sufficient.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* type $$Props = $attrs.Of<typeof Counter>
|
|
380
|
+
*/
|
|
381
|
+
Of: SvelteOriginOfTypeHelper
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Type helper interface for $attrs.Of<T>
|
|
386
|
+
* This uses the namespace + const merging trick to allow $attrs.Of<typeof Factory>
|
|
387
|
+
*/
|
|
388
|
+
interface SvelteOriginOfTypeHelper {
|
|
389
|
+
// This is a phantom interface - the actual type extraction happens via the namespace below
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Extract the return type of a generic factory function.
|
|
394
|
+
* Handles: typeof List<T> -> () => SvelteOriginFactory<...> -> SvelteOriginFactory<...>
|
|
395
|
+
*/
|
|
396
|
+
type SvelteOriginUnwrapFactoryFn<T> =
|
|
397
|
+
T extends SvelteOriginFactory<any, any>
|
|
398
|
+
? T
|
|
399
|
+
: T extends () => infer R
|
|
400
|
+
? SvelteOriginUnwrapFactoryFn<R>
|
|
401
|
+
: T extends (...args: any[]) => infer R
|
|
402
|
+
? SvelteOriginUnwrapFactoryFn<R>
|
|
403
|
+
: never
|
|
404
|
+
|
|
405
|
+
// ============================================================================
|
|
406
|
+
// Global Declarations
|
|
407
|
+
// ============================================================================
|
|
408
|
+
|
|
409
|
+
declare global {
|
|
410
|
+
/**
|
|
411
|
+
* Define an origin (state + attrs + methods)
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* export const Counter = $origin({
|
|
415
|
+
* props: $attrs({
|
|
416
|
+
* label: 'Counter',
|
|
417
|
+
* count: $bindable(0)
|
|
418
|
+
* }),
|
|
419
|
+
* _internal: $state(0),
|
|
420
|
+
* get double() { return $derived(this.props.count * 2) },
|
|
421
|
+
* increment() { this.props.count++ }
|
|
422
|
+
* })
|
|
423
|
+
*/
|
|
424
|
+
function $origin<T extends SvelteOriginDefinition>(
|
|
425
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T>>
|
|
426
|
+
): SvelteOriginFactory<T, SvelteOriginExtractAttrs<T>>
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Define an origin with an init callback
|
|
430
|
+
*
|
|
431
|
+
* The callback runs when the component is created and has access to
|
|
432
|
+
* the full instance via `this`. Return a cleanup function to run on unmount.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* export const Counter = $origin({
|
|
436
|
+
* props: $attrs({ count: $bindable(0) }),
|
|
437
|
+
* increment() { this.props.count++ }
|
|
438
|
+
* }, function() {
|
|
439
|
+
* console.log('Created with count:', this.props.count)
|
|
440
|
+
* return () => console.log('Unmounting')
|
|
441
|
+
* })
|
|
442
|
+
*/
|
|
443
|
+
function $origin<T extends SvelteOriginDefinition>(
|
|
444
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T>>,
|
|
445
|
+
onInit: SvelteOriginInitCallback<T>
|
|
446
|
+
): SvelteOriginFactory<T, SvelteOriginExtractAttrs<T>>
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* Define an origin extending parent(s)
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* export const SuperCounter = $origin([Counter], {
|
|
453
|
+
* increment() {
|
|
454
|
+
* this.super.increment()
|
|
455
|
+
* console.log('Incremented!')
|
|
456
|
+
* }
|
|
457
|
+
* })
|
|
458
|
+
*/
|
|
459
|
+
function $origin<
|
|
460
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
461
|
+
T extends SvelteOriginDefinition
|
|
462
|
+
>(
|
|
463
|
+
parents: [...Parents],
|
|
464
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T> & { super: SvelteOriginExtractParentInstance<Parents> }>
|
|
465
|
+
): SvelteOriginFactory<
|
|
466
|
+
SvelteOriginMerged<Parents, T>,
|
|
467
|
+
SvelteOriginMergeAttrs<Parents, SvelteOriginExtractAttrs<T>>
|
|
468
|
+
>
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Define an origin extending parent(s) with an init callback
|
|
472
|
+
*
|
|
473
|
+
* @example
|
|
474
|
+
* export const SuperCounter = $origin([Counter], {
|
|
475
|
+
* increment() { this.super.increment() }
|
|
476
|
+
* }, function() {
|
|
477
|
+
* console.log('SuperCounter created')
|
|
478
|
+
* return () => console.log('SuperCounter unmounting')
|
|
479
|
+
* })
|
|
480
|
+
*/
|
|
481
|
+
function $origin<
|
|
482
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
483
|
+
T extends SvelteOriginDefinition
|
|
484
|
+
>(
|
|
485
|
+
parents: [...Parents],
|
|
486
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T> & { super: SvelteOriginExtractParentInstance<Parents> }>,
|
|
487
|
+
onInit: SvelteOriginInitCallback<SvelteOriginMerged<Parents, T>>
|
|
488
|
+
): SvelteOriginFactory<
|
|
489
|
+
SvelteOriginMerged<Parents, T>,
|
|
490
|
+
SvelteOriginMergeAttrs<Parents, SvelteOriginExtractAttrs<T>>
|
|
491
|
+
>
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* $attrs global for svelte-origin macros
|
|
495
|
+
*
|
|
496
|
+
* Can be used as:
|
|
497
|
+
* - A function: $attrs({...}) to create standalone attrs factory or inline schema
|
|
498
|
+
* - A function with parents: $attrs([Parent], {...}) to extend attrs
|
|
499
|
+
* - An object: $attrs.origin(...), $attrs.for(...), $attrs.Of<T>
|
|
500
|
+
*/
|
|
501
|
+
const $attrs: SvelteOriginAttrsGlobal
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* Mark a prop as bindable (two-way binding) with an optional default value.
|
|
505
|
+
*
|
|
506
|
+
* Returns the value type (T) for compatibility with Svelte's $bindable,
|
|
507
|
+
* but also acts as a marker for the origin's attr schema.
|
|
508
|
+
*
|
|
509
|
+
* @example
|
|
510
|
+
* props: $attrs({
|
|
511
|
+
* count: $bindable(0), // bindable with default 0
|
|
512
|
+
* name: $bindable<string>() // bindable, required (no default)
|
|
513
|
+
* })
|
|
514
|
+
*/
|
|
515
|
+
function $bindable<T>(defaultValue: T): T & SvelteOriginBindable<T>
|
|
516
|
+
function $bindable<T>(): (T | undefined) & SvelteOriginBindable<T | undefined>
|
|
517
|
+
|
|
518
|
+
/**
|
|
519
|
+
* Namespace for $attrs.Of<T> type helper.
|
|
520
|
+
* This enables the syntax: type $$Props = $attrs.Of<typeof Factory>
|
|
521
|
+
*/
|
|
522
|
+
namespace $attrs {
|
|
523
|
+
/**
|
|
524
|
+
* Extract ALL component props from an origin factory (including inherited attrs).
|
|
525
|
+
*
|
|
526
|
+
* Supports:
|
|
527
|
+
* - Direct factories: `$attrs.Of<typeof Counter>`
|
|
528
|
+
* - Generic factory functions: `$attrs.Of<typeof List<T>>` or `$attrs.Of<ReturnType<typeof List<T>>>`
|
|
529
|
+
*
|
|
530
|
+
* @example
|
|
531
|
+
* type $$Props = $attrs.Of<typeof Counter>
|
|
532
|
+
* type $$Props = $attrs.Of<typeof ExtendedCounter> // Includes parent attrs!
|
|
533
|
+
* type $$Props = $attrs.Of<typeof Counter> & { extraProp?: string }
|
|
534
|
+
*
|
|
535
|
+
* // Generic origins
|
|
536
|
+
* const List = <T>() => $origin({ attrs: $attrs({ items: [] as T[] }) })
|
|
537
|
+
* type $$Props = $attrs.Of<ReturnType<typeof List<T>>>
|
|
538
|
+
*/
|
|
539
|
+
type Of<T> =
|
|
540
|
+
SvelteOriginUnwrapFactoryFn<T> extends SvelteOriginFactory<any, infer AllAttrs>
|
|
541
|
+
? SvelteOriginAllAttrsInput<SvelteOriginStripFactoryProps<AllAttrs>>
|
|
542
|
+
: T extends (attrs: infer A) => any
|
|
543
|
+
? A & Record<string, any>
|
|
544
|
+
: Record<string, any>
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export type {
|
|
549
|
+
SvelteOriginBindable,
|
|
550
|
+
SvelteOriginDefinition,
|
|
551
|
+
SvelteOriginFactory,
|
|
552
|
+
SvelteOriginInstance,
|
|
553
|
+
SvelteOriginFullInstance,
|
|
554
|
+
SvelteOriginAttrSchema,
|
|
555
|
+
SvelteOriginAttrSchemaEntry,
|
|
556
|
+
SvelteOriginAttrsGlobal,
|
|
557
|
+
SvelteOriginAttrsInput,
|
|
558
|
+
SvelteOriginAllAttrsInput,
|
|
559
|
+
SvelteOriginReactiveAttrs,
|
|
560
|
+
SvelteOriginUnwrapBindable,
|
|
561
|
+
SvelteOriginExtractAttrs,
|
|
562
|
+
SvelteOriginStripFactoryProps,
|
|
563
|
+
SvelteOriginComponentProps,
|
|
564
|
+
SvelteOriginFactoryAttrs,
|
|
565
|
+
SvelteOriginMergeAttrs,
|
|
566
|
+
SvelteOriginMerged,
|
|
567
|
+
SvelteOriginExtendedThis,
|
|
568
|
+
SvelteOriginPublicKeys,
|
|
569
|
+
SvelteOriginExtractParentAttrs,
|
|
570
|
+
SvelteOriginUnwrapFactoryFn,
|
|
571
|
+
SvelteOriginAttrsFactory,
|
|
572
|
+
SvelteOriginExtractAttrsFromFactory,
|
|
573
|
+
SvelteOriginMergeAttrFactories,
|
|
574
|
+
SvelteOriginCleanupFn,
|
|
575
|
+
SvelteOriginInitCallback
|
|
576
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* svelte-origin
|
|
3
|
+
*
|
|
4
|
+
* Compiler-assisted state and prop ergonomics for Svelte 5
|
|
5
|
+
*/
|
|
6
|
+
export * from './runtime';
|
|
7
|
+
export type { SvelteOriginBindable, SvelteOriginDefinition, SvelteOriginFactory, SvelteOriginInstance, SvelteOriginAttrSchema, SvelteOriginAttrSchemaEntry, SvelteOriginAttrsGlobal, SvelteOriginAttrsInput, SvelteOriginAllAttrsInput, SvelteOriginReactiveAttrs, SvelteOriginUnwrapBindable, SvelteOriginExtractAttrs, SvelteOriginStripFactoryProps, SvelteOriginComponentProps, SvelteOriginFactoryAttrs, SvelteOriginMergeAttrs, SvelteOriginMerged, SvelteOriginExtendedThis, SvelteOriginPublicKeys, SvelteOriginExtractParentAttrs, SvelteOriginUnwrapFactoryFn } from './globals';
|
|
8
|
+
export { transformScript, type TransformOptions, type TransformResult } from './transform/core';
|
|
9
|
+
export { svelteOriginDts, type DtsGeneratorOptions } from './vite-dts';
|