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
//! Vide backend (PLAN.md §6).
//!
//! Vide's `create` takes one flat table where string keys are properties and
//! events and numeric keys are children, so LuauX maps onto it almost 1:1. Vide
//! handles parenting, fragment recursion, event connection, ordering, and
//! reactive updates — leaving the compiler only the compile-time work.
//!
//! Output is line-preserving: each entry lands on the source line its attribute
//! or child was written on, and the whole emission spans exactly the lines the
//! LuauX did (PLAN.md §5.5).

use super::writer::Writer;
use super::{Backend, EmitContext, EmitError};
use crate::markup::*;
use crate::resolve::Resolution;
use crate::roblox;

// The element factory is configurable (`[factory] create`) and reaches the backend
// through EmitContext, so there is no constant for it here.

/// Merge helper for spread attributes. Inlined into the output rather than
/// required, so luaux has no runtime dependency (see `crate::imports`).
const MERGE_PROPS: &str = crate::imports::MERGE_HELPER;

/// Reads a possibly-source value. Inlined rather than taken from Vide, so
/// interpolated text costs no dependency (see `crate::imports`).
const READ: &str = crate::imports::READ_HELPER;

pub struct Vide;

impl Backend for Vide {
    fn name(&self) -> &'static str {
        "vide"
    }

    fn emit(&self, node: &Node, context: &EmitContext<'_>) -> Result<String, EmitError> {
        let mut writer = Writer::new(context, node.span().start);
        emit_node(node, context, &mut writer)?;
        Ok(writer.finish())
    }
}

/// One entry in an emitted table.
enum Entry<'a> {
    /// `Name = value`
    Pair { offset: usize, text: String },
    /// A nested element or fragment.
    Node { offset: usize, node: &'a Node },
    /// An expression child, emitted verbatim.
    Expression { offset: usize, expression: &'a str },
    /// A retained comment, already Luau. Carries no value, so it needs its own
    /// separator handling — a comment cannot sit between two commas.
    Comment { offset: usize, luau: &'a str },
}

impl Entry<'_> {
    fn offset(&self) -> usize {
        match self {
            Entry::Pair { offset, .. }
            | Entry::Node { offset, .. }
            | Entry::Expression { offset, .. }
            | Entry::Comment { offset, .. } => *offset,
        }
    }
}

fn emit_node(
    node: &Node,
    context: &EmitContext<'_>,
    writer: &mut Writer<'_>,
) -> Result<(), EmitError> {
    match node {
        Node::Element(element) => emit_element(element, context, writer),
        Node::Fragment(fragment) => {
            // A fragment is a plain table; Vide recurses tables in numeric
            // position, so it needs no runtime representation of its own.
            let entries = child_entries(&fragment.children, false);
            emit_table(&entries, fragment.span, context, writer)
        }
    }
}

fn emit_element(
    element: &Element,
    context: &EmitContext<'_>,
    writer: &mut Writer<'_>,
) -> Result<(), EmitError> {
    // Resolution rejects a name that is neither a Roblox class nor bound in the
    // file, so a typo like `<Frmae/>` fails here with a did-you-mean instead of
    // compiling into a call to an undefined global (PLAN.md §3.1).
    // An unresolved name is emitted as written and its attributes are left
    // alone: with no class there is nothing to check them against, and checking
    // anyway would report one error per attribute, all of them caused by the
    // single mistake already reported on the tag.
    let (intrinsic, resolved) = match context.resolve(&element.name, element.span.start) {
        Resolution::Intrinsic(class) => (Some(class), true),
        Resolution::Component => (None, true),
        Resolution::Unresolved(written) => (Some(written), false),
    };

    // Nothing else about an unresolved element is worth checking. Its text
    // children and its attributes are judged against a class that does not
    // exist, so every complaint would be downstream of the one already recorded
    // on the tag itself.
    let plan = match resolved {
        true => plan_text(element, intrinsic.as_deref(), context)?,
        false => TextPlan::default(),
    };

    match &intrinsic {
        Some(class) => {
            context.used_create();
            writer.push(&format!("{}(\"{class}\")(", context.create()));
        }
        None => writer.push(&format!("{}(", element.name.as_written())),
    }

    // Attributes split into groups at each spread: a run of named attributes
    // becomes one table, each spread contributes its own argument, and
    // `mergeProps` joins them in source order.
    let mut groups: Vec<Vec<Entry>> = vec![Vec::new()];
    let mut spreads: Vec<(usize, &str)> = Vec::new();
    let mut order: Vec<Group> = Vec::new();

    for attribute in &element.attributes {
        match attribute {
            Attribute::Spread { expression, span } => {
                if !groups.last().expect("a group").is_empty() {
                    order.push(Group::Table(groups.len() - 1));
                    groups.push(Vec::new());
                }
                order.push(Group::Spread(spreads.len()));
                spreads.push((span.start, expression));
            }
            Attribute::Named { name, value, span } => {
                // Rule 5: text between the tags overrides a `Text` attribute.
                if plan.text.is_some() && name == "Text" {
                    continue;
                }

                // Aliases resolve to canonical Roblox names here, so emitted
                // code is the same regardless of a project's luaux.toml.
                let key = match (&intrinsic, resolved) {
                    (Some(class), true) => context.resolve_attribute(class, name, span.start),
                    _ => name.clone(),
                };

                groups.last_mut().expect("a group").push(Entry::Pair {
                    offset: span.start,
                    text: format!("{key} = {}", attribute_value(value)),
                });
            }
        }
    }

    let last = groups.len() - 1;
    if let Some(text) = &plan.text {
        groups[last].push(Entry::Pair {
            offset: plan.offset,
            text: format!("Text = {text}"),
        });
    }
    groups[last].extend(child_entries(&element.children, plan.consumed_expressions));

    if !groups[last].is_empty() || order.is_empty() {
        order.push(Group::Table(last));
    }

    let uses_merge = !spreads.is_empty();
    if uses_merge {
        context.used_merge_props();
        writer.push(&format!("{MERGE_PROPS}("));
    }

    for (index, group) in order.iter().enumerate() {
        if index > 0 {
            writer.push(",");
            match group {
                Group::Spread(spread) => writer.break_or_space(spreads[*spread].0),
                Group::Table(table) => {
                    let offset = groups[*table]
                        .first()
                        .map(Entry::offset)
                        .unwrap_or(element.span.start);
                    writer.break_or_space(offset);
                }
            }
        } else if let Group::Spread(spread) = group {
            // A *leading* spread still belongs on the line it was written on. A
            // table positions each of its own entries, so it needs nothing here
            // — but a spread is pushed verbatim, and without this it lands on
            // the opening tag's line while the line it was written on comes out
            // blank. Hovering it then asks about a line with nothing on it.
            //
            // `to` is a no-op when the spread is already on this line, so a
            // single-line element is untouched.
            writer.to(spreads[*spread].0);
        }

        match group {
            Group::Spread(spread) => writer.push(spreads[*spread].1),
            Group::Table(table) => emit_table(&groups[*table], element.span, context, writer)?,
        }
    }

    // The closing parenthesis sits on the line of the closing tag, so the
    // element spans exactly the lines the LuauX did.
    //
    // `emit_table` already does this for the table it emits, which covers most
    // elements — but an element whose attributes are *all* spreads emits no
    // table at all (the trailing group is empty and is dropped above), and then
    // nothing else would. `<Component {props} />` written across lines is the
    // ordinary way to forward props, and it was losing every line it spanned.
    writer.to(element.span.end.saturating_sub(1));

    if uses_merge {
        writer.push(")");
    }

    writer.push(")");
    Ok(())
}

enum Group {
    Table(usize),
    Spread(usize),
}

fn emit_table(
    entries: &[Entry<'_>],
    span: Span,
    context: &EmitContext<'_>,
    writer: &mut Writer<'_>,
) -> Result<(), EmitError> {
    // An empty table still has to span the lines the element did. An opening and
    // closing tag on separate lines with nothing between them is an ordinary
    // shape — a container waiting for its children — and emitting a bare `{}`
    // where the opening tag stood silently shortens the file.
    //
    // Nothing about the output looks wrong afterwards, which is what makes it
    // expensive. Every line below moves up, so no run of text lines up with the
    // source, and a language server mapping luau-lsp's answers back onto the
    // `.luaux` drops all of them: the markup keeps working while the Luau half
    // of the file goes silent.
    if entries.is_empty() {
        let close = span.end.saturating_sub(1);

        if writer.will_break(close) {
            writer.push("{");
            writer.to(close);
            writer.push("}");
        } else {
            writer.push("{}");
        }

        return Ok(());
    }

    writer.push("{");

    // A comment is not a table field, so it never takes a comma — and the comma
    // has to be written *immediately after its value*, before any comment that
    // follows. Deferring it until the next entry puts it after the comment,
    // which is valid Lua but reads as though the comment owned it, and no
    // formatter will move it back.
    let last_value = entries
        .iter()
        .rposition(|entry| !matches!(entry, Entry::Comment { .. }));

    for (index, entry) in entries.iter().enumerate() {
        if let Entry::Comment { offset, luau } = entry {
            writer.break_or_space(*offset);
            writer.push(luau);
            continue;
        }

        writer.break_or_space(entry.offset());

        match entry {
            Entry::Comment { .. } => unreachable!("handled above"),
            Entry::Pair { text, .. } => writer.push(text),
            Entry::Node { node, .. } => emit_node(node, context, writer)?,
            // Emitted bare. An earlier design wrapped these in a one-element
            // table to stop a nil leaving a hole in the array part — but Vide
            // iterates children with generalised `for k, v in t`, which skips
            // absent keys rather than stopping at them. `ipairs` would truncate;
            // Vide does not use it. Verified by tests/runtime.
            Entry::Expression { expression, .. } => writer.push(expression),
        }

        if Some(index) != last_value {
            writer.push(",");
        }
    }

    // The closing brace sits on the line of the closing tag, so the emission
    // spans exactly the lines the LuauX did.
    let close = span.end.saturating_sub(1);

    if writer.will_break(close) {
        // Trailing comma goes before the break, per Luau style — but only if a
        // value was written, since a comment cannot carry one.
        if last_value.is_some() {
            writer.push(",");
        }
        writer.to(close);
    } else {
        writer.push(" ");
    }

    writer.push("}");
    Ok(())
}

fn attribute_value(value: &AttributeValue) -> String {
    match value {
        AttributeValue::Expression(expression) => expression.clone(),
        AttributeValue::StringLiteral(literal) => literal.clone(),
        AttributeValue::Boolean => "true".to_string(),
    }
}

fn child_entries<'a>(children: &'a [Child], expressions_are_text: bool) -> Vec<Entry<'a>> {
    let mut entries = Vec::new();

    for child in children {
        match child {
            Child::Node(node) => entries.push(Entry::Node {
                offset: node.span().start,
                node,
            }),
            // Text is always folded into the `Text` property by plan_text.
            Child::Text { .. } => {}
            Child::Comment { luau, span } => entries.push(Entry::Comment {
                offset: span.start,
                luau,
            }),
            Child::Expression { .. } if expressions_are_text => {}
            Child::Expression { expression, span } => entries.push(Entry::Expression {
                offset: span.start,
                expression,
            }),
        }
    }

    entries
}

/// How an element's children divide between the `Text` property and Vide's
/// numeric child slots.
#[derive(Default)]
struct TextPlan {
    /// Encoded Luau value for the `Text` property, if any.
    text: Option<String>,
    /// Whether expression children were folded into `text` rather than left as
    /// children.
    consumed_expressions: bool,
    /// Source offset of the first text part, so `Text = …` lands on its line.
    offset: usize,
}

/// Text and expression children that lower to the `Text` property (PLAN.md §6.2).
///
/// Vide's numeric slots take Instances, tables, and functions — not strings — so
/// bare text has to become a property at compile time.
fn plan_text(
    element: &Element,
    intrinsic: Option<&str>,
    context: &EmitContext<'_>,
) -> Result<TextPlan, EmitError> {
    let has_text_literal = element
        .children
        .iter()
        .any(|child| matches!(child, Child::Text { .. }));
    let has_nodes = element
        .children
        .iter()
        .any(|child| matches!(child, Child::Node(_)));
    let has_expressions = element
        .children
        .iter()
        .any(|child| matches!(child, Child::Expression { .. }));

    if !has_text_literal && !has_expressions {
        return Ok(TextPlan::default());
    }

    let Some(class) = intrinsic else {
        // Components take children, not text. Nothing here knows their props.
        if has_text_literal {
            return Err(EmitError::new(
                format!(
                    "<{}> is a component, so it cannot take bare text",
                    element.name.as_written()
                ),
                element.span.start,
                element.name.as_written().len() + 1,
            )
            .with_help("pass the text as a prop instead"));
        }
        return Ok(TextPlan::default());
    };

    if !roblox::has_text_property(class) {
        if has_text_literal {
            return Err(EmitError::new(
                format!("<{class}> has no Text property"),
                element.span.start,
                class.len() + 1,
            )
            .with_help("wrap the text in a <TextLabel>"));
        }
        // Expressions are ordinary children on a class with no text.
        return Ok(TextPlan::default());
    }

    // `<TextButton>{label}<UICorner/></TextButton>` is genuinely ambiguous: the
    // expression could be the button's text or another child, and nothing here
    // can tell. Emitting a guess produces code that fails inside Vide at
    // runtime, so refuse and ask for the explicit form.
    if has_expressions && has_nodes {
        return Err(EmitError::new(
            format!(
                "<{class}> has both an expression child and element children, so it is unclear \
                 whether the expression is text or a child"
            ),
            element.span.start,
            class.len() + 1,
        )
        .with_help("write it as Text={...} instead"));
    }

    let mut parts = Vec::new();
    let mut offset = element.span.start;

    for (index, child) in element.children.iter().enumerate() {
        match child {
            Child::Text { text, span } => {
                if index == 0 || parts.is_empty() {
                    offset = span.start;
                }
                parts.push(TextPart::Literal(text.clone()));
            }
            Child::Expression { expression, span } => {
                if parts.is_empty() {
                    offset = span.start;
                }
                parts.push(TextPart::Expression(expression.clone()));
            }
            Child::Node(_) | Child::Comment { .. } => {}
        }
    }

    let text = encode_text(&parts);
    if text.contains(&format!("{READ}(")) {
        context.used_read();
    }

    Ok(TextPlan {
        text: Some(text),
        consumed_expressions: has_expressions,
        offset,
    })
}

enum TextPart {
    Literal(String),
    Expression(String),
}

/// Encodes text parts as the value of the `Text` property.
///
/// Three shapes, and the choice is what makes `<TextLabel>Clicked {count}
/// times</TextLabel>` reactive:
///
/// * **Literals only** — a plain quoted string. Nothing to track.
/// * **A single expression** — emitted bare. Vide already treats a function
///   value on a property key as a source and creates an effect, so `Text = label`
///   is correct whether `label` is a source or a plain string.
/// * **Mixed** — a thunk: `function() return `…{read(x)}…` end`. Interpolation
///   builds a string, which would stringify a source to `function: 0x…`, so each
///   expression is read and the whole thing is wrapped so Vide re-runs it when
///   any source inside changes.
fn encode_text(parts: &[TextPart]) -> String {
    let expressions = parts
        .iter()
        .filter(|part| matches!(part, TextPart::Expression(_)))
        .count();

    if expressions == 0 {
        return encode_plain(parts);
    }

    if let [TextPart::Expression(expression)] = parts {
        return expression.clone();
    }

    format!("function() return {} end", encode_interpolated(parts))
}

/// Double quotes, matching Luau convention and stylua's default. Attribute
/// literals are captured verbatim and keep whatever the author wrote; this
/// governs only strings luaux itself builds, which is text children.
fn encode_plain(parts: &[TextPart]) -> String {
    let mut out = String::from("\"");

    for part in parts {
        if let TextPart::Literal(text) = part {
            for character in text.chars() {
                match character {
                    '\\' => out.push_str("\\\\"),
                    '"' => out.push_str("\\\""),
                    '\n' => out.push_str("\\n"),
                    '\r' => out.push_str("\\r"),
                    _ => out.push(character),
                }
            }
        }
    }

    out.push('"');
    out
}

fn encode_interpolated(parts: &[TextPart]) -> String {
    let mut out = String::from("`");

    for part in parts {
        match part {
            TextPart::Literal(text) => {
                for character in text.chars() {
                    match character {
                        '\\' => out.push_str("\\\\"),
                        '`' => out.push_str("\\`"),
                        '{' => out.push_str("\\{"),
                        '}' => out.push_str("\\}"),
                        '\n' => out.push_str("\\n"),
                        '\r' => out.push_str("\\r"),
                        _ => out.push(character),
                    }
                }
            }
            TextPart::Expression(expression) => {
                out.push_str(&format!("{{{READ}({expression})}}"));
            }
        }
    }

    out.push('`');
    out
}