luaux 0.1.3

LuauX — JSX-style markup for Luau
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
//! Compiling a `.luaux` source to `.luau`.
//!
//! The transform is local (PLAN.md §5.1): LuauX regions are replaced and every
//! other byte passes through untouched. Expressions captured inside LuauX are
//! themselves compiled, so `{cond and <X/> or nil}` works at any depth.

use crate::backend::{Backend, EmitContext, EmitError};
use crate::config::{Config, LintLevel};
use crate::lexer::{LexError, Lexer};
use crate::lint;
use crate::markup::{self, Attribute, AttributeValue, Child, Element, Fragment, Node};
use crate::markup_scan::Scanner;
use crate::resolve::{blank_luaux_regions, Resolver};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompileError {
    pub message: String,
    pub offset: usize,
    /// Length of the offending text, for underlining. Zero means "point here".
    pub length: usize,
    /// Suggestion shown separately from the message.
    pub help: Option<String>,
}

impl fmt::Display for CompileError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} (at byte {})", self.message, self.offset)
    }
}

impl std::error::Error for CompileError {}

impl From<LexError> for CompileError {
    fn from(error: LexError) -> Self {
        Self {
            message: error.message,
            offset: error.offset,
            length: 0,
            help: None,
        }
    }
}

impl From<markup::ParseError> for CompileError {
    fn from(error: markup::ParseError) -> Self {
        // An empty hole is usually a deletion accident; a value-less one in an
        // attribute is someone trying to comment where a value is required.
        let help = if error.message.contains("is empty") {
            Some("remove it, or put an expression in it".to_string())
        } else if error.message.contains("contains no value") {
            Some("a comment only stands in for a child, not a prop".to_string())
        } else {
            None
        };

        Self {
            message: error.message,
            offset: error.offset,
            length: 0,
            help,
        }
    }
}

impl From<EmitError> for CompileError {
    fn from(error: EmitError) -> Self {
        Self {
            message: error.message,
            offset: error.offset,
            length: error.length,
            help: error.help,
        }
    }
}

/// A non-fatal diagnostic. Same shape as an error so the CLI renders both the
/// same way.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Warning {
    pub message: String,
    pub offset: usize,
    pub length: usize,
    pub help: Option<String>,
}

pub fn compile(source: &str, backend: &dyn Backend) -> Result<String, CompileError> {
    Ok(compile_configured(source, backend, Config::default())?.0)
}

/// A compile that produced output, and everything it has to say about it.
///
/// `errors` is not a contradiction: a resolution error — an unknown element, an
/// attribute a class does not have — is **recovered** from, so the output is
/// complete and still Luau while being wrong at those positions. That split is
/// what lets a language server report every mistake at once and still hand the
/// generated file to a type checker, instead of one mistake costing the whole
/// file's analysis.
///
/// A caller that writes the output to disk must treat a non-empty `errors` as
/// failure — [`compile_configured`] does exactly that.
#[derive(Debug, Clone)]
pub struct Compiled {
    pub output: String,
    pub warnings: Vec<Warning>,
    /// Resolution errors, in source order. Recovered from, never guessed at.
    pub errors: Vec<CompileError>,
}

/// Compiles with project aliases, recovering from resolution errors.
///
/// Parse errors still stop the compile and come back as `Err`: recovering from
/// `<Frame` with no `>` means deciding what the author meant, and a wrong guess
/// buries the real mistake under invented ones. Resolution errors need no such
/// guess — the tree is already built and each name is checked on its own — so
/// they are collected and the file carries on.
pub fn compile_recovering(
    source: &str,
    backend: &dyn Backend,
    config: Config,
) -> Result<Compiled, CompileError> {
    compile_inner(source, backend, config)
}

/// Compiles with project aliases from `luaux.toml` (PLAN.md §8).
///
/// Fails on the first resolution error, so output that reaches disk is never
/// knowingly wrong. Tooling that wants every error at once — and the output
/// alongside them — wants [`compile_recovering`].
pub fn compile_configured(
    source: &str,
    backend: &dyn Backend,
    config: Config,
) -> Result<(String, Vec<Warning>), CompileError> {
    let compiled = compile_inner(source, backend, config)?;

    match compiled.errors.into_iter().next() {
        Some(error) => Err(error),
        None => Ok((compiled.output, compiled.warnings)),
    }
}

fn compile_inner(
    source: &str,
    backend: &dyn Backend,
    config: Config,
) -> Result<Compiled, CompileError> {
    // Name resolution needs the whole file, so it runs once up front: blank the
    // LuauX regions (`.luaux` is not parseable as Luau), collect every binding,
    // and thread the result through — including into nested expressions, which
    // are compiled separately but must still see the file's bindings.
    let spans = luaux_spans(source)?;
    let blanked = blank_luaux_regions(source, &spans);
    let level = config.static_conditional_child;
    let imports = config.clone();
    let resolver = Resolver::new(&blanked, config);

    let mut warnings = Vec::new();
    let mut errors = Vec::new();
    let mut helpers = crate::backend::Helpers::default();
    let output = compile_with(
        source,
        backend,
        &resolver,
        level,
        &mut warnings,
        &mut errors,
        &mut helpers,
    )?;

    // Only the outermost call injects: nested expressions are compiled on their
    // own but are spliced back into this file, so their helper usage belongs to
    // this preamble.
    let output = crate::imports::inject(&output, helpers, resolver.bound(), &imports)?;

    // Source order, because a list of diagnostics is read top to bottom and
    // nested regions are compiled out of order.
    errors.sort_by_key(|error| error.offset);

    Ok(Compiled {
        output,
        warnings,
        errors,
    })
}

#[doc(hidden)]
pub fn luaux_spans_for_test(source: &str) -> Vec<(usize, usize)> {
    luaux_spans(source).unwrap_or_default()
}

/// Byte ranges of the outermost LuauX regions. Nested LuauX lies inside these, so
/// blanking the outer ranges is enough to make the file parseable.
fn luaux_spans(source: &str) -> Result<Vec<(usize, usize)>, CompileError> {
    let mut lexer = Lexer::new(source);
    let mut scanner = Scanner::new(source);
    let mut spans = Vec::new();

    while let Some(token) = lexer.next_token() {
        let token = token?;
        let lookahead = Lexer::at(source, token.end);

        if !scanner.feed(token, &lookahead) {
            continue;
        }

        let (_, end) = markup::parse_node(source, token.start)?;
        spans.push((token.start, end));

        lexer.seek(end);
        scanner.note_luaux_region();
    }

    Ok(spans)
}

#[allow(clippy::too_many_arguments)]
fn compile_with(
    source: &str,
    backend: &dyn Backend,
    resolver: &Resolver,
    level: LintLevel,
    warnings: &mut Vec<Warning>,
    errors: &mut Vec<CompileError>,
    helpers: &mut crate::backend::Helpers,
) -> Result<String, CompileError> {
    let mut lexer = Lexer::new(source);
    let mut scanner = Scanner::new(source);
    let context = EmitContext::new(source, resolver);

    let mut out = String::with_capacity(source.len());
    let mut cursor = 0usize;

    // Lexing and LuauX parsing interleave. A `.luaux` file is not Luau end to end
    // — LuauX text is a different lexical mode where `don't`, a backtick, or `--`
    // has no Luau meaning — so the lexer runs only until a LuauX region opens,
    // then resumes past it.
    loop {
        let token = match lexer.next_token() {
            None => break,
            Some(token) => token?,
        };

        let lookahead = Lexer::at(source, token.end);

        if !scanner.feed(token, &lookahead) {
            continue;
        }

        let (mut node, end) = markup::parse_node(source, token.start)?;
        compile_embedded(
            &mut node, backend, resolver, level, warnings, errors, helpers,
        )?;

        out.push_str(&source[cursor..token.start]);

        // Whatever happens next, the errors already recorded are real and come
        // first. Losing them to a later fatal one would report the downstream
        // mistake and hide the upstream one.
        let emitted = backend.emit(&node, &context);
        errors.extend(context.take_errors().into_iter().map(CompileError::from));

        out.push_str(&emitted?);
        cursor = end;

        let used = context.helpers();
        helpers.create |= used.create;
        helpers.read |= used.read;
        helpers.merge_props |= used.merge_props;

        lexer.seek(end);
        scanner.note_luaux_region();
    }

    out.push_str(&source[cursor..]);
    Ok(out)
}

/// Compiles and then re-parses the output as Luau (PLAN.md §5.4, step 8).
///
/// A codegen bug that emits invalid Luau should surface here, not on the user's
/// next rojo sync. One parse turns a confusing downstream failure into a clear
/// internal error.
pub fn compile_verified(
    source: &str,
    backend: &dyn Backend,
    config: &Config,
) -> Result<(String, Vec<Warning>), CompileError> {
    let (output, warnings) = compile_configured(source, backend, config.clone())?;

    if let Err(errors) =
        full_moon::parse_fallible(&output, full_moon::LuaVersion::luau()).into_result()
    {
        let detail = errors
            .iter()
            .map(|error| error.to_string())
            .collect::<Vec<_>>()
            .join("; ");

        return Err(CompileError {
            message: format!(
                "internal error: the {} backend emitted invalid Luau — {detail}",
                backend.name()
            ),
            offset: 0,
            length: 0,
            help: Some("this is a luaux bug; please report it".into()),
        });
    }

    Ok((output, warnings))
}

/// Compiles LuauX appearing inside captured Luau expressions.
///
/// Expressions are held verbatim, so nested LuauX is still source text at this
/// point. Recursing here is what makes `{items:map(function() return <X/> end)}`
/// work.
#[allow(clippy::too_many_arguments)]
fn compile_embedded(
    node: &mut Node,
    backend: &dyn Backend,
    resolver: &Resolver,
    level: LintLevel,
    warnings: &mut Vec<Warning>,
    errors: &mut Vec<CompileError>,
    helpers: &mut crate::backend::Helpers,
) -> Result<(), CompileError> {
    match node {
        Node::Element(element) => {
            compile_element(element, backend, resolver, level, warnings, errors, helpers)
        }
        Node::Fragment(fragment) => compile_fragment(
            fragment, backend, resolver, level, warnings, errors, helpers,
        ),
    }
}

#[allow(clippy::too_many_arguments)]
fn compile_element(
    element: &mut Element,
    backend: &dyn Backend,
    resolver: &Resolver,
    level: LintLevel,
    warnings: &mut Vec<Warning>,
    errors: &mut Vec<CompileError>,
    helpers: &mut crate::backend::Helpers,
) -> Result<(), CompileError> {
    for attribute in &mut element.attributes {
        match attribute {
            Attribute::Spread { expression, .. } => {
                *expression = compile_with(
                    expression, backend, resolver, level, warnings, errors, helpers,
                )?;
            }
            Attribute::Named { value, .. } => {
                if let AttributeValue::Expression(expression) = value {
                    *expression = compile_with(
                        expression, backend, resolver, level, warnings, errors, helpers,
                    )?;
                }
            }
        }
    }

    compile_children(
        &mut element.children,
        backend,
        resolver,
        level,
        warnings,
        errors,
        helpers,
    )
}

#[allow(clippy::too_many_arguments)]
fn compile_fragment(
    fragment: &mut Fragment,
    backend: &dyn Backend,
    resolver: &Resolver,
    level: LintLevel,
    warnings: &mut Vec<Warning>,
    errors: &mut Vec<CompileError>,
    helpers: &mut crate::backend::Helpers,
) -> Result<(), CompileError> {
    // Text in an element becomes its `Text` property. A fragment is a plain
    // table with no element to carry it, and Vide's numeric slots take an
    // Instance, a table or a function — never a string. So the text has nowhere
    // to go, and the backend used to drop it without a word.
    if let Some(Child::Text { text, span }) = fragment
        .children
        .iter()
        .find(|child| matches!(child, Child::Text { .. }))
    {
        return Err(CompileError {
            message: "a fragment cannot hold text".to_string(),
            offset: span.start,
            length: span.end.saturating_sub(span.start),
            help: Some(format!(
                "a fragment is a plain table, so there is no element for the text \
                 to belong to; wrap it in one, as <TextLabel>{text}</TextLabel>"
            )),
        });
    }

    compile_children(
        &mut fragment.children,
        backend,
        resolver,
        level,
        warnings,
        errors,
        helpers,
    )
}

#[allow(clippy::too_many_arguments)]
fn compile_children(
    children: &mut [Child],
    backend: &dyn Backend,
    resolver: &Resolver,
    level: LintLevel,
    warnings: &mut Vec<Warning>,
    errors: &mut Vec<CompileError>,
    helpers: &mut crate::backend::Helpers,
) -> Result<(), CompileError> {
    for child in children {
        match child {
            Child::Node(node) => {
                compile_embedded(node, backend, resolver, level, warnings, errors, helpers)?
            }
            Child::Expression { expression, span } => {
                // §11.1 runs on the original text, where LuauX is still `<...>`.
                if level != LintLevel::Off {
                    let spans = luaux_spans(expression).unwrap_or_default();

                    if lint::has_unwrapped_luaux(expression, &spans) {
                        let warning = lint::static_conditional_child(span.start, expression.len());

                        if level == LintLevel::Error {
                            return Err(CompileError {
                                message: warning.message,
                                offset: warning.offset,
                                length: warning.length,
                                help: warning.help,
                            });
                        }

                        warnings.push(warning);
                    }
                }

                *expression = compile_with(
                    expression, backend, resolver, level, warnings, errors, helpers,
                )?;
                // Rule 2 — checked after compiling, so nested LuauX has already
                // become ordinary Luau and the expression parses.
                lint::check_conditional_child(expression, span.start)?;
            }
            Child::Text { .. } | Child::Comment { .. } => {}
        }
    }

    Ok(())
}

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

    /// Fixtures do not import Vide, so the factory-in-scope check would fire.
    /// That check is covered directly by `imports::tests`.
    fn test_config() -> Config {
        Config::with_create("create")
    }

    /// Bound on line 1 so the factory-in-scope check passes without changing
    /// any fixture's line count.
    const BINDING: &str = "local create = _G.create; ";

    fn try_build(source: &str) -> Result<String, CompileError> {
        compile_configured(&format!("{BINDING}{source}"), &Vide, test_config())
            .map(|(output, _)| output)
    }

    fn build(source: &str) -> String {
        strip_preamble(&try_build(source).expect("compile"))
    }

    /// Removes injected helper `require`s so a test can assert on the code it
    /// is actually about. Covered directly by `imports::tests`.
    fn strip_preamble(output: &str) -> String {
        let mut rest = output;

        while let Some(end) = rest.find("; ") {
            let head = &rest[..end];

            let injected = head.starts_with("local function __luaux_")
                || head == BINDING.trim_end_matches("; ");

            if !injected {
                break;
            }

            rest = &rest[end + 2..];
        }

        rest.to_string()
    }

    /// Message and help joined, so assertions can check either half.
    fn build_err(source: &str) -> String {
        let error = try_build(source).expect_err("should fail");
        match error.help {
            Some(help) => format!("{}{help}", error.message),
            None => error.message,
        }
    }

    #[test]
    fn passes_through_sources_without_luaux() {
        let source = "local x = 1\nreturn x < 2\n";
        assert_eq!(build(source), source);
    }

    #[test]
    fn preserves_surrounding_source_exactly() {
        assert_eq!(
            build("local a = 1\nlocal e = <Frame/>\nreturn a"),
            "local a = 1\nlocal e = create(\"Frame\")({})\nreturn a"
        );
    }

    #[test]
    fn emits_attributes() {
        assert_eq!(
            build(r#"local e = <Frame Name='a' Size={UDim2.new(1, 0)} Visible />"#),
            "local e = create(\"Frame\")({ Name = 'a', Size = UDim2.new(1, 0), Visible = true })"
        );
    }

    #[test]
    fn emits_nested_children() {
        assert_eq!(
            build("local e = <Frame><TextLabel/></Frame>"),
            "local e = create(\"Frame\")({ create(\"TextLabel\")({}) })"
        );
    }

    #[test]
    fn emits_text_as_the_text_property() {
        assert_eq!(
            build("local e = <TextLabel>Hello</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"Hello\" })"
        );
    }

    #[test]
    fn interpolated_text_is_reactive() {
        // The headline case: `count` is a Vide source, so the text has to be a
        // thunk that reads it, not a string that stringifies the function.
        assert_eq!(
            build("local e = <TextLabel>Clicked {count} times</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = function() return `Clicked {__luaux_read(count)} times` end })"
        );
        assert_eq!(
            build("local e = <TextLabel>Name: {name}</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = function() return `Name: {__luaux_read(name)}` end })"
        );
    }

    #[test]
    fn a_lone_expression_needs_no_thunk() {
        // Vide already treats a function on a property key as a source, so
        // `Text = label` is right whether label is a source or a plain string.
        assert_eq!(
            build("local e = <TextButton>{label}</TextButton>"),
            "local e = create(\"TextButton\")({ Text = label })"
        );
    }

    #[test]
    fn literal_only_text_stays_a_plain_string() {
        assert_eq!(
            build("local e = <TextLabel>Hello</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"Hello\" })"
        );
    }

    #[test]
    fn text_children_override_a_text_attribute() {
        // PROPOSAL.md Rule 5.
        assert_eq!(
            build(r#"local e = <TextLabel Text="A">B</TextLabel>"#),
            "local e = create(\"TextLabel\")({ Text = \"B\" })"
        );
    }

    #[test]
    fn escapes_braces_and_backticks_in_text() {
        // PROPOSAL.md Examples 10 and 13.
        assert_eq!(
            build(r"local e = <TextLabel>literal \{text}</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"literal {text}\" })"
        );
        assert_eq!(
            build(r"local e = <TextLabel>slash \\{text}</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = function() return `slash \\\\{__luaux_read(text)}` end })"
        );
        assert_eq!(
            build("local e = <TextLabel>a ` b</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"a ` b\" })"
        );
    }

    #[test]
    fn expression_children_are_emitted_verbatim() {
        // Deliberately *not* wrapped in a one-element table: Vide iterates
        // children with generalised `for k, v in t`, which skips an absent key
        // rather than stopping at it, so a nil child cannot truncate its
        // siblings. Verified against real Vide in tests/runtime.
        assert_eq!(
            build("local e = <Frame>{cond and child or nil}</Frame>"),
            "local e = create(\"Frame\")({ cond and child or nil })"
        );
    }

    #[test]
    fn a_fragment_rejects_text_rather_than_dropping_it() {
        // A fragment has no element to carry a `Text` property, so text in one
        // has nowhere to go. It used to vanish silently.
        for source in [
            "local a = <>hello</>",
            "local a = <>Count: {n} items</>",
            "local a = <Frame><>text</></Frame>",
        ] {
            let error = build_err(source);
            assert!(
                error.contains("fragment cannot hold text"),
                "{source}: {error}"
            );
        }

        // An expression child is fine — a table slot can hold one.
        assert!(build("local a = <>{n}</>").contains("{ n }"));
    }

    #[test]
    fn emits_fragments_as_plain_tables() {
        // Vide recurses tables in numeric position, so a fragment needs no
        // runtime representation of its own.
        assert_eq!(
            build("local e = (<><Frame/><TextLabel/></>)"),
            "local e = ({ create(\"Frame\")({}), create(\"TextLabel\")({}) })"
        );
    }

    #[test]
    fn handles_luaux_text_the_luau_lexer_could_not() {
        // Apostrophes, backticks and `--` are meaningless in LuauX text but would
        // each derail a whole-file Luau tokenizer.
        assert_eq!(
            build("local e = <TextLabel>don't</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"don't\" })"
        );
        assert_eq!(
            build("local e = <TextLabel>a -- b</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"a -- b\" })"
        );
        assert_eq!(
            build("local e = <TextLabel>[[x]]</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"[[x]]\" })"
        );
    }

    #[test]
    fn resumes_lexing_correctly_after_a_region() {
        // The scanner treats a consumed region as an expression, so the `<`
        // that follows is a comparison, not a second region.
        assert_eq!(
            build("local ok = count < 2\nlocal e = <Frame/>\nlocal also = count < 3"),
            "local ok = count < 2\nlocal e = create(\"Frame\")({})\nlocal also = count < 3"
        );
    }

    #[test]
    fn bound_names_are_components() {
        assert_eq!(
            build("local Receipt = require('./Receipt')\nlocal e = <Receipt Name={n} />"),
            "local Receipt = require('./Receipt')\nlocal e = Receipt({ Name = n })"
        );
        // Dotted names never need a binding — no Roblox class contains a dot.
        assert_eq!(build("local e = <Foo.Bar/>"), "local e = Foo.Bar({})");
    }

    fn build_with(source: &str, config: &str) -> String {
        strip_preamble(
            &compile_configured(
                &format!("{BINDING}{source}"),
                &Vide,
                crate::Config::parse(config).expect("config"),
            )
            .expect("compile")
            .0,
        )
    }

    /// Warnings raised while compiling with the default config.
    fn warnings_for(source: &str) -> Vec<Warning> {
        compile_configured(&format!("{BINDING}{source}"), &Vide, test_config())
            .expect("compile")
            .1
    }

    fn build_with_err(source: &str, config: &str) -> String {
        let error = compile_configured(
            &format!("{BINDING}{source}"),
            &Vide,
            crate::Config::parse(config).expect("config"),
        )
        .expect_err("should fail");
        match error.help {
            Some(help) => format!("{}{help}", error.message),
            None => error.message,
        }
    }

    #[test]
    fn text_children_are_double_quoted_and_escape_double_quotes() {
        // An apostrophe is common in UI copy and now needs no escape.
        assert_eq!(
            build("local e = <TextLabel>don't</TextLabel>"),
            "local e = create(\"TextLabel\")({ Text = \"don't\" })"
        );

        // A double quote does, and the result must still parse.
        let out = compile_verified(
            &format!("{BINDING}local e = <TextLabel>say \"hi\"</TextLabel>"),
            &Vide,
            &crate::Config::default(),
        );
        let out = out.expect("valid Luau").0;
        assert!(out.contains(r#"Text = "say \"hi\"""#), "{out}");
    }

    #[test]
    fn element_aliases_resolve_to_canonical_classes() {
        // The attribute keeps its single quotes: a string literal is the
        // author's own Luau, re-emitted byte for byte. Only strings luaux
        // *builds* — text children — are double-quoted.
        assert_eq!(
            build_with(
                "local e = <text Text='hi'/>",
                "[elements]\nTextLabel = \"text\"\n"
            ),
            "local e = create(\"TextLabel\")({ Text = 'hi' })"
        );
    }

    #[test]
    fn property_aliases_emit_canonical_names() {
        // Emitted code is identical regardless of the project's aliases.
        assert_eq!(
            build_with(
                "local e = <Frame bgColor={c}/>",
                "[properties.Frame]\nBackgroundColor3 = \"bgColor\"\n"
            ),
            "local e = create(\"Frame\")({ BackgroundColor3 = c })"
        );
    }

    #[test]
    fn an_alias_retires_the_original_spelling() {
        let message = build_with_err(
            "local e = <TextLabel/>",
            "[elements]\nTextLabel = \"text\"\n",
        );
        assert!(message.contains("use <text>"), "{message}");

        let message = build_with_err(
            "local e = <Frame BackgroundColor3={c}/>",
            "[properties.Frame]\nBackgroundColor3 = \"bgColor\"\n",
        );
        assert!(message.contains("use bgColor"), "{message}");
    }

    #[test]
    fn unknown_attributes_are_rejected() {
        // PROPOSAL.md's own examples carried this bug: a Frame has
        // BackgroundColor3, not Color3.
        let message = build_err("local e = <Frame Color3={c}/>");
        assert!(
            message.contains("no property or event named Color3"),
            "{message}"
        );
        assert!(message.contains("did you mean"), "{message}");

        // Inherited properties and events are both accepted.
        assert!(try_build("local e = <Frame BackgroundColor3={c}/>").is_ok());
        assert!(try_build("local e = <Frame Name='a'/>").is_ok());
        assert!(try_build("local e = <TextButton Activated={f}/>").is_ok());

        // Components take arbitrary props, so nothing is checked there.
        assert!(try_build("local Row = f()\nlocal e = <Row Whatever={1}/>").is_ok());
    }

    #[test]
    fn read_only_properties_are_rejected() {
        // ContentText exists on TextLabel but cannot be assigned.
        let message = build_err("local e = <TextLabel ContentText='x'/>");
        assert!(
            message.contains("no property or event named ContentText"),
            "{message}"
        );
    }

    #[test]
    fn unbound_names_are_rejected() {
        // The correctness gap this closes: without resolution, `<Frmae/>`
        // compiled to a call to an undefined global and only failed at runtime.
        let message = build_err("local e = <Frmae/>");
        assert!(message.contains("did you mean <Frame>"), "{message}");

        let message = build_err("local e = <Receipt/>");
        assert!(message.contains("not defined"), "{message}");
    }

    #[test]
    fn resolution_sees_bindings_from_the_whole_file() {
        // A component used inside a nested expression still resolves, even
        // though that expression is compiled on its own.
        assert!(try_build(
            "local Row = require('./Row')\nlocal e = <Frame>{cond and <Row/> or nil}</Frame>"
        )
        .is_ok());

        // And one declared *after* its use.
        assert!(try_build("local e = <Frame>{Row}</Frame>\nlocal function Row() end").is_ok());
    }

    #[test]
    fn compiles_luaux_nested_inside_expressions() {
        assert_eq!(
            build("local e = <Frame>{cond and <TextLabel/> or nil}</Frame>"),
            "local e = create(\"Frame\")({ cond and create(\"TextLabel\")({}) or nil })"
        );
    }

    #[test]
    fn compiles_luaux_inside_attribute_expressions() {
        assert_eq!(
            build("local e = <Frame Size={f(<TextLabel/>)} />"),
            "local e = create(\"Frame\")({ Size = f(create(\"TextLabel\")({})) })"
        );
    }

    #[test]
    fn emits_merge_props_for_spreads() {
        assert_eq!(
            build("local e = <Frame {props} Name={n} />"),
            "local e = create(\"Frame\")(__luaux_merge(props, { Name = n }))"
        );
    }

    #[test]
    fn compiles_multiple_sites() {
        assert_eq!(
            build("local a = <Frame/>\nlocal b = <TextLabel/>"),
            "local a = create(\"Frame\")({})\nlocal b = create(\"TextLabel\")({})"
        );
    }

    #[test]
    fn rejects_text_on_a_class_without_a_text_property() {
        assert!(build_err("local e = <Frame>hello</Frame>").contains("no Text property"));
    }

    /// Every resolution error at once, and output alongside them.
    ///
    /// Stopping at the first costs the author every other diagnostic *and* the
    /// generated Luau, so nothing downstream — a type checker, a language
    /// server — can say anything about the file either. One mistake should cost
    /// one diagnostic.
    fn recovered(source: &str) -> Compiled {
        compile_recovering(&format!("{BINDING}{source}"), &Vide, test_config())
            .expect("a resolution error is not fatal")
    }

    #[test]
    fn every_unknown_element_is_reported_not_just_the_first() {
        let compiled = recovered("local e = <Frmae><Recieve/><Buton/></Frmae>");

        let messages: Vec<&str> = compiled
            .errors
            .iter()
            .map(|error| error.message.as_str())
            .collect();
        assert_eq!(messages.len(), 3, "{messages:#?}");

        assert!(messages[0].contains("Frmae"), "{messages:#?}");
        assert!(messages[1].contains("Recieve"), "{messages:#?}");
        assert!(messages[2].contains("Buton"), "{messages:#?}");
    }

    #[test]
    fn errors_are_reported_in_source_order() {
        let compiled = recovered("local e = <Frame><Buton/><Aaa/></Frame>");
        let offsets: Vec<usize> = compiled.errors.iter().map(|error| error.offset).collect();

        assert_eq!(offsets.len(), 2);
        assert!(offsets[0] < offsets[1], "{offsets:?}");
    }

    #[test]
    fn every_unknown_attribute_is_reported() {
        let compiled = recovered("local e = <Frame Nonsense={1} Rubbish={2}/>");
        assert_eq!(compiled.errors.len(), 2, "{:#?}", compiled.errors);
    }

    /// The output is still Luau, so whatever reads it next keeps working. That
    /// is the whole point of recovering rather than collecting.
    #[test]
    fn a_file_with_a_bad_tag_still_produces_luau() {
        let compiled = recovered("local e = <Frmae Size={1}/>\nlocal after = 1");

        assert!(compiled.output.contains("Frmae"), "{}", compiled.output);
        assert!(compiled.output.contains("after"), "{}", compiled.output);
        full_moon::parse_fallible(&compiled.output, full_moon::LuaVersion::luau())
            .into_result()
            .expect("recovered output is still Luau");
    }

    /// An unknown tag has an unknown class, so its attributes cannot be checked
    /// against anything — reporting them would be one mistake reported four
    /// times.
    #[test]
    fn attributes_of_an_unknown_element_do_not_cascade() {
        let compiled = recovered("local e = <Frmae Text='a' Size={1} Visible/>");
        assert_eq!(compiled.errors.len(), 1, "{:#?}", compiled.errors);
    }

    #[test]
    fn text_children_of_an_unknown_element_do_not_cascade() {
        let compiled = recovered("local e = <Frmae>hello</Frmae>");
        assert_eq!(compiled.errors.len(), 1, "{:#?}", compiled.errors);
    }

    /// Recovery is for resolution, not for parsing. Guessing what an unclosed
    /// tag meant buries the real mistake under invented ones.
    #[test]
    fn a_parse_error_is_still_fatal() {
        assert!(
            compile_recovering(&format!("{BINDING}local e = <Frame"), &Vide, test_config())
                .is_err()
        );
    }

    /// Output that reaches disk is never knowingly wrong, so the entry point the
    /// CLI uses still fails on the first error.
    #[test]
    fn the_writing_entry_point_still_fails() {
        assert!(try_build("local e = <Frmae/>").is_err());
    }

    #[test]
    fn rejects_bare_text_on_a_component() {
        assert!(build_err("local e = <Receipt>hello</Receipt>").contains("component"));
    }

    #[test]
    fn helpers_are_inlined_not_required() {
        // luaux emits no `require` at all: helpers are inlined and the factory
        // is the author's own import.
        let out = try_build("local e = <Frame>{--[[c]]}</Frame>").expect("compile");
        assert!(!out.contains("require"), "{out}");
    }

    #[test]
    fn inlines_the_merge_helper_only_when_a_spread_is_used() {
        let plain = try_build("local e = <Frame/>").expect("compile");
        assert!(!plain.contains("__luaux_merge"), "{plain}");

        let spread = try_build("local p = {}\nlocal e = <Frame {p}/>").expect("compile");
        assert!(spread.contains("local function __luaux_merge"), "{spread}");
    }

    #[test]
    fn inlines_read_only_when_text_interpolates() {
        let plain = try_build("local e = <TextLabel>Hi</TextLabel>").expect("compile");
        assert!(!plain.contains("__luaux_read"), "{plain}");

        let interpolated =
            try_build("local e = <TextLabel>Hi {name}</TextLabel>").expect("compile");
        assert!(
            interpolated.contains("local function __luaux_read"),
            "{interpolated}"
        );
    }

    #[test]
    fn helpers_used_only_inside_nested_expressions_are_still_inlined() {
        let out = try_build(
            "local cond = true\nlocal e = <Frame>{cond and <TextLabel>a {b}</TextLabel> or nil}</Frame>",
        )
        .expect("compile");
        assert!(out.contains("local function __luaux_read"), "{out}");
    }

    #[test]
    fn the_factory_must_be_in_scope() {
        let error = compile_configured("local e = <Frame/>", &Vide, Config::default())
            .expect_err("should fail");
        assert!(
            error.message.contains("`create` is not in scope"),
            "{error:?}"
        );

        // And a dotted factory checks its root.
        let ok = compile_configured(
            "local vide = require('./vide')\nlocal e = <Frame/>",
            &Vide,
            Config::with_create("vide.create"),
        );
        assert!(ok.is_ok(), "{ok:?}");
    }

    /// A factory reached through a method call has to *emit* as well as
    /// resolve. `scope:New("Frame")({})` is legal Luau — a method call is a
    /// function call, so calling its result is too — and the spread form has to
    /// hold up the same way. Neither was covered by the in-scope test alone,
    /// which never reaches codegen.
    #[test]
    fn a_method_factory_emits_and_reparses() {
        // The spread case below overflows the harness's own 2 MB inside
        // full_moon — the very thing the CLI's `STACK` exists for.
        std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(|| {
                let config = Config::with_create("scope:New");

                let (plain, _) = compile_verified(
                    "local scope = _G.s\nlocal e = <Frame Size={1}/>",
                    &Vide,
                    &config,
                )
                .expect("valid Luau");
                assert!(
                    plain.contains("scope:New(\"Frame\")({ Size = 1 })"),
                    "{plain}"
                );

                let (spread, _) = compile_verified(
                    "local scope = _G.s\nlocal p = {}\nlocal e = <Frame {p} Size={1}/>",
                    &Vide,
                    &config,
                )
                .expect("valid Luau");
                assert!(
                    spread.contains("scope:New(\"Frame\")(__luaux_merge(p, { Size = 1 }))"),
                    "{spread}"
                );
            })
            .expect("spawn")
            .join()
            .expect("method factory thread");
    }

    /// `const` is Luau, and the import it declares binds like any other.
    ///
    /// Collecting bindings missed it, which failed in the least useful way
    /// available: a file that imported Vide on its first line was told Vide was
    /// not in scope, with a help line suggesting it import Vide.
    #[test]
    fn a_const_import_satisfies_the_factory() {
        let ok = compile_configured(
            "const vide = require('./vide')\nlocal e = <Frame/>",
            &Vide,
            Config::with_create("vide.create"),
        );
        assert!(ok.is_ok(), "{ok:?}");
    }

    #[test]
    fn warns_about_conditional_children_that_never_update() {
        // §11.1 — the LuauX is built once, so the condition looks live but is not.
        let warnings = warnings_for("local e = <Frame>{cond() and <TextLabel/> or nil}</Frame>");
        assert_eq!(warnings.len(), 1, "{warnings:?}");
        assert!(warnings[0].message.contains("built once"), "{warnings:?}");

        // Wrapped in a function, Vide tracks it — no warning.
        assert!(warnings_for(
            "local e = <Frame>{function() return cond() and <TextLabel/> or nil end}</Frame>"
        )
        .is_empty());

        // The idiomatic map form must not nag.
        assert!(warnings_for(
            "local e = <Frame>{items:map(function(i) return <TextLabel/> end)}</Frame>"
        )
        .is_empty());

        // No LuauX in the expression, nothing to say.
        assert!(warnings_for("local e = <Frame>{items}</Frame>").is_empty());
    }

    #[test]
    fn the_lint_level_is_configurable() {
        let source = "local e = <Frame>{cond() and <TextLabel/> or nil}</Frame>";

        let off = compile_configured(
            &format!("{BINDING}{source}"),
            &Vide,
            crate::Config::parse("[lints]\nstatic_conditional_child = \"off\"\n").expect("config"),
        )
        .expect("compile");
        assert!(off.1.is_empty());

        let escalated = compile_configured(
            &format!("{BINDING}{source}"),
            &Vide,
            crate::Config::parse("[lints]\nstatic_conditional_child = \"error\"\n")
                .expect("config"),
        );
        assert!(escalated.is_err());
    }

    #[test]
    fn comments_are_dropped_by_default_and_kept_on_request() {
        let source = "local e = <Frame><!-- why -->{--[[ how ]]}<TextLabel/></Frame>";

        // Default: no trace of them.
        // Emitted as Luau block comments, and — crucially — without a comma,
        // since a comment is not a table field.
        let kept = build(source);
        assert!(kept.contains("--[[ why ]]"), "{kept}");
        assert!(kept.contains("--[[ how ]]"), "{kept}");
        assert!(
            !kept.contains("]],"),
            "a comment must not take a comma: {kept}"
        );
    }

    #[test]
    fn a_multi_line_markup_comment_keeps_its_shape() {
        let source = format!(
            "{BINDING}local e = (\n  <Frame>\n    <!--\n      first line\n      second line\n    -->\n    <TextLabel>x</TextLabel>\n  </Frame>\n)"
        );
        let out = build(&source);

        // The opening bracket must not drag the first line up with it.
        assert!(out.contains("--[[\n      first line"), "{out}");
        assert!(out.contains("second line\n    ]]"), "{out}");

        // And the emission still spans exactly the lines the source did.
        assert_eq!(out.lines().count(), source.lines().count(), "{out}");
    }

    #[test]
    fn a_comment_ending_in_a_bracket_does_not_close_early() {
        // `]` meeting the closing `]]` would form `]]]` and terminate a byte
        // early, leaving a stray bracket as code.
        let out = compile_verified(
            &format!("{BINDING}local e = <Frame><!-- see list[1] --></Frame>"),
            &Vide,
            &crate::Config::default(),
        );
        assert!(out.is_ok(), "{out:?}");

        let nested = compile_verified(
            &format!("{BINDING}local e = <Frame><!-- a ]] b --></Frame>"),
            &Vide,
            &crate::Config::default(),
        );
        assert!(nested.is_ok(), "{nested:?}");
        assert!(
            nested.unwrap().0.contains("--[=["),
            "bracket level must rise"
        );
    }

    #[test]
    fn a_kept_comment_beside_no_value_still_emits_valid_luau() {
        // `{ --[[c]] }` — a table holding only a comment.
        let out = compile_verified(
            &format!("{BINDING}local e = <Frame><!-- only --></Frame>"),
            &Vide,
            &crate::Config::default(),
        );
        assert!(out.is_ok(), "{out:?}");
    }

    #[test]
    fn rejects_nil_on_the_left_of_and() {
        // PROPOSAL.md Rule 2. `cond and nil or x` is always `x` in Lua, so the
        // condition silently does nothing.
        let message = build_err("local e = <Frame>{cond and nil or child}</Frame>");
        assert!(message.contains("no effect"), "{message}");
        assert!(message.contains("if cond then"), "{message}");

        // The faithful shape is fine — an element is always truthy.
        assert!(try_build("local e = <Frame>{cond and child or nil}</Frame>").is_ok());
        assert!(try_build("local e = <Frame>{if cond then a else b}</Frame>").is_ok());
    }

    #[test]
    fn rejects_ambiguous_expression_beside_element_children() {
        // On a Text-bearing class, `{label}` could be the text or a child, and
        // guessing emits code that fails inside Vide at runtime.
        let message = build_err("local e = <TextButton>{label}<UICorner/></TextButton>");
        assert!(message.contains("unclear"), "unexpected: {message}");
        assert!(message.contains("Text={...}"), "unexpected: {message}");

        // Unambiguous either side of it.
        assert!(try_build("local e = <TextButton>Click<UICorner/></TextButton>").is_ok());
        assert!(try_build("local e = <TextButton>{label}</TextButton>").is_ok());
        assert!(try_build("local e = <Frame>{child}<TextLabel/></Frame>").is_ok());
    }

    /// Multi-line fixtures, shared by the line-preservation and validity tests.
    const MULTILINE_FIXTURES: &[&str] = &[
        "local e = (\n  <Frame\n    Size={size}\n    Visible\n  >\n    <TextLabel>Hi</TextLabel>\n  </Frame>\n)\n",
        "local e = (\n  <Frame>\n    <TextLabel>\n      Clicked {count} times\n    </TextLabel>\n    <UICorner/>\n  </Frame>\n)\n",
        "local e = (\n  <>\n    <Frame/>\n    <TextLabel/>\n  </>\n)\n",
        "local Button = f()\nlocal e = (\n  <Button\n    OnClick={function()\n      count(count() + 1)\n    end}\n  />\n)\n",
        "local e = (\n  <Frame>\n    {cond and (\n      <TextLabel/>\n    ) or nil}\n  </Frame>\n)\n",
        "local e = (\n  <Frame\n    {props}\n    Name={n}\n  />\n)\n",
        // Nothing between the tags. Every fixture above has an attribute or a
        // child, so none of them reached the empty-table path — which is how it
        // came to lose lines unnoticed.
        "local e = (\n  <Frame>\n  </Frame>\n)\n",
        "local e = (\n  <Frame>\n\n  </Frame>\n)\n",
        "local e = (\n  <>\n  </>\n)\n",
        "local e = (\n  <Frame>\n    <TextLabel>\n    </TextLabel>\n  </Frame>\n)\n",
        // Nothing *but* spreads. These emit no table, so the closing brace that
        // usually carries the emission down to the closing tag is never written
        // — the fixture above with a `Name` beside its spread hides that, since
        // the named attribute is enough to bring the table back.
        "local e = (\n  <Frame\n    {props}\n  />\n)\n",
        "local e = (\n  <Frame\n    {props}\n  >\n  </Frame>\n)\n",
        "local e = (\n  <Frame\n    {props}\n    {props}\n  />\n)\n",
    ];

    /// The generated `.luau` must have the same number of lines as the `.luaux`
    /// it came from (PLAN.md §5.5). Luau has no runtime source maps, so matching
    /// line numbers are the only thing making a stack trace or a luau-lsp
    /// diagnostic point at the right place.
    #[test]
    fn output_preserves_line_count() {
        for fixture in MULTILINE_FIXTURES {
            let compiled = build(fixture);
            assert_eq!(
                compiled.lines().count(),
                fixture.lines().count(),
                "line count changed\n--- in ---\n{fixture}\n--- out ---\n{compiled}"
            );
        }
    }

    /// An element with nothing in it is still as tall as it was written.
    ///
    /// The closing brace follows the closing tag on every other path; the empty
    /// one used to emit `{}` and stop, which shortened the file by however many
    /// lines the element spanned. That is invisible in the output — it is
    /// correct Luau, just in the wrong place — and it costs the whole file its
    /// luau-lsp answers, because a map built on matching line numbers then lines
    /// nothing up.
    #[test]
    fn an_empty_element_spanning_lines_keeps_them() {
        let source = "local e = (\n  <Frame>\n\n  </Frame>\n)\nreturn e\n";
        let compiled = build(source);

        assert_eq!(
            compiled.lines().count(),
            source.lines().count(),
            "{compiled}"
        );

        // And the statement after it is still on its own line, which is the
        // property that actually matters to a stack trace.
        let lines: Vec<&str> = compiled.lines().collect();
        assert!(lines[1].contains("create(\"Frame\")"), "{compiled}");
        assert!(lines[5].contains("return e"), "{compiled}");
    }

    /// One line in, one line out: the fix must not start breaking tables that
    /// were never multi-line to begin with.
    #[test]
    fn an_empty_element_on_one_line_stays_on_one_line() {
        let compiled = build("local e = <Frame></Frame>\n");

        assert_eq!(compiled.lines().count(), 1, "{compiled}");
        assert!(compiled.contains("create(\"Frame\")({})"), "{compiled}");
    }

    /// A spread lands on its own line even when it is the first thing in the
    /// element.
    ///
    /// Every group after the first is positioned before it is written; the first
    /// was not, which a table survives — it positions its own entries — and a
    /// spread does not. The spread came out on the opening tag's line and the
    /// line it was written on came out blank, so hovering it asked luau-lsp
    /// about an empty line.
    #[test]
    fn a_leading_spread_lands_on_its_source_line() {
        let source = "local e = (\n  <Frame\n    {props}\n    Name={n}\n  />\n)\n";
        let compiled = build(source);
        let lines: Vec<&str> = compiled.lines().collect();

        assert_eq!(
            compiled.lines().count(),
            source.lines().count(),
            "{compiled}"
        );
        assert!(lines[2].contains("props"), "{compiled}");
        assert!(lines[3].contains("Name = n"), "{compiled}");
    }

    #[test]
    fn entries_land_on_their_source_lines() {
        let source = "local e = (\n  <Frame\n    Size={size}\n    Visible\n  >\n    <TextLabel>Hi</TextLabel>\n  </Frame>\n)\n";
        let compiled = build(source);
        let lines: Vec<&str> = compiled.lines().collect();

        assert!(lines[1].contains("create(\"Frame\")"), "{compiled}");
        assert!(lines[2].contains("Size = size"), "{compiled}");
        assert!(lines[3].contains("Visible = true"), "{compiled}");
        assert!(lines[5].contains("create(\"TextLabel\")"), "{compiled}");
    }

    /// Every compiled fixture must re-parse as Luau. This is the guard that
    /// stops a codegen bug reaching the user's rojo sync.
    #[test]
    fn generated_output_is_valid_luau() {
        let fixtures = [
            "local e = <Frame/>",
            r#"local e = <Frame Name='a' Size={UDim2.new(1, 0)} Visible />"#,
            "local e = <Frame><TextLabel>Hi</TextLabel></Frame>",
            "local e = <TextLabel>Name: {name}</TextLabel>",
            "local e = (<><Frame/><TextLabel/></>)",
            "local e = <Frame>{cond and <TextLabel/> or nil}</Frame>",
            "local e = <Frame {props} Name={n} />",
            "local Receipt = f()\nlocal e = <Receipt Name={n}><TextLabel>Debit</TextLabel></Receipt>",
            "local e = <TextLabel>don't</TextLabel>",
            r"local e = <TextLabel>slash \\{text}</TextLabel>",
            "return function(props) return <Frame>{props.children}</Frame> end",
        ];

        // full_moon's recursive-descent parser has large stack frames in debug
        // builds — enough to exhaust a test thread's 2 MB on the inlined merge
        // helper. Give it room rather than shrinking the fixtures to suit the
        // harness. The CLI runs on a larger stack for the same reason; see
        // `STACK` in luaux-cli's main.rs.
        std::thread::Builder::new()
            .stack_size(16 * 1024 * 1024)
            .spawn(move || {
                for fixture in fixtures.iter().chain(MULTILINE_FIXTURES.iter()) {
                    let bound = format!("{BINDING}{fixture}");
                    compile_verified(&bound, &Vide, &test_config())
                        .unwrap_or_else(|error| panic!("{fixture}\n  -> {error}"));
                }
            })
            .expect("spawn")
            .join()
            .expect("verification thread");
    }
}