fallow-extract 2.100.0

AST extraction engine for fallow codebase intelligence (parser, complexity, SFC / Astro / MDX / CSS)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
//! Vue/Svelte Single File Component (SFC) script and style extraction.
//!
//! Extracts `<script>` block content from `.vue` and `.svelte` files using regex,
//! handling `lang`, `src` metadata, and `generic` attributes, and filtering
//! HTML comments. Vue external script references are emitted as graph edges;
//! Svelte markup-level script `src` references are treated as runtime HTML.
//! Also extracts `<style>` block sources (`@import` / `@use` / `@forward` /
//! `@plugin` and `<style src="...">`) so referenced CSS / SCSS files become
//! reachable from the component, preventing false `unused-files` reports on
//! co-located styles.

use std::path::Path;
use std::sync::LazyLock;

use oxc_allocator::Allocator;
use oxc_ast_visit::Visit;
use oxc_parser::Parser;
use oxc_span::SourceType;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::asset_url::normalize_asset_url;
use crate::parse::{
    compute_auto_import_candidates, compute_import_binding_usage, compute_semantic_usage,
};
use crate::sfc_template::{SfcKind, collect_template_usage_with_bound_targets};
use crate::source_map::ExtractionResult;
use crate::visitor::ModuleInfoExtractor;
use crate::{ImportInfo, ImportedName, ModuleInfo};
use fallow_types::discover::FileId;
use fallow_types::extract::{FunctionComplexity, byte_offset_to_line_col, compute_line_offsets};
use oxc_span::Span;

/// Regex to extract `<script>` block content from Vue/Svelte SFCs.
/// The attrs pattern handles `>` inside quoted attribute values (e.g., `generic="T extends Foo<Bar>"`).
static SCRIPT_BLOCK_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    crate::static_regex(
        r#"(?is)<script\b(?P<attrs>(?:[^>"']|"[^"]*"|'[^']*')*)>(?P<body>[\s\S]*?)</script>"#,
    )
});

/// Regex to extract the `lang` attribute value from a script tag.
static LANG_ATTR_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r#"lang\s*=\s*["'](\w+)["']"#));

/// Regex to extract the `src` attribute value from a script tag.
/// Requires whitespace (or start of string) before `src` to avoid matching `data-src` etc.
static SRC_ATTR_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r#"(?:^|\s)src\s*=\s*["']([^"']+)["']"#));

/// Regex to detect Vue's bare `setup` attribute.
static SETUP_ATTR_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r"(?:^|\s)setup(?:\s|$)"));

/// Regex to detect Svelte's `context="module"` attribute (Svelte 4).
static CONTEXT_MODULE_ATTR_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r#"context\s*=\s*["']module["']"#));

/// Regex to detect Svelte 5's bare `module` script attribute (`<script module>`,
/// `<script module lang="ts">`). Anchored like [`SETUP_ATTR_RE`] so `module` must
/// be a standalone attribute, not a substring of another attr name (e.g.
/// `data-module`) or value.
static SVELTE_MODULE_ATTR_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r"(?:^|\s)module(?:\s|$|=)"));

/// Regex to extract Vue's `generic="..."` attribute value (script-setup
/// generics). Matches the contents between the quotes and stops at the
/// closing quote, mirroring `LANG_ATTR_RE`.
static VUE_GENERIC_ATTR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    crate::static_regex(r#"(?:^|\s)generic\s*=\s*"([^"]*)"|(?:^|\s)generic\s*=\s*'([^']*)'"#)
});

/// Regex to extract Svelte's `generics="..."` attribute value (Svelte 4
/// generic script attribute, repurposed by some Svelte 5 code).
static SVELTE_GENERICS_ATTR_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    crate::static_regex(r#"(?:^|\s)generics\s*=\s*"([^"]*)"|(?:^|\s)generics\s*=\s*'([^']*)'"#)
});

/// Regex to match HTML comments for filtering script blocks inside comments.
static HTML_COMMENT_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r"(?s)<!--.*?-->"));

/// Regex to detect a whole-object prop/attr spread in a Vue template:
/// `v-bind="$attrs"`, `v-bind="$props"`, or `v-bind="props"` (with single or
/// double quotes). A bound prop may be consumed indirectly, so the
/// `unused-component-prop` detector abstains on the whole file when this matches.
static PROPS_ATTRS_SPREAD_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r#"v-bind\s*=\s*["'](?:\$attrs|\$props|props)["']"#));

/// FP-1 (unused-load-data-key): a SvelteKit route component passing the whole
/// `data` prop opaquely in MARKUP, where a child reads arbitrary keys the
/// detector cannot see. Matches `data={data}` (whole-prop pass to a child) and
/// `{...data}` (Svelte template spread). The script-side `const x = {...data}` /
/// `fn(data)` / `const X = data` forms are captured by the JS visitor instead.
/// Only a whole-`data` pass forces the abstain; `data.x` member access stays a
/// credited consumer.
static SVELTE_TEMPLATE_DATA_WHOLE_USE_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r"(?:=\s*\{\s*data\s*\}|\{\s*\.\.\.\s*data\s*\})"));

/// Matches an emit-style call in template markup: a callee identifier (or
/// `$emit`) followed by `(` and its first argument. Group 1 is the callee name
/// (filtered against the harvested emit binding / `$emit` by the caller), groups
/// 2 and 3 are a string-literal first arg (single- or double-quoted: the event
/// name, credited as used), and group 4 is the first non-space character of a
/// NON-literal first arg (a dynamic emit, whose event name is unknowable, forcing
/// a whole-file abstain). Event names allow kebab and namespaced forms
/// (`update:modelValue`, `my-event`). The Rust `regex` crate has no
/// backreferences, so the two quote styles are separate alternatives.
static TEMPLATE_EMIT_CALL_RE: LazyLock<regex::Regex> =
    LazyLock::new(|| crate::static_regex(r#"([\w$]+)\s*\(\s*(?:'([\w:-]*)'|"([\w:-]*)"|(\S))"#));

/// Regex to extract `<style>` block content from Vue/Svelte SFCs.
/// Mirrors `SCRIPT_BLOCK_RE`: handles `>` inside quoted attribute values and
/// captures the body so `@import` / `@use` / `@forward` directives can be parsed.
static STYLE_BLOCK_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    crate::static_regex(
        r#"(?is)<style\b(?P<attrs>(?:[^>"']|"[^"]*"|'[^']*')*)>(?P<body>[\s\S]*?)</style>"#,
    )
});

/// Static asset references in SFC markup: `<img src="./logo.png">`,
/// `<source src="...">`, `<video poster="...">`, etc.
///
/// Scoped to genuine asset elements (`img` / `source` / `video` / `audio` /
/// `track` / `embed`) so a custom component's `src` PROP (`<MyImage src="./x">`)
/// is never misread as an asset edge. ONLY plain relative literals (`./` or
/// `../`) are captured: dynamic bindings (`:src`, `v-bind:src`, `bind:src`,
/// `src={...}`, `data-src`), alias-prefixed (`@/`), root-relative (`/foo`),
/// remote, interpolated (`{{ }}` / `{ }`), and query/hash-suffixed values are
/// all skipped (the value class excludes `{`, `?`, `#`, whitespace, and angle
/// brackets, and the alternation anchors on a leading `./` or `../`). A
/// captured ref becomes a `SideEffect` import; an existing asset resolves to
/// `ExternalFile` (no finding) and a genuinely-missing one surfaces as
/// `unresolved-import` on the trusted resolver path.
static TEMPLATE_ASSET_RE: LazyLock<regex::Regex> = LazyLock::new(|| {
    crate::static_regex(
        r#"(?si)<(?:img|source|video|audio|track|embed)\b(?:[^>"']|"[^"]*"|'[^']*')*?\s(?:src|poster)\s*=\s*(?:"((?:\./|\.\./)[^"<>{}?#\s]*)"|'((?:\./|\.\./)[^'<>{}?#\s]*)')"#,
    )
});

/// Mask `<script>` / `<style>` blocks and HTML comments to equal-length spaces
/// so a markup-region scan (asset refs) sees only the template, while byte
/// offsets still map 1:1 into the original source for line/col reporting.
fn mask_non_markup_regions(source: &str) -> String {
    let mut masked = source.to_string();
    for re in [&*SCRIPT_BLOCK_RE, &*STYLE_BLOCK_RE, &*HTML_COMMENT_RE] {
        masked = re
            .replace_all(&masked, |caps: &regex::Captures<'_>| {
                " ".repeat(caps[0].len())
            })
            .into_owned();
    }
    masked
}

/// Collect static relative asset references from SFC markup as
/// `(normalized_specifier, value_span)` pairs. See [`TEMPLATE_ASSET_RE`].
fn collect_template_asset_refs(source: &str) -> Vec<(String, Span)> {
    let masked = mask_non_markup_regions(source);
    let mut refs = Vec::new();
    for caps in TEMPLATE_ASSET_RE.captures_iter(&masked) {
        let Some(value) = caps.get(1).or_else(|| caps.get(2)) else {
            continue;
        };
        let raw = value.as_str();
        if raw.is_empty() {
            continue;
        }
        refs.push((
            normalize_asset_url(raw),
            Span::new(value.start() as u32, value.end() as u32),
        ));
    }
    refs
}

/// An extracted `<script>` block from a Vue or Svelte SFC.
pub struct SfcScript {
    /// The script body text.
    pub body: String,
    /// Whether the script uses TypeScript (`lang="ts"` or `lang="tsx"`).
    pub is_typescript: bool,
    /// Whether the script uses JSX syntax (`lang="tsx"` or `lang="jsx"`).
    pub is_jsx: bool,
    /// Byte offset of the script body within the full SFC source.
    pub byte_offset: usize,
    /// External script source path from `src` attribute.
    pub src: Option<String>,
    /// Span of the `src` attribute value in the full SFC source.
    pub src_span: Option<Span>,
    /// Whether this script is a Vue `<script setup>` block.
    pub is_setup: bool,
    /// Whether this script is a Svelte module-context block.
    pub is_context_module: bool,
    /// Type-parameter list from a `generic="..."` (Vue) or `generics="..."`
    /// (Svelte) attribute on the script tag. Holds the bare constraint, no
    /// surrounding angle brackets, e.g. `T extends Test<boolean>`.
    pub generic_attr: Option<String>,
}

/// Extract all `<script>` blocks from a Vue/Svelte SFC source string.
pub fn extract_sfc_scripts(source: &str) -> Vec<SfcScript> {
    let comment_ranges: Vec<(usize, usize)> = HTML_COMMENT_RE
        .find_iter(source)
        .map(|m| (m.start(), m.end()))
        .collect();

    SCRIPT_BLOCK_RE
        .captures_iter(source)
        .filter(|cap| {
            let start = cap.get(0).map_or(0, |m| m.start());
            !comment_ranges
                .iter()
                .any(|&(cs, ce)| start >= cs && start < ce)
        })
        .map(|cap| {
            let attrs = cap.name("attrs").map_or("", |m| m.as_str());
            let body_match = cap.name("body");
            let byte_offset = body_match.map_or(0, |m| m.start());
            let body = body_match.map_or("", |m| m.as_str()).to_string();
            let lang = LANG_ATTR_RE
                .captures(attrs)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str());
            let is_typescript = matches!(lang, Some("ts" | "tsx"));
            let is_jsx = matches!(lang, Some("tsx" | "jsx"));
            let src = SRC_ATTR_RE
                .captures(attrs)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string());
            let attrs_start = cap.name("attrs").map_or(0, |m| m.start());
            let src_span = SRC_ATTR_RE.captures(attrs).and_then(|c| c.get(1)).map(|m| {
                Span::new(
                    (attrs_start + m.start()) as u32,
                    (attrs_start + m.end()) as u32,
                )
            });
            let is_setup = SETUP_ATTR_RE.is_match(attrs);
            // Svelte module context: Svelte 4 `context="module"` OR Svelte 5's
            // bare `module` attribute. Both scope declarations to the module
            // script (not the instance), so `is_template_visible_script` returns
            // false and the instance/module split for runes harvest is correct.
            let is_context_module =
                CONTEXT_MODULE_ATTR_RE.is_match(attrs) || SVELTE_MODULE_ATTR_RE.is_match(attrs);
            let generic_attr = VUE_GENERIC_ATTR_RE
                .captures(attrs)
                .or_else(|| SVELTE_GENERICS_ATTR_RE.captures(attrs))
                .and_then(|cap| cap.get(1).or_else(|| cap.get(2)))
                .map(|m| m.as_str().to_string())
                .filter(|value| !value.trim().is_empty());
            SfcScript {
                body,
                is_typescript,
                is_jsx,
                byte_offset,
                src,
                src_span,
                is_setup,
                is_context_module,
                generic_attr,
            }
        })
        .collect()
}

/// An extracted `<style>` block from a Vue or Svelte SFC.
pub struct SfcStyle {
    /// The style body text (CSS / SCSS / Sass / Less / Stylus / PostCSS source).
    pub body: String,
    /// The `lang` attribute value (`scss`, `sass`, `less`, `stylus`, `postcss`, ...).
    /// `None` for plain `<style>` (CSS).
    pub lang: Option<String>,
    /// External style source path from the `src` attribute (`<style src="./theme.scss">`).
    pub src: Option<String>,
    /// Span of the `src` attribute value in the full SFC source.
    pub src_span: Option<Span>,
    /// Byte offset of the style body within the full SFC source.
    pub byte_offset: usize,
}

/// A source region extracted from a larger file while preserving the byte
/// offset of the region body in the original source.
pub struct SourceRegion {
    /// Region body text.
    pub body: String,
    /// Byte offset of `body` within the original source.
    pub byte_offset: usize,
}

/// Extract template markup regions from a Vue/Svelte SFC.
///
/// The returned regions exclude `<script>` blocks, `<style>` blocks, and HTML
/// comments, so callers can tokenize authored markup without reading code or
/// comments as template text. Offsets always point into the original SFC source.
#[must_use]
pub fn extract_sfc_template_regions(source: &str) -> Vec<SourceRegion> {
    let mut ranges: Vec<(usize, usize)> = SCRIPT_BLOCK_RE
        .find_iter(source)
        .chain(STYLE_BLOCK_RE.find_iter(source))
        .chain(HTML_COMMENT_RE.find_iter(source))
        .map(|m| (m.start(), m.end()))
        .collect();
    ranges.sort_unstable_by_key(|(start, _)| *start);
    ranges_to_gaps(source, &ranges)
}

/// Extract all `<style>` blocks from a Vue/Svelte SFC source string.
///
/// Mirrors [`extract_sfc_scripts`]: filters blocks inside HTML comments and
/// captures the `lang` and `src` attributes so the caller can route the body to
/// the right preprocessor's import scanner (currently only CSS / SCSS / Sass) or
/// seed the `src` reference as a side-effect import.
pub fn extract_sfc_styles(source: &str) -> Vec<SfcStyle> {
    let comment_ranges: Vec<(usize, usize)> = HTML_COMMENT_RE
        .find_iter(source)
        .map(|m| (m.start(), m.end()))
        .collect();

    STYLE_BLOCK_RE
        .captures_iter(source)
        .filter(|cap| {
            let start = cap.get(0).map_or(0, |m| m.start());
            !comment_ranges
                .iter()
                .any(|&(cs, ce)| start >= cs && start < ce)
        })
        .map(|cap| {
            let attrs = cap.name("attrs").map_or("", |m| m.as_str());
            let body = cap.name("body").map_or("", |m| m.as_str()).to_string();
            let byte_offset = cap.name("body").map_or(0, |m| m.start());
            let lang = LANG_ATTR_RE
                .captures(attrs)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string());
            let src = SRC_ATTR_RE
                .captures(attrs)
                .and_then(|c| c.get(1))
                .map(|m| m.as_str().to_string());
            let attrs_start = cap.name("attrs").map_or(0, |m| m.start());
            let src_span = SRC_ATTR_RE.captures(attrs).and_then(|c| c.get(1)).map(|m| {
                Span::new(
                    (attrs_start + m.start()) as u32,
                    (attrs_start + m.end()) as u32,
                )
            });
            SfcStyle {
                body,
                lang,
                src,
                src_span,
                byte_offset,
            }
        })
        .collect()
}

fn ranges_to_gaps(source: &str, ranges: &[(usize, usize)]) -> Vec<SourceRegion> {
    let mut regions = Vec::new();
    let mut cursor = 0;
    for &(start, end) in ranges {
        if start > cursor {
            push_region(source, cursor, start, &mut regions);
        }
        cursor = cursor.max(end);
    }
    if cursor < source.len() {
        push_region(source, cursor, source.len(), &mut regions);
    }
    regions
}

fn push_region(source: &str, start: usize, end: usize, regions: &mut Vec<SourceRegion>) {
    let Some(body) = source.get(start..end) else {
        return;
    };
    if body.trim().is_empty() {
        return;
    }
    regions.push(SourceRegion {
        body: body.to_string(),
        byte_offset: start,
    });
}

/// Check if a file path is a Vue or Svelte SFC (`.vue` or `.svelte`).
#[must_use]
pub fn is_sfc_file(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|ext| ext == "vue" || ext == "svelte")
}

/// Parse an SFC file by extracting and combining all `<script>` and `<style>` blocks.
pub(crate) fn parse_sfc_to_module(
    file_id: FileId,
    path: &Path,
    source: &str,
    content_hash: u64,
    need_complexity: bool,
) -> ModuleInfo {
    let scripts = extract_sfc_scripts(source);
    let styles = extract_sfc_styles(source);
    let kind = sfc_kind(path);
    let mut combined = empty_sfc_module(file_id, source, content_hash);
    let mut template_visible_imports: FxHashSet<String> = FxHashSet::default();
    let mut template_visible_bound_targets: FxHashMap<String, String> = FxHashMap::default();
    let mut props_return_binding: Option<String> = None;
    let mut emit_return_binding: Option<String> = None;

    for script in &scripts {
        merge_script_into_module(&mut SfcScriptMergeInput {
            kind,
            script,
            combined: &mut combined,
            template_visible_imports: &mut template_visible_imports,
            template_visible_bound_targets: &mut template_visible_bound_targets,
            props_return_binding: &mut props_return_binding,
            emit_return_binding: &mut emit_return_binding,
            need_complexity,
        });
    }

    for style in &styles {
        merge_style_into_module(style, &mut combined);
    }

    // Whole-object prop/attr spread in the template (`v-bind="$attrs"`,
    // `v-bind="$props"`, `v-bind="props"`) can consume a prop indirectly, so the
    // `unused-component-prop` detector must abstain on the whole file.
    if kind == SfcKind::Vue
        && !combined.component_props.is_empty()
        && PROPS_ATTRS_SPREAD_RE.is_match(source)
    {
        combined.has_props_attrs_fallthrough = true;
    }

    apply_template_usage(
        kind,
        source,
        &template_visible_imports,
        &template_visible_bound_targets,
        props_return_binding.as_deref(),
        kind == SfcKind::Svelte && is_sveltekit_route_data_component(path),
        &mut combined,
    );

    // Synthetic per-SFC `<template>` complexity: count template control flow
    // (`v-if`/`v-for`, `{#if}`/`{#each}`) and bound-expression/interpolation
    // complexity so a template-heavy SFC is not scored as artificially simple.
    // The scanners mask `<script>`/`<style>`/comments, so script control flow is
    // NOT double-counted (it is scored by `translate_script_complexity` above).
    // Mirrors Angular's `template_complexity` synthetic entry; no new rule or
    // threshold, the entry folds into the existing complexity aggregate.
    if need_complexity {
        let template_complexity = match kind {
            SfcKind::Vue => crate::template_complexity::compute_vue_template_complexity(source),
            SfcKind::Svelte => {
                crate::template_complexity::compute_svelte_template_complexity(source)
            }
        };
        combined.complexity.extend(template_complexity);
    }

    // Credit `<emit_binding>('event')` / `$emit('event')` calls in the template
    // (`@click="emit('close')"`), which the script-only emit usage walk cannot
    // see. A dynamic template emit (`$emit(someVar)`) abstains the whole file.
    if kind == SfcKind::Vue && !combined.component_emits.is_empty() {
        apply_template_emit_usage(source, emit_return_binding.as_deref(), &mut combined);
    }

    // Harvest Svelte template `on:<name>` listener bindings on component tags
    // into the per-file listened set; the `unused-svelte-event` detector unions
    // these project-wide to decide which dispatched events are dead.
    if kind == SfcKind::Svelte {
        combined.svelte_listened_events =
            crate::sfc_template::collect_svelte_listened_events(source);
    }

    // Static relative asset references in markup (`<img src="./logo.png">`)
    // become SideEffect imports so a genuinely-missing asset surfaces as
    // `unresolved-import` (existing assets resolve to `ExternalFile`, no finding).
    for (specifier, span) in collect_template_asset_refs(source) {
        combined.imports.push(ImportInfo {
            source: specifier,
            imported_name: ImportedName::SideEffect,
            local_name: String::new(),
            is_type_only: false,
            from_style: false,
            span,
            source_span: span,
        });
    }

    combined.unused_import_bindings.sort_unstable();
    combined.unused_import_bindings.dedup();
    combined.type_referenced_import_bindings.sort_unstable();
    combined.type_referenced_import_bindings.dedup();
    combined.value_referenced_import_bindings.sort_unstable();
    combined.value_referenced_import_bindings.dedup();
    combined.auto_import_candidates.sort_unstable();
    combined.auto_import_candidates.dedup();

    combined
}

fn sfc_kind(path: &Path) -> SfcKind {
    if path.extension().and_then(|ext| ext.to_str()) == Some("vue") {
        SfcKind::Vue
    } else {
        SfcKind::Svelte
    }
}

/// SvelteKit route components receive a `data` prop populated by the route's
/// `load()` return object. This predicate gates the `data`-as-template-root
/// credit (unused-load-data-key Primitive B) to exactly those files. It matches
/// `+page.svelte` / `+layout.svelte` AND their layout-reset variants
/// (`+page@.svelte`, `+page@named.svelte`, `+page@(group).svelte`, and the
/// `+layout@...` forms), all of which still receive the `data` prop. `+error.svelte`
/// is excluded (it receives `$page.error`, not the `load()` `data` prop), and a
/// non-route file like `+pageHelper.svelte` is excluded by the grammar (the part
/// after `+page` must be empty or start with `@`). The leading `+` is a
/// SvelteKit-only filename convention, so no ordinary `.svelte` component matches.
fn is_sveltekit_route_data_component(path: &Path) -> bool {
    let Some(stem) = path
        .file_name()
        .and_then(|name| name.to_str())
        .and_then(|name| name.strip_suffix(".svelte"))
    else {
        return false;
    };
    ["+page", "+layout"].iter().any(|prefix| {
        stem.strip_prefix(prefix)
            .is_some_and(|rest| rest.is_empty() || rest.starts_with('@'))
    })
}

fn empty_sfc_module(file_id: FileId, source: &str, content_hash: u64) -> ModuleInfo {
    let parsed = crate::suppress::parse_suppressions_from_source(source);

    ModuleInfo {
        file_id,
        exports: Vec::new(),
        imports: Vec::new(),
        re_exports: Vec::new(),
        dynamic_imports: Vec::new(),
        dynamic_import_patterns: Vec::new(),
        require_calls: Vec::new(),
        package_path_references: Vec::new(),
        member_accesses: Vec::new(),
        whole_object_uses: Vec::new(),
        has_cjs_exports: false,
        has_angular_component_template_url: false,
        content_hash,
        suppressions: parsed.suppressions,
        unknown_suppression_kinds: parsed.unknown_kinds,
        unused_import_bindings: Vec::new(),
        type_referenced_import_bindings: Vec::new(),
        value_referenced_import_bindings: Vec::new(),
        line_offsets: compute_line_offsets(source),
        complexity: Vec::new(),
        flag_uses: Vec::new(),
        class_heritage: vec![],
        injection_tokens: vec![],
        local_type_declarations: Vec::new(),
        public_signature_type_references: Vec::new(),
        namespace_object_aliases: Vec::new(),
        iconify_prefixes: Vec::new(),
        iconify_icon_names: Vec::new(),
        auto_import_candidates: Vec::new(),
        directives: Vec::new(),
        client_only_dynamic_import_spans: Vec::new(),
        security_sinks: Vec::new(),
        security_sinks_skipped: 0,
        security_unresolved_callee_sites: Vec::new(),
        tainted_bindings: Vec::new(),
        sanitized_sink_args: Vec::new(),
        security_control_sites: Vec::new(),
        callee_uses: Vec::new(),
        misplaced_directives: Vec::new(),
        inline_server_action_exports: Vec::new(),
        di_key_sites: Vec::new(),
        has_dynamic_provide: false,
        referenced_import_bindings: Vec::new(),
        component_props: Vec::new(),
        has_props_attrs_fallthrough: false,
        has_define_expose: false,
        has_define_model: false,
        has_unharvestable_props: false,
        component_emits: Vec::new(),
        angular_inputs: Vec::new(),
        angular_outputs: Vec::new(),
        angular_component_selectors: Vec::new(),
        angular_used_selectors: Vec::new(),
        angular_entry_component_refs: Vec::new(),
        has_dynamic_component_render: false,
        has_unharvestable_emits: false,
        has_dynamic_emit: false,
        has_emit_whole_object_use: false,
        load_return_keys: Vec::new(),
        has_unharvestable_load: false,
        has_load_data_whole_use: false,
        has_page_data_store_whole_use: false,
        component_functions: Vec::new(),
        react_props: Vec::new(),
        hook_uses: Vec::new(),
        render_edges: Vec::new(),
        svelte_dispatched_events: Vec::new(),
        svelte_listened_events: Vec::new(),
        has_dynamic_dispatch: false,
    }
}

struct SfcScriptMergeInput<'a> {
    kind: SfcKind,
    script: &'a SfcScript,
    combined: &'a mut ModuleInfo,
    template_visible_imports: &'a mut FxHashSet<String>,
    template_visible_bound_targets: &'a mut FxHashMap<String, String>,
    props_return_binding: &'a mut Option<String>,
    emit_return_binding: &'a mut Option<String>,
    need_complexity: bool,
}

fn merge_script_into_module(input: &mut SfcScriptMergeInput<'_>) {
    if input.kind == SfcKind::Vue
        && let Some(src) = &input.script.src
    {
        add_script_src_import(input.combined, src, input.script.src_span);
    }

    let allocator = Allocator::default();
    let parser_return = Parser::new(
        &allocator,
        &input.script.body,
        source_type_for_script(input.script),
    )
    .parse();
    let mut extractor = ModuleInfoExtractor::new();
    extractor.visit_program(&parser_return.program);
    let extraction = ExtractionResult::contiguous(&input.script.body, input.script.byte_offset);
    extractor.remap_spans_with(|span| extraction.remap_span(span));
    extractor.resolve_typed_destructure_bindings();

    let augmented_body = build_generic_attr_probe_source(input.script);
    let empty_template_used = rustc_hash::FxHashSet::default();
    let (binding_usage, auto_import_candidates) = if let Some(augmented) = augmented_body.as_deref()
    {
        let augmented_return =
            Parser::new(&allocator, augmented, source_type_for_script(input.script)).parse();
        (
            compute_import_binding_usage(
                &augmented_return.program,
                &extractor.imports,
                &empty_template_used,
            ),
            compute_auto_import_candidates(&parser_return.program),
        )
    } else {
        let semantic_usage = compute_semantic_usage(
            &parser_return.program,
            &extractor.imports,
            &empty_template_used,
        );
        (
            semantic_usage.import_binding_usage,
            semantic_usage.auto_import_candidates,
        )
    };
    input
        .combined
        .unused_import_bindings
        .extend(binding_usage.unused.iter().cloned());
    input
        .combined
        .type_referenced_import_bindings
        .extend(binding_usage.type_referenced.iter().cloned());
    input
        .combined
        .value_referenced_import_bindings
        .extend(binding_usage.value_referenced.iter().cloned());
    input
        .combined
        .auto_import_candidates
        .extend(auto_import_candidates);
    if input.need_complexity {
        input
            .combined
            .complexity
            .extend(translate_script_complexity(
                input.script,
                &parser_return.program,
                &input.combined.line_offsets,
            ));
    }

    // Vue prop/emit harvesting (`<script setup>` macros + Options API) for the
    // `unused-component-prop` / `unused-component-emit` detectors. Extracted to a
    // helper to keep this function under the unit-size lint.
    if input.kind == SfcKind::Vue {
        merge_vue_props_emits_into(input, &parser_return.program);
    }

    // Svelte 5 `$props()` rune harvesting for `unused-component-prop`. `$props`
    // is an instance-only rune, so harvest ONLY the template-visible instance
    // script, never the module script (`<script context="module">` /
    // `<script module>`).
    if input.kind == SfcKind::Svelte && is_template_visible_script(input.kind, input.script) {
        merge_svelte_props_into(
            input.combined,
            &parser_return.program,
            input.script.byte_offset,
        );
    }

    if is_template_visible_script(input.kind, input.script) {
        input.template_visible_imports.extend(
            extractor
                .imports
                .iter()
                .filter(|import| !import.local_name.is_empty())
                .map(|import| import.local_name.clone()),
        );
        input.template_visible_bound_targets.extend(
            extractor
                .binding_target_names()
                .iter()
                .filter(|(local, _)| !local.starts_with("this."))
                .map(|(local, target)| (local.clone(), target.clone())),
        );
    }

    // Dispatched events recorded by the visitor carry body-relative spans, like
    // props/emits above. Remap the entries this script contributes onto the SFC
    // source via the script byte offset so the finding line/col points at the
    // real `dispatch(...)` call, not a body-relative position.
    let dispatch_base = input.combined.svelte_dispatched_events.len();
    extractor.merge_into(input.combined);
    for event in &mut input.combined.svelte_dispatched_events[dispatch_base..] {
        event.span_start += input.script.byte_offset as u32;
    }
}

/// Harvest Svelte 5 `$props()` declared props from an instance `<script>`
/// program into `combined.component_props` (reusing the Vue IR + abstain flags),
/// remapping each prop's body-relative span onto the SFC source via `byte_offset`.
fn merge_svelte_props_into(
    combined: &mut ModuleInfo,
    program: &oxc_ast::ast::Program<'_>,
    byte_offset: usize,
) {
    let harvest = crate::sfc_props::harvest_svelte_props(program);
    if harvest.has_unharvestable_props {
        combined.has_unharvestable_props = true;
    }
    if harvest.has_props_attrs_fallthrough {
        combined.has_props_attrs_fallthrough = true;
    }
    for mut prop in harvest.props {
        prop.span_start += byte_offset as u32;
        combined.component_props.push(prop);
    }
}

/// Harvest Vue prop/emit declarations into `combined`, remapping body-relative
/// spans onto the SFC source via the script byte offset. The `<script setup>`
/// path harvests `defineProps` / `defineEmits` (and the `defineExpose` /
/// `defineModel` abstain flags + return bindings); the non-setup path harvests
/// the Options API `props:` / `emits:` (same IR, same abstain flags, same remap,
/// only the harvest source differs).
fn merge_vue_props_emits_into(
    input: &mut SfcScriptMergeInput<'_>,
    program: &oxc_ast::ast::Program<'_>,
) {
    let byte_offset = input.script.byte_offset as u32;
    if input.script.is_setup {
        let harvest = crate::sfc_props::harvest_define_props(program);
        if harvest.has_unharvestable_props {
            input.combined.has_unharvestable_props = true;
        }
        if harvest.has_props_attrs_fallthrough {
            input.combined.has_props_attrs_fallthrough = true;
        }
        if harvest.has_define_expose {
            input.combined.has_define_expose = true;
        }
        if harvest.has_define_model {
            input.combined.has_define_model = true;
        }
        if let Some(binding) = harvest.props_return_binding {
            *input.props_return_binding = Some(binding);
        }
        for mut prop in harvest.props {
            prop.span_start += byte_offset;
            input.combined.component_props.push(prop);
        }

        let emit_harvest = crate::sfc_props::harvest_define_emits(program);
        if emit_harvest.has_unharvestable_emits {
            input.combined.has_unharvestable_emits = true;
        }
        if emit_harvest.has_dynamic_emit {
            input.combined.has_dynamic_emit = true;
        }
        if emit_harvest.has_emit_whole_object_use {
            input.combined.has_emit_whole_object_use = true;
        }
        if let Some(binding) = emit_harvest.emit_binding {
            *input.emit_return_binding = Some(binding);
        }
        for mut emit in emit_harvest.emits {
            emit.span_start += byte_offset;
            input.combined.component_emits.push(emit);
        }
    } else {
        let harvest = crate::sfc_props::harvest_options_api_props(program);
        if harvest.has_unharvestable_props {
            input.combined.has_unharvestable_props = true;
        }
        if harvest.has_props_attrs_fallthrough {
            input.combined.has_props_attrs_fallthrough = true;
        }
        for mut prop in harvest.props {
            prop.span_start += byte_offset;
            input.combined.component_props.push(prop);
        }

        let emit_harvest = crate::sfc_props::harvest_options_api_emits(program);
        if emit_harvest.has_unharvestable_emits {
            input.combined.has_unharvestable_emits = true;
        }
        if emit_harvest.has_dynamic_emit {
            input.combined.has_dynamic_emit = true;
        }
        for mut emit in emit_harvest.emits {
            emit.span_start += byte_offset;
            input.combined.component_emits.push(emit);
        }
    }
}

fn translate_script_complexity(
    script: &SfcScript,
    program: &oxc_ast::ast::Program<'_>,
    sfc_line_offsets: &[u32],
) -> Vec<FunctionComplexity> {
    let script_line_offsets = compute_line_offsets(&script.body);
    let mut complexity =
        crate::complexity::compute_complexity(program, &script.body, &script_line_offsets);
    let (body_start_line, body_start_col) =
        byte_offset_to_line_col(sfc_line_offsets, script.byte_offset as u32);

    for function in &mut complexity {
        function.line = body_start_line + function.line.saturating_sub(1);
        if function.line == body_start_line {
            function.col += body_start_col;
        }
    }

    complexity
}

fn add_script_src_import(module: &mut ModuleInfo, source: &str, source_span: Option<Span>) {
    let span = source_span.unwrap_or_default();
    module.imports.push(ImportInfo {
        source: normalize_asset_url(source),
        imported_name: ImportedName::SideEffect,
        local_name: String::new(),
        is_type_only: false,
        from_style: false,
        span,
        source_span: span,
    });
}

/// `lang` attribute values whose body we know how to scan for `@import` /
/// `@use` / `@forward` / `@plugin` directives. Plain `<style>` (no `lang`) is treated as
/// CSS. `less`, `stylus`, and `postcss` bodies are NOT scanned because their
/// import syntax differs (`@import (reference)` modifiers, etc.); their
/// `<style src="...">` references are still seeded.
fn style_lang_is_scss(lang: Option<&str>) -> bool {
    matches!(lang, Some("scss" | "sass"))
}

fn style_lang_is_css_like(lang: Option<&str>) -> bool {
    lang.is_none() || matches!(lang, Some("css"))
}

fn merge_style_into_module(style: &SfcStyle, combined: &mut ModuleInfo) {
    if let Some(src) = &style.src {
        let span = style.src_span.unwrap_or_default();
        combined.imports.push(ImportInfo {
            source: normalize_asset_url(src),
            imported_name: ImportedName::SideEffect,
            local_name: String::new(),
            is_type_only: false,
            from_style: true,
            span,
            source_span: span,
        });
    }

    let lang = style.lang.as_deref();
    let is_scss = style_lang_is_scss(lang);
    let is_css_like = style_lang_is_css_like(lang);
    if !is_scss && !is_css_like {
        return;
    }

    for source in crate::css::extract_css_import_sources(&style.body, is_scss) {
        let source_span = Span::new(
            style.byte_offset as u32 + source.span.start,
            style.byte_offset as u32 + source.span.end,
        );
        combined.imports.push(ImportInfo {
            source: source.normalized,
            imported_name: if source.is_plugin {
                ImportedName::Default
            } else {
                ImportedName::SideEffect
            },
            local_name: String::new(),
            is_type_only: false,
            from_style: true,
            span: source_span,
            source_span,
        });
    }
}

fn source_type_for_script(script: &SfcScript) -> SourceType {
    match (script.is_typescript, script.is_jsx) {
        (true, true) => SourceType::tsx(),
        (true, false) => SourceType::ts(),
        (false, true) => SourceType::jsx(),
        (false, false) => SourceType::mjs(),
    }
}

/// Build an augmented script body that pins the `generic="..."` constraint as
/// a synthetic local type alias. The alias is unexported and uses a sentinel
/// name so it can't collide with user code. Returns `None` when there is no
/// generic attribute to pin (the common case), so callers fall back to the
/// raw body without paying for a second parse.
fn build_generic_attr_probe_source(script: &SfcScript) -> Option<String> {
    let constraint = script.generic_attr.as_deref()?.trim();
    if constraint.is_empty() {
        return None;
    }
    Some(format!(
        "{}\n;type __FALLOW_GENERIC_ATTR_PROBE<{}> = unknown;\n",
        script.body, constraint,
    ))
}

fn apply_template_usage(
    kind: SfcKind,
    source: &str,
    template_visible_imports: &FxHashSet<String>,
    template_visible_bound_targets: &FxHashMap<String, String>,
    props_return_binding: Option<&str>,
    credit_load_data: bool,
    combined: &mut ModuleInfo,
) {
    // Props are NOT imports, so the template scanner does not credit them by
    // default. Thread the harvested prop names (and the `defineProps` return
    // binding, so `props.<name>` template member accesses are emitted) in as an
    // additional credited set alongside `template_visible_imports`. Crediting a
    // prop name against an import is inert (no import binding shares the name),
    // so the unused-import retain is unaffected.
    let mut credited: FxHashSet<String> = template_visible_imports.clone();
    // unused-load-data-key Primitive B: a SvelteKit route component
    // (`+page.svelte` / `+layout.svelte`) receives a `data` prop populated by
    // the route's `load()` return object. The prop is template-visible
    // (`{data.x}`, `{#each data.items as i}`) but is neither an import nor a
    // tracked binding, so the member-access scanner gates it out by default.
    // Credit `data` as a recognized root so its template member accesses
    // (`data.<key>`) are emitted for the cross-file load-data-key join. Gated to
    // route components only (`credit_load_data`): a non-route component's
    // `let { data } = $props()` is a different, parent-passed `data`, so
    // crediting it as load data would be semantically wrong. Inert for every
    // other detector unless a tracked export/instance is named `data` (mirrors
    // Primitive A); the load-data-key join is the only consumer of `data.<key>`.
    if credit_load_data {
        credited.insert("data".to_string());
        // FP-1: a route component spreading / passing the whole `data` prop in
        // markup consumes arbitrary keys opaquely; force the detector to abstain
        // on this route. A false abstain only suppresses findings, never creates
        // one, so matching the full source (script + template) is safe.
        if SVELTE_TEMPLATE_DATA_WHOLE_USE_RE.is_match(source) {
            combined.has_load_data_whole_use = true;
        }
    }
    if !combined.component_props.is_empty() {
        for prop in &combined.component_props {
            // Credit both the declared name (Vue exposes props by name in the
            // template) and the destructure local (a renamed prop is used via it).
            credited.insert(prop.name.clone());
            credited.insert(prop.local.clone());
        }
        // Vue's implicit `$props` whole-props object is always available in a
        // template; credit `$props.<name>` member accesses too.
        credited.insert("$props".to_string());
        if let Some(binding) = props_return_binding {
            credited.insert(binding.to_string());
        }
    }

    // unused-load-data-key Primitive B: a route `data` prop is usually typed as
    // SvelteKit's generated `PageData` / `LayoutData` (`export let data:
    // PageData`). That typed binding (`data -> PageData`) would otherwise make the
    // template scanner remap `{data.x}` / `<Child foo={data.x} />` member accesses
    // onto the generated type (`PageData.x`), dropping the `data`-keyed access the
    // cross-file load-data join needs. Drop `data` from the bound-targets for the
    // template scan so its template member accesses stay keyed on `data`. The
    // generated `$types` aliases carry no real members for any member detector, so
    // losing the type-keyed template credit is inert; script-side `data` reads are
    // unaffected (their resolution already ran during `merge_into`).
    let template_usage = if credit_load_data && template_visible_bound_targets.contains_key("data")
    {
        let mut filtered = template_visible_bound_targets.clone();
        filtered.remove("data");
        collect_template_usage_with_bound_targets(kind, source, &credited, &filtered)
    } else {
        collect_template_usage_with_bound_targets(
            kind,
            source,
            &credited,
            template_visible_bound_targets,
        )
    };

    // A template reference credits `used_in_template`: either a bare prop name in
    // `used_bindings` (destructured prop form, or template uses the bare name) OR
    // a `<props>.<name>` / `$props.<name>` member access (the
    // `const props = defineProps()` form and Vue's implicit `$props`).
    if !combined.component_props.is_empty() {
        let member_used: FxHashSet<&str> = template_usage
            .member_accesses
            .iter()
            .filter(|access| {
                access.object == "$props"
                    || props_return_binding.is_some_and(|binding| access.object == binding)
            })
            .map(|access| access.member.as_str())
            .collect();
        for prop in &mut combined.component_props {
            if template_usage.used_bindings.contains(&prop.name)
                || template_usage.used_bindings.contains(&prop.local)
                || member_used.contains(prop.name.as_str())
            {
                prop.used_in_template = true;
            }
        }
    }

    // A custom-named `defineProps` return spread as a whole object in the template
    // (`const myProps = defineProps(); <Child v-bind="myProps" />`) consumes every
    // prop opaquely; the literal `props`/`$props`/`$attrs` regex misses a custom
    // name. The scanner records a bare `v-bind="myProps"` value as a used binding
    // (not a whole-object use), so a bare reference to the return binding in either
    // set means abstain on the whole file.
    if let Some(binding) = props_return_binding
        && (template_usage.used_bindings.contains(binding)
            || template_usage
                .whole_object_uses
                .iter()
                .any(|used| used == binding))
    {
        combined.has_props_attrs_fallthrough = true;
    }

    combined
        .unused_import_bindings
        .retain(|binding| !template_usage.used_bindings.contains(binding));
    combined
        .member_accesses
        .extend(template_usage.member_accesses);
    combined
        .whole_object_uses
        .extend(template_usage.whole_object_uses);
    combined
        .security_sinks
        .extend(template_usage.security_sinks);
    if !template_usage.unresolved_tag_names.is_empty() {
        let mut names: Vec<String> = template_usage.unresolved_tag_names.into_iter().collect();
        names.sort_unstable();
        combined.auto_import_candidates.extend(names);
        combined.auto_import_candidates.dedup();
    }
}

/// Credit emit events fired from the `<template>` (`@click="emit('close')"`,
/// `@click="$emit('remove')"`, `:close="{ onClick: () => emit('close') }"`),
/// which the script-only emit usage walk in `harvest_define_emits` cannot see.
///
/// Scans the template-only region (scripts/styles/comments masked) for
/// [`TEMPLATE_EMIT_CALL_RE`]: a call whose callee is the harvested emit binding
/// (`emit` / `emits` / whatever it was bound to) or the implicit `$emit` (always
/// available in a Vue template regardless of `<script setup>` binding). A
/// string-literal first arg credits the matching `ComponentEmit` as used; a
/// non-literal first arg (a variable / template-literal) is a dynamic template
/// emit whose event is unknowable, so the whole file abstains (`has_dynamic_emit`)
/// to preserve the zero-FP doctrine.
///
/// Over-crediting is the safe direction (it only suppresses a finding), so a
/// liberal raw-source scan is intentional here. The scan is byte-safe: the regex
/// runs over the `&str` template and only reads captured-group text, never
/// slicing at arbitrary byte offsets.
fn apply_template_emit_usage(
    source: &str,
    emit_return_binding: Option<&str>,
    combined: &mut ModuleInfo,
) {
    let masked = mask_non_markup_regions(source);
    let mut used: FxHashSet<String> = FxHashSet::default();
    let mut dynamic = false;

    for caps in TEMPLATE_EMIT_CALL_RE.captures_iter(&masked) {
        let Some(callee) = caps.get(1) else {
            continue;
        };
        let callee = callee.as_str();
        let is_emit_call =
            callee == "$emit" || emit_return_binding.is_some_and(|binding| callee == binding);
        if !is_emit_call {
            continue;
        }
        if let Some(event) = caps.get(2).or_else(|| caps.get(3)) {
            // String-literal first arg (single- or double-quoted): the event
            // name. Credit it as used.
            used.insert(event.as_str().to_string());
        } else if caps.get(4).is_some() {
            // Non-literal first arg (`$emit(someVar)`, `emit(\`x\`)`): the event
            // cannot be known. Abstain on the whole file.
            dynamic = true;
        }
    }

    if dynamic {
        combined.has_dynamic_emit = true;
    }
    if !used.is_empty() {
        for emit in &mut combined.component_emits {
            if used.contains(&emit.name) {
                emit.used = true;
            }
        }
    }
}

fn is_template_visible_script(kind: SfcKind, script: &SfcScript) -> bool {
    match kind {
        SfcKind::Vue => script.is_setup,
        SfcKind::Svelte => !script.is_context_module,
    }
}

#[cfg(all(test, not(miri)))]
mod tests {
    use super::*;

    #[test]
    fn is_sfc_file_vue() {
        assert!(is_sfc_file(Path::new("App.vue")));
    }

    #[test]
    fn is_sfc_file_svelte() {
        assert!(is_sfc_file(Path::new("Counter.svelte")));
    }

    #[test]
    fn is_sfc_file_rejects_ts() {
        assert!(!is_sfc_file(Path::new("utils.ts")));
    }

    #[test]
    fn is_sfc_file_rejects_jsx() {
        assert!(!is_sfc_file(Path::new("App.jsx")));
    }

    #[test]
    fn is_sfc_file_rejects_astro() {
        assert!(!is_sfc_file(Path::new("Layout.astro")));
    }

    #[test]
    fn single_plain_script() {
        let scripts = extract_sfc_scripts("<script>const x = 1;</script>");
        assert_eq!(scripts.len(), 1);
        assert_eq!(scripts[0].body, "const x = 1;");
        assert!(!scripts[0].is_typescript);
        assert!(!scripts[0].is_jsx);
        assert!(scripts[0].src.is_none());
    }

    #[test]
    fn single_ts_script() {
        let scripts = extract_sfc_scripts(r#"<script lang="ts">const x: number = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript);
        assert!(!scripts[0].is_jsx);
    }

    #[test]
    fn single_tsx_script() {
        let scripts = extract_sfc_scripts(r#"<script lang="tsx">const el = <div />;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript);
        assert!(scripts[0].is_jsx);
    }

    #[test]
    fn single_jsx_script() {
        let scripts = extract_sfc_scripts(r#"<script lang="jsx">const el = <div />;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_typescript);
        assert!(scripts[0].is_jsx);
    }

    #[test]
    fn two_script_blocks() {
        let source = r#"
<script lang="ts">
export default {};
</script>
<script setup lang="ts">
const count = 0;
</script>
"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 2);
        assert!(scripts[0].body.contains("export default"));
        assert!(scripts[1].body.contains("count"));
    }

    #[test]
    fn script_setup_extracted() {
        let scripts =
            extract_sfc_scripts(r#"<script setup lang="ts">import { ref } from 'vue';</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.contains("import"));
        assert!(scripts[0].is_typescript);
    }

    #[test]
    fn script_src_detected() {
        let scripts = extract_sfc_scripts(r#"<script src="./component.ts" lang="ts"></script>"#);
        assert_eq!(scripts.len(), 1);
        assert_eq!(scripts[0].src.as_deref(), Some("./component.ts"));
    }

    // -- Svelte module-context recognition (W1.1 piece 1) ----------------------

    #[test]
    fn svelte4_context_module_is_module_context() {
        let scripts =
            extract_sfc_scripts(r#"<script context="module">export const x = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_context_module);
    }

    #[test]
    fn svelte5_bare_module_attr_is_module_context() {
        let scripts = extract_sfc_scripts(r"<script module>export const x = 1;</script>");
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_context_module);
    }

    #[test]
    fn svelte5_module_with_lang_is_module_context() {
        let scripts =
            extract_sfc_scripts(r#"<script module lang="ts">export const x = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_context_module);
        assert!(scripts[0].is_typescript);
    }

    #[test]
    fn plain_script_is_not_module_context() {
        let scripts = extract_sfc_scripts(r"<script>const x = 1;</script>");
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_context_module);
    }

    #[test]
    fn lang_ts_script_is_not_module_context() {
        let scripts = extract_sfc_scripts(r#"<script lang="ts">const x = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_context_module);
    }

    #[test]
    fn data_module_attr_is_not_module_context() {
        // The `(?:^|\s)module(?:\s|$|=)` anchor must not match `data-module`.
        let scripts =
            extract_sfc_scripts(r#"<script data-module="x" lang="ts">const x = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_context_module);
    }

    #[test]
    fn bare_module_script_is_not_template_visible() {
        // AC-2: a bare `<script module>` is scoped as module context, so its
        // imports are NOT credited as template-visible (matching `context="module"`).
        let module_script = SfcScript {
            body: String::new(),
            is_typescript: false,
            is_jsx: false,
            byte_offset: 0,
            src: None,
            src_span: None,
            is_setup: false,
            is_context_module: true,
            generic_attr: None,
        };
        assert!(!is_template_visible_script(SfcKind::Svelte, &module_script));
        let instance_script = SfcScript {
            is_context_module: false,
            ..module_script
        };
        assert!(is_template_visible_script(
            SfcKind::Svelte,
            &instance_script
        ));
    }

    #[test]
    fn data_src_not_treated_as_src() {
        let scripts =
            extract_sfc_scripts(r#"<script lang="ts" data-src="./nope.ts">const x = 1;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].src.is_none());
    }

    #[test]
    fn script_inside_html_comment_filtered() {
        let source = r#"
<!-- <script lang="ts">import { bad } from 'bad';</script> -->
<script lang="ts">import { good } from 'good';</script>
"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.contains("good"));
    }

    #[test]
    fn spanning_comment_filters_script() {
        let source = r#"
<!-- disabled:
<script lang="ts">import { bad } from 'bad';</script>
-->
<script lang="ts">const ok = true;</script>
"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.contains("ok"));
    }

    #[test]
    fn string_containing_comment_markers_not_corrupted() {
        let source = r#"
<script setup lang="ts">
const marker = "<!-- not a comment -->";
import { ref } from 'vue';
</script>
"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.contains("import"));
    }

    #[test]
    fn generic_attr_with_angle_bracket() {
        let source =
            r#"<script setup lang="ts" generic="T extends Foo<Bar>">const x = 1;</script>"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert_eq!(scripts[0].body, "const x = 1;");
    }

    #[test]
    fn nested_generic_attr() {
        let source = r#"<script setup lang="ts" generic="T extends Map<string, Set<number>>">const x = 1;</script>"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert_eq!(scripts[0].body, "const x = 1;");
    }

    #[test]
    fn lang_single_quoted() {
        let scripts = extract_sfc_scripts("<script lang='ts'>const x = 1;</script>");
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript);
    }

    #[test]
    fn uppercase_script_tag() {
        let scripts = extract_sfc_scripts(r#"<SCRIPT lang="ts">const x = 1;</SCRIPT>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript);
    }

    #[test]
    fn no_script_block() {
        let scripts = extract_sfc_scripts("<template><div>Hello</div></template>");
        assert!(scripts.is_empty());
    }

    #[test]
    fn empty_script_body() {
        let scripts = extract_sfc_scripts(r#"<script lang="ts"></script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.is_empty());
    }

    #[test]
    fn whitespace_only_script() {
        let scripts = extract_sfc_scripts("<script lang=\"ts\">\n  \n</script>");
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.trim().is_empty());
    }

    #[test]
    fn byte_offset_is_set() {
        let source = r#"<template><div/></template><script lang="ts">code</script>"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        let offset = scripts[0].byte_offset;
        assert_eq!(&source[offset..offset + 4], "code");
    }

    #[test]
    fn script_with_extra_attributes() {
        let scripts = extract_sfc_scripts(
            r#"<script lang="ts" id="app" type="module" data-custom="val">const x = 1;</script>"#,
        );
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript);
        assert!(scripts[0].src.is_none());
    }

    #[test]
    fn multiple_script_blocks_exports_combined() {
        let source = r#"
<script lang="ts">
export const version = '1.0';
</script>
<script setup lang="ts">
import { ref } from 'vue';
const count = ref(0);
</script>
"#;
        let info = parse_sfc_to_module(FileId(0), Path::new("Dual.vue"), source, 0, false);
        assert!(
            info.exports
                .iter()
                .any(|e| matches!(&e.name, crate::ExportName::Named(n) if n == "version")),
            "export from <script> block should be extracted"
        );
        assert!(
            info.imports.iter().any(|i| i.source == "vue"),
            "import from <script setup> block should be extracted"
        );
    }

    #[test]
    fn lang_tsx_detected_as_typescript_jsx() {
        let scripts =
            extract_sfc_scripts(r#"<script lang="tsx">const el = <div>{x}</div>;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].is_typescript, "lang=tsx should be typescript");
        assert!(scripts[0].is_jsx, "lang=tsx should be jsx");
    }

    #[test]
    fn multiline_html_comment_filters_all_script_blocks_inside() {
        let source = r#"
<!--
  This whole section is disabled:
  <script lang="ts">import { bad1 } from 'bad1';</script>
  <script lang="ts">import { bad2 } from 'bad2';</script>
-->
<script lang="ts">import { good } from 'good';</script>
"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 1);
        assert!(scripts[0].body.contains("good"));
    }

    #[test]
    fn script_src_generates_side_effect_import() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("External.vue"),
            r#"<script src="./external-logic.ts" lang="ts"></script>"#,
            0,
            false,
        );
        assert!(
            info.imports
                .iter()
                .any(|i| i.source == "./external-logic.ts"
                    && matches!(i.imported_name, ImportedName::SideEffect)),
            "script src should generate a side-effect import"
        );
    }

    #[test]
    fn parse_sfc_no_script_returns_empty_module() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Empty.vue"),
            "<template><div>Hello</div></template>",
            42,
            false,
        );
        assert!(info.imports.is_empty());
        assert!(info.exports.is_empty());
        assert_eq!(info.content_hash, 42);
        assert_eq!(info.file_id, FileId(0));
    }

    #[test]
    fn parse_sfc_has_line_offsets() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("LineOffsets.vue"),
            r#"<script lang="ts">const x = 1;</script>"#,
            0,
            false,
        );
        assert!(!info.line_offsets.is_empty());
    }

    #[test]
    fn parse_sfc_has_suppressions() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Suppressions.vue"),
            r#"<script lang="ts">
// fallow-ignore-file
export const foo = 1;
</script>"#,
            0,
            false,
        );
        assert!(!info.suppressions.is_empty());
    }

    #[test]
    fn source_type_jsx_detection() {
        let scripts = extract_sfc_scripts(r#"<script lang="jsx">const el = <div />;</script>"#);
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_typescript);
        assert!(scripts[0].is_jsx);
    }

    #[test]
    fn source_type_plain_js_detection() {
        let scripts = extract_sfc_scripts("<script>const x = 1;</script>");
        assert_eq!(scripts.len(), 1);
        assert!(!scripts[0].is_typescript);
        assert!(!scripts[0].is_jsx);
    }

    #[test]
    fn is_sfc_file_rejects_no_extension() {
        assert!(!is_sfc_file(Path::new("Makefile")));
    }

    #[test]
    fn is_sfc_file_rejects_mdx() {
        assert!(!is_sfc_file(Path::new("post.mdx")));
    }

    #[test]
    fn is_sfc_file_rejects_css() {
        assert!(!is_sfc_file(Path::new("styles.css")));
    }

    #[test]
    fn multiple_script_blocks_both_have_offsets() {
        let source = r#"<script lang="ts">const a = 1;</script>
<script setup lang="ts">const b = 2;</script>"#;
        let scripts = extract_sfc_scripts(source);
        assert_eq!(scripts.len(), 2);
        let offset0 = scripts[0].byte_offset;
        let offset1 = scripts[1].byte_offset;
        assert_eq!(
            &source[offset0..offset0 + "const a = 1;".len()],
            "const a = 1;"
        );
        assert_eq!(
            &source[offset1..offset1 + "const b = 2;".len()],
            "const b = 2;"
        );
    }

    #[test]
    fn script_with_src_and_lang() {
        let scripts = extract_sfc_scripts(r#"<script src="./logic.ts" lang="tsx"></script>"#);
        assert_eq!(scripts.len(), 1);
        assert_eq!(scripts[0].src.as_deref(), Some("./logic.ts"));
        assert!(scripts[0].is_typescript);
        assert!(scripts[0].is_jsx);
    }

    #[test]
    fn extract_style_block_lang_scss() {
        let source = r#"<template/><style lang="scss">@import 'Foo';</style>"#;
        let styles = extract_sfc_styles(source);
        assert_eq!(styles.len(), 1);
        assert_eq!(styles[0].lang.as_deref(), Some("scss"));
        assert!(styles[0].body.contains("@import"));
        assert!(styles[0].src.is_none());
    }

    #[test]
    fn extract_style_block_with_src() {
        let source = r#"<style src="./theme.scss" lang="scss"></style>"#;
        let styles = extract_sfc_styles(source);
        assert_eq!(styles.len(), 1);
        assert_eq!(styles[0].src.as_deref(), Some("./theme.scss"));
        assert_eq!(styles[0].lang.as_deref(), Some("scss"));
    }

    #[test]
    fn extract_style_block_plain_no_lang() {
        let source = r"<style>.foo { color: red; }</style>";
        let styles = extract_sfc_styles(source);
        assert_eq!(styles.len(), 1);
        assert!(styles[0].lang.is_none());
    }

    #[test]
    fn extract_multiple_style_blocks() {
        let source = r#"<style lang="scss">@import 'a';</style>
<style scoped lang="scss">@import 'b';</style>"#;
        let styles = extract_sfc_styles(source);
        assert_eq!(styles.len(), 2);
    }

    #[test]
    fn style_block_inside_html_comment_filtered() {
        let source = r#"<!-- <style lang="scss">@import 'bad';</style> -->
<style lang="scss">@import 'good';</style>"#;
        let styles = extract_sfc_styles(source);
        assert_eq!(styles.len(), 1);
        assert!(styles[0].body.contains("good"));
    }

    #[test]
    fn parse_sfc_extracts_style_imports_with_from_style_flag() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Foo.vue"),
            r#"<template/><style lang="scss">@import 'Foo';</style>"#,
            0,
            false,
        );
        let style_import = info
            .imports
            .iter()
            .find(|i| i.source == "./Foo")
            .expect("scss @import 'Foo' should be normalized to ./Foo");
        assert!(
            style_import.from_style,
            "imports from <style> blocks must carry from_style=true so the resolver \
             enables SCSS partial fallback for the SFC importer"
        );
        assert!(matches!(
            style_import.imported_name,
            ImportedName::SideEffect
        ));
    }

    #[test]
    fn parse_sfc_extracts_style_plugin_as_default_import() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Foo.vue"),
            r#"<template/><style>@plugin "./tailwind-plugin.js";</style>"#,
            0,
            false,
        );
        let plugin_import = info
            .imports
            .iter()
            .find(|i| i.source == "./tailwind-plugin.js")
            .expect("style @plugin should create an import");
        assert!(plugin_import.from_style);
        assert!(matches!(plugin_import.imported_name, ImportedName::Default));
    }

    #[test]
    fn parse_sfc_extracts_style_src_with_from_style_flag() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Bar.vue"),
            r#"<style src="./Bar.scss" lang="scss"></style>"#,
            0,
            false,
        );
        let style_src = info
            .imports
            .iter()
            .find(|i| i.source == "./Bar.scss")
            .expect("<style src=\"./Bar.scss\"> should produce a side-effect import");
        assert!(style_src.from_style);
    }

    #[test]
    fn parse_sfc_skips_unsupported_style_lang_body_but_keeps_src() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Baz.vue"),
            r#"<style lang="postcss" src="./Baz.pcss">@custom-rule "skipped";</style>"#,
            0,
            false,
        );
        assert!(
            info.imports.iter().any(|i| i.source == "./Baz.pcss"),
            "src reference should still be seeded for unsupported lang"
        );
        assert!(
            !info.imports.iter().any(|i| i.source.contains("skipped")),
            "postcss body should not be scanned for @import directives"
        );
    }

    fn asset_refs(source: &str) -> Vec<String> {
        super::collect_template_asset_refs(source)
            .into_iter()
            .map(|(s, _)| s)
            .collect()
    }

    #[test]
    fn captures_static_relative_template_asset_refs() {
        assert_eq!(
            asset_refs(r#"<template><img src="./logo.png" /></template>"#),
            vec!["./logo.png".to_string()]
        );
        assert_eq!(
            asset_refs(r#"<source src="../media/clip.mp4">"#),
            vec!["../media/clip.mp4".to_string()]
        );
        assert_eq!(
            asset_refs(r#"<video poster="./thumb.jpg"></video>"#),
            vec!["./thumb.jpg".to_string()]
        );
    }

    #[test]
    fn skips_dynamic_alias_root_remote_and_query_asset_refs() {
        // Dynamic bindings (Vue `:src`, `v-bind:src`, Svelte `bind:src` / `src={}`).
        assert!(asset_refs(r#"<img :src="logo" />"#).is_empty());
        assert!(asset_refs(r#"<img v-bind:src="logo" />"#).is_empty());
        assert!(asset_refs(r#"<img bind:src="logo" />"#).is_empty());
        assert!(asset_refs(r"<img src={logo} />").is_empty());
        assert!(asset_refs(r#"<img data-src="./x.png" />"#).is_empty());
        // Alias-prefixed, root-relative, remote, bare: not plain relative literals.
        assert!(asset_refs(r#"<img src="@/assets/x.png" />"#).is_empty());
        assert!(asset_refs(r#"<img src="/logo.png" />"#).is_empty());
        assert!(asset_refs(r#"<img src="https://cdn/x.png" />"#).is_empty());
        // Query / hash suffix abstains (the resolver cannot verify them).
        assert!(asset_refs(r#"<img src="./x.png?inline" />"#).is_empty());
        // Interpolated value abstains.
        assert!(asset_refs(r#"<img src="{{ logo }}" />"#).is_empty());
    }

    #[test]
    fn skips_custom_component_src_prop() {
        // A custom component's `src` PROP must never be read as an asset edge.
        assert!(asset_refs(r#"<MyImage src="./x.png" />"#).is_empty());
        assert!(asset_refs(r#"<AppIcon src="../icons/y.svg" />"#).is_empty());
    }

    #[test]
    fn skips_asset_refs_inside_script_style_and_comments() {
        // Masked regions must not contribute asset refs.
        assert!(asset_refs(r#"<script>const x = "<img src='./a.png'>"</script>"#).is_empty());
        assert!(asset_refs(r#"<style>/* <img src="./b.png"> */ .x{}</style>"#).is_empty());
        assert!(asset_refs(r#"<!-- <img src="./c.png" /> -->"#).is_empty());
    }

    #[test]
    fn parse_sfc_emits_template_asset_as_side_effect_import() {
        let info = parse_sfc_to_module(
            FileId(0),
            Path::new("Hero.vue"),
            r#"<template><img src="./hero.png" /></template><script>let x=1</script>"#,
            0,
            false,
        );
        assert!(
            info.imports.iter().any(|i| i.source == "./hero.png"
                && matches!(i.imported_name, ImportedName::SideEffect)
                && !i.from_style),
            "template <img src> should seed a SideEffect import: {:?}",
            info.imports
        );
    }

    // -- Svelte 5 `$props()` rune harvest (W1.1 piece 2) -----------------------

    fn svelte_props(source: &str) -> Vec<crate::ModuleInfo> {
        vec![parse_sfc_to_module(
            FileId(0),
            Path::new("Component.svelte"),
            source,
            0,
            false,
        )]
    }

    fn prop_names(info: &crate::ModuleInfo) -> Vec<String> {
        let mut names: Vec<String> = info
            .component_props
            .iter()
            .map(|p| p.name.clone())
            .collect();
        names.sort();
        names
    }

    #[test]
    fn svelte_shorthand_props_harvested() {
        // AC-3: `let { a, b } = $props()` harvests `a`, `b` with `local == name`.
        let info = &svelte_props(r"<script>let { a, b } = $props();</script>")[0];
        assert_eq!(prop_names(info), vec!["a", "b"]);
        for prop in &info.component_props {
            assert_eq!(prop.local, prop.name);
        }
    }

    #[test]
    fn svelte_renamed_prop_tracks_local_and_script_use() {
        // AC-4: `let { a: alias } = $props()` harvests `a` with `local == "alias"`,
        // and a reference to `alias` sets `used_in_script` for prop `a`.
        let info =
            &svelte_props(r"<script>let { a: alias } = $props(); console.log(alias);</script>")[0];
        assert_eq!(prop_names(info), vec!["a"]);
        let prop = &info.component_props[0];
        assert_eq!(prop.local, "alias");
        assert!(
            prop.used_in_script,
            "alias is referenced, so a is used in script"
        );
    }

    #[test]
    fn svelte_unreferenced_prop_is_unused_in_script() {
        let info = &svelte_props(r"<script>let { a } = $props();</script>")[0];
        assert_eq!(prop_names(info), vec!["a"]);
        assert!(!info.component_props[0].used_in_script);
    }

    #[test]
    fn svelte_default_prop_peeled() {
        // AC-5: `let { a = 1 } = $props()` harvests `a` (default peeled).
        let info = &svelte_props(r"<script>let { a = 1 } = $props();</script>")[0];
        assert_eq!(prop_names(info), vec!["a"]);
    }

    #[test]
    fn svelte_bindable_default_peeled() {
        // The bindable form `let { a = $bindable() } = $props()`: `a` is still a
        // declared prop (the default value is irrelevant to the local name).
        let info = &svelte_props(r"<script>let { a = $bindable() } = $props();</script>")[0];
        assert_eq!(prop_names(info), vec!["a"]);
    }

    #[test]
    fn svelte_rest_element_sets_fallthrough_abstain() {
        // AC-6: `let { a, ...rest } = $props()` sets has_props_attrs_fallthrough.
        let info = &svelte_props(r"<script>let { a, ...rest } = $props();</script>")[0];
        assert!(info.has_props_attrs_fallthrough);
    }

    #[test]
    fn svelte_bare_identifier_binding_sets_unharvestable_abstain() {
        // AC-7: `let p = $props()` (no destructure) sets has_unharvestable_props.
        let info = &svelte_props(r"<script>let p = $props(); console.log(p.x);</script>")[0];
        assert!(info.has_unharvestable_props);
        assert!(info.component_props.is_empty());
    }

    #[test]
    fn svelte_nested_destructure_sets_unharvestable_abstain() {
        // A nested destructure (`{ a: { x } }`) cannot be flattened. Abstain.
        let info = &svelte_props(r"<script>let { a: { x } } = $props();</script>")[0];
        assert!(info.has_unharvestable_props);
    }

    #[test]
    fn svelte_prop_used_only_in_markup_credited_as_template_root() {
        // AC-8: a prop used only in markup (`{a}`) is credited via
        // `apply_template_usage`, so `used_in_template` is set (parity with Vue).
        let info = &svelte_props(r"<script>let { a } = $props();</script><p>{a}</p>")[0];
        assert_eq!(prop_names(info), vec!["a"]);
        assert!(
            info.component_props[0].used_in_template,
            "a is used in markup, so used_in_template should be true"
        );
    }

    #[test]
    fn svelte_module_script_props_not_harvested() {
        // `$props()` is instance-only; a module-context script must not harvest.
        let info = &svelte_props(
            r"<script module>let { a } = $props();</script><script>let { b } = $props();</script>",
        )[0];
        // Only the instance script's `b` is harvested.
        assert_eq!(prop_names(info), vec!["b"]);
    }

    // -- Svelte custom-event dispatch harvest (unused-svelte-event) ------------

    fn dispatched_names(info: &crate::ModuleInfo) -> Vec<String> {
        let mut names: Vec<String> = info
            .svelte_dispatched_events
            .iter()
            .map(|e| e.name.clone())
            .collect();
        names.sort();
        names
    }

    #[test]
    fn svelte_dispatch_literal_event_is_harvested() {
        let info = &svelte_props(
            r"<script>import { createEventDispatcher } from 'svelte';
              const dispatch = createEventDispatcher();
              function save() { dispatch('save'); }</script>",
        )[0];
        assert_eq!(dispatched_names(info), vec!["save"]);
        assert!(!info.has_dynamic_dispatch);
    }

    #[test]
    fn svelte_dispatch_without_svelte_import_is_ignored() {
        // A local `createEventDispatcher` not imported from `svelte` is not a
        // dispatcher; the `dispatch('save')` call records nothing.
        let info = &svelte_props(
            r"<script>function createEventDispatcher() { return () => {}; }
              const dispatch = createEventDispatcher();
              dispatch('save');</script>",
        )[0];
        assert!(info.svelte_dispatched_events.is_empty());
    }

    #[test]
    fn svelte_dynamic_dispatch_sets_abstain() {
        let info = &svelte_props(
            r"<script>import { createEventDispatcher } from 'svelte';
              const dispatch = createEventDispatcher();
              function fire(name) { dispatch(name); }</script>",
        )[0];
        assert!(
            info.has_dynamic_dispatch,
            "a non-literal dispatch arg must set the abstain flag"
        );
    }

    #[test]
    fn svelte_dispatch_whole_value_use_sets_abstain() {
        let info = &svelte_props(
            r"<script>import { createEventDispatcher } from 'svelte';
              const dispatch = createEventDispatcher();
              forward(dispatch);</script>",
        )[0];
        assert!(
            info.has_dynamic_dispatch,
            "passing the dispatch binding as a whole value must set the abstain flag"
        );
    }

    #[test]
    fn svelte_listened_event_on_component_is_harvested() {
        let info =
            &svelte_props(r"<script>import Child from './Child.svelte';</script><Child on:save />")
                [0];
        assert!(info.svelte_listened_events.contains(&"save".to_string()));
    }
}