luaux 0.1.1

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
//! `luaux.toml` — element and property aliases (PLAN.md §8).
//!
//! ```toml
//! [elements]              # overrides for default (Roblox) element names
//! TextLabel = "text"
//!
//! [properties]            # applies to all elements
//! TextColor3 = "textColor"
//!
//! [properties.Frame]      # per-class; beats the global table
//! BackgroundColor3 = "bgColor"
//! ```
//!
//! Overrides are **exclusive**: renaming a name retires the original, so once
//! `TextLabel = "text"` is declared, `<TextLabel>` is an error and `<text>` is
//! the spelling. That is what makes a project's vocabulary consistent rather
//! than offering two ways to write everything.
//!
//! A per-class entry can map a name to itself — `[properties.TextLabel]
//! TextColor3 = "TextColor3"` — to opt one class back out of a global rename.

use crate::roblox;
use serde::Deserialize;
use std::collections::HashMap;
use std::fmt;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigError {
    pub message: String,
}

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

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

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawConfig {
    #[serde(default)]
    elements: HashMap<String, String>,
    #[serde(default)]
    properties: HashMap<String, PropertyEntry>,
    #[serde(default)]
    lints: RawLints,
    #[serde(default)]
    factory: RawFactory,

    #[serde(default)]
    build: RawBuild,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawBuild {
    #[serde(rename = "in")]
    input: Option<String>,
    #[serde(rename = "out")]
    output: Option<String>,
    include: Option<Vec<String>>,
    exclude: Option<Vec<String>>,
    clean: Option<bool>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawFactory {
    create: Option<String>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
struct RawLints {
    static_conditional_child: Option<String>,
}

/// A blanket renaming scheme, set with `all` in `[elements]` or `[properties]`.
///
/// `Pascal` is the identity: Roblox's own spelling, and the default.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Casing {
    #[default]
    Pascal,
    Camel,
    Snake,
    Flat,
}

impl Casing {
    fn parse(text: &str) -> Option<Self> {
        match text {
            "PascalCase" => Some(Self::Pascal),
            "camelCase" => Some(Self::Camel),
            "snake_case" => Some(Self::Snake),
            "flatcase" => Some(Self::Flat),
            _ => None,
        }
    }

    /// Rewrites a Roblox name into this scheme.
    ///
    /// Word boundaries are taken from the canonical PascalCase spelling, so
    /// `UICorner` splits as `UI` + `Corner` rather than per capital, giving
    /// `ui_corner` instead of `u_i_corner`.
    pub fn apply(&self, name: &str) -> String {
        if *self == Casing::Pascal {
            return name.to_string();
        }

        if *self == Casing::Flat {
            return name.to_lowercase();
        }

        let mut words: Vec<String> = Vec::new();
        let chars: Vec<char> = name.chars().collect();
        let mut start = 0;

        for index in 1..chars.len() {
            let previous = chars[index - 1];
            let current = chars[index];
            let next = chars.get(index + 1).copied();

            // lower→upper ends a word: `textLabel` -> `text` `Label`.
            // upper→upper→lower ends one too: `UICorner` -> `UI` `Corner`.
            let boundary = (previous.is_lowercase() || previous.is_numeric())
                && current.is_uppercase()
                || previous.is_uppercase()
                    && current.is_uppercase()
                    && next.is_some_and(char::is_lowercase);

            if boundary {
                words.push(chars[start..index].iter().collect());
                start = index;
            }
        }

        words.push(chars[start..].iter().collect());

        match self {
            Casing::Snake => words
                .iter()
                .map(|word| word.to_lowercase())
                .collect::<Vec<_>>()
                .join("_"),
            Casing::Camel => words
                .iter()
                .enumerate()
                .map(|(index, word)| {
                    if index == 0 {
                        word.to_lowercase()
                    } else {
                        let mut chars = word.chars();
                        match chars.next() {
                            Some(first) => {
                                first.to_uppercase().collect::<String>()
                                    + &chars.as_str().to_lowercase()
                            }
                            None => String::new(),
                        }
                    }
                })
                .collect(),
            Casing::Pascal | Casing::Flat => unreachable!("handled above"),
        }
    }
}

/// Picks the canonical name when several collapse onto one under a casing.
///
/// Roblox ships deprecated spellings beside modern ones (`brickColor` beside
/// `BrickColor`), and they differ only by case. Prefer whatever is not
/// deprecated; if that leaves no single winner — `FormFactor`/`formFactor` are
/// both deprecated — fall back to byte order, which puts the uppercase form
/// first. See docs/adr/0001-casing-key.md.
pub fn preferred<'a>(candidates: &mut Vec<&'a str>) -> Option<&'a str> {
    if candidates.len() > 1 && candidates.iter().any(|name| !roblox::is_deprecated(name)) {
        candidates.retain(|name| !roblox::is_deprecated(name));
    }

    candidates.sort_unstable();
    candidates.first().copied()
}

/// How a lint reports. `Warn` is the default for `static_conditional_child`: the
/// pattern it catches is usually a mistake, but a conditional on a constant is
/// legitimate and would otherwise be unsilenceable (PLAN.md §11.1).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum LintLevel {
    Off,
    #[default]
    Warn,
    Error,
}

impl LintLevel {
    fn parse(text: &str) -> Option<Self> {
        match text {
            "off" => Some(Self::Off),
            "warn" => Some(Self::Warn),
            "error" => Some(Self::Error),
            _ => None,
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum PropertyEntry {
    /// `TextColor3 = "textColor"` — a global rename.
    Alias(String),
    /// `[properties.Frame]` — a per-class table.
    PerClass(HashMap<String, String>),
}

/// Resolved alias tables, keyed for lookup by what the user *writes*.
#[derive(Debug, Clone)]
pub struct Config {
    /// alias → canonical class name
    element_alias: HashMap<String, String>,
    /// canonical class name → alias that replaced it
    element_renamed: HashMap<String, String>,
    /// canonical property → alias, applying to every class
    global_properties: HashMap<String, String>,
    /// class → (canonical property → alias)
    class_properties: HashMap<String, HashMap<String, String>>,
    /// `[elements] all` — a blanket scheme, beaten by any explicit entry.
    element_casing: Casing,
    /// `[properties] all` — likewise, for properties and events.
    property_casing: Casing,
    /// §11.1 — LuauX in a child expression that no function encloses.
    pub static_conditional_child: LintLevel,
    /// Paths and file selection.
    pub build: Build,
    /// Expression called to construct an element — TypeScript's `jsxFactory`.
    ///
    /// Naming, not shape: whatever this points at must still match Vide's
    /// `create(class)(propsAndChildren)` contract. A different *shape* needs a
    /// backend, not a name.
    pub create: String,
}

/// Bare `create`, matching the common `local create = vide.create`. Projects
/// that keep Vide in one binding set `create = "vide.create"`.
pub const DEFAULT_CREATE: &str = "create";

/// Which files a build considers, and where they go.
#[derive(Debug, Clone)]
pub struct Build {
    pub input: Option<PathBuf>,
    pub output: Option<PathBuf>,
    /// Globs a file must match to be considered. A pattern with no `/` matches
    /// at any depth, as in gitignore.
    pub include: Vec<String>,
    pub exclude: Vec<String>,
    /// Delete outputs whose source is gone, so a renamed file leaves no stale
    /// twin for rojo to keep syncing.
    pub clean: bool,
}

impl Default for Build {
    fn default() -> Self {
        Self {
            input: None,
            output: None,
            include: vec!["**".to_string()],
            exclude: Vec::new(),
            clean: false,
        }
    }
}

impl Default for Config {
    fn default() -> Self {
        Self {
            element_alias: HashMap::new(),
            element_renamed: HashMap::new(),
            global_properties: HashMap::new(),
            class_properties: HashMap::new(),
            element_casing: Casing::default(),
            property_casing: Casing::default(),
            static_conditional_child: LintLevel::default(),
            build: Build::default(),
            create: DEFAULT_CREATE.to_string(),
        }
    }
}

impl Config {
    /// The blanket scheme for element names, for tooling that has to render or
    /// complete names in the project's own spelling.
    pub fn element_casing(&self) -> Casing {
        self.element_casing
    }

    /// The blanket scheme for properties and events.
    pub fn property_casing(&self) -> Casing {
        self.property_casing
    }

    /// A config whose only non-default setting is the element factory.
    pub fn with_create(create: impl Into<String>) -> Self {
        Self {
            create: create.into(),
            ..Self::default()
        }
    }

    /// Loads `luaux.toml` from `directory`, or returns an empty config if there
    /// is none. Absent config is not an error; luaux works without one.
    pub fn load(directory: &Path) -> Result<Self, ConfigError> {
        Self::load_reporting(directory).map(|(config, _)| config)
    }

    /// Loads, also returning notes about settings that are accepted but inert.
    pub fn load_reporting(directory: &Path) -> Result<(Self, Vec<String>), ConfigError> {
        let path = directory.join("luaux.toml");

        let text = match std::fs::read_to_string(&path) {
            Ok(text) => text,
            Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
                return Ok((Self::default(), Vec::new()))
            }
            Err(error) => {
                return Err(ConfigError {
                    message: format!("{}: {error}", path.display()),
                })
            }
        };

        Self::parse_reporting(&text)
    }

    /// Parses, discarding warnings. For callers that only need the config.
    pub fn parse(text: &str) -> Result<Self, ConfigError> {
        Self::parse_reporting(text).map(|(config, _)| config)
    }

    /// Parses, also returning notes about settings that are accepted but inert.
    pub fn parse_reporting(text: &str) -> Result<(Self, Vec<String>), ConfigError> {
        let raw: RawConfig = toml::from_str(text).map_err(|error| ConfigError {
            message: format!("luaux.toml: {}", error.message()),
        })?;

        let mut config = Config::default();

        if let Some(level) = &raw.lints.static_conditional_child {
            config.static_conditional_child =
                LintLevel::parse(level).ok_or_else(|| ConfigError {
                    message: format!(
                        "luaux.toml: [lints] static_conditional_child = \"{level}\" is not one of \
                         off, warn, error"
                    ),
                })?;
        }

        let warnings = Vec::new();

        config.build.input = raw.build.input.map(PathBuf::from);
        config.build.output = raw.build.output.map(PathBuf::from);
        if let Some(include) = raw.build.include {
            config.build.include = include;
        }
        if let Some(exclude) = raw.build.exclude {
            config.build.exclude = exclude;
        }
        config.build.clean = raw.build.clean.unwrap_or(false);

        if let Some(create) = raw.factory.create {
            if create.trim().is_empty() {
                return Err(ConfigError {
                    message: "luaux.toml: [factory] create cannot be empty".to_string(),
                });
            }
            config.create = create;
        }

        // `all` is reserved in both tables. No Roblox class or member is named
        // `all`, so claiming it costs nothing.
        if let Some(value) = raw.elements.get("all") {
            config.element_casing = Casing::parse(value).ok_or_else(|| ConfigError {
                message: format!(
                    "luaux.toml: [elements] all = \"{value}\" is not one of PascalCase, \
                     camelCase, snake_case, flatcase"
                ),
            })?;
        }

        for (class, alias) in raw.elements.iter().filter(|(key, _)| *key != "all") {
            if !roblox::is_class(class) {
                return Err(ConfigError {
                    message: format!(
                        "luaux.toml: [elements] {class} is not a creatable Roblox class{}",
                        suggest_class(class)
                    ),
                });
            }

            if let Some(existing) = config.element_alias.get(alias) {
                return Err(ConfigError {
                    message: format!(
                        "luaux.toml: [elements] {existing} and {class} both claim the alias \
                         \"{alias}\""
                    ),
                });
            }

            config.element_alias.insert(alias.clone(), class.clone());
            config.element_renamed.insert(class.clone(), alias.clone());
        }

        if let Some(PropertyEntry::Alias(value)) = raw.properties.get("all") {
            config.property_casing = Casing::parse(value).ok_or_else(|| ConfigError {
                message: format!(
                    "luaux.toml: [properties] all = \"{value}\" is not one of PascalCase, \
                     camelCase, snake_case, flatcase"
                ),
            })?;
        }

        for (key, entry) in raw.properties.iter().filter(|(key, _)| *key != "all") {
            match entry {
                PropertyEntry::Alias(alias) => {
                    if !roblox::is_member_name(key) {
                        return Err(ConfigError {
                            message: format!(
                                "luaux.toml: [properties] {key} is not a property or event of any \
                                 Roblox class{}",
                                suggest_member_anywhere(key)
                            ),
                        });
                    }
                    config.global_properties.insert(key.clone(), alias.clone());
                }
                PropertyEntry::PerClass(entries) => {
                    if !roblox::is_class(key) {
                        return Err(ConfigError {
                            message: format!(
                                "luaux.toml: [properties.{key}] is not a creatable Roblox class{}",
                                suggest_class(key)
                            ),
                        });
                    }

                    for property in entries.keys() {
                        if !roblox::has_property(key, property) && !roblox::is_event(key, property)
                        {
                            return Err(ConfigError {
                                message: format!(
                                    "luaux.toml: [properties.{key}] {key} has no property or event \
                                     named {property}{}",
                                    suggest_member(key, property)
                                ),
                            });
                        }
                    }

                    config.class_properties.insert(key.clone(), entries.clone());
                }
            }
        }

        Ok((config, warnings))
    }

    /// Maps a written tag to a canonical class name.
    ///
    /// `Ok(None)` means the tag is not an alias and should be resolved normally.
    pub fn resolve_element(&self, written: &str) -> Result<Option<&str>, String> {
        if let Some(class) = self.element_alias.get(written) {
            return Ok(Some(class));
        }

        // Writing the original name of something that was renamed.
        if let Some(alias) = self.element_renamed.get(written) {
            if alias != written {
                return Err(format!(
                    "<{written}> was renamed by luaux.toml; use <{alias}>"
                ));
            }
        }

        if self.element_casing != Casing::Pascal {
            // An explicit entry already had its chance above, so `all` only
            // ever answers for names it did not claim.
            let mut matches: Vec<&str> = roblox::creatable_classes()
                .filter(|class| !self.element_renamed.contains_key(*class))
                .filter(|class| self.element_casing.apply(class) == written)
                .collect();

            if let Some(class) = preferred(&mut matches) {
                return Ok(Some(class));
            }

            // Under a blanket rename the canonical spelling retires, exactly as
            // a single override retires the name it replaces.
            if roblox::is_class(written) {
                return Err(format!(
                    "<{written}> was renamed by [elements] all; use <{}>",
                    self.element_casing.apply(written)
                ));
            }
        }

        Ok(None)
    }

    /// How `class` is spelled in this project — its alias, if one was configured.
    ///
    /// The inverse of [`Config::resolve_element`], and the question tooling asks:
    /// offering `<TextLabel>` in a project that renamed it to `text` would be
    /// offering the one spelling that is an error, since overrides are exclusive.
    pub fn element_name<'a>(&'a self, class: &'a str) -> &'a str {
        self.element_renamed
            .get(class)
            .map_or(class, String::as_str)
    }

    /// How a canonical property or event is spelled on `class`.
    ///
    /// Per-class entries beat the global table, matching
    /// [`Config::resolve_property`] — including the identity override a class
    /// uses to opt back out of a global rename.
    pub fn property_name<'a>(&'a self, class: &str, canonical: &'a str) -> &'a str {
        if let Some(alias) = self
            .class_properties
            .get(class)
            .and_then(|entries| entries.get(canonical))
        {
            return alias;
        }

        self.global_properties
            .get(canonical)
            .map_or(canonical, String::as_str)
    }

    /// Maps a written attribute on `class` to its canonical property name.
    pub fn resolve_property(&self, class: &str, written: &str) -> Result<String, String> {
        // Per-class entries beat the global table, so a class can opt back out
        // of a global rename by mapping a name to itself.
        let effective: HashMap<&str, &str> = self
            .global_properties
            .iter()
            .map(|(canonical, alias)| (canonical.as_str(), alias.as_str()))
            .chain(
                self.class_properties
                    .get(class)
                    .into_iter()
                    .flatten()
                    .map(|(canonical, alias)| (canonical.as_str(), alias.as_str())),
            )
            .collect();

        for (canonical, alias) in &effective {
            if *alias == written {
                return Ok((*canonical).to_string());
            }
        }

        if let Some(alias) = effective.get(written) {
            if *alias != written {
                return Err(format!("{written} was renamed by luaux.toml; use {alias}"));
            }
        }

        if self.property_casing != Casing::Pascal {
            let mut matches: Vec<&str> = roblox::properties(class)
                .chain(roblox::events(class))
                .filter(|member| !effective.contains_key(*member))
                .filter(|member| self.property_casing.apply(member) == written)
                .collect();

            if let Some(member) = preferred(&mut matches) {
                return Ok(member.to_string());
            }

            if roblox::has_property(class, written) || roblox::is_event(class, written) {
                return Err(format!(
                    "{written} was renamed by [properties] all; use {}",
                    self.property_casing.apply(written)
                ));
            }
        }

        Ok(written.to_string())
    }
}

fn suggest_class(name: &str) -> String {
    match roblox::closest_class(name) {
        Some(class) => format!("; did you mean {class}?"),
        None => String::new(),
    }
}

fn suggest_member(class: &str, name: &str) -> String {
    match roblox::closest_members(class, name).as_slice() {
        [] => String::new(),
        [one] => format!("; did you mean {one}?"),
        [rest @ .., last] => format!("; did you mean {} or {last}?", rest.join(", ")),
    }
}

fn suggest_member_anywhere(name: &str) -> String {
    match roblox::closest_member_anywhere(name) {
        Some(member) => format!("; did you mean {member}?"),
        None => String::new(),
    }
}

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

    fn parse(text: &str) -> Config {
        Config::parse(text).expect("parse")
    }

    fn parse_err(text: &str) -> String {
        Config::parse(text).expect_err("should fail").message
    }

    #[test]
    fn lints_default_to_warn_and_are_configurable() {
        assert_eq!(parse("").static_conditional_child, LintLevel::Warn);
        assert_eq!(
            parse("[lints]\nstatic_conditional_child = \"off\"\n").static_conditional_child,
            LintLevel::Off
        );
        assert_eq!(
            parse("[lints]\nstatic_conditional_child = \"error\"\n").static_conditional_child,
            LintLevel::Error
        );
    }

    #[test]
    fn rejects_an_unknown_lint_level() {
        let error = parse_err("[lints]\nstatic_conditional_child = \"loud\"\n");
        assert!(error.contains("not one of off, warn, error"), "{error}");
    }

    #[test]
    fn the_element_factory_defaults_to_bare_create() {
        assert_eq!(parse("").create, DEFAULT_CREATE);
        assert_eq!(
            parse("[factory]\ncreate = \"vide.create\"\n").create,
            "vide.create"
        );
    }

    #[test]
    fn rejects_an_empty_factory() {
        assert!(parse_err("[factory]\ncreate = \"\"\n").contains("cannot be empty"));
    }

    #[test]
    fn reads_build_paths_and_selection() {
        let config = parse(
            "[build]\nin = \"src\"\nout = \"build\"\ninclude = [\"**\"]\nexclude = [\"**/*.spec.luaux\"]\nclean = true\n",
        );

        assert_eq!(config.build.input.unwrap().to_str(), Some("src"));
        assert_eq!(config.build.output.unwrap().to_str(), Some("build"));
        assert_eq!(config.build.exclude, ["**/*.spec.luaux"]);
        assert!(config.build.clean);
    }

    #[test]
    fn build_defaults_are_inert() {
        let config = parse("");
        assert!(config.build.input.is_none());
        assert!(config.build.output.is_none());
        assert!(!config.build.clean);
        // Everything is considered unless narrowed.
        assert_eq!(config.build.include, ["**"]);
    }

    #[test]
    fn casing_splits_on_canonical_word_boundaries() {
        // Boundaries come from the PascalCase spelling, so an acronym stays
        // whole: UICorner is UI + Corner, never U + I + Corner.
        for (name, snake, camel, flat) in [
            ("TextLabel", "text_label", "textLabel", "textlabel"),
            ("UICorner", "ui_corner", "uiCorner", "uicorner"),
            (
                "UIAspectRatioConstraint",
                "ui_aspect_ratio_constraint",
                "uiAspectRatioConstraint",
                "uiaspectratioconstraint",
            ),
            (
                "BackgroundColor3",
                "background_color3",
                "backgroundColor3",
                "backgroundcolor3",
            ),
            ("Frame", "frame", "frame", "frame"),
        ] {
            assert_eq!(Casing::Snake.apply(name), snake, "{name}");
            assert_eq!(Casing::Camel.apply(name), camel, "{name}");
            assert_eq!(Casing::Flat.apply(name), flat, "{name}");
            assert_eq!(Casing::Pascal.apply(name), name, "{name}");
        }
    }

    #[test]
    fn casing_is_injective_over_every_class() {
        // If two classes collapsed onto one spelling, `all` would be ambiguous
        // for elements. Measured at zero; this guards against an API dump that
        // introduces one.
        for casing in [Casing::Snake, Casing::Camel, Casing::Flat] {
            let mut seen: HashMap<String, &str> = HashMap::new();
            for class in roblox::creatable_classes() {
                if let Some(other) = seen.insert(casing.apply(class), class) {
                    panic!("{casing:?}: {other} and {class} collide");
                }
            }
        }
    }

    #[test]
    fn an_explicit_entry_beats_the_blanket_scheme() {
        let config = parse("[elements]\nall = \"camelCase\"\nTextLabel = \"text\"\n");

        // The override wins, and takes its class out of the scheme entirely.
        assert_eq!(config.resolve_element("text").unwrap(), Some("TextLabel"));
        assert!(config.resolve_element("textLabel").unwrap().is_none());

        // Everything it did not claim still follows `all`.
        assert_eq!(config.resolve_element("frame").unwrap(), Some("Frame"));
        assert_eq!(
            config.resolve_element("uiCorner").unwrap(),
            Some("UICorner")
        );
    }

    #[test]
    fn a_blanket_scheme_retires_the_canonical_spelling() {
        let config = parse("[elements]\nall = \"snake_case\"\n");
        let error = config.resolve_element("Frame").expect_err("retired");
        assert!(error.contains("use <frame>"), "{error}");
    }

    #[test]
    fn a_collision_prefers_the_name_roblox_has_not_deprecated() {
        // ChildAdded and childAdded are both on Instance, so this pair is
        // inherited by every class (docs/adr/0001-casing-key.md).
        let config = parse("[properties]\nall = \"snake_case\"\n");
        assert_eq!(
            config.resolve_property("Frame", "child_added").unwrap(),
            "ChildAdded"
        );

        let camel = parse("[properties]\nall = \"camelCase\"\n");
        assert_eq!(
            camel.resolve_property("Part", "brickColor").unwrap(),
            "BrickColor"
        );
    }

    #[test]
    fn a_collision_with_no_undeprecated_name_is_still_deterministic() {
        // FormFactor and formFactor are both deprecated, so the tiebreak is byte
        // order, which puts the uppercase spelling first.
        let config = parse("[properties]\nall = \"camelCase\"\n");
        let resolved = config.resolve_property("Part", "formFactor");
        assert_eq!(resolved.unwrap(), "FormFactor");
    }

    #[test]
    fn properties_follow_their_own_scheme_and_overrides() {
        let config = parse("[properties]\nall = \"snake_case\"\nBackgroundColor3 = \"bg\"\n");
        assert_eq!(
            config
                .resolve_property("Frame", "background_transparency")
                .unwrap(),
            "BackgroundTransparency"
        );
        assert_eq!(
            config.resolve_property("Frame", "bg").unwrap(),
            "BackgroundColor3"
        );
        // The override removed it from the scheme.
        assert_eq!(
            config
                .resolve_property("Frame", "background_color3")
                .unwrap(),
            "background_color3"
        );
    }

    #[test]
    fn rejects_an_unknown_casing() {
        assert!(parse_err("[elements]\nall = \"kebab-case\"\n").contains("PascalCase"));
        assert!(parse_err("[properties]\nall = \"KEBAB\"\n").contains("snake_case"));
    }

    #[test]
    fn a_genuinely_unknown_section_still_errors() {
        // So a typo is caught rather than silently ignored.
        assert!(parse_err("[buld]\nclean = true\n").contains("unknown field"));
    }

    #[test]
    fn an_absent_config_is_not_an_error() {
        let config = Config::load(Path::new("/nonexistent-directory-for-luaux"));
        assert!(config.is_ok());
    }

    #[test]
    fn resolves_element_aliases() {
        let config = parse("[elements]\nTextLabel = \"text\"\n");
        assert_eq!(config.resolve_element("text"), Ok(Some("TextLabel")));
        // Unaliased names pass through untouched.
        assert_eq!(config.resolve_element("Frame"), Ok(None));
    }

    #[test]
    fn an_override_retires_the_original_name() {
        let config = parse("[elements]\nTextLabel = \"text\"\n");
        let error = config.resolve_element("TextLabel").expect_err("retired");
        assert!(error.contains("use <text>"), "{error}");
    }

    #[test]
    fn rejects_duplicate_element_aliases() {
        let error = parse_err("[elements]\nTextLabel = \"text\"\nTextButton = \"text\"\n");
        assert!(error.contains("both claim the alias"), "{error}");
    }

    #[test]
    fn rejects_unknown_element_keys() {
        let error = parse_err("[elements]\nFrmae = \"frame\"\n");
        assert!(error.contains("not a creatable Roblox class"), "{error}");
        assert!(error.contains("did you mean Frame?"), "{error}");
    }

    #[test]
    fn resolves_global_property_aliases() {
        let config = parse("[properties]\nTextColor3 = \"textColor\"\n");
        assert_eq!(
            config.resolve_property("TextLabel", "textColor"),
            Ok("TextColor3".into())
        );
        // And retires the original spelling.
        assert!(config.resolve_property("TextLabel", "TextColor3").is_err());
    }

    #[test]
    fn per_class_entries_beat_the_global_table() {
        let config = parse(
            "[properties]\nBackgroundColor3 = \"bg\"\n\n[properties.Frame]\nBackgroundColor3 = \"bgColor\"\n",
        );
        assert_eq!(
            config.resolve_property("Frame", "bgColor"),
            Ok("BackgroundColor3".into())
        );
        // The global alias no longer applies to Frame...
        assert_eq!(config.resolve_property("Frame", "bg"), Ok("bg".into()));
        // ...but still applies elsewhere.
        assert_eq!(
            config.resolve_property("TextLabel", "bg"),
            Ok("BackgroundColor3".into())
        );
    }

    #[test]
    fn a_class_can_opt_out_of_a_global_rename() {
        // PROPOSAL.md's identity-override trick.
        let config = parse(
            "[properties]\nTextColor3 = \"textColor\"\n\n[properties.TextLabel]\nTextColor3 = \"TextColor3\"\n",
        );
        assert_eq!(
            config.resolve_property("TextLabel", "TextColor3"),
            Ok("TextColor3".into())
        );
        assert_eq!(
            config.resolve_property("TextButton", "textColor"),
            Ok("TextColor3".into())
        );
    }

    /// Every spelling offered must be one that resolves, or a project's
    /// completions would suggest the errors its own config created.
    #[test]
    fn the_offered_spelling_is_the_one_that_resolves() {
        let config = parse(
            "[elements]\nTextLabel = \"text\"\n\n[properties]\nTextColor3 = \"textColor\"\n\n\
             [properties.Frame]\nBackgroundColor3 = \"bgColor\"\n",
        );

        assert_eq!(config.element_name("TextLabel"), "text");
        assert_eq!(config.element_name("Frame"), "Frame");
        assert_eq!(
            config.resolve_element(config.element_name("TextLabel")),
            Ok(Some("TextLabel"))
        );

        assert_eq!(config.property_name("TextLabel", "TextColor3"), "textColor");
        assert_eq!(config.property_name("Frame", "BackgroundColor3"), "bgColor");
        // Untouched names are spelled as themselves.
        assert_eq!(config.property_name("Frame", "Name"), "Name");

        for (class, canonical) in [("TextLabel", "TextColor3"), ("Frame", "BackgroundColor3")] {
            assert_eq!(
                config.resolve_property(class, config.property_name(class, canonical)),
                Ok(canonical.to_string()),
                "{class}.{canonical}"
            );
        }
    }

    #[test]
    fn an_identity_override_is_offered_as_the_original_name() {
        let config = parse(
            "[properties]\nTextColor3 = \"textColor\"\n\n[properties.TextLabel]\nTextColor3 = \"TextColor3\"\n",
        );

        assert_eq!(
            config.property_name("TextLabel", "TextColor3"),
            "TextColor3"
        );
        assert_eq!(
            config.property_name("TextButton", "TextColor3"),
            "textColor"
        );
    }

    #[test]
    fn rejects_unknown_property_keys() {
        let error = parse_err("[properties]\nTextColour3 = \"textColor\"\n");
        assert!(error.contains("not a property or event of any"), "{error}");
        assert!(error.contains("TextColor3"), "{error}");
    }

    #[test]
    fn rejects_per_class_keys_the_class_does_not_have() {
        let error = parse_err("[properties.Frame]\nText = \"label\"\n");
        assert!(
            error.contains("has no property or event named Text"),
            "{error}"
        );
    }

    #[test]
    fn rejects_unknown_per_class_tables() {
        let error = parse_err("[properties.Frmae]\nName = \"id\"\n");
        assert!(error.contains("not a creatable Roblox class"), "{error}");
    }
}