macroforge_ts_syn 0.5.0

TypeScript syntax types for build-time macro code generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
//! A `syn`-like stream for TypeScript macro input and output.
//!
//! This module provides a [`TsStream`] type and [`ParseTs`] trait. This is the
//! low-level API; most macro authors will prefer using
//! [`DeriveInput`](crate::DeriveInput) from the [`derive`](crate::derive) module.
//!
//! ## Overview
//!
//! - [`TsStream`] - Source text plus the macro context, imports and patches
//! - [`ParseTs`] - A trait for types that can be parsed from a [`TsStream`]
//!
//! ## Basic Usage
//!
//! Parse snippets into an OXC arena with the crate-level `parse_*` helpers:
//!
//! ```rust
//! use macroforge_ts_syn::{expr_to_string, parse_expr, parse_program, parse_statement, stmt_to_string};
//!
//! let allocator = macroforge_ts_syn::oxc::allocator::Allocator::default();
//! let expr = parse_expr(&allocator, "x + y")?;
//! let stmt = parse_statement(&allocator, "const x = 5;")?;
//! let module = parse_program(&allocator, "export class Foo {}")?;
//!
//! assert_eq!(expr_to_string(&expr), "x + y");
//! assert_eq!(stmt_to_string(&stmt).trim(), "const x = 5;");
//! assert_eq!(module.body.len(), 1);
//! # Ok::<(), macroforge_ts_syn::TsSynError>(())
//! ```
//!
//! ## Macro Context
//!
//! When used within the macro system, [`TsStream`] carries context information
//! about the macro invocation:
//!
//! ```rust,no_run
//! use macroforge_ts_syn::TsStream;
//!
//! fn example(stream: TsStream) {
//!     // In a macro implementation
//!     let ctx = stream.context().expect("macro context");
//!     let decorator_span = ctx.decorator_span;
//!     let _target = &ctx.target;
//! }
//! ```
//!
//! ## Adding Imports
//!
//! [`TsStream`] provides helpers for adding imports that will be inserted
//! at the top of the file:
//!
//! ```rust,no_run
//! use macroforge_ts_syn::{TsStream, TsSynError};
//!
//! fn main() -> Result<(), TsSynError> {
//!     let source = "class Foo {}";
//!     let mut stream = TsStream::new(source, "input.ts")?;
//!
//!     // Add a runtime import
//!     stream.add_import("decode", "./runtime");
//!
//!     // Add a type-only import
//!     stream.add_type_import("Options", "./types");
//!
//!     // Convert to result
//!     let _result = stream.into_result();
//!     Ok(())
//! }
//! ```

use crate::TsSynError;

/// Configuration for a single import to be added to a file.
///
/// Used with [`TsStream::add_imports`] to batch-add multiple imports.
///
/// # Example
///
/// ```rust,ignore
/// use macroforge_ts_syn::ImportConfig;
///
/// const MY_IMPORTS: &[ImportConfig] = &[
///     ImportConfig::value("ok", "__mf_ok", "@macroforge/core/endec"),
///     ImportConfig::type_only("Result", "__mf_Result", "@macroforge/core/endec"),
/// ];
///
/// stream.add_imports(MY_IMPORTS);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct ImportConfig {
    /// The original export name from the source module
    pub name: &'static str,
    /// The alias to use in the generated import (typically `__mf_` prefixed)
    pub alias: &'static str,
    /// The module path to import from
    pub module: &'static str,
    /// Whether this is a type-only import (`import type { ... }`)
    pub is_type: bool,
}

impl ImportConfig {
    /// Create a value import config (runtime import).
    pub const fn value(name: &'static str, alias: &'static str, module: &'static str) -> Self {
        Self {
            name,
            alias,
            module,
            is_type: false,
        }
    }

    /// Create a type-only import config.
    pub const fn type_only(name: &'static str, alias: &'static str, module: &'static str) -> Self {
        Self {
            name,
            alias,
            module,
            is_type: true,
        }
    }
}

/// TypeScript source carrying its macro context, analogous to `syn::parse::ParseBuffer`.
///
/// `TsStream` manages the parsing context for TypeScript source code. It holds:
/// - The source code being parsed
/// - An optional macro context with span information
/// - Accumulated runtime patches (imports, etc.)
///
/// # Creating a TsStream
///
/// ```rust
/// use macroforge_ts_syn::{TsStream, TsSynError};
///
/// fn main() -> Result<(), TsSynError> {
///     // From source code
///     let _stream = TsStream::new("const x = 5;", "input.ts")?;
///
///     // From an owned string
///     let generated_code = "const y = 10;".to_string();
///     let _stream = TsStream::from_string(generated_code);
///     Ok(())
/// }
/// ```
///
/// With macro context (typically used by the host system):
///
/// ```rust,ignore
/// use macroforge_ts_syn::{TsStream, TsSynError, MacroContextIR, SpanIR, ClassIR};
///
/// // MacroContextIR requires a target type - this is typically provided by the host
/// let source = "class Foo {}";
/// let ctx = MacroContextIR::new_derive_class(/* ... */);
/// let stream = TsStream::with_context(source, "input.ts", ctx)?;
/// ```
///
/// # Parsing
///
/// ```rust
/// use macroforge_ts_syn::{TsStream, TsSynError};
///
/// fn main() -> Result<(), TsSynError> {
///     let stream = TsStream::new("const x = 5;", "input.ts")?;
///     let allocator = macroforge_ts_syn::oxc::allocator::Allocator::default();
///     let _stmt = stream.parse_stmt(&allocator)?;
///     Ok(())
/// }
/// ```
///
/// # Working with Imports
///
/// ```rust,ignore
/// use macroforge_ts_syn::{TsStream, TsSynError};
///
/// fn main() -> Result<(), TsSynError> {
///     let source = "class Foo {}";
///     let mut stream = TsStream::new(source, "file.ts")?;
///
///     // These imports will be added to the file
///     stream.add_import("decode", "./runtime");
///     stream.add_type_import("Options", "./types");
///
///     // Get the result with accumulated patches
///     let _result = stream.into_result();
///     Ok(())
/// }
/// ```
pub struct TsStream {
    source: String,
    file_name: String,
    /// Macro context data (decorator span, target span, etc.)
    /// This is populated when TsStream is created by the macro host
    pub ctx: Option<crate::abi::MacroContextIR>,
    /// Runtime patches to apply (e.g., imports at file level)
    pub runtime_patches: Vec<crate::abi::Patch>,
    /// Where this stream's code should be inserted relative to the target.
    /// Defaults to `Below` (after the target declaration).
    pub insert_pos: crate::abi::InsertPos,
    /// Cross-module function suffixes for auto-import resolution.
    /// External macros register suffixes here so the framework can resolve
    /// cross-module references following the `{camelCaseTypeName}{Suffix}` pattern.
    pub cross_module_suffixes: Vec<String>,
    /// Cross-module type suffixes for auto-import resolution.
    /// These resolve `{PascalCaseTypeName}{Suffix}` type references and generate
    /// `import type` statements. Used for types like `ColorsErrors`, `ColorsTainted`.
    pub cross_module_type_suffixes: Vec<String>,
    /// Warnings and notes the macro reports while still producing output.
    diagnostics: Vec<crate::abi::Diagnostic>,
}

impl TsStream {
    /// Parse the first statement of the stream into `allocator`.
    pub fn parse_stmt<'a>(
        &'a self,
        allocator: &'a oxc::allocator::Allocator,
    ) -> Result<oxc::ast::ast::Statement<'a>, TsSynError> {
        let source_type = oxc::span::SourceType::ts()
            .with_typescript(true)
            .with_jsx(self.file_name.ends_with(".tsx"));
        let ret = oxc::parser::Parser::new(allocator, &self.source, source_type).parse();
        if !ret.diagnostics.is_empty() {
            return Err(TsSynError::Parse(format!(
                "parse errors: {:?}",
                ret.diagnostics
            )));
        }

        ret.program
            .body
            .into_iter()
            .next()
            .ok_or_else(|| TsSynError::Parse("No statement found".to_string()))
    }
}

impl TsStream {
    /// Create a new parsing stream from source code.
    pub fn new(source: &str, file_name: &str) -> Result<Self, TsSynError> {
        Ok(TsStream {
            source: source.to_string(),
            file_name: file_name.to_string(),
            ctx: None,
            runtime_patches: vec![],
            insert_pos: crate::abi::InsertPos::default(),
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
            diagnostics: vec![],
        })
    }

    /// Create a new parsing stream from an owned string.
    pub fn from_string(source: String) -> Self {
        TsStream {
            source,
            file_name: "macro_output.ts".to_string(),
            ctx: None,
            runtime_patches: vec![],
            insert_pos: crate::abi::InsertPos::default(),
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
            diagnostics: vec![],
        }
    }

    /// Create a new parsing stream with a specific insert position.
    pub fn with_insert_pos(source: String, insert_pos: crate::abi::InsertPos) -> Self {
        TsStream {
            source,
            file_name: "macro_output.ts".to_string(),
            ctx: None,
            runtime_patches: vec![],
            insert_pos,
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
            diagnostics: vec![],
        }
    }

    /// Get the source code of the stream.
    pub fn source(&self) -> &str {
        &self.source
    }

    /// Takes the source code out of the stream, leaving it empty. The rest of
    /// the stream (patches, suffixes, diagnostics) stays, ready to `merge`.
    pub fn take_source(&mut self) -> String {
        std::mem::take(&mut self.source)
    }

    /// Appends the stream's source to `out` where `out`'s last line leaves
    /// off: every later line is indented as that line is, so a splice keeps
    /// the nesting of the code around it.
    pub fn splice_source_into(&mut self, out: &mut String) {
        let line_start = out.rfind('\n').map_or(0, |newline| newline + 1);
        let current = out.get(line_start..).unwrap_or_default();
        let indent = current
            .get(..current.len() - current.trim_start_matches(' ').len())
            .unwrap_or_default()
            .to_string();
        let source = self.take_source();
        for (index, line) in source.split('\n').enumerate() {
            if index > 0 {
                out.push('\n');
                if line.trim().is_empty() {
                    continue;
                }
                out.push_str(&indent);
            }
            out.push_str(line);
        }
    }

    /// Reports a diagnostic, usually a warning or a note, alongside the
    /// stream's output. To fail the macro, return a
    /// [`MacroforgeError`](crate::MacroforgeError) instead.
    pub fn add_diagnostic(&mut self, diagnostic: crate::abi::Diagnostic) {
        self.diagnostics.push(diagnostic);
    }

    /// Reports several diagnostics; see [`Self::add_diagnostic`].
    pub fn add_diagnostics(
        &mut self,
        diagnostics: impl IntoIterator<Item = crate::abi::Diagnostic>,
    ) {
        self.diagnostics.extend(diagnostics);
    }

    /// The diagnostics reported on this stream so far.
    pub fn diagnostics(&self) -> &[crate::abi::Diagnostic] {
        &self.diagnostics
    }

    /// Create a new parsing stream with macro context attached.
    /// This is used by the macro host to provide context to macros.
    pub fn with_context(
        source: &str,
        file_name: &str,
        ctx: crate::abi::MacroContextIR,
    ) -> Result<Self, TsSynError> {
        Ok(TsStream {
            source: source.to_string(),
            file_name: file_name.to_string(),
            ctx: Some(ctx),
            runtime_patches: vec![],
            insert_pos: crate::abi::InsertPos::default(),
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
            diagnostics: vec![],
        })
    }

    /// Get the macro context if available
    pub fn context(&self) -> Option<&crate::abi::MacroContextIR> {
        self.ctx.as_ref()
    }

    /// Convert the stream into a MacroResult.
    /// Captures any imports registered via `add_import()` etc. from the thread-local
    /// registry so they survive encoding across process boundaries.
    pub fn into_result(self) -> crate::abi::MacroResult {
        let imports = crate::import_registry::with_registry_mut(|r| r.take_generated_imports());
        crate::abi::MacroResult {
            runtime_patches: self.runtime_patches,
            type_patches: vec![],
            diagnostics: self.diagnostics,
            tokens: Some(self.source),
            insert_pos: self.insert_pos,
            debug: None,
            cross_module_suffixes: self.cross_module_suffixes,
            cross_module_type_suffixes: self.cross_module_type_suffixes,
            imports,
            // The entry point a macro is loaded through fills this in.
            registry_reads: None,
        }
    }

    /// Add an import statement. Registers in the [`ImportRegistry`](crate::ImportRegistry)
    /// for idempotent deduplication. No patches are emitted: the registry emits all
    /// generated imports at the end of expansion.
    pub fn add_import(&mut self, specifier: &str, module: &str) {
        let (original, local) = parse_import_specifier(specifier);
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(&local, original.as_deref(), module, false);
        });
    }

    /// Add a type-only import statement. Registers in the [`ImportRegistry`](crate::ImportRegistry).
    pub fn add_type_import(&mut self, specifier: &str, module: &str) {
        let (original, local) = parse_import_specifier(specifier);
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(&local, original.as_deref(), module, true);
        });
    }

    /// Returns the module specifier this stream would use to import `type_name`,
    /// without emitting an import. Resolution comes from the attached or
    /// thread-local [`MacroContextIR`](crate::abi::ir::context::MacroContextIR).
    /// Returns `None` if the type is co-located, unknown, or no context is available.
    pub fn module_specifier_for(&self, type_name: &str) -> Option<String> {
        if let Some(ctx) = self.ctx.as_ref() {
            return ctx.import_specifier_for(type_name);
        }
        crate::context_registry::with_context(|ctx| {
            ctx.and_then(|c| c.import_specifier_for(type_name))
        })
    }

    /// Resolves the module for `type_name` and emits a value import for
    /// `local_name` from that module. Returns `true` when an import was queued.
    /// No-op when the type can't be resolved (e.g. co-located or unknown).
    pub fn add_import_for(&mut self, local_name: &str, type_name: &str) -> bool {
        let Some(module) = self.module_specifier_for(type_name) else {
            return false;
        };
        self.add_import(local_name, &module);
        true
    }

    /// Type-only variant of [`Self::add_import_for`].
    pub fn add_type_import_for(&mut self, local_name: &str, type_name: &str) -> bool {
        let Some(module) = self.module_specifier_for(type_name) else {
            return false;
        };
        self.add_type_import(local_name, &module);
        true
    }

    /// Batch helper: emits N imports for the same `type_name`, resolving the
    /// module exactly once. Each entry is `(local_name, is_type_only)`.
    /// Returns `true` when imports were queued.
    pub fn add_helpers_for(&mut self, type_name: &str, helpers: &[(&str, bool)]) -> bool {
        let Some(module) = self.module_specifier_for(type_name) else {
            return false;
        };
        for (name, is_type) in helpers {
            if *is_type {
                self.add_type_import(name, &module);
            } else {
                self.add_import(name, &module);
            }
        }
        true
    }

    /// Add an import with automatic `__mf_` alias to avoid collisions with user imports.
    ///
    /// # Example
    /// ```ignore
    /// stream.add_aliased_import("DecodeContext", "@macroforge/core/endec");
    /// // Generates: import { DecodeContext as __mf_DecodeContext } from "@macroforge/core/endec";
    /// ```
    pub fn add_aliased_import(&mut self, name: &str, module: &str) {
        let alias = format!("__mf_{name}");
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(&alias, Some(name), module, false);
        });
    }

    /// Add a type-only import with automatic `__mf_` alias.
    ///
    /// # Example
    /// ```ignore
    /// stream.add_aliased_type_import("DecodeOptions", "@macroforge/core/endec");
    /// // Generates: import type { DecodeOptions as __mf_DecodeOptions } from "@macroforge/core/endec";
    /// ```
    pub fn add_aliased_type_import(&mut self, name: &str, module: &str) {
        let alias = format!("__mf_{name}");
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(&alias, Some(name), module, true);
        });
    }

    /// Add an import with a custom alias.
    ///
    /// # Example
    /// ```ignore
    /// stream.add_import_as("resultOk", "__mf_resultOk", "@macroforge/core/endec");
    /// // Generates: import { resultOk as __mf_resultOk } from "@macroforge/core/endec";
    /// ```
    pub fn add_import_as(&mut self, name: &str, alias: &str, module: &str) {
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(alias, Some(name), module, false);
        });
    }

    /// Add a type-only import with a custom alias.
    ///
    /// # Example
    /// ```ignore
    /// stream.add_type_import_as("Result", "__mf_Result", "@macroforge/core/endec");
    /// // Generates: import type { Result as __mf_Result } from "@macroforge/core/endec";
    /// ```
    pub fn add_type_import_as(&mut self, name: &str, alias: &str, module: &str) {
        crate::import_registry::with_registry_mut(|r| {
            r.request_import(alias, Some(name), module, true);
        });
    }

    /// Add multiple imports from a slice of [`ImportConfig`].
    ///
    /// This is the preferred way to add macro-related imports, as it handles
    /// both value and type imports with proper aliasing.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use macroforge_ts_syn::ImportConfig;
    ///
    /// const ENDEC_IMPORTS: &[ImportConfig] = &[
    ///     ImportConfig::value("DecodeContext", "__mf_DecodeContext", "@macroforge/core/endec"),
    ///     ImportConfig::type_only("DecodeOptions", "__mf_DecodeOptions", "@macroforge/core/endec"),
    /// ];
    ///
    /// stream.add_imports(ENDEC_IMPORTS);
    /// ```
    pub fn add_imports(&mut self, imports: &[ImportConfig]) {
        crate::import_registry::with_registry_mut(|r| {
            for import in imports {
                r.request_import(
                    import.alias,
                    Some(import.name),
                    import.module,
                    import.is_type,
                );
            }
        });
    }

    /// Register a cross-module function suffix for auto-import resolution.
    ///
    /// When the generated code references a function like `companyNameGetFields()`,
    /// the framework needs to know that `GetFields` is a valid suffix to resolve
    /// against imported types. Built-in macros (Default, Encode, etc.) have
    /// their suffixes hardcoded; external macros use this method to register theirs.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // In your macro's generate function:
    /// output.add_cross_module_suffix("GetFields");
    /// // Now if the file imports `CompanyName` from `./account.svelte`,
    /// // and the generated code references `companyNameGetFields()`,
    /// // the framework will auto-add: import { companyNameGetFields } from "./account.svelte";
    /// ```
    pub fn add_cross_module_suffix(&mut self, suffix: &str) {
        self.cross_module_suffixes.push(suffix.to_string());
    }

    /// Register a cross-module type suffix for PascalCase type reference auto-import.
    ///
    /// Unlike `add_cross_module_suffix` (which resolves `{camelCase}{Suffix}` function calls),
    /// this resolves `{PascalCase}{Suffix}` type references and generates `import type` statements.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // In your macro's generate function:
    /// output.add_cross_module_type_suffix("Errors");
    /// // Now if the file imports `Colors` from `./shared.svelte`,
    /// // and the generated code references `ColorsErrors` (type position),
    /// // the framework will auto-add: import type { ColorsErrors } from "./shared.svelte";
    /// ```
    pub fn add_cross_module_type_suffix(&mut self, suffix: &str) {
        self.cross_module_type_suffixes.push(suffix.to_string());
    }

    /// Merge another TsStream into this one.
    ///
    /// Combines the source code, runtime patches, cross-module suffixes and
    /// diagnostics of both streams. The insert position of `self` is preserved.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let standalone = ts_template! { export function foo() {} };
    /// let class_body = ts_template!(Within { static bar() {} });
    ///
    /// // Instead of: ts_template! { {$typescript standalone} {$typescript class_body} }
    /// let combined = standalone.merge(class_body);
    /// ```
    pub fn merge(mut self, other: Self) -> Self {
        // Combine source code with a separator only when needed.
        if !self.source.is_empty() && !other.source.is_empty() {
            let left_ends_ws = self
                .source
                .chars()
                .last()
                .map(|c| c.is_whitespace())
                .unwrap_or(false);
            let right_starts_ws = other
                .source
                .chars()
                .next()
                .map(|c| c.is_whitespace())
                .unwrap_or(false);
            if !(left_ends_ws || right_starts_ws) {
                self.source.push('\n');
            }
        }
        self.source.push_str(&other.source);

        // Merge runtime patches
        self.runtime_patches.extend(other.runtime_patches);

        // Merge cross-module suffixes
        self.cross_module_suffixes
            .extend(other.cross_module_suffixes);
        self.cross_module_type_suffixes
            .extend(other.cross_module_type_suffixes);
        self.diagnostics.extend(other.diagnostics);

        self
    }

    /// Merge multiple TsStreams into one.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let combined = TsStream::merge_all([stream1, stream2, stream3]);
    /// ```
    pub fn merge_all(streams: impl IntoIterator<Item = Self>) -> Self {
        let mut iter = streams.into_iter();
        match iter.next() {
            Some(first) => iter.fold(first, |acc, stream| acc.merge(stream)),
            None => Self::from_string(String::new()),
        }
    }
}

/// A trait for types that can be parsed from a [`TsStream`], analogous to `syn::parse::Parse`.
///
/// [`DeriveInput`](crate::DeriveInput) implements `ParseTs`, which is what the
/// `parse_ts_macro_input!` macro relies on. Implement it for a custom input type
/// to use that type with the macro.
pub trait ParseTs: Sized {
    /// Parse a value of this type from a parsing stream.
    ///
    /// # Errors
    ///
    /// Returns [`TsSynError::Parse`] if the source code doesn't match the
    /// expected syntax for this type.
    fn parse(input: &mut TsStream) -> Result<Self, TsSynError>;
}

/// Parse an import specifier like `"Foo as Bar"` into `(Some("Foo"), "Bar")`,
/// or `"Foo"` into `(None, "Foo")`.
fn parse_import_specifier(specifier: &str) -> (Option<String>, String) {
    if let Some(idx) = specifier.find(" as ") {
        let original = specifier[..idx].trim().to_string();
        let local = specifier[idx + 4..].trim().to_string();
        (Some(original), local)
    } else {
        (None, specifier.trim().to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_add_cross_module_suffix() {
        let mut stream = TsStream::from_string("export function foo() {}".to_string());
        assert!(stream.cross_module_suffixes.is_empty());

        stream.add_cross_module_suffix("GetFields");
        assert_eq!(stream.cross_module_suffixes, vec!["GetFields"]);

        stream.add_cross_module_suffix("CustomSuffix");
        assert_eq!(
            stream.cross_module_suffixes,
            vec!["GetFields", "CustomSuffix"]
        );
    }

    #[test]
    fn test_cross_module_suffixes_propagate_to_result() {
        let mut stream = TsStream::from_string("export function foo() {}".to_string());
        stream.add_cross_module_suffix("GetFields");
        stream.add_cross_module_suffix("OtherSuffix");

        let result = stream.into_result();
        assert_eq!(
            result.cross_module_suffixes,
            vec!["GetFields", "OtherSuffix"]
        );
    }

    #[test]
    fn test_merge_combines_cross_module_suffixes() {
        let mut a = TsStream::from_string("export function a() {}".to_string());
        a.add_cross_module_suffix("GetFields");

        let mut b = TsStream::from_string("export function b() {}".to_string());
        b.add_cross_module_suffix("CustomSuffix");

        let merged = a.merge(b);
        assert_eq!(
            merged.cross_module_suffixes,
            vec!["GetFields", "CustomSuffix"]
        );
    }

    #[test]
    fn test_empty_cross_module_suffixes_by_default() {
        let stream = TsStream::from_string("const x = 1;".to_string());
        assert!(stream.cross_module_suffixes.is_empty());

        let result = stream.into_result();
        assert!(result.cross_module_suffixes.is_empty());
    }
}

#[cfg(test)]
mod import_for_tests {
    use super::*;
    use crate::abi::ir::context::{MacroContextIR, MacroKind, TargetIR};
    use crate::abi::ir::interface::InterfaceIR;
    use crate::abi::ir::type_registry::{TypeDefinitionIR, TypeRegistry, TypeRegistryEntry};
    use crate::import_registry::{ImportRegistry, take_registry};
    use crate::{SpanIR, context_registry};

    fn empty_interface(name: &str) -> InterfaceIR {
        InterfaceIR {
            name: name.to_string(),
            span: SpanIR::new(0, 0),
            body_span: SpanIR::new(0, 0),
            type_params: vec![],
            heritage: vec![],
            fields: vec![],
            methods: vec![],
            decorators: vec![],
        }
    }

    fn make_ctx_with_registry(file_name: &str, type_name: &str, type_path: &str) -> MacroContextIR {
        let mut registry = TypeRegistry::new();
        registry.types.insert(
            type_name.to_string(),
            TypeRegistryEntry {
                name: type_name.to_string(),
                file_path: type_path.to_string(),
                is_exported: true,
                definition: TypeDefinitionIR::Interface(empty_interface(type_name)),
                file_imports: vec![],
            },
        );
        MacroContextIR {
            abi_version: 1,
            macro_kind: MacroKind::Derive,
            macro_name: "Test".to_string(),
            module_path: "@test".to_string(),
            decorator_span: SpanIR::new(0, 0),
            macro_name_span: None,
            target_span: SpanIR::new(0, 0),
            file_name: file_name.to_string(),
            target: TargetIR::Interface(empty_interface("Probe")),
            target_source: String::new(),
            import_registry: ImportRegistry::new(),
            config: None,
            type_registry: registry,
            resolved_fields: None,
            expansion_id: 0,
        }
    }

    /// Helper that resets the thread-local registries before each test runs
    /// (the tests in this module share thread-local state).
    fn reset_thread_locals() {
        context_registry::clear_context();
        let _ = take_registry();
        crate::import_registry::install_registry(ImportRegistry::new());
    }

    #[test]
    fn add_import_for_resolves_via_thread_local_context() {
        reset_thread_locals();
        let ctx = make_ctx_with_registry(
            "/proj/src/order.svelte.ts",
            "Customer",
            "/proj/src/customer.svelte.ts",
        );
        context_registry::install_context(ctx);

        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_import_for("customerDefaultValue", "Customer");
        assert!(added);

        let result = stream.into_result();
        let customer = result
            .imports
            .iter()
            .find(|i| i.local_name == "customerDefaultValue")
            .expect("expected customerDefaultValue import to be queued");
        assert_eq!(customer.source_module, "./customer.svelte");
        assert!(!customer.is_type_only);

        context_registry::clear_context();
    }

    #[test]
    fn add_helpers_for_batches_correctly() {
        reset_thread_locals();
        let ctx = make_ctx_with_registry(
            "/proj/src/order.svelte.ts",
            "Customer",
            "/proj/src/customer.svelte.ts",
        );
        context_registry::install_context(ctx);

        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_helpers_for(
            "Customer",
            &[
                ("customerDefaultValue", false),
                ("CustomerErrors", true),
                ("customerHasShape", false),
            ],
        );
        assert!(added);

        let result = stream.into_result();

        let value = result
            .imports
            .iter()
            .find(|i| i.local_name == "customerDefaultValue")
            .expect("value import missing");
        assert_eq!(value.source_module, "./customer.svelte");
        assert!(!value.is_type_only);

        let type_import = result
            .imports
            .iter()
            .find(|i| i.local_name == "CustomerErrors")
            .expect("type import missing");
        assert_eq!(type_import.source_module, "./customer.svelte");
        assert!(type_import.is_type_only);

        let has_shape = result
            .imports
            .iter()
            .find(|i| i.local_name == "customerHasShape")
            .expect("has_shape import missing");
        assert_eq!(has_shape.source_module, "./customer.svelte");
        assert!(!has_shape.is_type_only);

        context_registry::clear_context();
    }

    #[test]
    fn add_import_for_returns_false_without_context() {
        reset_thread_locals();
        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_import_for("customerDefaultValue", "Customer");
        assert!(!added);
        let result = stream.into_result();
        assert!(result.imports.is_empty());
    }

    #[test]
    fn add_import_for_returns_false_for_co_located_type() {
        reset_thread_locals();
        let ctx = make_ctx_with_registry(
            "/proj/src/customer.svelte.ts",
            "Customer",
            "/proj/src/customer.svelte.ts",
        );
        context_registry::install_context(ctx);

        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_import_for("customerDefaultValue", "Customer");
        assert!(!added);
        let result = stream.into_result();
        assert!(result.imports.is_empty());

        context_registry::clear_context();
    }

    /// Pre-registering a variant's source module through
    /// `install_source_imports` shadows any later `add_type_import` for the
    /// same name: `request_import` skips it because `source_imports` already
    /// holds the name, so the bare variant type is referenced in generated code
    /// and never imported. This pins that interaction, so a macro that
    /// pre-registers a variant is known to lose the import.
    #[test]
    fn add_helpers_for_emits_bare_variant_type_import() {
        reset_thread_locals();
        let ctx = make_ctx_with_registry(
            "/proj/src/employee.svelte.ts",
            "LinkedUser",
            "/proj/src/linked-user.svelte.ts",
        );
        context_registry::install_context(ctx);

        // Simulate the buggy pre-registration: pretend something put
        // `LinkedUser` into `source_imports` before the helper batch ran.
        crate::import_registry::with_registry_mut(|r| {
            r.install_source_imports(vec![crate::import_registry::SourceImportEntry {
                local_name: "LinkedUser".to_string(),
                source_module: "./linked-user.svelte".to_string(),
                original_name: None,
                is_type_only: true,
            }]);
        });

        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_helpers_for(
            "LinkedUser",
            &[
                ("LinkedUserFieldControllers", true),
                ("LinkedUser", true),
                ("linkedUserGetControllers", false),
                ("linkedUserDefaultErrors", false),
                ("linkedUserDefaultTainted", false),
                ("linkedUserDefaultValue", false),
                ("linkedUserIs", false),
            ],
        );
        assert!(added, "add_helpers_for should have resolved LinkedUser");

        let result = stream.into_result();

        // The derived `FieldControllers` helper should be present.
        let field_controllers = result
            .imports
            .iter()
            .find(|i| i.local_name == "LinkedUserFieldControllers")
            .expect("LinkedUserFieldControllers type import missing");
        assert_eq!(field_controllers.source_module, "./linked-user.svelte");
        assert!(field_controllers.is_type_only);

        // `request_import` skips a name already in `source_imports`, so
        // `LinkedUser`, installed as a source import, gets no generated line.
        // Variant types must therefore be resolved, not pre-registered.
        let bare = result.imports.iter().find(|i| i.local_name == "LinkedUser");
        assert!(
            bare.is_none(),
            "bare LinkedUser should be dedup'd against source_imports: \
             callers must NOT pre-register variant types in source_imports \
             if they also want a generated `import type {{ LinkedUser }}` line"
        );

        // All five value helpers should still be present (they have distinct
        // local names that aren't dedup'd against the source-imports entry).
        for name in [
            "linkedUserGetControllers",
            "linkedUserDefaultErrors",
            "linkedUserDefaultTainted",
            "linkedUserDefaultValue",
            "linkedUserIs",
        ] {
            let entry = result
                .imports
                .iter()
                .find(|i| i.local_name == name)
                .unwrap_or_else(|| panic!("{name} value import missing"));
            assert_eq!(entry.source_module, "./linked-user.svelte");
            assert!(!entry.is_type_only);
        }

        context_registry::clear_context();
    }

    /// Companion to `add_helpers_for_emits_bare_variant_type_import`: without
    /// the pre-registration, the bare type import is emitted normally.
    #[test]
    fn add_helpers_for_emits_bare_type_when_source_imports_clean() {
        reset_thread_locals();
        let ctx = make_ctx_with_registry(
            "/proj/src/entry.svelte.ts",
            "CustomerReferral",
            "/proj/src/customer-referral.svelte.ts",
        );
        context_registry::install_context(ctx);

        let mut stream = TsStream::from_string(String::new());
        let added = stream.add_helpers_for(
            "CustomerReferral",
            &[
                ("CustomerReferralFieldControllers", true),
                ("CustomerReferral", true),
                ("customerReferralIs", false),
            ],
        );
        assert!(added);

        let result = stream.into_result();
        let bare = result
            .imports
            .iter()
            .find(|i| i.local_name == "CustomerReferral")
            .expect("bare CustomerReferral type import missing");
        assert_eq!(bare.source_module, "./customer-referral.svelte");
        assert!(bare.is_type_only);

        context_registry::clear_context();
    }
}