typespec_rs 0.5.0

A Rust implementation of the TypeSpec type system — parser, checker, and emitter
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
//! Diagnostics module for TypeSpec-Rust
//!
//! Ported from TypeSpec compiler/src/core/diagnostics.ts
//! and TypeSpec compiler/src/core/diagnostic-creator.ts
//!
//! This module provides diagnostic collection, reporting, and creation utilities.

/// Diagnostic severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagnosticSeverity {
    Error,
    Warning,
    Information,
    Hint,
}

impl std::fmt::Display for DiagnosticSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DiagnosticSeverity::Error => write!(f, "error"),
            DiagnosticSeverity::Warning => write!(f, "warning"),
            DiagnosticSeverity::Information => write!(f, "info"),
            DiagnosticSeverity::Hint => write!(f, "hint"),
        }
    }
}

/// A diagnostic message with location information
#[derive(Debug, Clone, PartialEq)]
pub struct Diagnostic {
    /// Diagnostic code (e.g., "invalid-argument")
    pub code: String,
    /// Diagnostic severity
    pub severity: DiagnosticSeverity,
    /// The diagnostic message
    pub message: String,
    /// Optional URL for more information
    pub url: Option<String>,
    /// Source location
    pub location: Option<SourceLocation>,
    /// Related locations
    pub related: Vec<RelatedLocation>,
}

impl Diagnostic {
    /// Create a new diagnostic
    pub fn new(code: &str, message: &str) -> Self {
        Self {
            code: code.to_string(),
            severity: DiagnosticSeverity::Error,
            message: message.to_string(),
            url: None,
            location: None,
            related: Vec::new(),
        }
    }

    /// Create an error diagnostic
    pub fn error(code: &str, message: &str) -> Self {
        Self::new(code, message)
    }

    /// Create a warning diagnostic
    pub fn warning(code: &str, message: &str) -> Self {
        Self {
            code: code.to_string(),
            severity: DiagnosticSeverity::Warning,
            message: message.to_string(),
            url: None,
            location: None,
            related: Vec::new(),
        }
    }

    /// Check if this diagnostic is an error
    pub fn is_error(&self) -> bool {
        self.severity == DiagnosticSeverity::Error
    }
}

/// Source location for a diagnostic
#[derive(Debug, Clone, PartialEq)]
pub struct SourceLocation {
    /// Source file path
    pub file: String,
    /// Start position
    pub start: usize,
    /// End position
    pub end: usize,
    /// Whether this is a synthetic location
    pub is_synthetic: bool,
}

/// Related location for a diagnostic
#[derive(Debug, Clone, PartialEq)]
pub struct RelatedLocation {
    /// Location message
    pub message: String,
    /// The related location
    pub location: Option<SourceLocation>,
}

/// Diagnostic collector - collects diagnostics from operations
#[derive(Debug, Clone, Default)]
pub struct DiagnosticCollector {
    diagnostics: Vec<Diagnostic>,
}

impl DiagnosticCollector {
    /// Create a new diagnostic collector
    pub fn new() -> Self {
        Self {
            diagnostics: Vec::new(),
        }
    }

    /// Add a diagnostic to the collector
    pub fn add(&mut self, diagnostic: Diagnostic) {
        self.diagnostics.push(diagnostic);
    }

    /// Get all collected diagnostics
    pub fn diagnostics(&self) -> &[Diagnostic] {
        &self.diagnostics
    }

    /// Clear all collected diagnostics
    pub fn clear(&mut self) {
        self.diagnostics.clear();
    }

    /// Check if there are any diagnostics
    pub fn has_diagnostics(&self) -> bool {
        !self.diagnostics.is_empty()
    }

    /// Check if any error-level diagnostics were reported
    pub fn has_error(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == DiagnosticSeverity::Error)
    }

    /// Pipe a result through the collector (for diagnostic accessor pattern)
    pub fn pipe<T>(&mut self, result: (T, Vec<Diagnostic>)) -> T {
        let (value, diags) = result;
        for diag in diags {
            self.add(diag);
        }
        value
    }
}

/// Ignore diagnostics and return just the value
pub fn ignore_diagnostics<T>(result: (T, Vec<Diagnostic>)) -> T {
    result.0
}

/// Create a synthetic source location
pub fn create_synthetic_source_location(loc: &str) -> SourceLocation {
    SourceLocation {
        file: loc.to_string(),
        start: 0,
        end: 0,
        is_synthetic: true,
    }
}

/// Get related locations for a diagnostic (template instantiation trace).
/// Ported from TS getRelatedLocations().
pub fn get_related_locations(_diagnostic: &Diagnostic) -> Vec<RelatedLocation> {
    // TODO: Requires template mapper infrastructure to trace template instantiations.
    // Returns empty until checker template system supports instantiation tracing.
    Vec::new()
}

/// Get the source location for a diagnostic target.
/// Ported from TS getSourceLocation().
/// Currently simplified — returns the diagnostic's own location or a synthetic one.
pub fn get_source_location(diagnostic: &Diagnostic) -> Option<SourceLocation> {
    diagnostic.location.clone()
}

/// Get the template instantiation trace for a diagnostic target.
/// Ported from TS getDiagnosticTemplateInstantitationTrace().
/// Returns nodes involved in template instantiation chain.
/// Currently returns empty — requires template mapper infrastructure.
pub fn get_diagnostic_template_instantiation_trace(
    _diagnostic: &Diagnostic,
) -> Vec<SourceLocation> {
    // TODO: Requires template mapper to trace instantiation chain.
    // Will walk: target.templateMapper → source.node → source.mapper → ...
    Vec::new()
}

// ============================================================================
// Diagnostic Creator
// Ported from TypeSpec compiler/src/core/diagnostic-creator.ts
// ============================================================================

/// Definition of a single diagnostic message within a diagnostic code.
#[derive(Debug, Clone)]
pub struct DiagnosticMessageDefinition {
    /// The message text, optionally a format string with {key} placeholders
    pub text: String,
}

/// Definition of a diagnostic code with its severity and messages.
#[derive(Debug, Clone)]
pub struct DiagnosticDefinition {
    /// The severity of this diagnostic
    pub severity: DiagnosticSeverity,
    /// Map of message ID to message definition (at minimum "default")
    pub messages: Vec<(String, DiagnosticMessageDefinition)>,
    /// Optional URL for more information
    pub url: Option<String>,
}

impl DiagnosticDefinition {
    /// Create a simple error diagnostic definition with a default message
    pub fn error(text: &str) -> Self {
        Self {
            severity: DiagnosticSeverity::Error,
            messages: vec![(
                "default".to_string(),
                DiagnosticMessageDefinition {
                    text: text.to_string(),
                },
            )],
            url: None,
        }
    }

    /// Create a simple warning diagnostic definition with a default message
    pub fn warning(text: &str) -> Self {
        Self {
            severity: DiagnosticSeverity::Warning,
            messages: vec![(
                "default".to_string(),
                DiagnosticMessageDefinition {
                    text: text.to_string(),
                },
            )],
            url: None,
        }
    }

    /// Create an error diagnostic definition with multiple message variants.
    /// This matches the TS pattern where a diagnostic code can have
    /// multiple message IDs (e.g., "default", "addedAfter", "removedBefore").
    pub fn error_with_messages(messages: Vec<(&str, &str)>) -> Self {
        Self {
            severity: DiagnosticSeverity::Error,
            messages: messages
                .into_iter()
                .map(|(id, text)| {
                    (
                        id.to_string(),
                        DiagnosticMessageDefinition {
                            text: text.to_string(),
                        },
                    )
                })
                .collect(),
            url: None,
        }
    }

    /// Create a warning diagnostic definition with multiple message variants.
    pub fn warning_with_messages(messages: Vec<(&str, &str)>) -> Self {
        Self {
            severity: DiagnosticSeverity::Warning,
            messages: messages
                .into_iter()
                .map(|(id, text)| {
                    (
                        id.to_string(),
                        DiagnosticMessageDefinition {
                            text: text.to_string(),
                        },
                    )
                })
                .collect(),
            url: None,
        }
    }

    /// Get the message text for a given message ID
    pub fn get_message(&self, message_id: &str) -> Option<&str> {
        self.messages
            .iter()
            .find(|(id, _)| id == message_id)
            .map(|(_, def)| def.text.as_str())
    }
}

/// A map of diagnostic codes to their definitions.
/// Key is the diagnostic code string.
pub type DiagnosticMap = std::collections::HashMap<String, DiagnosticDefinition>;

/// Diagnostic creator that creates typed diagnostics from a diagnostic map.
/// Ported from TS createDiagnosticCreator()
#[derive(Debug, Clone)]
pub struct DiagnosticCreator {
    /// The diagnostic map this creator is based on
    diagnostics: DiagnosticMap,
    /// Optional library name for namespacing diagnostic codes
    library_name: Option<String>,
}

impl DiagnosticCreator {
    /// Create a new diagnostic creator from a diagnostic map and optional library name.
    pub fn new(diagnostics: DiagnosticMap, library_name: Option<String>) -> Self {
        Self {
            diagnostics,
            library_name,
        }
    }

    /// Create a diagnostic from a code and optional format values.
    ///
    /// # Arguments
    /// * `code` - The diagnostic code
    /// * `message_id` - Optional message ID (defaults to "default")
    /// * `format` - Optional format values for message interpolation
    ///
    /// If the code or message_id is not found, returns a fallback diagnostic
    /// instead of panicking (ICE protection, ported from upstream fix).
    pub fn create_diagnostic(
        &self,
        code: &str,
        message_id: Option<&str>,
        format: &[(String, String)],
    ) -> Diagnostic {
        let def = match self.diagnostics.get(code) {
            Some(d) => d,
            None => {
                // ICE protection: return fallback diagnostic instead of panicking
                let full_code = match &self.library_name {
                    Some(lib) => format!("{}/{}", lib, code),
                    None => code.to_string(),
                };
                let mut message = format!("Unknown diagnostic code '{}'", code);
                for (key, value) in format {
                    message = message.replace(&format!("{{{}}}", key), value);
                }
                return Diagnostic::warning(&full_code, &message);
            }
        };

        let msg_id = message_id.unwrap_or("default");
        let message_text = match def.get_message(msg_id) {
            Some(text) => text,
            None => {
                // ICE protection: use default message or fallback
                def.get_message("default").unwrap_or(code)
            }
        };

        // Interpolate format values into the message
        let mut message = message_text.to_string();
        for (key, value) in format {
            message = message.replace(&format!("{{{}}}", key), value);
        }

        let full_code = match &self.library_name {
            Some(lib) => format!("{}/{}", lib, code),
            None => code.to_string(),
        };

        let mut diag = Diagnostic::new(&full_code, &message);
        diag.severity = def.severity;
        if let Some(url) = &def.url {
            diag.url = Some(url.clone());
        }
        diag
    }

    /// Get the diagnostic map
    pub fn diagnostics(&self) -> &DiagnosticMap {
        &self.diagnostics
    }

    /// Get the number of diagnostic definitions
    pub fn len(&self) -> usize {
        self.diagnostics.len()
    }

    /// Check if the diagnostic map is empty
    pub fn is_empty(&self) -> bool {
        self.diagnostics.is_empty()
    }

    /// Get the library name
    pub fn library_name(&self) -> Option<&str> {
        self.library_name.as_deref()
    }
}

// ============================================================================
// Compiler Options
// Ported from TypeSpec compiler/src/core/options.ts
// ============================================================================

/// Compiler options for TypeSpec compilation.
/// Ported from TS CompilerOptions interface.
#[derive(Debug, Clone, Default)]
pub struct CompilerOptions {
    /// Miscellaneous options
    pub misc_options: std::collections::HashMap<String, String>,
    /// Default output directory used by emitters (default: ./tsp-output)
    pub output_dir: Option<String>,
    /// Path to config YAML file
    pub config: Option<String>,
    /// List of emitters to use
    pub emit: Option<Vec<String>>,
    /// List emitted outputs and their paths
    pub list_files: bool,
    /// Emitter options, keyed by emitter name
    pub options: std::collections::HashMap<String, std::collections::HashMap<String, String>>,
    /// Suppress all deprecated warnings
    pub ignore_deprecated: bool,
    /// Don't load the standard library
    pub nostdlib: bool,
    /// Do not run emitters (same as emit: [])
    pub no_emit: bool,
    /// Runs emitters but do not write output
    pub dry_run: bool,
    /// Additional imports
    pub additional_imports: Option<Vec<String>>,
    /// Treat warnings as errors
    pub warning_as_error: bool,
    /// Indicates compilation for live analysis in language server
    pub design_time_build: bool,
    /// Trace areas to enable
    pub trace: Option<Vec<String>>,
}

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

    #[test]
    fn test_diagnostic_new() {
        let diag = Diagnostic::new("test-code", "Test message");
        assert_eq!(diag.code, "test-code");
        assert_eq!(diag.message, "Test message");
        assert_eq!(diag.severity, DiagnosticSeverity::Error);
    }

    #[test]
    fn test_diagnostic_error() {
        let diag = Diagnostic::error("err-code", "Error message");
        assert_eq!(diag.severity, DiagnosticSeverity::Error);
    }

    #[test]
    fn test_diagnostic_warning() {
        let diag = Diagnostic::warning("warn-code", "Warning message");
        assert_eq!(diag.severity, DiagnosticSeverity::Warning);
    }

    #[test]
    fn test_diagnostic_collector_new() {
        let collector = DiagnosticCollector::new();
        assert!(!collector.has_diagnostics());
    }

    #[test]
    fn test_diagnostic_collector_add() {
        let mut collector = DiagnosticCollector::new();
        collector.add(Diagnostic::error("code", "message"));
        assert!(collector.has_diagnostics());
        assert_eq!(collector.diagnostics().len(), 1);
    }

    #[test]
    fn test_diagnostic_collector_clear() {
        let mut collector = DiagnosticCollector::new();
        collector.add(Diagnostic::error("code", "message"));
        collector.clear();
        assert!(!collector.has_diagnostics());
    }

    #[test]
    fn test_diagnostic_collector_pipe() {
        let mut collector = DiagnosticCollector::new();
        let result = collector.pipe((42, vec![Diagnostic::error("code", "msg")]));
        assert_eq!(result, 42);
        assert_eq!(collector.diagnostics().len(), 1);
    }

    #[test]
    fn test_ignore_diagnostics() {
        let result = ignore_diagnostics((42, vec![Diagnostic::error("code", "msg")]));
        assert_eq!(result, 42);
    }

    #[test]
    fn test_source_location() {
        let loc = SourceLocation {
            file: "test.tsp".to_string(),
            start: 10,
            end: 20,
            is_synthetic: false,
        };
        assert_eq!(loc.file, "test.tsp");
        assert_eq!(loc.start, 10);
        assert_eq!(loc.end, 20);
    }

    // ============================================================================
    // DiagnosticSeverity Display tests
    // ============================================================================

    #[test]
    fn test_severity_display() {
        assert_eq!(format!("{}", DiagnosticSeverity::Error), "error");
        assert_eq!(format!("{}", DiagnosticSeverity::Warning), "warning");
        assert_eq!(format!("{}", DiagnosticSeverity::Information), "info");
        assert_eq!(format!("{}", DiagnosticSeverity::Hint), "hint");
    }

    // ============================================================================
    // DiagnosticDefinition tests
    // ============================================================================

    #[test]
    fn test_diagnostic_definition_error() {
        let def = DiagnosticDefinition::error("Something went wrong");
        assert_eq!(def.severity, DiagnosticSeverity::Error);
        assert_eq!(def.get_message("default"), Some("Something went wrong"));
        assert!(def.url.is_none());
    }

    #[test]
    fn test_diagnostic_definition_warning() {
        let def = DiagnosticDefinition::warning("Be careful");
        assert_eq!(def.severity, DiagnosticSeverity::Warning);
        assert_eq!(def.get_message("default"), Some("Be careful"));
    }

    #[test]
    fn test_diagnostic_definition_with_url() {
        let def = DiagnosticDefinition {
            severity: DiagnosticSeverity::Error,
            messages: vec![(
                "default".to_string(),
                DiagnosticMessageDefinition {
                    text: "Error".to_string(),
                },
            )],
            url: Some("https://example.com/docs".to_string()),
        };
        assert_eq!(def.url, Some("https://example.com/docs".to_string()));
    }

    #[test]
    fn test_diagnostic_definition_multiple_messages() {
        let def = DiagnosticDefinition {
            severity: DiagnosticSeverity::Error,
            messages: vec![
                (
                    "default".to_string(),
                    DiagnosticMessageDefinition {
                        text: "Default message".to_string(),
                    },
                ),
                (
                    "atPath".to_string(),
                    DiagnosticMessageDefinition {
                        text: "Error at path {path}".to_string(),
                    },
                ),
            ],
            url: None,
        };
        assert_eq!(def.get_message("default"), Some("Default message"));
        assert_eq!(def.get_message("atPath"), Some("Error at path {path}"));
        assert_eq!(def.get_message("nonexistent"), None);
    }

    // ============================================================================
    // DiagnosticCreator tests
    // ============================================================================

    #[test]
    fn test_diagnostic_creator_simple() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "invalid-argument".to_string(),
                DiagnosticDefinition::error("Invalid argument"),
            )]),
            None,
        );
        let diag = creator.create_diagnostic("invalid-argument", None, &[]);
        assert_eq!(diag.code, "invalid-argument");
        assert_eq!(diag.message, "Invalid argument");
        assert_eq!(diag.severity, DiagnosticSeverity::Error);
    }

    #[test]
    fn test_diagnostic_creator_with_library_name() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "invalid-argument".to_string(),
                DiagnosticDefinition::error("Invalid argument"),
            )]),
            Some("myLib".to_string()),
        );
        let diag = creator.create_diagnostic("invalid-argument", None, &[]);
        assert_eq!(diag.code, "myLib/invalid-argument");
    }

    #[test]
    fn test_diagnostic_creator_with_format() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "wrong-type".to_string(),
                DiagnosticDefinition::error("Expected {expected} but got {actual}"),
            )]),
            None,
        );
        let diag = creator.create_diagnostic(
            "wrong-type",
            None,
            &[
                ("expected".to_string(), "string".to_string()),
                ("actual".to_string(), "number".to_string()),
            ],
        );
        assert_eq!(diag.message, "Expected string but got number");
    }

    #[test]
    fn test_diagnostic_creator_warning() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "deprecated".to_string(),
                DiagnosticDefinition::warning("'{name}' is deprecated"),
            )]),
            None,
        );
        let diag = creator.create_diagnostic(
            "deprecated",
            None,
            &[("name".to_string(), "oldApi".to_string())],
        );
        assert_eq!(diag.severity, DiagnosticSeverity::Warning);
        assert_eq!(diag.message, "'oldApi' is deprecated");
    }

    #[test]
    fn test_diagnostic_creator_with_message_id() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "invalid-value".to_string(),
                DiagnosticDefinition {
                    severity: DiagnosticSeverity::Error,
                    messages: vec![
                        (
                            "default".to_string(),
                            DiagnosticMessageDefinition {
                                text: "Invalid value".to_string(),
                            },
                        ),
                        (
                            "atPath".to_string(),
                            DiagnosticMessageDefinition {
                                text: "Invalid value at path {path}".to_string(),
                            },
                        ),
                    ],
                    url: None,
                },
            )]),
            None,
        );
        let diag = creator.create_diagnostic(
            "invalid-value",
            Some("atPath"),
            &[("path".to_string(), "foo.bar".to_string())],
        );
        assert_eq!(diag.message, "Invalid value at path foo.bar");
    }

    #[test]
    fn test_diagnostic_creator_unknown_code_returns_fallback() {
        let creator = DiagnosticCreator::new(
            HashMap::from([(
                "known-code".to_string(),
                DiagnosticDefinition::error("Known"),
            )]),
            Some("myLib".to_string()),
        );
        let diag = creator.create_diagnostic("unknown-code", None, &[]);
        assert_eq!(diag.code, "myLib/unknown-code");
        assert_eq!(diag.severity, DiagnosticSeverity::Warning);
        assert!(diag.message.contains("unknown-code"));
    }

    #[test]
    fn test_diagnostic_creator_accessors() {
        let creator = DiagnosticCreator::new(
            HashMap::from([("test".to_string(), DiagnosticDefinition::error("Test"))]),
            Some("myLib".to_string()),
        );
        assert_eq!(creator.library_name(), Some("myLib"));
        assert_eq!(creator.diagnostics().len(), 1);
    }

    // ============================================================================
    // CompilerOptions tests
    // ============================================================================

    #[test]
    fn test_compiler_options_default() {
        let opts = CompilerOptions::default();
        assert!(opts.output_dir.is_none());
        assert!(opts.emit.is_none());
        assert!(!opts.list_files);
        assert!(!opts.nostdlib);
        assert!(!opts.no_emit);
        assert!(!opts.dry_run);
        assert!(!opts.ignore_deprecated);
        assert!(!opts.warning_as_error);
        assert!(!opts.design_time_build);
    }

    #[test]
    fn test_compiler_options_with_values() {
        let opts = CompilerOptions {
            output_dir: Some("./tsp-output".to_string()),
            emit: Some(vec!["@typespec/openapi".to_string()]),
            nostdlib: true,
            warning_as_error: true,
            ..Default::default()
        };
        assert_eq!(opts.output_dir.as_deref(), Some("./tsp-output"));
        assert_eq!(opts.emit.as_ref().map(|v| v.len()), Some(1));
        assert!(opts.nostdlib);
        assert!(opts.warning_as_error);
    }

    #[test]
    fn test_compiler_options_additional_imports() {
        let opts = CompilerOptions {
            additional_imports: Some(vec!["../common/main.tsp".to_string()]),
            ..Default::default()
        };
        assert_eq!(opts.additional_imports.as_ref().map(|v| v.len()), Some(1));
    }

    #[test]
    fn test_compiler_options_trace() {
        let opts = CompilerOptions {
            trace: Some(vec!["binder".to_string(), "checker".to_string()]),
            ..Default::default()
        };
        assert_eq!(opts.trace.as_ref().map(|v| v.len()), Some(2));
    }

    // ============================================================================
    // get_related_locations / get_source_location / get_diagnostic_template_instantiation_trace tests
    // ============================================================================

    #[test]
    fn test_get_related_locations_no_location() {
        let diag = Diagnostic::error("test", "msg");
        let related = get_related_locations(&diag);
        assert!(related.is_empty());
    }

    #[test]
    fn test_get_source_location_with_location() {
        let mut diag = Diagnostic::error("test", "msg");
        diag.location = Some(SourceLocation {
            file: "test.tsp".to_string(),
            start: 0,
            end: 10,
            is_synthetic: false,
        });
        let loc = get_source_location(&diag);
        assert!(loc.is_some());
        assert_eq!(loc.unwrap().file, "test.tsp");
    }

    #[test]
    fn test_get_source_location_without_location() {
        let diag = Diagnostic::error("test", "msg");
        let loc = get_source_location(&diag);
        assert!(loc.is_none());
    }

    #[test]
    fn test_get_diagnostic_template_instantiation_trace() {
        let diag = Diagnostic::error("test", "msg");
        let trace = get_diagnostic_template_instantiation_trace(&diag);
        // Currently returns empty — will be populated when template system is complete
        assert!(trace.is_empty());
    }
}