luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
use crate::pretty_printer::with_parse;
use luau_common::ByteSlice;
use luau_syntax::parser::ParseOptions;
use luau_syntax::pretty_printer::{
    PrintableAstNodeRef, pretty_print_block, pretty_print_with_types, to_string,
};

// PrettyPrinter.test.cpp: prettyPrint_to_string
#[test]
fn pretty_print_to_string() {
    with_parse(
        "local a: string = 'hello'",
        ParseOptions::default(),
        |result| {
            let [statement] = result.root.as_slice() else {
                panic!("expected local statement");
            };
            let local = statement.as_local().expect("expected local statement");

            assert_eq!(
                to_string(PrintableAstNodeRef::Statement(result.root[0])).as_bytes(),
                b"local a: string = 'hello'"
            );
            assert_eq!(
                to_string(PrintableAstNodeRef::Type(
                    local.bindings[0]
                        .annotation
                        .expect("expected type annotation"),
                ))
                .as_bytes(),
                b"string"
            );
            assert_eq!(
                to_string(PrintableAstNodeRef::Expression(local.values[0])).as_bytes(),
                b"'hello'"
            );
        },
    );
}

// PrettyPrinter.test.cpp: prettyPrint_AstStatBlock_overload
#[test]
fn pretty_print_block_overload() {
    with_parse("local a = 1", ParseOptions::default(), |result| {
        assert_eq!(pretty_print_block(result.root).as_bytes(), b"local a = 1");
    });
}

// PrettyPrinter.test.cpp: prettyPrint_declare_global_stat
#[test]
fn pretty_print_declare_global_stat() {
    let code = "declare _G: any";

    with_parse(
        code,
        ParseOptions::default().with_declaration_syntax(true),
        |result| {
            assert_eq!(
                pretty_print_with_types(result.root).as_bytes(),
                code.as_bytes()
            );
        },
    );
}