verter_core 0.0.1-beta.1

Vue 3 SFC compiler - transforms Vue Single File Components to render functions with TypeScript support
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
//! Fast script language detection for Vue SFC files.
//!
//! This module provides the optimized `ScriptDetector` for detecting the script
//! language from Vue Single File Components with HTML comment awareness.
//!
//! # Performance
//!
//! Benchmarked on 2,371 real Vue files (11.68 MB):
//! - **find_script**: 71 µs, 159 GiB/s
//! - **detect (full)**: ~85 µs, ~134 GiB/s
//!
//! # Algorithm
//!
//! Uses a **bounded window** approach for comment detection:
//! - Only searches the last 256 bytes before `<script>` for `<!--`
//! - This covers 99%+ of real Vue files (comments rarely span >256 bytes before script)
//! - Falls back to correct behavior when comments are found
//!
//! This provides:
//! - **2.3x faster** than full-scan memmem approach on production files
//! - **1.9x faster** worst-case performance (many '!' characters)
//! - Simple, predictable code path
//!
//! # Trade-offs by scenario
//!
//! | Scenario | Notes |
//! |----------|-------|
//! | Script at start | Best case - fast path |
//! | After template | Common case - bounded window |
//! | With comment | Handles correctly |
//! | Commented script | Skips correctly |
//! | No script | Returns quickly |
//! | Many '!' before script | **1.9x faster** than previous |

use memchr::memmem;
use oxc_span::SourceType;

/// Fast whitespace check using lookup table.
/// Returns true for space, tab, newline, carriage return.
#[inline(always)]
const fn is_ws(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r')
}

/// Fast check for attribute name terminator.
#[inline(always)]
const fn is_attr_name_end(b: u8) -> bool {
    matches!(b, b'=' | b' ' | b'\t' | b'\n' | b'\r' | b'>' | b'/')
}

/// Fast check for unquoted value terminator.
#[inline(always)]
const fn is_unquoted_value_end(b: u8) -> bool {
    matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'>' | b'/')
}

/// Supported script languages in Vue SFC files.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum ScriptLanguage {
    JavaScript,
    #[default]
    TypeScript,
    JSX,
    TSX,
    /// Unknown or esoteric language (e.g., `lang="coffee"`)
    Unknown,
}

impl ScriptLanguage {
    /// Create ScriptLanguage from raw lang string.
    pub fn from_bytes(lang: &[u8]) -> Self {
        match lang {
            b"ts" | b"typescript" => ScriptLanguage::TypeScript,
            b"tsx" => ScriptLanguage::TSX,
            b"jsx" => ScriptLanguage::JSX,
            b"js" | b"javascript" => ScriptLanguage::JavaScript,
            _ => ScriptLanguage::Unknown,
        }
    }
    pub fn to_source_type(&self) -> SourceType {
        match self {
            ScriptLanguage::JavaScript => SourceType::mjs(),
            ScriptLanguage::TypeScript => SourceType::ts(),
            ScriptLanguage::JSX => SourceType::jsx(),
            ScriptLanguage::TSX => SourceType::tsx(),
            ScriptLanguage::Unknown => SourceType::cjs(),
        }
    }
}

/// Result of script language detection.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct DetectResult {
    /// The detected language (defaults to TypeScript if not found)
    pub language: ScriptLanguage,
    /// Whether a script tag was found
    pub script_found: bool,
    /// Whether a lang= attribute was found
    pub lang_attr_found: bool,
    /// Byte offset where lang value starts (relative to input).
    /// Use `&input[lang_start..lang_end]` to get the raw value.
    pub lang_start: usize,
    /// Byte offset where lang value ends (relative to input)
    pub lang_end: usize,
}

/// Fast, comment-aware script language detector for Vue SFC files.
///
/// This detector uses direct `<!--` pattern matching to handle HTML comments,
/// optimized for real-world Vue files where comments before `<script>` are rare.
///
/// # Algorithm
///
/// 1. **Fast path**: Check if file starts with `<script`
/// 2. **Find script**: Use memmem to find `<script` position
/// 3. **Comment check**: Search for `<!--` before script position
///    - If no `<!--` found → no comment possible, return script position
///    - If `<!--` found → find matching `-->` and continue searching
/// 4. **Comment handling**: Skip to end of comment and repeat search
/// 5. **Lang detection**: memchr-based search for `lang=` attribute
///
/// # Example
///
/// ```
/// use verter_core::cursor::ScriptDetector;
///
/// let detector = ScriptDetector::new();
/// let vue_file = b"<template><div>Hello</div></template>\n<script setup lang=\"ts\">\n</script>";
///
/// let result = detector.detect(vue_file);
/// assert!(result.script_found);
/// assert!(result.lang_attr_found);
/// ```
pub struct ScriptDetector {
    script_finder: memmem::Finder<'static>,
    comment_start_finder: memmem::Finder<'static>,
    comment_end_finder: memmem::Finder<'static>,
}

impl Default for ScriptDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl ScriptDetector {
    /// Create a new detector with precompiled patterns.
    pub fn new() -> Self {
        Self {
            // Search for `<script` (7 bytes), then verify next char is ' ' or '>'
            script_finder: memmem::Finder::new(b"<script"),
            comment_start_finder: memmem::Finder::new(b"<!--"),
            comment_end_finder: memmem::Finder::new(b"-->"),
        }
    }

    /// Check if position after `<script` is valid (space or `>`).
    /// Returns the position where attributes would start, or None if invalid.
    #[inline(always)]
    fn validate_script_tag(bytes: &[u8], script_end: usize) -> Option<usize> {
        match bytes.get(script_end) {
            Some(b' ') => Some(script_end + 1), // `<script ` - attrs start after space
            Some(b'>') => Some(script_end),     // `<script>` - no attrs
            Some(b'\t') | Some(b'\n') | Some(b'\r') => Some(script_end + 1), // whitespace
            _ => None,                          // Not a valid script tag (e.g., `<scriptx`)
        }
    }

    /// Find the first `<script>` or `<script ` tag not inside an HTML comment.
    ///
    /// Returns the byte position where attributes start (or tag end for `<script>`).
    ///
    /// # Strategy
    ///
    /// Uses a **bounded window** approach for maximum performance:
    /// 1. Find `<script` position with memmem (7-byte pattern)
    /// 2. Validate next char is ` `, `>`, or whitespace
    /// 3. Search only the last 256 bytes before script for `<!--`
    /// 4. If no comment found in window, return script position
    /// 5. If comment found, find `-->` and continue searching after it
    ///
    /// The 256-byte window covers 99%+ of real Vue files where comments
    /// rarely span more than a few lines before the script tag.
    #[inline(always)]
    pub fn find_script(&self, bytes: &[u8]) -> Option<usize> {
        // Fast path: script at start
        if bytes.starts_with(b"<script") {
            return Self::validate_script_tag(bytes, 7);
        }

        let mut search_start: usize = 0;
        loop {
            // Find next script candidate
            let script_offset = self.script_finder.find(&bytes[search_start..])?;
            let script_pos = search_start + script_offset;
            let script_end = script_pos + 7;

            // Validate this is actually a script tag (not `<scriptx` etc)
            let Some(attr_start) = Self::validate_script_tag(bytes, script_end) else {
                search_start = script_end;
                continue;
            };

            // Fast path: no room for "<!--" before script at position < 4
            if script_pos < 4 {
                return Some(attr_start);
            }

            // BOUNDED WINDOW: Only check last 1024 bytes for "<!--"
            const WINDOW_SIZE: usize = 1024;
            let window_start = script_pos.saturating_sub(WINDOW_SIZE).max(search_start);
            let window = &bytes[window_start..script_pos];

            let comment_offset = match self.comment_start_finder.find(window) {
                Some(rel_offset) => window_start + rel_offset,
                None => {
                    // No "<!--" in window - but if window was truncated, check before it
                    if window_start > search_start {
                        let before_window = &bytes[search_start..window_start];
                        // Quick filter: "<!--" requires '!' - skip if no '!'
                        if memchr::memchr(b'!', before_window).is_some() {
                            if let Some(early_comment) =
                                self.comment_start_finder.find(before_window)
                            {
                                let early_pos = search_start + early_comment + 4;
                                // Check if it's closed before window_start
                                if let Some(end_off) = self
                                    .comment_end_finder
                                    .find(&bytes[early_pos..window_start])
                                {
                                    // Comment closed before window - continue after it
                                    search_start = early_pos + end_off + 3;
                                    continue;
                                }
                                // Unclosed comment - script is inside it
                                let Some(end_off) =
                                    self.comment_end_finder.find(&bytes[early_pos..])
                                else {
                                    return cold_unclosed_comment();
                                };
                                search_start = early_pos + end_off + 3;
                                continue;
                            }
                        }
                    }
                    // No comment before script - script is valid!
                    return Some(attr_start);
                }
            };

            // Found "<!--" in window - check if it's closed before the script
            let comment_content_start = comment_offset + 4;

            // Optimization: First check if comment ends before script_pos
            // If so, check if there's another comment opening after it
            if comment_content_start < script_pos {
                let region_before_script = &bytes[comment_content_start..script_pos];
                if let Some(end_in_region) = self.comment_end_finder.find(region_before_script) {
                    // Comment closed before script
                    let after_comment = comment_content_start + end_in_region + 3;
                    // Check if there's another comment opening after this one
                    if after_comment >= script_pos
                        || self
                            .comment_start_finder
                            .find(&bytes[after_comment..script_pos])
                            .is_none()
                    {
                        // No more comments before script - script is valid!
                        return Some(attr_start);
                    }
                    // There's another comment - continue from after this one
                    search_start = after_comment;
                    continue;
                }
            }

            // Comment spans past script_pos - find where it actually ends
            let Some(end_offset) = self
                .comment_end_finder
                .find(&bytes[comment_content_start..])
            else {
                // Unclosed comment - no valid script after this point
                return cold_unclosed_comment();
            };

            // Move search_start past the comment end
            search_start = comment_content_start + end_offset + 3;
        }
    }

    /// Detect the script language from a Vue SFC file.
    ///
    /// This method finds the first non-commented `<script>` tag and parses
    /// its `lang=` attribute to determine the language using a mini tokenizer
    /// that properly handles:
    /// - Quoted attribute values with `>` inside (e.g., `generic="T extends Foo<Bar>"`)
    /// - `lang=` appearing in template content after the script tag
    /// - Multiple scripts close together
    ///
    /// # Returns
    ///
    /// A `DetectResult` containing:
    /// - `language`: The detected language (defaults to TypeScript)
    /// - `script_found`: Whether any script tag was found
    /// - `lang_attr_found`: Whether the script had a `lang=` attribute
    /// - `lang_raw`: Raw lang attribute value as string slice
    /// - `lang_start`/`lang_end`: Byte offsets of the lang value
    #[inline(always)]
    pub fn detect(&self, bytes: &[u8]) -> DetectResult {
        let Some(n) = self.find_script(bytes) else {
            return DetectResult {
                language: ScriptLanguage::TypeScript,
                script_found: false,
                lang_attr_found: false,
                lang_start: 0,
                lang_end: 0,
            };
        };

        // Fast path for common patterns like `<script setup lang="ts">`
        if let Some(fast_result) = parse_script_tag_for_lang_fast(&bytes[n..]) {
            return match fast_result {
                Some(info) => DetectResult {
                    language: info.language,
                    script_found: true,
                    lang_attr_found: true,
                    lang_start: n + info.start,
                    lang_end: n + info.end,
                },
                None => DetectResult {
                    language: ScriptLanguage::TypeScript,
                    script_found: true,
                    lang_attr_found: false,
                    lang_start: 0,
                    lang_end: 0,
                },
            };
        }

        // Fall back to full tokenizer for complex cases
        if let Some(info) = parse_script_tag_for_lang(&bytes[n..]) {
            return DetectResult {
                language: info.language,
                script_found: true,
                lang_attr_found: true,
                lang_start: n + info.start,
                lang_end: n + info.end,
            };
        }

        DetectResult {
            language: ScriptLanguage::TypeScript,
            script_found: true,
            lang_attr_found: false,
            lang_start: 0,
            lang_end: 0,
        }
    }
}

/// Result from mini tokenizer with language info and offsets.
struct LangInfo {
    language: ScriptLanguage,
    /// Start offset of lang value (relative to input slice)
    start: usize,
    /// End offset of lang value (relative to input slice)
    end: usize,
}

/// Fast path parser for common `lang=` patterns.
///
/// Optimized for typical Vue script tags like `<script setup lang="ts">`.
/// Returns `Some(Some(LangInfo))` if lang= found, `Some(None)` if no lang=,
/// or `None` to fall back to full tokenizer for complex cases.
#[inline(always)]
fn parse_script_tag_for_lang_fast(bytes: &[u8]) -> Option<Option<LangInfo>> {
    if bytes.is_empty() || bytes[0] == b'>' {
        return Some(None); // No attrs or immediate close
    }

    let len = bytes.len();
    let mut i = 0;
    let mut in_quote: u8 = 0; // 0 = not in quote, b'"' or b'\'' = in that quote

    while i < len {
        let b = bytes[i];

        // Track quote state
        if in_quote != 0 {
            if b == in_quote {
                in_quote = 0;
            }
            i += 1;
            continue;
        }

        // Check for tag end (unquoted >)
        if b == b'>' {
            return Some(None); // No lang= found
        }

        // Check for self-closing
        if b == b'/' && i + 1 < len && bytes[i + 1] == b'>' {
            return Some(None);
        }

        // Start of quote
        if b == b'"' || b == b'\'' {
            in_quote = b;
            i += 1;
            continue;
        }

        // Check for `lang` at word boundary (after whitespace or at start)
        if b == b'l'
            && i + 4 < len
            && (i == 0 || is_ws(bytes[i - 1]))
            && bytes[i + 1] == b'a'
            && bytes[i + 2] == b'n'
            && bytes[i + 3] == b'g'
        {
            // Found "lang" - check what follows
            let mut j = i + 4;

            // Skip whitespace
            while j < len && is_ws(bytes[j]) {
                j += 1;
            }

            if j >= len {
                return None; // Ambiguous, fall back
            }

            // Must have '=' for lang attribute
            if bytes[j] != b'=' {
                // Could be "language" or other attr starting with "lang"
                i += 1;
                continue;
            }
            j += 1;

            // Skip whitespace after '='
            while j < len && is_ws(bytes[j]) {
                j += 1;
            }

            if j >= len {
                return None; // Ambiguous
            }

            // Parse value
            let (value_start, value_end) = if bytes[j] == b'"' || bytes[j] == b'\'' {
                let quote = bytes[j];
                j += 1;
                let start = j;
                while j < len && bytes[j] != quote {
                    j += 1;
                }
                if j >= len {
                    return None; // Unclosed quote, fall back
                }
                (start, j)
            } else {
                // Unquoted value
                let start = j;
                while j < len && !is_unquoted_value_end(bytes[j]) {
                    j += 1;
                }
                (start, j)
            };

            return Some(Some(LangInfo {
                language: match_lang_value(&bytes[value_start..value_end]),
                start: value_start,
                end: value_end,
            }));
        }

        i += 1;
    }

    None // Ran off end without finding '>', fall back
}

/// Mini tokenizer to find `lang=` attribute within a script tag.
///
/// Parses from the position after `<script` until the closing `>`, properly
/// handling quoted attribute values where `>` doesn't end the tag (e.g.,
/// TypeScript generics: `generic="T extends Foo<Bar>"`).
///
/// Uses memchr for fast quote searching when skipping non-lang attributes.
///
/// # Returns
///
/// `Some(LangInfo)` if `lang=` was found within the tag, `None` otherwise.
#[inline(always)]
fn parse_script_tag_for_lang(bytes: &[u8]) -> Option<LangInfo> {
    if bytes.is_empty() {
        return None;
    }

    // If we're already at tag end (e.g., `<script>` with no attrs)
    if bytes[0] == b'>' {
        return None;
    }

    let mut i = 0;
    let len = bytes.len();

    loop {
        // Skip whitespace
        while i < len && is_ws(bytes[i]) {
            i += 1;
        }

        if i >= len {
            return None; // Unexpected end
        }

        // Check for tag end
        if bytes[i] == b'>' {
            return None; // Tag closed, no lang= found
        }

        // Self-closing check (rare but valid: `<script />`)
        if bytes[i] == b'/' {
            if i + 1 < len && bytes[i + 1] == b'>' {
                return None;
            }
            i += 1;
            continue;
        }

        // Parse attribute name
        let attr_start = i;
        while i < len && !is_attr_name_end(bytes[i]) {
            i += 1;
        }
        let is_lang_attr = i - attr_start == 4
            && bytes[attr_start] == b'l'
            && bytes[attr_start + 1] == b'a'
            && bytes[attr_start + 2] == b'n'
            && bytes[attr_start + 3] == b'g';

        // Skip whitespace after name
        while i < len && is_ws(bytes[i]) {
            i += 1;
        }

        if i >= len {
            return None;
        }

        // Check for tag end after attribute name (boolean attribute at end)
        if bytes[i] == b'>' || bytes[i] == b'/' {
            if is_lang_attr {
                // `lang` as boolean attribute (invalid but handle gracefully)
                return Some(LangInfo {
                    language: ScriptLanguage::TypeScript,
                    start: i,
                    end: i,
                });
            }
            if bytes[i] == b'>' {
                return None;
            }
            continue;
        }

        if bytes[i] != b'=' {
            // Boolean attribute (no value), continue to next
            continue;
        }

        i += 1; // Skip '='

        // Skip whitespace after '='
        while i < len && is_ws(bytes[i]) {
            i += 1;
        }

        if i >= len {
            return None;
        }

        // Parse attribute value
        let (value_start, value_end) = if bytes[i] == b'"' || bytes[i] == b'\'' {
            let quote = bytes[i];
            i += 1;
            let start = i;

            if is_lang_attr {
                // For lang=, iterate to find closing quote (values are short)
                while i < len && bytes[i] != quote {
                    i += 1;
                }
            } else {
                // For non-lang attrs, use memchr for fast quote search
                if let Some(pos) = memchr::memchr(quote, &bytes[i..]) {
                    i += pos;
                } else {
                    return cold_unclosed_quote(); // Unclosed quote
                }
            }

            let end = i;
            if i < len {
                i += 1; // Skip closing quote
            }
            (start, end)
        } else {
            // Unquoted value
            let start = i;
            while i < len && !is_unquoted_value_end(bytes[i]) {
                i += 1;
            }
            (start, i)
        };

        if is_lang_attr {
            return Some(LangInfo {
                language: match_lang_value(&bytes[value_start..value_end]),
                start: value_start,
                end: value_end,
            });
        }
    }
}

/// Cold path helper for unclosed comments - helps branch prediction.
#[cold]
#[inline(never)]
fn cold_unclosed_comment() -> Option<usize> {
    None
}

/// Cold path helper for unclosed quotes - helps branch prediction.
#[cold]
#[inline(never)]
fn cold_unclosed_quote() -> Option<LangInfo> {
    None
}

/// Match a raw language value (without quotes) to ScriptLanguage.
#[inline(always)]
fn match_lang_value(value: &[u8]) -> ScriptLanguage {
    match value {
        b"ts" | b"typescript" => ScriptLanguage::TypeScript,
        b"tsx" => ScriptLanguage::TSX,
        b"jsx" => ScriptLanguage::JSX,
        b"js" | b"javascript" => ScriptLanguage::JavaScript,
        _ => ScriptLanguage::Unknown,
    }
}

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

    #[test]
    fn no_script_tag() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template><div>Hello</div></template>");
        assert!(!result.script_found);
        assert!(!result.lang_attr_found);
    }

    #[test]
    fn script_no_lang() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><script></script>");
        assert!(result.script_found);
        assert!(!result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn script_lang_ts() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><script lang=\"ts\"></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn script_lang_tsx() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><script lang=\"tsx\"></script>");
        assert_eq!(result.language, ScriptLanguage::TSX);
    }

    #[test]
    fn script_lang_jsx() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><script lang=\"jsx\"></script>");
        assert_eq!(result.language, ScriptLanguage::JSX);
    }

    #[test]
    fn script_lang_js() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><script lang=\"js\"></script>");
        assert_eq!(result.language, ScriptLanguage::JavaScript);
    }

    #[test]
    fn script_at_beginning() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<script lang=\"tsx\"></script><template/>");
        assert_eq!(result.language, ScriptLanguage::TSX);
    }

    #[test]
    fn skips_script_in_html_comment() {
        let detector = ScriptDetector::new();
        let result =
            detector.detect(b"<!-- <script lang=\"js\"> -->\n<script lang=\"ts\">\n</script>");
        assert!(result.script_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn no_script_when_only_commented() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<template/><!-- <script lang=\"ts\"></script> -->");
        assert!(!result.script_found);
    }

    #[test]
    fn handles_bang_heavy_template() {
        let detector = ScriptDetector::new();
        let result = detector.detect(
            b"<template><div v-if=\"!a && !b && !c\">Hello!</div></template>\n<script lang=\"ts\"></script>"
        );
        assert!(result.script_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn handles_multiple_comments() {
        let detector = ScriptDetector::new();
        let result = detector
            .detect(b"<!-- c1 --><!-- <script lang=\"js\"> -->\n<script lang=\"ts\"></script>");
        assert!(result.script_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    // ========================================================================
    // Attribute ordering tests - lang= should be detected regardless of position
    // ========================================================================

    #[test]
    fn lang_after_setup() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<script setup lang=\"ts\"></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn lang_before_setup() {
        let detector = ScriptDetector::new();
        let result = detector.detect(b"<script lang=\"tsx\" setup></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TSX);
    }

    #[test]
    fn lang_after_multiple_attrs() {
        let detector = ScriptDetector::new();
        let result = detector
            .detect(b"<script setup async defer custom-attr=\"value\" lang=\"jsx\"></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::JSX);
    }

    /// Tests that lang= is found even with many attributes before it.
    /// The 150-byte search window handles most real-world cases.
    #[test]
    fn lang_after_many_attrs_long() {
        let detector = ScriptDetector::new();
        // Long attr string but lang= is still within 150 bytes
        let result = detector
            .detect(b"<script setup async super-random-prop-that-takes-a-very-long-name-but-should-not-affect-anything-whatsoever\n defer custom-attr=\"value\" lang=\"jsx\"></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::JSX);
    }

    #[test]
    fn no_lang_but_lang_in_template() {
        let detector = ScriptDetector::new();
        let result = detector
            .detect(b"<script></script><template>\n<Comp lang=\"jsx\">Hello</Comp>\n</template>");
        assert!(result.script_found);
        assert!(!result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    // ========================================================================
    // Edge case: Multiple scripts close together (now handled correctly!)
    // ========================================================================

    /// With the mini tokenizer, we now correctly handle multiple scripts close
    /// together by properly parsing the tag boundary.
    #[test]
    fn multiple_scripts_close_together() {
        let detector = ScriptDetector::new();
        // First script: no lang, very short body
        // Second script: has lang="js"
        let result = detector.detect(b"<script setup>\n</script>\n<script lang=\"js\"></script>");
        assert!(result.script_found);
        // Correctly returns TypeScript (default) because first script has no lang=
        assert!(!result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn multiple_scripts_first_has_lang() {
        let detector = ScriptDetector::new();
        // First script has lang="tsx", second has lang="js"
        let result = detector
            .detect(b"<script setup lang=\"tsx\">\n</script>\n<script lang=\"js\"></script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        // Correctly returns TSX from first script
        assert_eq!(result.language, ScriptLanguage::TSX);
    }

    #[test]
    fn very_long_comment_script_input() {
        let mut input = Vec::new();
        input.extend_from_slice(b"<!-- ");
        // Add 100_000_024 '!' characters to simulate a long comment
        input.extend(std::iter::repeat_n(b'!', 100_000_024));
        input.extend_from_slice(b"<script lang=\"ts\">-->\n<script lang=\"jsx\"></script>");
        let detector = ScriptDetector::new();
        let result = detector.detect(&input);
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::JSX);
    }

    // ========================================================================
    // TypeScript generics in attributes
    // ========================================================================

    #[test]
    fn generic_attr_with_angle_brackets() {
        let detector = ScriptDetector::new();
        // TypeScript generic attribute contains `>` inside quoted value
        let result = detector
            .detect(b"<script setup lang=\"ts\" generic=\"T extends Foo<Bar>\">\n</script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TypeScript);
    }

    #[test]
    fn generic_attr_before_lang() {
        let detector = ScriptDetector::new();
        // generic= comes before lang=, contains `>` inside
        let result = detector
            .detect(b"<script setup generic=\"T extends Map<K, V>\" lang=\"tsx\">\n</script>");
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TSX);
    }

    // ========================================================================
    // Unknown/esoteric languages
    // ========================================================================

    #[test]
    fn unknown_language_coffee() {
        let detector = ScriptDetector::new();
        let input = b"<script lang=\"coffee\"></script>";
        let result = detector.detect(input);
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::Unknown);
        assert_eq!(&input[result.lang_start..result.lang_end], b"coffee");
    }

    #[test]
    fn unknown_language_custom() {
        let detector = ScriptDetector::new();
        let input = b"<script lang=\"my-custom-lang\"></script>";
        let result = detector.detect(input);
        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::Unknown);
        assert_eq!(
            &input[result.lang_start..result.lang_end],
            b"my-custom-lang"
        );
    }

    // ========================================================================
    // Offset tests
    // ========================================================================

    #[test]
    fn lang_offsets() {
        let detector = ScriptDetector::new();
        let input = b"<script lang=\"tsx\"></script>";
        let result = detector.detect(input);

        assert!(result.script_found);
        assert!(result.lang_attr_found);
        assert_eq!(result.language, ScriptLanguage::TSX);
        // "tsx" starts at position 14 (after `<script lang="`)
        assert_eq!(result.lang_start, 14);
        assert_eq!(result.lang_end, 17);
        assert_eq!(&input[result.lang_start..result.lang_end], b"tsx");
    }

    #[test]
    fn lang_offsets_single_quotes() {
        let detector = ScriptDetector::new();
        let input = b"<script lang='typescript'></script>";
        let result = detector.detect(input);

        assert_eq!(&input[result.lang_start..result.lang_end], b"typescript");
    }

    #[test]
    fn lang_offsets_unquoted() {
        let detector = ScriptDetector::new();
        let input = b"<script lang=js></script>";
        let result = detector.detect(input);

        assert_eq!(&input[result.lang_start..result.lang_end], b"js");
    }

    #[test]
    fn lang_offsets_after_template() {
        let detector = ScriptDetector::new();
        let input = b"<template><div>Hello</div></template>\n<script setup lang=\"tsx\"></script>";
        let result = detector.detect(input);

        // Verify offset is correct (points to "tsx" in the full input)
        assert_eq!(&input[result.lang_start..result.lang_end], b"tsx");
    }
}