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.
Files changed (49) hide show
  1. package/LLM.md +545 -0
  2. package/README.md +678 -1
  3. package/dist/aliases.d.ts +45 -0
  4. package/dist/cli.d.ts +20 -0
  5. package/dist/cli.js +163 -0
  6. package/dist/generate-dts.d.ts +31 -0
  7. package/dist/globals.d.ts +649 -0
  8. package/dist/index.d.ts +9 -0
  9. package/dist/index.js +108 -0
  10. package/dist/plugin.d.ts +176 -0
  11. package/dist/plugin.js +81 -0
  12. package/dist/post-process.d.ts +31 -0
  13. package/dist/post-process.js +99 -0
  14. package/dist/preprocess.d.ts +119 -0
  15. package/dist/preprocess.js +81 -0
  16. package/dist/runtime/attrs.d.ts +65 -0
  17. package/dist/runtime/index.d.ts +8 -0
  18. package/dist/runtime/index.js +28 -0
  19. package/dist/runtime/origin.d.ts +170 -0
  20. package/dist/runtime/types.d.ts +193 -0
  21. package/dist/runtime-inline.d.ts +117 -0
  22. package/dist/runtime-inline.min.js +1 -0
  23. package/dist/schema-resolver.d.ts +61 -0
  24. package/dist/shared-state.d.ts +47 -0
  25. package/dist/transform/ast-parser.d.ts +282 -0
  26. package/dist/transform/attrs-for-transform.d.ts +17 -0
  27. package/dist/transform/attrs-origin-transform.d.ts +38 -0
  28. package/dist/transform/attrs-schema.d.ts +63 -0
  29. package/dist/transform/codegen.d.ts +15 -0
  30. package/dist/transform/comments.d.ts +31 -0
  31. package/dist/transform/core.d.ts +37 -0
  32. package/dist/transform/declaration-parser.d.ts +77 -0
  33. package/dist/transform/element-types.d.ts +10 -0
  34. package/dist/transform/expression-utils.d.ts +59 -0
  35. package/dist/transform/origin-body-helpers.d.ts +143 -0
  36. package/dist/transform/origin-create-transform.d.ts +32 -0
  37. package/dist/transform/origin-destructure-transform.d.ts +62 -0
  38. package/dist/transform/origin-transform.d.ts +51 -0
  39. package/dist/transform/patterns.d.ts +86 -0
  40. package/dist/transform/reserved-keywords.d.ts +87 -0
  41. package/dist/transform/schema-codegen.d.ts +105 -0
  42. package/dist/transform/schema-parse.d.ts +50 -0
  43. package/dist/transform/schema-types.d.ts +40 -0
  44. package/dist/transform/schema.d.ts +11 -0
  45. package/dist/transform/standalone-attrs-transform.d.ts +36 -0
  46. package/dist/utils.d.ts +21 -0
  47. package/dist/vite-dts.d.ts +17 -0
  48. package/dist/vite-dts.js +24 -0
  49. package/package.json +79 -3
@@ -0,0 +1,282 @@
1
+ /**
2
+ * AST Parser utilities for svelte-origin
3
+ *
4
+ * Uses acorn + @sveltejs/acorn-typescript for reliable TypeScript/JavaScript parsing.
5
+ * This provides future-safe parsing for complex expressions that regex can't handle.
6
+ *
7
+ * Why acorn + @sveltejs/acorn-typescript?
8
+ * - Maintained by Svelte team (Rich Harris et al.)
9
+ * - 0 dependencies (the TypeScript plugin)
10
+ * - Small bundle: acorn ~27KB gzip, plugin ~16KB gzip
11
+ * - Supports full TypeScript syntax, decorators, JSX/TSX
12
+ *
13
+ * @see https://github.com/sveltejs/acorn-typescript
14
+ */
15
+ import { type Options } from 'acorn';
16
+ import type * as ESTree from 'estree';
17
+ /**
18
+ * Acorn nodes include start/end positions not in ESTree types
19
+ */
20
+ export type AcornNode<T extends ESTree.Node = ESTree.Node> = T & {
21
+ start: number;
22
+ end: number;
23
+ };
24
+ /**
25
+ * Parse result with error handling
26
+ */
27
+ export type ParseResult<T> = {
28
+ ok: true;
29
+ value: T;
30
+ } | {
31
+ ok: false;
32
+ error: string;
33
+ };
34
+ /**
35
+ * Helper to safely get node positions (acorn adds these)
36
+ */
37
+ export declare function getNodePosition(node: ESTree.Node): {
38
+ start: number;
39
+ end: number;
40
+ };
41
+ /**
42
+ * Parse TypeScript/JavaScript source code into an AST
43
+ */
44
+ export declare function parse(source: string, options?: Partial<Options>): ESTree.Program;
45
+ /**
46
+ * Safely parse source, returning a result type instead of throwing
47
+ */
48
+ export declare function tryParse(source: string, options?: Partial<Options>): ParseResult<ESTree.Program>;
49
+ /**
50
+ * Parse a single expression (for parsing values, defaults, etc.)
51
+ */
52
+ export declare function parseExpression(source: string, options?: Partial<Options>): ESTree.Expression;
53
+ /**
54
+ * Safely parse an expression, returning a result type instead of throwing
55
+ */
56
+ export declare function tryParseExpression(source: string, options?: Partial<Options>): ParseResult<ESTree.Expression>;
57
+ /**
58
+ * Parse an object literal expression from source like `{ a: 1, b: 2 }`
59
+ * Wraps in parens to allow parsing as expression
60
+ */
61
+ export declare function parseObjectLiteral(source: string): ParseResult<AcornNode<ESTree.ObjectExpression>>;
62
+ /**
63
+ * Generate code from an AST node
64
+ */
65
+ export declare function generate(node: ESTree.Node): string;
66
+ /**
67
+ * Parsed object member with full metadata
68
+ */
69
+ export interface ParsedObjectMember {
70
+ /** The property key (name or computed expression source) */
71
+ key: string;
72
+ /** The kind of member */
73
+ kind: 'property' | 'method' | 'getter' | 'setter';
74
+ /** The value source code */
75
+ value: string;
76
+ /** Whether the key is computed [expr] */
77
+ computed: boolean;
78
+ /** Whether it's an async method */
79
+ async: boolean;
80
+ /** Whether it's a generator method */
81
+ generator: boolean;
82
+ /** Start position in original source (adjusted for wrapper) */
83
+ start: number;
84
+ /** End position in original source (adjusted for wrapper) */
85
+ end: number;
86
+ /** The raw AST node for advanced use */
87
+ node: ESTree.Property | ESTree.SpreadElement;
88
+ }
89
+ /**
90
+ * Parse an object literal and extract its members with full metadata.
91
+ * Uses AST for reliable parsing of complex expressions.
92
+ *
93
+ * @param objectSource - The content inside {...}, not including braces
94
+ * @returns Array of parsed members, or null if parsing fails
95
+ */
96
+ export declare function parseObjectMembersAST(objectSource: string): ParsedObjectMember[] | null;
97
+ /**
98
+ * Result from analyzing an expression
99
+ */
100
+ export interface ExpressionAnalysis {
101
+ type: ESTree.Node['type'];
102
+ hasThisReferences: boolean;
103
+ hasSuperReferences: boolean;
104
+ identifiers: Set<string>;
105
+ memberExpressions: Array<{
106
+ object: string;
107
+ property: string;
108
+ start: number;
109
+ end: number;
110
+ }>;
111
+ callExpressions: Array<{
112
+ callee: string;
113
+ start: number;
114
+ end: number;
115
+ }>;
116
+ }
117
+ /**
118
+ * Analyze an expression for references and structure
119
+ */
120
+ export declare function analyzeExpression(source: string): ExpressionAnalysis;
121
+ /**
122
+ * Find all `this.propertyName` references in an expression
123
+ * Returns an array of property names
124
+ */
125
+ export declare function findThisReferences(source: string): string[];
126
+ /**
127
+ * Position and metadata for a this.* reference
128
+ */
129
+ export interface ThisReference {
130
+ /** Property name being accessed (e.g., 'count' in this.count) */
131
+ property: string;
132
+ /** Start position in source */
133
+ start: number;
134
+ /** End position in source */
135
+ end: number;
136
+ /** Whether this is part of a member chain (e.g., this.props.x) */
137
+ isChained: boolean;
138
+ /** The full chain if chained (e.g., ['props', 'x']) */
139
+ chain: string[];
140
+ }
141
+ /**
142
+ * Collect all this.* references with their positions
143
+ * Uses AST for accurate identification
144
+ */
145
+ export declare function collectThisReferences(source: string): ThisReference[];
146
+ /**
147
+ * Transform `this.x` references in source code using AST-based identification.
148
+ * Much more reliable than regex for complex expressions.
149
+ *
150
+ * @param source - Source code containing this.* references
151
+ * @param transform - Function to transform each reference
152
+ */
153
+ export declare function transformThisReferences(source: string, transform: (ref: ThisReference) => string): string;
154
+ /**
155
+ * Check if source contains any `this` references
156
+ */
157
+ export declare function hasThisReferences(source: string): boolean;
158
+ /**
159
+ * Check if source contains any `super` references
160
+ */
161
+ export declare function hasSuperReferences(source: string): boolean;
162
+ /**
163
+ * Result of extracting $bindable from an expression
164
+ */
165
+ export interface BindableExtraction {
166
+ isBindable: boolean;
167
+ /** The inner value (argument to $bindable) */
168
+ innerValue: string;
169
+ /** Any generic type parameter, e.g., '<number>' */
170
+ genericType: string | null;
171
+ }
172
+ /**
173
+ * Extract $bindable from a value expression using AST.
174
+ * Handles complex cases like $bindable<Map<K, V>>(new Map())
175
+ */
176
+ export declare function extractBindableAST(value: string): BindableExtraction;
177
+ /**
178
+ * Information about a call expression
179
+ */
180
+ export interface CallExpressionInfo {
181
+ /** The callee name or chain (e.g., '$state', '$derived.by') */
182
+ callee: string;
183
+ /** Arguments as source strings */
184
+ arguments: string[];
185
+ /** Generic type parameters if any */
186
+ typeParams: string | null;
187
+ /** Start position */
188
+ start: number;
189
+ /** End position */
190
+ end: number;
191
+ }
192
+ /**
193
+ * Parse a call expression and extract its components
194
+ */
195
+ export declare function parseCallExpression(source: string): CallExpressionInfo | null;
196
+ /**
197
+ * Result of extracting $state from an expression
198
+ */
199
+ export interface StateExtraction {
200
+ isState: boolean;
201
+ /** The variant: '$state', '$state.raw', '$state.frozen' */
202
+ variant: string;
203
+ /** The initial value argument */
204
+ initialValue: string;
205
+ /** Any generic type parameter */
206
+ genericType: string | null;
207
+ }
208
+ /**
209
+ * Extract $state (and variants) from a value expression using AST.
210
+ */
211
+ export declare function extractStateAST(value: string): StateExtraction;
212
+ /**
213
+ * Result of extracting $derived from an expression
214
+ */
215
+ export interface DerivedExtraction {
216
+ isDerived: boolean;
217
+ /** The variant: '$derived' or '$derived.by' */
218
+ variant: string;
219
+ /** The expression or function argument */
220
+ argument: string;
221
+ }
222
+ /**
223
+ * Extract $derived (and variants) from a value expression using AST.
224
+ */
225
+ export declare function extractDerivedAST(value: string): DerivedExtraction;
226
+ /**
227
+ * Strip TypeScript type annotations from an expression
228
+ * Useful for extracting runtime values from typed code
229
+ */
230
+ export declare function stripTypeAnnotations(source: string): string;
231
+ /**
232
+ * Split object literal content by top-level commas using AST.
233
+ * Much more reliable than regex for complex expressions.
234
+ *
235
+ * @param objectContent - The content inside {...}, not including braces
236
+ * @returns Array of member source strings, or null if parsing fails
237
+ */
238
+ export declare function splitObjectMembersAST(objectContent: string): string[] | null;
239
+ /**
240
+ * Find matching bracket using AST-based depth tracking.
241
+ * More reliable than character-by-character scanning.
242
+ *
243
+ * @param source - Full source code
244
+ * @param openIndex - Position of opening bracket
245
+ * @param openChar - The opening bracket character
246
+ * @param closeChar - The closing bracket character
247
+ * @returns Position of matching close bracket, or -1 if not found
248
+ */
249
+ export declare function findMatchingBracketAST(source: string, openIndex: number, openChar?: string, closeChar?: string): number;
250
+ /**
251
+ * Find the position of top-level 'as' type assertion using AST awareness
252
+ * Returns -1 if not found at top level
253
+ */
254
+ export declare function findTopLevelAsAST(value: string): number;
255
+ /**
256
+ * Parse a function body and extract return statement value
257
+ * Useful for analyzing getter functions
258
+ */
259
+ export declare function extractReturnValue(functionBody: string): string | null;
260
+ /** Type guard for SpreadElement */
261
+ export declare function isSpreadElement(node: ESTree.Node): node is ESTree.SpreadElement;
262
+ /** Type guard for Property */
263
+ export declare function isProperty(node: ESTree.Node): node is ESTree.Property;
264
+ /** Type guard for Identifier */
265
+ export declare function isIdentifier(node: ESTree.Node): node is ESTree.Identifier;
266
+ /** Type guard for CallExpression */
267
+ export declare function isCallExpression(node: ESTree.Node): node is ESTree.CallExpression;
268
+ /** Type guard for MemberExpression */
269
+ export declare function isMemberExpression(node: ESTree.Node): node is ESTree.MemberExpression;
270
+ /** Type guard for FunctionExpression */
271
+ export declare function isFunctionExpression(node: ESTree.Node): node is ESTree.FunctionExpression;
272
+ /** Type guard for ArrowFunctionExpression */
273
+ export declare function isArrowFunctionExpression(node: ESTree.Node): node is ESTree.ArrowFunctionExpression;
274
+ /**
275
+ * Check if a string is a valid TypeScript/JavaScript expression
276
+ */
277
+ export declare function isValidExpression(source: string): boolean;
278
+ /**
279
+ * Check if a string is a valid TypeScript/JavaScript program
280
+ */
281
+ export declare function isValidProgram(source: string): boolean;
282
+ export type { ESTree };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * $origin.for() transformation logic.
3
+ *
4
+ * This module handles transforming $origin.for(X) calls which support:
5
+ * - $origin.for(OriginFactory) - get attrs for an origin factory
6
+ * - $origin.for('input') - get typed element attrs
7
+ */
8
+ import type MagicString from 'magic-string';
9
+ import { type SchemaResolver } from './schema';
10
+ /**
11
+ * Transform $origin.for(X) calls - sync fallback version.
12
+ */
13
+ export declare function transformAttrsForCallsSync(s: MagicString, source: string, neededImports: Set<string>): boolean;
14
+ /**
15
+ * Transform $origin.for(X) calls - async version with schema resolution.
16
+ */
17
+ export declare function transformAttrsForCalls(s: MagicString, source: string, neededImports: Set<string>, schemaResolver?: SchemaResolver): Promise<boolean>;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * $origin.component() transformation logic.
3
+ *
4
+ * This module handles transforming $origin.component(Factory)
5
+ * calls in component scripts into proper $props() destructuring and factory calls.
6
+ */
7
+ import type MagicString from 'magic-string';
8
+ import { type SchemaResolver } from './schema';
9
+ /**
10
+ * Information about a tracked origin instance (for destructuring transform)
11
+ */
12
+ export interface TrackedOriginInstance {
13
+ /** Variable name holding the origin instance */
14
+ varName: string;
15
+ /** The factory name/expression used to create it */
16
+ factoryExpr: string;
17
+ /** Start position in source */
18
+ startPos: number;
19
+ /** End position in source */
20
+ endPos: number;
21
+ }
22
+ /**
23
+ * Result from transformAttrsOriginCalls including $$Props info for later injection
24
+ */
25
+ export interface AttrsOriginTransformResult {
26
+ changed: boolean;
27
+ propsTypeDeclaration: string | null;
28
+ /** Tracked origin instances for destructuring transform */
29
+ originInstances: Map<string, TrackedOriginInstance>;
30
+ }
31
+ /**
32
+ * Transform $origin.component(X) calls - sync fallback version.
33
+ */
34
+ export declare function transformAttrsOriginCallsSync(s: MagicString, source: string, neededImports: Set<string>): AttrsOriginTransformResult;
35
+ /**
36
+ * Transform $origin.component(X) calls - async version with schema resolution.
37
+ */
38
+ export declare function transformAttrsOriginCalls(s: MagicString, source: string, neededImports: Set<string>, schemaResolver?: SchemaResolver): Promise<AttrsOriginTransformResult>;
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Attribute schema parsing and generation utilities.
3
+ *
4
+ * This module handles parsing $origin.props({...}) content and generating
5
+ * the __attrSchema object that describes each attribute's metadata.
6
+ */
7
+ /**
8
+ * Parsed attribute details for code generation
9
+ */
10
+ export interface AttrDetail {
11
+ key: string;
12
+ defaultValue: string;
13
+ bindable: boolean;
14
+ hasDefault: boolean;
15
+ }
16
+ /**
17
+ * Generate __attrSchema code from the attrs source.
18
+ *
19
+ * Input: 'label: "Counter", count: $bindable(0), step: undefined as undefined | number'
20
+ * Output: '{ label: { default: "Counter", bindable: false, hasDefault: true }, ... }'
21
+ */
22
+ export declare function generateAttrSchemaFromSource(attrsSource: string): string;
23
+ /**
24
+ * Parse attrs source and return structured details for each attr.
25
+ *
26
+ * Input: 'label: "Counter", count: $bindable(0), step: 1 as number'
27
+ * Output: [{ key: 'label', defaultValue: '"Counter"', bindable: false, hasDefault: true }, ...]
28
+ */
29
+ export declare function parseAttrsSource(attrsSource: string): AttrDetail[];
30
+ /**
31
+ * Split attrs source by top-level commas.
32
+ * Uses the shared splitByTopLevelCommas from comments.ts.
33
+ */
34
+ export { splitByTopLevelCommas as splitAttrsSource } from './comments';
35
+ /**
36
+ * Parse members from an object literal source.
37
+ * This is the type definition for parsed members.
38
+ */
39
+ export interface ObjectMember {
40
+ key: string;
41
+ value: string;
42
+ isGetter: boolean;
43
+ isSetter: boolean;
44
+ isMethod: boolean;
45
+ isAsync: boolean;
46
+ isGenerator: boolean;
47
+ isComputed: boolean;
48
+ }
49
+ /**
50
+ * Parse members from an object literal source.
51
+ *
52
+ * This regex-based parser handles:
53
+ * - Methods, getters, setters
54
+ * - Async and generator functions
55
+ * - Computed property names
56
+ * - Complex expressions in values
57
+ *
58
+ * Note: An AST-based version exists in ast-parser.ts (parseObjectMembersAST) but
59
+ * produces different output format for methods (value excludes opening paren).
60
+ * Downstream code expects value to include `(params) { body }` for methods,
61
+ * so the regex parser is used here.
62
+ */
63
+ export declare function parseObjectMembers(source: string): ObjectMember[];
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Code generation utilities for svelte-origin transformations
3
+ */
4
+ /**
5
+ * Generate the import statement for runtime functions.
6
+ *
7
+ * @example
8
+ * generateRuntimeImport(['__createOrigin', '__attrsFor'])
9
+ * // Returns: "import { __createOrigin, __attrsFor } from 'svelte-origin/runtime'"
10
+ */
11
+ export declare function generateRuntimeImport(functions: string[]): string;
12
+ /**
13
+ * Indent each line of code by a given amount
14
+ */
15
+ export declare function indent(code: string, tabs?: number): string;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Shared comment stripping utilities.
3
+ *
4
+ * These functions handle stripping comments from source code strings,
5
+ * used by both schema.ts and attrs-schema.ts for parsing attr definitions.
6
+ */
7
+ /**
8
+ * Strip leading comments (JSDoc, block, line) from a string.
9
+ */
10
+ export declare function stripLeadingComments(str: string): string;
11
+ /**
12
+ * Find orphaned block-comment close markers that exist without a corresponding open.
13
+ * Returns the index where to cut, or -1 if none found.
14
+ */
15
+ export declare function findOrphanedCommentClose(str: string): number;
16
+ /**
17
+ * Find where corrupted comment content starts before an orphaned close marker.
18
+ * Scans forward to find the last position that looks like valid JS.
19
+ */
20
+ export declare function findCorruptedContentStart(str: string, closeMarkerIdx: number): number;
21
+ /**
22
+ * Strip trailing inline comments from a value string.
23
+ * Handles block comments at the end of a value.
24
+ * Must be careful not to strip comments inside strings or nested structures.
25
+ */
26
+ export declare function stripTrailingComments(str: string): string;
27
+ /**
28
+ * Split content by top-level commas (not inside nested structures).
29
+ * Handles strings, comments, and generics correctly.
30
+ */
31
+ export declare function splitByTopLevelCommas(source: string): string[];
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Core transformation logic for svelte-origin
3
+ *
4
+ * Main entry point for transformations that convert macro syntax into runtime calls.
5
+ */
6
+ import MagicString from 'magic-string';
7
+ import type { SchemaResolver } from './schema';
8
+ export type { SchemaResolver } from './schema';
9
+ export type { AttrDetail } from './attrs-schema';
10
+ export type { AttrsOriginTransformResult } from './attrs-origin-transform';
11
+ export interface TransformOptions {
12
+ filename?: string;
13
+ isComponent?: boolean;
14
+ schemaResolver?: SchemaResolver;
15
+ }
16
+ export interface TransformResult {
17
+ code: string;
18
+ changed: boolean;
19
+ map?: ReturnType<MagicString['generateMap']>;
20
+ }
21
+ /**
22
+ * Transform a script/module containing svelte-origin macros.
23
+ *
24
+ * @overload Sync version when no schemaResolver is provided
25
+ * @overload Async version when schemaResolver is provided
26
+ */
27
+ export declare function transformScript(source: string, options?: TransformOptions & {
28
+ schemaResolver?: undefined;
29
+ }): TransformResult;
30
+ export declare function transformScript(source: string, options: TransformOptions & {
31
+ schemaResolver: SchemaResolver;
32
+ }): Promise<TransformResult>;
33
+ /**
34
+ * Transform script content from the script hook.
35
+ * Designed for preprocessor script hook where content is already extracted.
36
+ */
37
+ export declare function transformScriptContent(source: string, options: TransformOptions): Promise<TransformResult>;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * TypeScript/JavaScript declaration parsing utilities.
3
+ *
4
+ * This module handles parsing variable declarations from source code,
5
+ * with full support for TypeScript type annotations, generics, and exports.
6
+ */
7
+ /**
8
+ * Information about a variable declaration found in source code.
9
+ * Handles all TypeScript declaration variants:
10
+ *
11
+ * Basic:
12
+ * let x = value
13
+ * const x = value
14
+ *
15
+ * With type annotation:
16
+ * let x: Type = value
17
+ * const x: Type<T> = value
18
+ * const x: Type<T, U<V>> = value
19
+ *
20
+ * With export:
21
+ * export let x = value
22
+ * export const x: Type = value
23
+ *
24
+ * Complex types:
25
+ * let x: Type | Other = value
26
+ * let x: Type & Other = value
27
+ * let x: () => Type = value
28
+ */
29
+ export interface VariableDeclaration {
30
+ /** The variable name */
31
+ varName: string;
32
+ /** Start index of the entire declaration (including export/let/const) */
33
+ startIndex: number;
34
+ /** Index where the value expression starts (after =) */
35
+ valueStartIndex: number;
36
+ /** Whether it has 'export' keyword */
37
+ hasExport: boolean;
38
+ /** The type annotation if present (without the colon) */
39
+ typeAnnotation: string | null;
40
+ }
41
+ /**
42
+ * Find all variable declarations that are assigned a specific macro call.
43
+ * Handles all TypeScript declaration patterns:
44
+ *
45
+ * - let x = $macro(...)
46
+ * - const x = $macro(...)
47
+ * - let x: Type = $macro(...)
48
+ * - const x: Type<T> = $macro(...)
49
+ * - export let x = $macro(...)
50
+ * - export const x: Type = $macro(...)
51
+ *
52
+ * @param source The source code to search
53
+ * @param macroPattern The macro to find (e.g., '$origin.component', '$origin.for', '$origin')
54
+ * @returns Array of declarations with their metadata
55
+ */
56
+ export declare function findVariableDeclarationsWithMacro(source: string, macroPattern: string): (VariableDeclaration & {
57
+ macroOpenParen: number;
58
+ })[];
59
+ /**
60
+ * Look backwards from a position to find a variable declaration.
61
+ * Handles: [export] (let|const) varName [: Type] =
62
+ */
63
+ export declare function findDeclarationBefore(source: string, beforeIndex: number): VariableDeclaration | null;
64
+ /**
65
+ * Check if the source already has a $$Props type declaration.
66
+ * Looks for 'type $$Props' or 'interface $$Props' patterns.
67
+ */
68
+ export declare function hasExistingPropsDeclaration(source: string): boolean;
69
+ /**
70
+ * Find the position right after imports to inject $$Props.
71
+ * Returns the position after the last import statement, or after the script tag.
72
+ *
73
+ * Handles both single-line and multi-line imports:
74
+ * - import { x } from 'y'
75
+ * - import {\n x,\n y\n} from 'z'
76
+ */
77
+ export declare function findPropsInjectionPosition(source: string): number;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Get the svelte/elements type import for an HTML element name.
3
+ *
4
+ * Uses svelteHTML.IntrinsicElements for type-checking compatibility with Svelte's
5
+ * internal createElement function, which expects IntrinsicElements types.
6
+ *
7
+ * @param element - The HTML element name (e.g., 'input', 'button', 'div')
8
+ * @returns The full type expression using svelteHTML.IntrinsicElements
9
+ */
10
+ export declare function getElementTypeImport(element: string): string;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Expression parsing utilities for svelte-origin transforms
3
+ *
4
+ * Shared utilities for parsing and manipulating TypeScript expressions.
5
+ */
6
+ /**
7
+ * Check if an expression is a function call (ends with parentheses).
8
+ *
9
+ * This correctly handles:
10
+ * - Simple calls: `Factory()`
11
+ * - Generic calls: `Factory<T>()`
12
+ * - Nested calls: `outer(inner())`
13
+ *
14
+ * @example
15
+ * isCallExpression('Factory()') // true
16
+ * isCallExpression('Factory<T>()') // true
17
+ * isCallExpression('Factory') // false
18
+ * isCallExpression('Factory<T>') // false (generic type, not a call)
19
+ */
20
+ export declare function isCallExpression(expr: string): boolean;
21
+ /**
22
+ * Extract the factory expression from a call expression by stripping the trailing `()`.
23
+ *
24
+ * @example
25
+ * extractFactoryFromCall('List<T>()') // 'List<T>'
26
+ * extractFactoryFromCall('MyFactory()') // 'MyFactory'
27
+ * extractFactoryFromCall('outer(inner)') // 'outer'
28
+ */
29
+ export declare function extractFactoryFromCall(expr: string): string;
30
+ /**
31
+ * Check if an expression has generic type parameters.
32
+ *
33
+ * @example
34
+ * hasGenericTypeParams('Factory<T>') // true
35
+ * hasGenericTypeParams('Factory<T>()') // true
36
+ * hasGenericTypeParams('Factory') // false
37
+ * hasGenericTypeParams('Factory()') // false
38
+ */
39
+ export declare function hasGenericTypeParams(expr: string): boolean;
40
+ /**
41
+ * Extract the base factory name without generic type parameters or call parentheses.
42
+ *
43
+ * @example
44
+ * extractBaseFactoryName('Factory<T>()') // 'Factory'
45
+ * extractBaseFactoryName('Factory<T>') // 'Factory'
46
+ * extractBaseFactoryName('Factory()') // 'Factory'
47
+ * extractBaseFactoryName('Factory') // 'Factory'
48
+ */
49
+ export declare function extractBaseFactoryName(expr: string): string;
50
+ /**
51
+ * Normalize a factory expression to ensure it's callable.
52
+ * Adds `()` if expression has generic params but no call parens.
53
+ *
54
+ * @example
55
+ * normalizeFactoryCall('Factory<T>') // 'Factory<T>()'
56
+ * normalizeFactoryCall('Factory<T>()') // 'Factory<T>()'
57
+ * normalizeFactoryCall('Factory') // 'Factory'
58
+ */
59
+ export declare function normalizeFactoryCall(expr: string): string;