svelte-origin 0.0.0 → 1.0.0-next.49
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 +545 -0
- package/README.md +678 -1
- package/dist/aliases.d.ts +45 -0
- package/dist/cli.d.ts +20 -0
- package/dist/cli.js +163 -0
- package/dist/generate-dts.d.ts +31 -0
- package/dist/globals.d.ts +649 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +108 -0
- package/dist/plugin.d.ts +176 -0
- package/dist/plugin.js +81 -0
- package/dist/post-process.d.ts +31 -0
- package/dist/post-process.js +99 -0
- package/dist/preprocess.d.ts +119 -0
- package/dist/preprocess.js +81 -0
- package/dist/runtime/attrs.d.ts +65 -0
- package/dist/runtime/index.d.ts +8 -0
- package/dist/runtime/index.js +28 -0
- package/dist/runtime/origin.d.ts +170 -0
- package/dist/runtime/types.d.ts +193 -0
- package/dist/runtime-inline.d.ts +117 -0
- package/dist/runtime-inline.min.js +1 -0
- package/dist/schema-resolver.d.ts +61 -0
- package/dist/shared-state.d.ts +47 -0
- package/dist/transform/ast-parser.d.ts +282 -0
- package/dist/transform/attrs-for-transform.d.ts +17 -0
- package/dist/transform/attrs-origin-transform.d.ts +38 -0
- package/dist/transform/attrs-schema.d.ts +63 -0
- package/dist/transform/codegen.d.ts +15 -0
- package/dist/transform/comments.d.ts +31 -0
- package/dist/transform/core.d.ts +37 -0
- package/dist/transform/declaration-parser.d.ts +77 -0
- package/dist/transform/element-types.d.ts +10 -0
- package/dist/transform/expression-utils.d.ts +59 -0
- package/dist/transform/origin-body-helpers.d.ts +143 -0
- package/dist/transform/origin-create-transform.d.ts +32 -0
- package/dist/transform/origin-destructure-transform.d.ts +62 -0
- package/dist/transform/origin-transform.d.ts +51 -0
- package/dist/transform/patterns.d.ts +86 -0
- package/dist/transform/reserved-keywords.d.ts +87 -0
- package/dist/transform/schema-codegen.d.ts +105 -0
- package/dist/transform/schema-parse.d.ts +50 -0
- package/dist/transform/schema-types.d.ts +40 -0
- package/dist/transform/schema.d.ts +11 -0
- package/dist/transform/standalone-attrs-transform.d.ts +36 -0
- package/dist/utils.d.ts +21 -0
- package/dist/vite-dts.d.ts +17 -0
- package/dist/vite-dts.js +24 -0
- package/package.json +79 -3
|
@@ -0,0 +1,649 @@
|
|
|
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
|
+
* Getter that returns 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 $origin.props({...}) 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 $origin.props({...}) 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
|
+
* Getter that returns attachments object that can be spread onto elements.
|
|
284
|
+
*/
|
|
285
|
+
readonly $attachments: SvelteOriginAttachments
|
|
286
|
+
} & Omit<T, 'attrs' | 'props'>
|
|
287
|
+
|
|
288
|
+
// ============================================================================
|
|
289
|
+
// Helper Types for $origin
|
|
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
|
+
* Type helper interface for $origin.Props<T>
|
|
309
|
+
* This uses the namespace + const merging trick to allow $origin.Props<typeof Factory>
|
|
310
|
+
*/
|
|
311
|
+
interface SvelteOriginOfTypeHelper {
|
|
312
|
+
// This is a phantom interface - the actual type extraction happens via the namespace below
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Extract the return type of a generic factory function.
|
|
317
|
+
* Handles: typeof List<T> -> () => SvelteOriginFactory<...> -> SvelteOriginFactory<...>
|
|
318
|
+
*/
|
|
319
|
+
type SvelteOriginUnwrapFactoryFn<T> =
|
|
320
|
+
T extends SvelteOriginFactory<any, any>
|
|
321
|
+
? T
|
|
322
|
+
: T extends () => infer R
|
|
323
|
+
? SvelteOriginUnwrapFactoryFn<R>
|
|
324
|
+
: T extends (...args: any[]) => infer R
|
|
325
|
+
? SvelteOriginUnwrapFactoryFn<R>
|
|
326
|
+
: never
|
|
327
|
+
|
|
328
|
+
// ============================================================================
|
|
329
|
+
// $origin.bind Marker Type
|
|
330
|
+
// ============================================================================
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Marker type for reactive bindings in $origin.create().
|
|
334
|
+
* When used, the prop getter/setter will read/write from the bound variable.
|
|
335
|
+
*/
|
|
336
|
+
interface SvelteOriginBind<T = unknown> {
|
|
337
|
+
readonly __bind: true
|
|
338
|
+
readonly value: T
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ============================================================================
|
|
342
|
+
// $origin Global Interface (Unified API)
|
|
343
|
+
// ============================================================================
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Combined $origin interface with all call signatures and methods.
|
|
347
|
+
* This is the unified API for svelte-origin macros.
|
|
348
|
+
*/
|
|
349
|
+
interface SvelteOriginGlobal {
|
|
350
|
+
// -------------------------------------------------------------------------
|
|
351
|
+
// Function call signatures (for $origin({...}) definitions)
|
|
352
|
+
// -------------------------------------------------------------------------
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Define an origin (state + props + methods)
|
|
356
|
+
*/
|
|
357
|
+
<T extends SvelteOriginDefinition>(
|
|
358
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T>>
|
|
359
|
+
): SvelteOriginFactory<T, SvelteOriginExtractAttrs<T>>
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Define an origin with an init callback
|
|
363
|
+
*/
|
|
364
|
+
<T extends SvelteOriginDefinition>(
|
|
365
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T>>,
|
|
366
|
+
onInit: SvelteOriginInitCallback<T>
|
|
367
|
+
): SvelteOriginFactory<T, SvelteOriginExtractAttrs<T>>
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Define an origin extending parent(s)
|
|
371
|
+
*/
|
|
372
|
+
<
|
|
373
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
374
|
+
T extends SvelteOriginDefinition
|
|
375
|
+
>(
|
|
376
|
+
parents: [...Parents],
|
|
377
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T> & { super: SvelteOriginExtractParentInstance<Parents> }>
|
|
378
|
+
): SvelteOriginFactory<
|
|
379
|
+
SvelteOriginMerged<Parents, T>,
|
|
380
|
+
SvelteOriginMergeAttrs<Parents, SvelteOriginExtractAttrs<T>>
|
|
381
|
+
>
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Define an origin extending parent(s) with an init callback
|
|
385
|
+
*/
|
|
386
|
+
<
|
|
387
|
+
Parents extends SvelteOriginFactory<any, any>[],
|
|
388
|
+
T extends SvelteOriginDefinition
|
|
389
|
+
>(
|
|
390
|
+
parents: [...Parents],
|
|
391
|
+
definition: T & ThisType<SvelteOriginDefinitionThis<T> & { super: SvelteOriginExtractParentInstance<Parents> }>,
|
|
392
|
+
onInit: SvelteOriginInitCallback<SvelteOriginMerged<Parents, T>>
|
|
393
|
+
): SvelteOriginFactory<
|
|
394
|
+
SvelteOriginMerged<Parents, T>,
|
|
395
|
+
SvelteOriginMergeAttrs<Parents, SvelteOriginExtractAttrs<T>>
|
|
396
|
+
>
|
|
397
|
+
|
|
398
|
+
// -------------------------------------------------------------------------
|
|
399
|
+
// Methods
|
|
400
|
+
// -------------------------------------------------------------------------
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Define props schema for an origin.
|
|
404
|
+
* Replaces $origin.props({...}).
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* props: $origin.props({
|
|
408
|
+
* label: 'Counter',
|
|
409
|
+
* count: $bindable(0),
|
|
410
|
+
* step: 1
|
|
411
|
+
* })
|
|
412
|
+
*/
|
|
413
|
+
props: SvelteOriginPropsGlobal
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Create origin instance inside a Svelte component.
|
|
417
|
+
* Binds to component $props() automatically.
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* let counter = $origin.component(Counter)
|
|
421
|
+
*/
|
|
422
|
+
component<T extends SvelteOriginFactory<any, any>>(
|
|
423
|
+
factory: T
|
|
424
|
+
): SvelteOriginInstance<T extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Create origin instance from a generic factory function (component context)
|
|
428
|
+
*/
|
|
429
|
+
component<T extends () => SvelteOriginFactory<any, any>>(
|
|
430
|
+
factoryFn: T
|
|
431
|
+
): SvelteOriginInstance<ReturnType<T> extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Create origin instance programmatically (outside component context).
|
|
435
|
+
* Props are wrapped in $state for reactivity.
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* // Without props (uses defaults)
|
|
439
|
+
* const counter = $origin.create(Counter)
|
|
440
|
+
*
|
|
441
|
+
* // With props
|
|
442
|
+
* const counter = $origin.create(Counter, { count: 10 })
|
|
443
|
+
*
|
|
444
|
+
* // With reactive binding
|
|
445
|
+
* let myCount = $state(0)
|
|
446
|
+
* const counter = $origin.create(Counter, {
|
|
447
|
+
* count: $origin.bind(myCount)
|
|
448
|
+
* })
|
|
449
|
+
*/
|
|
450
|
+
create<T extends SvelteOriginFactory<any, any>>(
|
|
451
|
+
factory: T,
|
|
452
|
+
props?: SvelteOriginCreateProps<T>
|
|
453
|
+
): SvelteOriginInstance<T extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Create origin instance from a generic factory function (programmatic context)
|
|
457
|
+
*/
|
|
458
|
+
create<T extends () => SvelteOriginFactory<any, any>>(
|
|
459
|
+
factoryFn: T,
|
|
460
|
+
props?: SvelteOriginCreateProps<ReturnType<T>>
|
|
461
|
+
): SvelteOriginInstance<ReturnType<T> extends SvelteOriginFactory<infer D, any> ? D : never>
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Create a reactive binding for use in $origin.create() props.
|
|
465
|
+
* The prop will read from and write to the bound variable.
|
|
466
|
+
*
|
|
467
|
+
* @example
|
|
468
|
+
* let myValue = $state(0)
|
|
469
|
+
* const child = $origin.create(ChildOrigin, {
|
|
470
|
+
* value: $origin.bind(myValue)
|
|
471
|
+
* })
|
|
472
|
+
* // child.props.value === myValue (two-way bound)
|
|
473
|
+
*/
|
|
474
|
+
bind<T>(value: T): T & SvelteOriginBind<T>
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* Forward props for child origin component.
|
|
478
|
+
*
|
|
479
|
+
* @example
|
|
480
|
+
* let childProps = $origin.for(ChildOrigin)
|
|
481
|
+
*/
|
|
482
|
+
for<T extends SvelteOriginFactory<any, any>>(
|
|
483
|
+
factory: T,
|
|
484
|
+
mapping?: Partial<Record<string, (source: any) => any>>
|
|
485
|
+
): SvelteOriginComponentProps<T>
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* Typed rest props for HTML element wrapper.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* let inputProps = $origin.for('input')
|
|
492
|
+
*/
|
|
493
|
+
for<K extends keyof HTMLElementTagNameMap>(element: K): Partial<HTMLElementTagNameMap[K]>
|
|
494
|
+
for(element: string): Record<string, any>
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Type helper marker (actual type is in namespace below)
|
|
498
|
+
*/
|
|
499
|
+
Props: SvelteOriginOfTypeHelper
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Props schema creation interface.
|
|
504
|
+
*/
|
|
505
|
+
interface SvelteOriginPropsGlobal {
|
|
506
|
+
/**
|
|
507
|
+
* Create props extending parent(s)
|
|
508
|
+
*/
|
|
509
|
+
<
|
|
510
|
+
Parents extends SvelteOriginAttrsFactory<any>[],
|
|
511
|
+
T extends Record<string, any>
|
|
512
|
+
>(parents: [...Parents], schema: T): SvelteOriginAttrsFactory<SvelteOriginMergeAttrFactories<Parents> & T> & (SvelteOriginMergeAttrFactories<Parents> & T)
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Create props schema
|
|
516
|
+
*/
|
|
517
|
+
<T extends Record<string, any>>(schema: T): SvelteOriginAttrsFactory<T> & T
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Props type for $origin.create() - allows $origin.bind() markers
|
|
522
|
+
*/
|
|
523
|
+
type SvelteOriginCreateProps<T> = T extends SvelteOriginFactory<any, infer AllAttrs>
|
|
524
|
+
? { [K in keyof AllAttrs]?: SvelteOriginUnwrapBindable<AllAttrs[K]> | SvelteOriginBind<SvelteOriginUnwrapBindable<AllAttrs[K]>> }
|
|
525
|
+
: Record<string, any>
|
|
526
|
+
|
|
527
|
+
// ============================================================================
|
|
528
|
+
// Global Declarations
|
|
529
|
+
// ============================================================================
|
|
530
|
+
|
|
531
|
+
declare global {
|
|
532
|
+
/**
|
|
533
|
+
* $origin - Unified API for svelte-origin macros.
|
|
534
|
+
*
|
|
535
|
+
* Can be used as:
|
|
536
|
+
* - A function: `$origin({...})` to define an origin
|
|
537
|
+
* - An object with methods:
|
|
538
|
+
* - `$origin.props({...})` - Define props schema
|
|
539
|
+
* - `$origin.component(Factory)` - Create instance in component (binds to $props())
|
|
540
|
+
* - `$origin.create(Factory, props?)` - Create instance programmatically
|
|
541
|
+
* - `$origin.bind(value)` - Create reactive binding for $origin.create()
|
|
542
|
+
* - `$origin.for(Factory)` - Forward props to child component
|
|
543
|
+
* - `$origin.Props<typeof Factory>` - Extract component props type
|
|
544
|
+
*
|
|
545
|
+
* @example
|
|
546
|
+
* // Define an origin
|
|
547
|
+
* export const Counter = $origin({
|
|
548
|
+
* props: $origin.props({
|
|
549
|
+
* label: 'Counter',
|
|
550
|
+
* count: $bindable(0)
|
|
551
|
+
* }),
|
|
552
|
+
* _internal: $state(0),
|
|
553
|
+
* get double() { return $derived(this.props.count * 2) },
|
|
554
|
+
* increment() { this.props.count++ }
|
|
555
|
+
* })
|
|
556
|
+
*
|
|
557
|
+
* // Use in component
|
|
558
|
+
* let counter = $origin.component(Counter)
|
|
559
|
+
*
|
|
560
|
+
* // Create programmatically
|
|
561
|
+
* const counter = $origin.create(Counter, { count: 10 })
|
|
562
|
+
*/
|
|
563
|
+
const $origin: SvelteOriginGlobal
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Mark a prop as bindable (two-way binding) with an optional default value.
|
|
567
|
+
*
|
|
568
|
+
* Returns the value type (T) for compatibility with Svelte's $bindable,
|
|
569
|
+
* but also acts as a marker for the origin's attr schema.
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* props: $origin.props({
|
|
573
|
+
* count: $bindable(0), // bindable with default 0
|
|
574
|
+
* name: $bindable<string>() // bindable, required (no default)
|
|
575
|
+
* })
|
|
576
|
+
*/
|
|
577
|
+
function $bindable<T>(defaultValue: T): T & SvelteOriginBindable<T>
|
|
578
|
+
function $bindable<T>(): (T | undefined) & SvelteOriginBindable<T | undefined>
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Namespace for $origin.Props<T> type helper.
|
|
582
|
+
* This enables the syntax: type $$Props = $origin.Props<typeof Factory>
|
|
583
|
+
*/
|
|
584
|
+
namespace $origin {
|
|
585
|
+
/**
|
|
586
|
+
* Extract ALL component props from an origin factory (including inherited attrs).
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* type $$Props = $origin.Props<typeof Counter>
|
|
590
|
+
* type $$Props = $origin.Props<typeof ExtendedCounter> // Includes parent attrs!
|
|
591
|
+
*
|
|
592
|
+
* // Generic origins
|
|
593
|
+
* const List = <T>() => $origin({ props: $origin.props({ items: [] as T[] }) })
|
|
594
|
+
* type $$Props = $origin.Props<ReturnType<typeof List<T>>>
|
|
595
|
+
*/
|
|
596
|
+
type Props<T> =
|
|
597
|
+
SvelteOriginUnwrapFactoryFn<T> extends SvelteOriginFactory<any, infer AllAttrs>
|
|
598
|
+
? SvelteOriginAllAttrsInput<SvelteOriginStripFactoryProps<AllAttrs>>
|
|
599
|
+
: T extends (attrs: infer A) => any
|
|
600
|
+
? A & Record<string, any>
|
|
601
|
+
: Record<string, any>
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Extract the return type of an origin factory.
|
|
605
|
+
* Utility type for extracting instance type from factory.
|
|
606
|
+
*
|
|
607
|
+
* @example
|
|
608
|
+
* type CounterInstance = $origin.Of<typeof Counter>
|
|
609
|
+
*/
|
|
610
|
+
type Of<T> = T extends SvelteOriginFactory<infer D, any>
|
|
611
|
+
? SvelteOriginInstance<D>
|
|
612
|
+
: T extends () => SvelteOriginFactory<infer D, any>
|
|
613
|
+
? SvelteOriginInstance<D>
|
|
614
|
+
: never
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
export type {
|
|
619
|
+
SvelteOriginBindable,
|
|
620
|
+
SvelteOriginBind,
|
|
621
|
+
SvelteOriginDefinition,
|
|
622
|
+
SvelteOriginFactory,
|
|
623
|
+
SvelteOriginInstance,
|
|
624
|
+
SvelteOriginFullInstance,
|
|
625
|
+
SvelteOriginAttrSchema,
|
|
626
|
+
SvelteOriginAttrSchemaEntry,
|
|
627
|
+
SvelteOriginGlobal,
|
|
628
|
+
SvelteOriginPropsGlobal,
|
|
629
|
+
SvelteOriginCreateProps,
|
|
630
|
+
SvelteOriginAttrsInput,
|
|
631
|
+
SvelteOriginAllAttrsInput,
|
|
632
|
+
SvelteOriginReactiveAttrs,
|
|
633
|
+
SvelteOriginUnwrapBindable,
|
|
634
|
+
SvelteOriginExtractAttrs,
|
|
635
|
+
SvelteOriginStripFactoryProps,
|
|
636
|
+
SvelteOriginComponentProps,
|
|
637
|
+
SvelteOriginFactoryAttrs,
|
|
638
|
+
SvelteOriginMergeAttrs,
|
|
639
|
+
SvelteOriginMerged,
|
|
640
|
+
SvelteOriginExtendedThis,
|
|
641
|
+
SvelteOriginPublicKeys,
|
|
642
|
+
SvelteOriginExtractParentAttrs,
|
|
643
|
+
SvelteOriginUnwrapFactoryFn,
|
|
644
|
+
SvelteOriginAttrsFactory,
|
|
645
|
+
SvelteOriginExtractAttrsFromFactory,
|
|
646
|
+
SvelteOriginMergeAttrFactories,
|
|
647
|
+
SvelteOriginCleanupFn,
|
|
648
|
+
SvelteOriginInitCallback
|
|
649
|
+
}
|
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, 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';
|