macroforge_ts_syn 0.1.79

TypeScript syntax types for compile-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
//! A `syn`-like parsing API for TypeScript using SWC.
//!
//! This module provides a [`TsStream`] type and [`ParseTs`] trait that abstract
//! over SWC's parser, making it feel more like Rust's `syn` crate. This is the
//! low-level parsing API; most macro authors will prefer using
//! [`DeriveInput`](crate::DeriveInput) from the [`derive`](crate::derive) module.
//!
//! ## Overview
//!
//! - [`TsStream`] - A parsing stream wrapping SWC's parser
//! - [`ParseTs`] - A trait for types that can be parsed from TypeScript source
//! - [`parse_ts_str`] - Parse a string into any [`ParseTs`] type
//! - [`parse_ts_expr`], [`parse_ts_stmt`], [`parse_ts_module`] - Convenience functions
//! - [`format_ts_source`] - Format TypeScript code using SWC's emitter
//!
//! ## Basic Usage
//!
//! ```rust,no_run
//! use macroforge_ts_syn::{TsStream, parse_ts_str, TsSynError};
//! use swc_core::ecma::ast::{Expr, Stmt, Module};
//!
//! fn main() -> Result<(), TsSynError> {
//!     // Parse individual constructs
//!     let expr: Box<Expr> = parse_ts_str("x + y")?;
//!     let stmt: Stmt = parse_ts_str("const x = 5;")?;
//!     let module: Module = parse_ts_str("export class Foo {}")?;
//!
//!     // Or use TsStream directly
//!     let mut stream = TsStream::new("const x = 5;", "input.ts")?;
//!     let stmt = stream.parse_stmt()?;
//!     Ok(())
//! }
//! ```
//!
//! ## 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("deserialize", "./runtime");
//!
//!     // Add a type-only import
//!     stream.add_type_import("Options", "./types");
//!
//!     // Convert to result
//!     let _result = stream.into_result();
//!     Ok(())
//! }
//! ```
//!
//! ## ParseTs Implementations
//!
//! The following SWC types implement [`ParseTs`]:
//!
//! - `Ident` - Single identifier
//! - `Stmt` - Any statement
//! - `Box<Expr>` - Any expression
//! - `Module` - Complete module
//!
//! Custom types can implement [`ParseTs`] to enable parsing with [`TsStream::parse`].

#[cfg(feature = "swc")]
use swc_core::common::{FileName, SourceMap, sync::Lrc};
#[cfg(feature = "swc")]
use swc_core::ecma::ast::*;
#[cfg(feature = "swc")]
use swc_core::ecma::codegen::{Config, Emitter, text_writer::JsWriter};
#[cfg(feature = "swc")]
use swc_core::ecma::parser::{PResult, Parser, StringInput, Syntax, TsSyntax, lexer::Lexer};

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/reexports"),
///     ImportConfig::type_only("Result", "__mf_Result", "macroforge/reexports"),
/// ];
///
/// 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,
        }
    }
}

/// A parsing stream that wraps SWC's parser, 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,no_run
/// use macroforge_ts_syn::{TsStream, TsSynError};
///
/// fn main() -> Result<(), TsSynError> {
///     let mut stream = TsStream::new("const x = 5;", "input.ts")?;
///     // Parse specific constructs
///     let _stmt = stream.parse_stmt()?;
///     Ok(())
/// }
/// ```
///
/// # Working with Imports
///
/// ```rust,no_run
/// 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("deserialize", "./runtime");
///     stream.add_type_import("Options", "./types");
///
///     // Get the result with accumulated patches
///     let _result = stream.into_result();
///     Ok(())
/// }
/// ```
#[cfg(feature = "swc")]
pub struct TsStream {
    source_map: Lrc<SourceMap>,
    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>,
}

/// Formats TypeScript source code using SWC's emitter.
///
/// Attempts to parse and re-emit the code with consistent formatting. This is
/// useful for normalizing generated code output.
///
/// # Parsing Strategy
///
/// The function tries two parsing approaches:
/// 1. Parse as a complete module (for full file content)
/// 2. If that fails, wrap in a class and extract (for class members)
///
/// # Example
///
/// ```rust
/// use macroforge_ts_syn::format_ts_source;
///
/// let messy = "function foo(){return 42}";
/// let formatted = format_ts_source(messy);
/// assert!(formatted.contains("function foo()"));
/// assert!(formatted.contains("return 42"));
/// ```
///
/// # Fallback
///
/// If parsing fails entirely, returns the original source unchanged.
#[cfg(feature = "swc")]
pub fn format_ts_source(source: &str) -> String {
    let cm = Lrc::new(SourceMap::default());

    // 1. Try parsing as a valid module (e.g. full file content or statements)
    let fm = cm.new_source_file(FileName::Custom("fmt.ts".into()).into(), source.to_string());
    let syntax = Syntax::Typescript(TsSyntax {
        tsx: true,
        decorators: true,
        ..Default::default()
    });

    let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None);
    let mut parser = Parser::new_from(lexer);

    if let Ok(module) = parser.parse_module() {
        let mut buf = vec![];
        {
            let mut emitter = Emitter {
                cfg: Config::default().with_minify(false),
                cm: cm.clone(),
                comments: None,
                wr: JsWriter::new(cm.clone(), "\n", &mut buf, None),
            };

            if emitter.emit_module(&module).is_ok() {
                return String::from_utf8(buf).unwrap_or_else(|_| source.to_string());
            }
        }
    }

    // 2. If failed, try wrapping in a class (common for macro output generating methods/fields)
    let wrapped_source = format!("class __FmtWrapper {{ {} }}", source);
    let fm_wrapped = cm.new_source_file(
        FileName::Custom("fmt_wrapped.ts".into()).into(),
        wrapped_source,
    );

    let lexer = Lexer::new(
        syntax,
        EsVersion::latest(),
        StringInput::from(&*fm_wrapped),
        None,
    );
    let mut parser = Parser::new_from(lexer);

    if let Ok(module) = parser.parse_module() {
        let mut buf = vec![];
        {
            let mut emitter = Emitter {
                cfg: Config::default().with_minify(false),
                cm: cm.clone(),
                comments: None,
                wr: JsWriter::new(cm.clone(), "\n", &mut buf, None),
            };

            if emitter.emit_module(&module).is_ok() {
                let full_output = String::from_utf8(buf).unwrap_or_default();
                // Extract content between class braces
                // Output format: class __FmtWrapper {\n    content...\n}
                if let (Some(start), Some(end)) = (full_output.find('{'), full_output.rfind('}')) {
                    let content = &full_output[start + 1..end];
                    // Simple unindent: remove first newline and 4 spaces of indentation if present
                    let lines: Vec<&str> = content.lines().collect();
                    let mut result = String::new();
                    for line in lines {
                        // Naive unindent
                        let trimmed = line.strip_prefix("    ").unwrap_or(line);
                        if !trimmed.trim().is_empty() {
                            result.push_str(trimmed);
                            result.push('\n');
                        }
                    }
                    return result.trim().to_string();
                }
            }
        }
    }
    source.to_string()
}

#[cfg(feature = "swc")]
impl TsStream {
    /// Create a new parsing stream from source code.
    pub fn new(source: &str, file_name: &str) -> Result<Self, TsSynError> {
        Ok(TsStream {
            source_map: Lrc::new(Default::default()),
            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![],
        })
    }

    /// Create a new parsing stream from an owned string.
    pub fn from_string(source: String) -> Self {
        TsStream {
            source_map: Lrc::new(Default::default()),
            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![],
        }
    }

    /// 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_map: Lrc::new(Default::default()),
            source,
            file_name: "macro_output.ts".to_string(),
            ctx: None,
            runtime_patches: vec![],
            insert_pos,
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
        }
    }

    /// Create a new parsing stream with a specific insert position and runtime patches.
    /// Used by `ts_template!` to collect patches from embedded TsStreams.
    pub fn with_insert_pos_and_patches(
        source: String,
        insert_pos: crate::abi::InsertPos,
        runtime_patches: Vec<crate::abi::Patch>,
    ) -> Self {
        TsStream {
            source_map: Lrc::new(Default::default()),
            source,
            file_name: "macro_output.ts".to_string(),
            ctx: None,
            runtime_patches,
            insert_pos,
            cross_module_suffixes: vec![],
            cross_module_type_suffixes: vec![],
        }
    }

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

    /// 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_map: Lrc::new(Default::default()),
            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![],
        })
    }

    /// 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 serialization 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: vec![],
            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,
        }
    }

    /// 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);
        });
    }

    /// Add an import with automatic `__mf_` alias to avoid collisions with user imports.
    ///
    /// # Example
    /// ```ignore
    /// stream.add_aliased_import("DeserializeContext", "macroforge/serde");
    /// // Generates: import { DeserializeContext as __mf_DeserializeContext } from "macroforge/serde";
    /// ```
    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("DeserializeOptions", "macroforge/serde");
    /// // Generates: import type { DeserializeOptions as __mf_DeserializeOptions } from "macroforge/serde";
    /// ```
    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/reexports");
    /// // Generates: import { resultOk as __mf_resultOk } from "macroforge/reexports";
    /// ```
    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/reexports");
    /// // Generates: import type { Result as __mf_Result } from "macroforge/reexports";
    /// ```
    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 SERDE_IMPORTS: &[ImportConfig] = &[
    ///     ImportConfig::value("DeserializeContext", "__mf_DeserializeContext", "macroforge/serde"),
    ///     ImportConfig::type_only("DeserializeOptions", "__mf_DeserializeOptions", "macroforge/serde"),
    /// ];
    ///
    /// stream.add_imports(SERDE_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, Serialize, 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 and runtime patches from 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
    }

    /// 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()),
        }
    }

    /// Create a temporary parser for a parsing operation.
    /// This is an internal helper that manages SWC's complex lifetimes.
    fn with_parser<F, T>(&self, f: F) -> Result<T, TsSynError>
    where
        F: for<'a> FnOnce(&mut Parser<Lexer<'a>>) -> PResult<T>,
    {
        let fm = self.source_map.new_source_file(
            FileName::Custom(self.file_name.clone()).into(),
            self.source.clone(),
        );

        let syntax = Syntax::Typescript(TsSyntax {
            tsx: self.file_name.ends_with(".tsx"),
            decorators: true,
            ..Default::default()
        });

        let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None);

        let mut parser = Parser::new_from(lexer);
        f(&mut parser).map_err(|e| TsSynError::Parse(format!("{:?}", e)))
    }

    /// Parse a value of type T from the stream.
    /// This is analogous to `syn::ParseBuffer::parse::<T>()`.
    pub fn parse<T: ParseTs>(&mut self) -> Result<T, TsSynError> {
        T::parse(self)
    }

    /// Parse an identifier.
    pub fn parse_ident(&self) -> Result<Ident, TsSynError> {
        self.with_parser(|parser| {
            use swc_core::common::DUMMY_SP;
            use swc_core::ecma::parser::error::{Error, SyntaxError};
            // Parse as expression and extract identifier
            parser.parse_expr().and_then(|expr| match *expr {
                Expr::Ident(ident) => Ok(ident),
                _ => Err(Error::new(DUMMY_SP, SyntaxError::TS1003)),
            })
        })
    }

    /// Parse a statement.
    pub fn parse_stmt(&self) -> Result<Stmt, TsSynError> {
        self.with_parser(|parser| parser.parse_stmt_list_item())
    }

    /// Parse an expression.
    pub fn parse_expr(&self) -> Result<Box<Expr>, TsSynError> {
        self.with_parser(|parser| parser.parse_expr())
    }

    /// Parse a module (useful for parsing complete TypeScript files).
    pub fn parse_module(&self) -> Result<Module, TsSynError> {
        self.with_parser(|parser| parser.parse_module())
    }
}

/// A trait for types that can be parsed from TypeScript source, analogous to `syn::parse::Parse`.
///
/// Implement this trait to enable parsing your custom types with [`TsStream::parse`]
/// and [`parse_ts_str`].
///
/// # Built-in Implementations
///
/// The following SWC types already implement `ParseTs`:
/// - `Ident` - A single identifier
/// - `Stmt` - Any statement
/// - `Box<Expr>` - Any expression
/// - `Module` - A complete module
///
/// # Implementing ParseTs
///
/// ```rust,no_run
/// use macroforge_ts_syn::{ParseTs, TsStream, TsSynError, parse_ts_str};
///
/// struct MyType {
///     name: String,
///     value: i32,
/// }
///
/// impl ParseTs for MyType {
///     fn parse(input: &mut TsStream) -> Result<Self, TsSynError> {
///         // Use TsStream's parsing methods
///         let ident = input.parse_ident()?;
///
///         // Or parse sub-expressions
///         let _expr = input.parse_expr()?;
///
///         // Return the parsed type
///         Ok(MyType {
///             name: ident.sym.to_string(),
///             value: 42,
///         })
///     }
/// }
///
/// fn main() -> Result<(), TsSynError> {
///     // Now you can use it with parse_ts_str
///     let _my_value: MyType = parse_ts_str("identifier")?;
///     Ok(())
/// }
/// ```
///
/// # Using with DeriveInput
///
/// [`DeriveInput`](crate::DeriveInput) implements `ParseTs`, allowing it to be
/// parsed from a `TsStream` using the `parse_ts_macro_input!` 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>;
}

// Implement ParseTs for common SWC types
#[cfg(feature = "swc")]
impl ParseTs for Ident {
    fn parse(input: &mut TsStream) -> Result<Self, TsSynError> {
        input.parse_ident()
    }
}

#[cfg(feature = "swc")]
impl ParseTs for Stmt {
    fn parse(input: &mut TsStream) -> Result<Self, TsSynError> {
        input.parse_stmt()
    }
}

#[cfg(feature = "swc")]
impl ParseTs for Box<Expr> {
    fn parse(input: &mut TsStream) -> Result<Self, TsSynError> {
        input.parse_expr()
    }
}

#[cfg(feature = "swc")]
impl ParseTs for Module {
    fn parse(input: &mut TsStream) -> Result<Self, TsSynError> {
        input.parse_module()
    }
}

/// Parse a string of TypeScript code into a specific type.
/// This is analogous to `syn::parse_str`.
///
/// # Example
/// ```ignore
/// let ident: Ident = parse_ts_str("myVariable")?;
/// let expr: Box<Expr> = parse_ts_str("x + y")?;
/// let stmt: Stmt = parse_ts_str("const x = 5;")?;
/// ```
#[cfg(feature = "swc")]
pub fn parse_ts_str<T: ParseTs>(code: &str) -> Result<T, TsSynError> {
    let mut stream = TsStream::new(code, "input.ts")?;
    stream.parse()
}

/// Parse a snippet of TypeScript code as an expression.
#[cfg(feature = "swc")]
pub fn parse_ts_expr(code: &str) -> Result<Box<Expr>, TsSynError> {
    parse_ts_str(code)
}

/// Parse a snippet of TypeScript code as a statement.
#[cfg(feature = "swc")]
pub fn parse_ts_stmt(code: &str) -> Result<Stmt, TsSynError> {
    parse_ts_str(code)
}

/// Parse a snippet of TypeScript code as a module.
#[cfg(feature = "swc")]
pub fn parse_ts_module(code: &str) -> Result<Module, TsSynError> {
    parse_ts_str(code)
}

/// Parse a snippet of TypeScript code as a type annotation.
#[cfg(feature = "swc")]
pub fn parse_ts_type(code: &str) -> Result<TsType, TsSynError> {
    use swc_core::ecma::ast::{Decl, ModuleItem, Stmt};

    // Wrap as `type __T = <type>;` for parsing
    let wrapped = format!("type __T = {};", code);
    let module: Module = parse_ts_str(&wrapped)?;

    // Extract the type from the alias declaration
    for item in module.body {
        if let ModuleItem::Stmt(Stmt::Decl(Decl::TsTypeAlias(alias))) = item {
            return Ok(*alias.type_ann);
        }
    }

    Err(TsSynError::Parse(format!("Failed to parse type: {}", code)))
}

/// 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::*;

    #[cfg(feature = "swc")]
    #[test]
    fn test_parse_ident() {
        let result: Result<Ident, _> = parse_ts_str("myVariable");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().sym.as_ref(), "myVariable");
    }

    #[cfg(feature = "swc")]
    #[test]
    fn test_parse_expr() {
        let result = parse_ts_expr("1 + 2");
        assert!(result.is_ok());
    }

    #[cfg(feature = "swc")]
    #[test]
    fn test_parse_stmt() {
        let result = parse_ts_stmt("const x = 5;");
        assert!(result.is_ok(), "parse_ts_stmt failed: {:?}", result.err());
    }

    #[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());
    }
}