macroforge 0.1.33 → 0.1.34

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/index.d.ts CHANGED
@@ -1,204 +1,1138 @@
1
1
  /* auto-generated by NAPI-RS */
2
2
  /* eslint-disable */
3
+ /**
4
+ * Wrapper around `NativePositionMapper` for NAPI compatibility.
5
+ *
6
+ * This provides the same functionality as `NativePositionMapper` but with a
7
+ * different JavaScript class name. Used internally by [`NativePlugin::get_mapper`].
8
+ */
3
9
  export declare class NativeMapper {
10
+ /**
11
+ * Creates a new mapper wrapping the given source mapping.
12
+ *
13
+ * # Arguments
14
+ *
15
+ * * `mapping` - The source mapping result from macro expansion
16
+ */
4
17
  constructor(mapping: SourceMappingResult)
18
+ /** Checks if this mapper has no mapping data. */
5
19
  isEmpty(): boolean
20
+ /**
21
+ * Converts a position in the original source to expanded source.
22
+ * See [`NativePositionMapper::original_to_expanded`] for details.
23
+ */
6
24
  originalToExpanded(pos: number): number
25
+ /**
26
+ * Converts a position in the expanded source back to original.
27
+ * See [`NativePositionMapper::expanded_to_original`] for details.
28
+ */
7
29
  expandedToOriginal(pos: number): number | null
30
+ /**
31
+ * Returns the name of the macro that generated code at the given position.
32
+ * See [`NativePositionMapper::generated_by`] for details.
33
+ */
8
34
  generatedBy(pos: number): string | null
35
+ /**
36
+ * Maps a span from expanded source to original source.
37
+ * See [`NativePositionMapper::map_span_to_original`] for details.
38
+ */
9
39
  mapSpanToOriginal(start: number, length: number): SpanResult | null
40
+ /**
41
+ * Maps a span from original source to expanded source.
42
+ * See [`NativePositionMapper::map_span_to_expanded`] for details.
43
+ */
10
44
  mapSpanToExpanded(start: number, length: number): SpanResult
45
+ /**
46
+ * Checks if a position is inside macro-generated code.
47
+ * See [`NativePositionMapper::is_in_generated`] for details.
48
+ */
11
49
  isInGenerated(pos: number): boolean
12
50
  }
13
51
 
52
+ /**
53
+ * The main plugin class for macro expansion with caching support.
54
+ *
55
+ * `NativePlugin` is designed to be instantiated once and reused across multiple
56
+ * file processing operations. It maintains a cache of expansion results keyed
57
+ * by filepath and version, enabling efficient incremental processing.
58
+ *
59
+ * # Thread Safety
60
+ *
61
+ * The plugin is thread-safe through the use of `Mutex` for internal state.
62
+ * However, macro expansion itself runs in a separate thread with a 32MB stack
63
+ * to prevent stack overflow during deep AST recursion.
64
+ *
65
+ * # Example
66
+ *
67
+ * ```javascript
68
+ * // Create a single plugin instance (typically at startup)
69
+ * const plugin = new NativePlugin();
70
+ *
71
+ * // Process files with caching
72
+ * const result1 = plugin.process_file("src/foo.ts", code1, { version: "1" });
73
+ * const result2 = plugin.process_file("src/foo.ts", code2, { version: "1" }); // Cache hit!
74
+ * const result3 = plugin.process_file("src/foo.ts", code3, { version: "2" }); // Cache miss
75
+ *
76
+ * // Get a mapper for position translation
77
+ * const mapper = plugin.get_mapper("src/foo.ts");
78
+ * ```
79
+ */
14
80
  export declare class NativePlugin {
81
+ /**
82
+ * Creates a new `NativePlugin` instance.
83
+ *
84
+ * Initializes the plugin with an empty cache and sets up a default log file
85
+ * at `/tmp/macroforge-plugin.log` for debugging purposes.
86
+ *
87
+ * # Returns
88
+ *
89
+ * A new `NativePlugin` ready for processing files.
90
+ *
91
+ * # Side Effects
92
+ *
93
+ * Creates or clears the log file at `/tmp/macroforge-plugin.log`.
94
+ */
15
95
  constructor()
96
+ /**
97
+ * Writes a message to the plugin's log file.
98
+ *
99
+ * Useful for debugging macro expansion issues in production environments.
100
+ *
101
+ * # Arguments
102
+ *
103
+ * * `message` - The message to log
104
+ *
105
+ * # Note
106
+ *
107
+ * Messages are appended to the log file. If the log file hasn't been
108
+ * configured or cannot be written to, the message is silently dropped.
109
+ */
16
110
  log(message: string): void
111
+ /**
112
+ * Sets the path for the plugin's log file.
113
+ *
114
+ * # Arguments
115
+ *
116
+ * * `path` - The file path to use for logging
117
+ *
118
+ * # Note
119
+ *
120
+ * This does not create the file; it will be created when the first
121
+ * message is logged.
122
+ */
17
123
  setLogFile(path: string): void
124
+ /**
125
+ * Processes a TypeScript file through the macro expansion system.
126
+ *
127
+ * This is the main entry point for file processing. It handles caching,
128
+ * thread isolation (to prevent stack overflow), and error recovery.
129
+ *
130
+ * # Arguments
131
+ *
132
+ * * `_env` - NAPI environment (unused but required by NAPI)
133
+ * * `filepath` - Path to the file (used for TSX detection and caching)
134
+ * * `code` - The TypeScript source code to process
135
+ * * `options` - Optional configuration for expansion and caching
136
+ *
137
+ * # Returns
138
+ *
139
+ * An [`ExpandResult`] containing the expanded code, diagnostics, and source mapping.
140
+ *
141
+ * # Errors
142
+ *
143
+ * Returns an error if:
144
+ * - Thread spawning fails
145
+ * - The worker thread panics (often due to stack overflow)
146
+ * - Macro expansion fails internally
147
+ *
148
+ * # Performance
149
+ *
150
+ * - Uses a 32MB thread stack to prevent stack overflow during deep AST recursion
151
+ * - Caches results by filepath and version for efficient incremental processing
152
+ * - Early bailout for files without `@derive` decorators
153
+ *
154
+ * # Thread Safety
155
+ *
156
+ * Macro expansion runs in a separate thread because:
157
+ * 1. SWC AST operations can be deeply recursive, exceeding default stack limits
158
+ * 2. Node.js thread stack is typically only 2MB
159
+ * 3. Panics in the worker thread are caught and reported gracefully
160
+ */
18
161
  processFile(filepath: string, code: string, options?: ProcessFileOptions | undefined | null): ExpandResult
162
+ /**
163
+ * Retrieves a position mapper for a previously processed file.
164
+ *
165
+ * The mapper enables translation between original and expanded source positions,
166
+ * which is essential for IDE features like error reporting and navigation.
167
+ *
168
+ * # Arguments
169
+ *
170
+ * * `filepath` - Path to the file (must have been previously processed)
171
+ *
172
+ * # Returns
173
+ *
174
+ * `Some(NativeMapper)` if the file has been processed and has source mapping data,
175
+ * `None` if the file hasn't been processed or has no mapping (no macros expanded).
176
+ */
19
177
  getMapper(filepath: string): NativeMapper | null
178
+ /**
179
+ * Maps diagnostics from expanded source positions back to original source positions.
180
+ *
181
+ * This is used by IDE integrations to show errors at the correct locations
182
+ * in the user's original code, rather than in the macro-expanded output.
183
+ *
184
+ * # Arguments
185
+ *
186
+ * * `filepath` - Path to the file the diagnostics are for
187
+ * * `diags` - Diagnostics with positions in the expanded source
188
+ *
189
+ * # Returns
190
+ *
191
+ * Diagnostics with positions mapped back to the original source.
192
+ * If no mapper is available for the file, returns diagnostics unchanged.
193
+ */
20
194
  mapDiagnostics(filepath: string, diags: Array<JsDiagnostic>): Array<JsDiagnostic>
21
195
  }
22
196
 
197
+ /**
198
+ * Bidirectional position mapper for translating between original and expanded source positions.
199
+ *
200
+ * This mapper enables IDE features like error reporting, go-to-definition, and hover
201
+ * to work correctly with macro-expanded code by translating positions between the
202
+ * original source (what the user wrote) and the expanded source (what the compiler sees).
203
+ *
204
+ * # Performance
205
+ *
206
+ * Position lookups use binary search for O(log n) complexity, where n is the number
207
+ * of mapping segments. This is critical for responsive IDE interactions.
208
+ *
209
+ * # Example
210
+ *
211
+ * ```javascript
212
+ * const mapper = new PositionMapper(sourceMapping);
213
+ *
214
+ * // Convert original position to expanded
215
+ * const expandedPos = mapper.original_to_expanded(42);
216
+ *
217
+ * // Convert expanded position back to original (if not in generated code)
218
+ * const originalPos = mapper.expanded_to_original(100);
219
+ *
220
+ * // Check if a position is in macro-generated code
221
+ * if (mapper.is_in_generated(pos)) {
222
+ * const macro = mapper.generated_by(pos); // e.g., "Debug"
223
+ * }
224
+ * ```
225
+ */
23
226
  export declare class PositionMapper {
227
+ /**
228
+ * Creates a new position mapper from source mapping data.
229
+ *
230
+ * # Arguments
231
+ *
232
+ * * `mapping` - The source mapping result from macro expansion
233
+ *
234
+ * # Returns
235
+ *
236
+ * A new `NativePositionMapper` ready for position translation.
237
+ */
24
238
  constructor(mapping: SourceMappingResult)
239
+ /**
240
+ * Checks if this mapper has no mapping data.
241
+ *
242
+ * An empty mapper indicates no transformations occurred, so position
243
+ * translation is an identity operation.
244
+ *
245
+ * # Returns
246
+ *
247
+ * `true` if there are no segments and no generated regions.
248
+ */
25
249
  isEmpty(): boolean
250
+ /**
251
+ * Converts a position in the original source to the corresponding position in expanded source.
252
+ *
253
+ * Uses binary search for O(log n) lookup performance.
254
+ *
255
+ * # Arguments
256
+ *
257
+ * * `pos` - Byte offset in the original source
258
+ *
259
+ * # Returns
260
+ *
261
+ * The corresponding byte offset in the expanded source. If the position falls
262
+ * in a gap between segments, returns the position unchanged. If after the last
263
+ * segment, extrapolates based on the delta.
264
+ *
265
+ * # Algorithm
266
+ *
267
+ * 1. Binary search to find the segment containing or after `pos`
268
+ * 2. If inside a segment, compute offset within segment and translate
269
+ * 3. If after all segments, extrapolate from the last segment
270
+ * 4. Otherwise, return position unchanged (gap or before first segment)
271
+ */
26
272
  originalToExpanded(pos: number): number
273
+ /**
274
+ * Converts a position in the expanded source back to the original source position.
275
+ *
276
+ * Returns `None` if the position is inside macro-generated code that has no
277
+ * corresponding location in the original source.
278
+ *
279
+ * # Arguments
280
+ *
281
+ * * `pos` - Byte offset in the expanded source
282
+ *
283
+ * # Returns
284
+ *
285
+ * `Some(original_pos)` if the position maps to original code,
286
+ * `None` if the position is in macro-generated code.
287
+ */
27
288
  expandedToOriginal(pos: number): number | null
289
+ /**
290
+ * Returns the name of the macro that generated code at the given position.
291
+ *
292
+ * # Arguments
293
+ *
294
+ * * `pos` - Byte offset in the expanded source
295
+ *
296
+ * # Returns
297
+ *
298
+ * `Some(macro_name)` if the position is inside generated code (e.g., "Debug"),
299
+ * `None` if the position is in original (non-generated) code.
300
+ */
28
301
  generatedBy(pos: number): string | null
302
+ /**
303
+ * Maps a span (start + length) from expanded source to original source.
304
+ *
305
+ * # Arguments
306
+ *
307
+ * * `start` - Start byte offset in expanded source
308
+ * * `length` - Length of the span in bytes
309
+ *
310
+ * # Returns
311
+ *
312
+ * `Some(SpanResult)` with the mapped span in original source,
313
+ * `None` if either endpoint is in generated code.
314
+ */
29
315
  mapSpanToOriginal(start: number, length: number): SpanResult | null
316
+ /**
317
+ * Maps a span (start + length) from original source to expanded source.
318
+ *
319
+ * This always succeeds since every original position has an expanded equivalent.
320
+ *
321
+ * # Arguments
322
+ *
323
+ * * `start` - Start byte offset in original source
324
+ * * `length` - Length of the span in bytes
325
+ *
326
+ * # Returns
327
+ *
328
+ * A `SpanResult` with the mapped span in expanded source.
329
+ */
30
330
  mapSpanToExpanded(start: number, length: number): SpanResult
331
+ /**
332
+ * Checks if a position is inside macro-generated code.
333
+ *
334
+ * # Arguments
335
+ *
336
+ * * `pos` - Byte offset in the expanded source
337
+ *
338
+ * # Returns
339
+ *
340
+ * `true` if the position is inside a generated region, `false` otherwise.
341
+ */
31
342
  isInGenerated(pos: number): boolean
32
343
  }
33
344
  export type NativePositionMapper = PositionMapper
34
345
 
346
+ /**
347
+ * Returns debug information about all registered macro descriptors (debug API).
348
+ *
349
+ * This provides low-level access to the inventory-based macro registration
350
+ * system for debugging purposes.
351
+ *
352
+ * # Returns
353
+ *
354
+ * A vector of strings describing each registered macro descriptor.
355
+ */
35
356
  export declare function __macroforgeDebugDescriptors(): Array<string>
36
357
 
358
+ /**
359
+ * Returns all registered macro module names (debug API).
360
+ *
361
+ * Modules group related macros together (e.g., "builtin", "serde").
362
+ *
363
+ * # Returns
364
+ *
365
+ * A vector of module names.
366
+ */
37
367
  export declare function __macroforgeDebugGetModules(): Array<string>
38
368
 
369
+ /**
370
+ * Looks up a macro by module and name (debug API).
371
+ *
372
+ * Useful for testing macro registration and debugging lookup issues.
373
+ *
374
+ * # Arguments
375
+ *
376
+ * * `module` - The module name (e.g., "builtin")
377
+ * * `name` - The macro name (e.g., "Debug")
378
+ *
379
+ * # Returns
380
+ *
381
+ * A string describing whether the macro was found or not.
382
+ */
39
383
  export declare function __macroforgeDebugLookup(module: string, name: string): string
40
384
 
385
+ /**
386
+ * Returns the names of all registered macros.
387
+ *
388
+ * # Returns
389
+ *
390
+ * A vector of macro names (e.g., `["Debug", "Clone", "Serialize"]`).
391
+ */
41
392
  export declare function __macroforgeGetMacroNames(): Array<string>
42
393
 
394
+ /**
395
+ * Returns the complete manifest of all registered macros and decorators.
396
+ *
397
+ * This is a debug/introspection API that allows tooling to discover
398
+ * what macros are available at runtime.
399
+ *
400
+ * # Returns
401
+ *
402
+ * A [`MacroManifest`] containing all registered macros and decorators.
403
+ *
404
+ * # Example (JavaScript)
405
+ *
406
+ * ```javascript
407
+ * const manifest = __macroforgeGetManifest();
408
+ * console.log("Available macros:", manifest.macros.map(m => m.name));
409
+ * // ["Debug", "Clone", "PartialEq", "Hash", "Serialize", "Deserialize", ...]
410
+ * ```
411
+ */
43
412
  export declare function __macroforgeGetManifest(): MacroManifest
44
413
 
414
+ /**
415
+ * Checks if any macros are registered in this package.
416
+ *
417
+ * Useful for build tools to determine if macro expansion is needed.
418
+ *
419
+ * # Returns
420
+ *
421
+ * `true` if at least one macro is registered, `false` otherwise.
422
+ */
45
423
  export declare function __macroforgeIsMacroPackage(): boolean
46
424
 
47
425
  /**
48
- * r" Run this macro with the given context
49
- * r" Called by the TS plugin to execute macro expansion
426
+ * r" Run this macro with the given context.
427
+ * r"
428
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
429
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
430
+ * r" and serializes the result back to JSON for the TypeScript plugin.
431
+ * r"
432
+ * r" # Arguments
433
+ * r"
434
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
435
+ * r" - `target_source`: The TypeScript source code to transform
436
+ * r" - `file_name`: The source file path for error reporting
437
+ * r" - Additional context metadata
438
+ * r"
439
+ * r" # Returns
440
+ * r"
441
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
442
+ * r" or any diagnostic errors.
443
+ * r"
444
+ * r" # Errors
445
+ * r"
446
+ * r" Returns a NAPI error if:
447
+ * r" - The input JSON cannot be parsed
448
+ * r" - The `TsStream` cannot be created from the context
449
+ * r" - The result cannot be serialized to JSON
50
450
  */
51
451
  export declare function __macroforgeRunClone(contextJson: string): string
52
452
 
53
453
  /**
54
- * r" Run this macro with the given context
55
- * r" Called by the TS plugin to execute macro expansion
454
+ * r" Run this macro with the given context.
455
+ * r"
456
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
457
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
458
+ * r" and serializes the result back to JSON for the TypeScript plugin.
459
+ * r"
460
+ * r" # Arguments
461
+ * r"
462
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
463
+ * r" - `target_source`: The TypeScript source code to transform
464
+ * r" - `file_name`: The source file path for error reporting
465
+ * r" - Additional context metadata
466
+ * r"
467
+ * r" # Returns
468
+ * r"
469
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
470
+ * r" or any diagnostic errors.
471
+ * r"
472
+ * r" # Errors
473
+ * r"
474
+ * r" Returns a NAPI error if:
475
+ * r" - The input JSON cannot be parsed
476
+ * r" - The `TsStream` cannot be created from the context
477
+ * r" - The result cannot be serialized to JSON
56
478
  */
57
479
  export declare function __macroforgeRunDebug(contextJson: string): string
58
480
 
59
481
  /**
60
- * r" Run this macro with the given context
61
- * r" Called by the TS plugin to execute macro expansion
482
+ * r" Run this macro with the given context.
483
+ * r"
484
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
485
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
486
+ * r" and serializes the result back to JSON for the TypeScript plugin.
487
+ * r"
488
+ * r" # Arguments
489
+ * r"
490
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
491
+ * r" - `target_source`: The TypeScript source code to transform
492
+ * r" - `file_name`: The source file path for error reporting
493
+ * r" - Additional context metadata
494
+ * r"
495
+ * r" # Returns
496
+ * r"
497
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
498
+ * r" or any diagnostic errors.
499
+ * r"
500
+ * r" # Errors
501
+ * r"
502
+ * r" Returns a NAPI error if:
503
+ * r" - The input JSON cannot be parsed
504
+ * r" - The `TsStream` cannot be created from the context
505
+ * r" - The result cannot be serialized to JSON
62
506
  */
63
507
  export declare function __macroforgeRunDefault(contextJson: string): string
64
508
 
65
509
  /**
66
- * r" Run this macro with the given context
67
- * r" Called by the TS plugin to execute macro expansion
510
+ * r" Run this macro with the given context.
511
+ * r"
512
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
513
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
514
+ * r" and serializes the result back to JSON for the TypeScript plugin.
515
+ * r"
516
+ * r" # Arguments
517
+ * r"
518
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
519
+ * r" - `target_source`: The TypeScript source code to transform
520
+ * r" - `file_name`: The source file path for error reporting
521
+ * r" - Additional context metadata
522
+ * r"
523
+ * r" # Returns
524
+ * r"
525
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
526
+ * r" or any diagnostic errors.
527
+ * r"
528
+ * r" # Errors
529
+ * r"
530
+ * r" Returns a NAPI error if:
531
+ * r" - The input JSON cannot be parsed
532
+ * r" - The `TsStream` cannot be created from the context
533
+ * r" - The result cannot be serialized to JSON
68
534
  */
69
535
  export declare function __macroforgeRunDeserialize(contextJson: string): string
70
536
 
71
537
  /**
72
- * r" Run this macro with the given context
73
- * r" Called by the TS plugin to execute macro expansion
538
+ * r" Run this macro with the given context.
539
+ * r"
540
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
541
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
542
+ * r" and serializes the result back to JSON for the TypeScript plugin.
543
+ * r"
544
+ * r" # Arguments
545
+ * r"
546
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
547
+ * r" - `target_source`: The TypeScript source code to transform
548
+ * r" - `file_name`: The source file path for error reporting
549
+ * r" - Additional context metadata
550
+ * r"
551
+ * r" # Returns
552
+ * r"
553
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
554
+ * r" or any diagnostic errors.
555
+ * r"
556
+ * r" # Errors
557
+ * r"
558
+ * r" Returns a NAPI error if:
559
+ * r" - The input JSON cannot be parsed
560
+ * r" - The `TsStream` cannot be created from the context
561
+ * r" - The result cannot be serialized to JSON
74
562
  */
75
563
  export declare function __macroforgeRunHash(contextJson: string): string
76
564
 
77
565
  /**
78
- * r" Run this macro with the given context
79
- * r" Called by the TS plugin to execute macro expansion
566
+ * r" Run this macro with the given context.
567
+ * r"
568
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
569
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
570
+ * r" and serializes the result back to JSON for the TypeScript plugin.
571
+ * r"
572
+ * r" # Arguments
573
+ * r"
574
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
575
+ * r" - `target_source`: The TypeScript source code to transform
576
+ * r" - `file_name`: The source file path for error reporting
577
+ * r" - Additional context metadata
578
+ * r"
579
+ * r" # Returns
580
+ * r"
581
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
582
+ * r" or any diagnostic errors.
583
+ * r"
584
+ * r" # Errors
585
+ * r"
586
+ * r" Returns a NAPI error if:
587
+ * r" - The input JSON cannot be parsed
588
+ * r" - The `TsStream` cannot be created from the context
589
+ * r" - The result cannot be serialized to JSON
80
590
  */
81
591
  export declare function __macroforgeRunOrd(contextJson: string): string
82
592
 
83
593
  /**
84
- * r" Run this macro with the given context
85
- * r" Called by the TS plugin to execute macro expansion
594
+ * r" Run this macro with the given context.
595
+ * r"
596
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
597
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
598
+ * r" and serializes the result back to JSON for the TypeScript plugin.
599
+ * r"
600
+ * r" # Arguments
601
+ * r"
602
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
603
+ * r" - `target_source`: The TypeScript source code to transform
604
+ * r" - `file_name`: The source file path for error reporting
605
+ * r" - Additional context metadata
606
+ * r"
607
+ * r" # Returns
608
+ * r"
609
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
610
+ * r" or any diagnostic errors.
611
+ * r"
612
+ * r" # Errors
613
+ * r"
614
+ * r" Returns a NAPI error if:
615
+ * r" - The input JSON cannot be parsed
616
+ * r" - The `TsStream` cannot be created from the context
617
+ * r" - The result cannot be serialized to JSON
86
618
  */
87
619
  export declare function __macroforgeRunPartialEq(contextJson: string): string
88
620
 
89
621
  /**
90
- * r" Run this macro with the given context
91
- * r" Called by the TS plugin to execute macro expansion
622
+ * r" Run this macro with the given context.
623
+ * r"
624
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
625
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
626
+ * r" and serializes the result back to JSON for the TypeScript plugin.
627
+ * r"
628
+ * r" # Arguments
629
+ * r"
630
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
631
+ * r" - `target_source`: The TypeScript source code to transform
632
+ * r" - `file_name`: The source file path for error reporting
633
+ * r" - Additional context metadata
634
+ * r"
635
+ * r" # Returns
636
+ * r"
637
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
638
+ * r" or any diagnostic errors.
639
+ * r"
640
+ * r" # Errors
641
+ * r"
642
+ * r" Returns a NAPI error if:
643
+ * r" - The input JSON cannot be parsed
644
+ * r" - The `TsStream` cannot be created from the context
645
+ * r" - The result cannot be serialized to JSON
92
646
  */
93
647
  export declare function __macroforgeRunPartialOrd(contextJson: string): string
94
648
 
95
649
  /**
96
- * r" Run this macro with the given context
97
- * r" Called by the TS plugin to execute macro expansion
650
+ * r" Run this macro with the given context.
651
+ * r"
652
+ * r" This function is automatically generated and exposed to JavaScript via NAPI.
653
+ * r" It deserializes the macro context from JSON, executes the macro transformation,
654
+ * r" and serializes the result back to JSON for the TypeScript plugin.
655
+ * r"
656
+ * r" # Arguments
657
+ * r"
658
+ * r" * `context_json` - A JSON string containing the [`MacroContextIR`] with:
659
+ * r" - `target_source`: The TypeScript source code to transform
660
+ * r" - `file_name`: The source file path for error reporting
661
+ * r" - Additional context metadata
662
+ * r"
663
+ * r" # Returns
664
+ * r"
665
+ * r" A JSON string containing the [`MacroResult`] with the transformed code
666
+ * r" or any diagnostic errors.
667
+ * r"
668
+ * r" # Errors
669
+ * r"
670
+ * r" Returns a NAPI error if:
671
+ * r" - The input JSON cannot be parsed
672
+ * r" - The `TsStream` cannot be created from the context
673
+ * r" - The result cannot be serialized to JSON
98
674
  */
99
675
  export declare function __macroforgeRunSerialize(contextJson: string): string
100
676
 
677
+ /**
678
+ * Checks if the given TypeScript code has valid syntax.
679
+ *
680
+ * This function attempts to parse the code using SWC's TypeScript parser
681
+ * without performing any macro expansion.
682
+ *
683
+ * # Arguments
684
+ *
685
+ * * `code` - The TypeScript source code to check
686
+ * * `filepath` - The file path (used to determine if it's TSX based on extension)
687
+ *
688
+ * # Returns
689
+ *
690
+ * A [`SyntaxCheckResult`] indicating success or containing the parse error.
691
+ *
692
+ * # Example
693
+ *
694
+ * ```javascript
695
+ * const result = check_syntax("const x: number = 42;", "test.ts");
696
+ * if (!result.ok) {
697
+ * console.error("Syntax error:", result.error);
698
+ * }
699
+ * ```
700
+ */
101
701
  export declare function checkSyntax(code: string, filepath: string): SyntaxCheckResult
102
702
 
703
+ /**
704
+ * Entry for a registered decorator in the manifest.
705
+ *
706
+ * Used by [`MacroManifest`] to describe field-level decorators
707
+ * that can be used with macros.
708
+ */
103
709
  export interface DecoratorManifestEntry {
710
+ /** The module this decorator belongs to (e.g., "serde"). */
104
711
  module: string
712
+ /** The exported name of the decorator (e.g., "skip", "rename"). */
105
713
  export: string
714
+ /** The decorator kind: "class", "property", "method", "accessor", "parameter". */
106
715
  kind: string
716
+ /** Documentation string for the decorator. */
107
717
  docs: string
108
718
  }
109
719
 
720
+ /**
721
+ * The `@Derive` decorator function exported to JavaScript/TypeScript.
722
+ *
723
+ * This is a no-op function that exists purely for TypeScript type checking.
724
+ * The actual decorator processing happens during macro expansion, where
725
+ * `@derive(...)` decorators are recognized and transformed.
726
+ *
727
+ * # TypeScript Usage
728
+ *
729
+ * ```typescript
730
+ * import { Derive } from "macroforge-ts";
731
+ *
732
+ * @Derive(Debug, Clone, Serialize)
733
+ * class User {
734
+ * name: string;
735
+ * email: string;
736
+ * }
737
+ * ```
738
+ */
110
739
  export declare function Derive(...features: any[]): ClassDecorator
111
740
 
741
+ /**
742
+ * Options for macro expansion.
743
+ *
744
+ * Used by [`expand_sync`] to configure expansion behavior.
745
+ */
112
746
  export interface ExpandOptions {
747
+ /**
748
+ * If `true`, preserves `@derive` decorators in the output.
749
+ * If `false` (default), decorators are stripped after expansion.
750
+ */
113
751
  keepDecorators?: boolean
114
752
  }
115
753
 
754
+ /**
755
+ * Result of expanding macros in TypeScript source code.
756
+ *
757
+ * This is the primary return type for macro expansion operations,
758
+ * containing the expanded code, diagnostics, and source mapping.
759
+ *
760
+ * # Example
761
+ *
762
+ * ```ignore
763
+ * let result = expand_sync(env, code, filepath, None)?;
764
+ * if result.diagnostics.iter().any(|d| d.level == "error") {
765
+ * // Handle errors
766
+ * }
767
+ * // Use result.code for the expanded source
768
+ * ```
769
+ */
116
770
  export interface ExpandResult {
771
+ /** The expanded TypeScript code with all macros processed. */
117
772
  code: string
773
+ /** Optional TypeScript type declarations for generated methods. */
118
774
  types?: string
775
+ /** Optional JSON metadata about processed classes. */
119
776
  metadata?: string
777
+ /** Diagnostics (errors, warnings, info) from the expansion process. */
120
778
  diagnostics: Array<MacroDiagnostic>
779
+ /** Source mapping for position translation between original and expanded code. */
121
780
  sourceMapping?: SourceMappingResult
122
781
  }
123
782
 
124
- /** Expand macros in TypeScript code and return the transformed TS (types) and diagnostics */
783
+ /**
784
+ * Synchronously expands macros in TypeScript code.
785
+ *
786
+ * This is the standalone macro expansion function that doesn't use caching.
787
+ * For cached expansion, use [`NativePlugin::process_file`] instead.
788
+ *
789
+ * # Arguments
790
+ *
791
+ * * `_env` - NAPI environment (unused but required by NAPI)
792
+ * * `code` - The TypeScript source code to expand
793
+ * * `filepath` - The file path (used for TSX detection)
794
+ * * `options` - Optional configuration (e.g., `keep_decorators`)
795
+ *
796
+ * # Returns
797
+ *
798
+ * An [`ExpandResult`] containing the expanded code, diagnostics, and source mapping.
799
+ *
800
+ * # Errors
801
+ *
802
+ * Returns an error if:
803
+ * - Thread spawning fails
804
+ * - The worker thread panics
805
+ * - Macro host initialization fails
806
+ *
807
+ * # Performance
808
+ *
809
+ * - Uses a 32MB thread stack to prevent stack overflow
810
+ * - Performs early bailout for files without `@derive` decorators
811
+ *
812
+ * # Example
813
+ *
814
+ * ```javascript
815
+ * const result = expand_sync(env, code, "user.ts", { keep_decorators: false });
816
+ * console.log(result.code); // Expanded TypeScript code
817
+ * console.log(result.diagnostics); // Any warnings or errors
818
+ * ```
819
+ */
125
820
  export declare function expandSync(code: string, filepath: string, options?: ExpandOptions | undefined | null): ExpandResult
126
821
 
822
+ /**
823
+ * A region in the expanded source that was generated by a macro.
824
+ *
825
+ * These regions identify code that has no corresponding location in the
826
+ * original source because it was synthesized by a macro.
827
+ *
828
+ * # Example
829
+ *
830
+ * For a `@derive(Debug)` macro that generates a `toString()` method,
831
+ * a `GeneratedRegionResult` would mark the entire method body as generated
832
+ * with `source_macro = "Debug"`.
833
+ */
127
834
  export interface GeneratedRegionResult {
835
+ /** Byte offset where the generated region starts in the expanded source. */
128
836
  start: number
837
+ /** Byte offset where the generated region ends in the expanded source. */
129
838
  end: number
839
+ /** Name of the macro that generated this region (e.g., "Debug", "Clone"). */
130
840
  sourceMacro: string
131
841
  }
132
842
 
843
+ /**
844
+ * Information about an imported identifier from a TypeScript module.
845
+ *
846
+ * Used to track where decorators and macro-related imports come from.
847
+ */
133
848
  export interface ImportSourceResult {
134
- /** Local identifier name in the import statement */
849
+ /** Local identifier name in the import statement (e.g., `Derive` in `import { Derive }`). */
135
850
  local: string
136
- /** Module specifier this identifier was imported from */
851
+ /** Module specifier this identifier was imported from (e.g., `"macroforge-ts"`). */
137
852
  module: string
138
853
  }
139
854
 
855
+ /**
856
+ * A diagnostic from the TypeScript/JavaScript compiler or IDE.
857
+ *
858
+ * This structure mirrors TypeScript's diagnostic format for interoperability
859
+ * with language servers and IDEs.
860
+ */
140
861
  export interface JsDiagnostic {
862
+ /** Byte offset where the diagnostic starts. `None` for global diagnostics. */
141
863
  start?: number
864
+ /** Length of the diagnostic span in bytes. */
142
865
  length?: number
866
+ /** Human-readable diagnostic message. */
143
867
  message?: string
868
+ /** TypeScript diagnostic code (e.g., 2304 for "Cannot find name"). */
144
869
  code?: number
870
+ /** Diagnostic category: "error", "warning", "suggestion", "message". */
145
871
  category?: string
146
872
  }
147
873
 
874
+ /**
875
+ * A diagnostic message produced during macro expansion.
876
+ *
877
+ * Diagnostics can represent errors, warnings, or informational messages
878
+ * that occurred during the macro expansion process.
879
+ *
880
+ * # Fields
881
+ *
882
+ * * `level` - Severity level: "error", "warning", or "info"
883
+ * * `message` - Human-readable description of the issue
884
+ * * `start` - Optional byte offset where the issue starts in the source
885
+ * * `end` - Optional byte offset where the issue ends in the source
886
+ *
887
+ * # Example
888
+ *
889
+ * ```ignore
890
+ * MacroDiagnostic {
891
+ * level: "error".to_string(),
892
+ * message: "Unknown macro 'Foo'".to_string(),
893
+ * start: Some(42),
894
+ * end: Some(45),
895
+ * }
896
+ * ```
897
+ */
148
898
  export interface MacroDiagnostic {
899
+ /**
900
+ * Severity level of the diagnostic.
901
+ * One of: "error", "warning", "info".
902
+ */
149
903
  level: string
904
+ /** Human-readable message describing the diagnostic. */
150
905
  message: string
906
+ /**
907
+ * Byte offset in the original source where the issue starts.
908
+ * `None` if the diagnostic is not associated with a specific location.
909
+ */
151
910
  start?: number
911
+ /**
912
+ * Byte offset in the original source where the issue ends.
913
+ * `None` if the diagnostic is not associated with a specific location.
914
+ */
152
915
  end?: number
153
916
  }
154
917
 
918
+ /**
919
+ * Complete manifest of all available macros and decorators.
920
+ *
921
+ * This is returned by [`get_macro_manifest`] and is useful for:
922
+ * - IDE autocompletion
923
+ * - Documentation generation
924
+ * - Tooling integration
925
+ */
155
926
  export interface MacroManifest {
927
+ /** ABI version for compatibility checking. */
156
928
  version: number
929
+ /** All registered macros (derive, attribute, function). */
157
930
  macros: Array<MacroManifestEntry>
931
+ /** All registered field/class decorators. */
158
932
  decorators: Array<DecoratorManifestEntry>
159
933
  }
160
934
 
935
+ /**
936
+ * Entry for a registered macro in the manifest.
937
+ *
938
+ * Used by [`MacroManifest`] to describe available macros to tooling
939
+ * such as IDE extensions and documentation generators.
940
+ */
161
941
  export interface MacroManifestEntry {
942
+ /** The macro name (e.g., "Debug", "Clone", "Serialize"). */
162
943
  name: string
944
+ /** The macro kind: "derive", "attribute", or "function". */
163
945
  kind: string
946
+ /** Human-readable description of what the macro does. */
164
947
  description: string
948
+ /** The package that provides this macro (e.g., "macroforge-ts"). */
165
949
  package: string
166
950
  }
167
951
 
952
+ /**
953
+ * A segment mapping a range in the original source to a range in the expanded source.
954
+ *
955
+ * These segments form the core of the bidirectional source mapping system,
956
+ * enabling IDE features like "go to definition" and error reporting to work
957
+ * correctly with macro-expanded code.
958
+ *
959
+ * # Invariants
960
+ *
961
+ * - `original_start < original_end`
962
+ * - `expanded_start < expanded_end`
963
+ * - Segments are non-overlapping and sorted by position
964
+ */
168
965
  export interface MappingSegmentResult {
966
+ /** Byte offset where this segment starts in the original source. */
169
967
  originalStart: number
968
+ /** Byte offset where this segment ends in the original source. */
170
969
  originalEnd: number
970
+ /** Byte offset where this segment starts in the expanded source. */
171
971
  expandedStart: number
972
+ /** Byte offset where this segment ends in the expanded source. */
172
973
  expandedEnd: number
173
974
  }
174
975
 
976
+ /**
977
+ * Parses import statements from TypeScript code and returns their sources.
978
+ *
979
+ * This function extracts information about all import statements in the code,
980
+ * mapping each imported identifier to its source module. Useful for analyzing
981
+ * dependencies and understanding where decorators come from.
982
+ *
983
+ * # Arguments
984
+ *
985
+ * * `code` - The TypeScript source code to parse
986
+ * * `filepath` - The file path (used for TSX detection)
987
+ *
988
+ * # Returns
989
+ *
990
+ * A vector of [`ImportSourceResult`] entries, one for each imported identifier.
991
+ *
992
+ * # Errors
993
+ *
994
+ * Returns an error if the code cannot be parsed.
995
+ *
996
+ * # Example
997
+ *
998
+ * ```javascript
999
+ * // For code: import { Derive, Clone } from "macroforge-ts";
1000
+ * const imports = parse_import_sources(code, "test.ts");
1001
+ * // Returns: [
1002
+ * // { local: "Derive", module: "macroforge-ts" },
1003
+ * // { local: "Clone", module: "macroforge-ts" }
1004
+ * // ]
1005
+ * ```
1006
+ */
175
1007
  export declare function parseImportSources(code: string, filepath: string): Array<ImportSourceResult>
176
1008
 
1009
+ /**
1010
+ * Options for processing a file through the macro system.
1011
+ *
1012
+ * Used by [`NativePlugin::process_file`] to configure expansion behavior
1013
+ * and caching.
1014
+ */
177
1015
  export interface ProcessFileOptions {
1016
+ /**
1017
+ * If `true`, preserves `@derive` decorators in the output.
1018
+ * If `false` (default), decorators are stripped after expansion.
1019
+ */
178
1020
  keepDecorators?: boolean
1021
+ /**
1022
+ * Version string for cache invalidation.
1023
+ * When provided, cached results are only reused if versions match.
1024
+ */
179
1025
  version?: string
180
1026
  }
181
1027
 
1028
+ /**
1029
+ * Complete source mapping information for a macro expansion.
1030
+ *
1031
+ * Contains both preserved segments (original code that wasn't modified)
1032
+ * and generated regions (new code synthesized by macros).
1033
+ *
1034
+ * # Usage
1035
+ *
1036
+ * This mapping enables:
1037
+ * - Converting positions from original source to expanded source and vice versa
1038
+ * - Identifying which macro generated a given piece of code
1039
+ * - Mapping IDE diagnostics from expanded code back to original source
1040
+ */
182
1041
  export interface SourceMappingResult {
1042
+ /**
1043
+ * Segments mapping preserved regions between original and expanded source.
1044
+ * Sorted by position for efficient binary search lookups.
1045
+ */
183
1046
  segments: Array<MappingSegmentResult>
1047
+ /**
1048
+ * Regions in the expanded source that were generated by macros.
1049
+ * Used to identify synthetic code with no original source location.
1050
+ */
184
1051
  generatedRegions: Array<GeneratedRegionResult>
185
1052
  }
186
1053
 
1054
+ /**
1055
+ * A span (range) in source code, represented as start position and length.
1056
+ *
1057
+ * Used for mapping diagnostics and other positional information.
1058
+ */
187
1059
  export interface SpanResult {
1060
+ /** Byte offset where the span starts. */
188
1061
  start: number
1062
+ /** Length of the span in bytes. */
189
1063
  length: number
190
1064
  }
191
1065
 
1066
+ /**
1067
+ * Result of checking TypeScript syntax validity.
1068
+ *
1069
+ * Returned by [`check_syntax`] to indicate whether code parses successfully.
1070
+ */
192
1071
  export interface SyntaxCheckResult {
1072
+ /** `true` if the code parsed without errors, `false` otherwise. */
193
1073
  ok: boolean
1074
+ /** Error message if parsing failed, `None` if successful. */
194
1075
  error?: string
195
1076
  }
196
1077
 
1078
+ /**
1079
+ * Result of transforming TypeScript code through the macro system.
1080
+ *
1081
+ * This struct is returned by [`transform_sync`] and contains the transformed code
1082
+ * along with optional source maps, type declarations, and metadata about processed classes.
1083
+ *
1084
+ * # Fields
1085
+ *
1086
+ * * `code` - The transformed TypeScript/JavaScript code with macros expanded
1087
+ * * `map` - Optional source map for debugging (currently not implemented)
1088
+ * * `types` - Optional TypeScript type declarations for generated methods
1089
+ * * `metadata` - Optional JSON metadata about processed classes
1090
+ */
197
1091
  export interface TransformResult {
1092
+ /** The transformed TypeScript/JavaScript code with all macros expanded. */
198
1093
  code: string
1094
+ /**
1095
+ * Source map for mapping transformed positions back to original.
1096
+ * Currently always `None` - source mapping is handled separately via `SourceMappingResult`.
1097
+ */
199
1098
  map?: string
1099
+ /**
1100
+ * TypeScript type declarations (`.d.ts` content) for generated methods.
1101
+ * Used by IDEs to provide type information for macro-generated code.
1102
+ */
200
1103
  types?: string
1104
+ /**
1105
+ * JSON-serialized metadata about processed classes.
1106
+ * Contains information about which classes were processed and what was generated.
1107
+ */
201
1108
  metadata?: string
202
1109
  }
203
1110
 
1111
+ /**
1112
+ * Synchronously transforms TypeScript code through the macro expansion system.
1113
+ *
1114
+ * This is similar to [`expand_sync`] but returns a [`TransformResult`] which
1115
+ * includes source map information (when available).
1116
+ *
1117
+ * # Arguments
1118
+ *
1119
+ * * `_env` - NAPI environment (unused but required by NAPI)
1120
+ * * `code` - The TypeScript source code to transform
1121
+ * * `filepath` - The file path (used for TSX detection)
1122
+ *
1123
+ * # Returns
1124
+ *
1125
+ * A [`TransformResult`] containing the transformed code and metadata.
1126
+ *
1127
+ * # Errors
1128
+ *
1129
+ * Returns an error if:
1130
+ * - Thread spawning fails
1131
+ * - The worker thread panics
1132
+ * - Macro expansion fails
1133
+ *
1134
+ * # Thread Safety
1135
+ *
1136
+ * Uses a 32MB thread stack to prevent stack overflow during deep AST recursion.
1137
+ */
204
1138
  export declare function transformSync(code: string, filepath: string): TransformResult