luau-syntax 0.732.0

Luau lexer, parser, AST, CST, and source utilities
Documentation
use crate::pretty_printer::{
    assert_pretty_prints, assert_round_trips, assert_round_trips_with_types,
};
use luau_common::flags;

// PrettyPrinter.test.cpp: index_name_spaces_around_tokens
#[test]
fn index_name_spaces_around_tokens() {
    assert_round_trips_with_types("local _ = a.name");
    assert_round_trips_with_types("local _ = a   .name");
    assert_round_trips_with_types("local _ = a.   name");
}

// PrettyPrinter.test.cpp: index_name_ends_with_digit
#[test]
fn index_name_ends_with_digit() {
    assert_round_trips_with_types("sparkles.Color = Color3.new()");
}

// PrettyPrinter.test.cpp: prettyPrint_index_expr
#[test]
fn pretty_print_index_expr() {
    assert_round_trips_with_types("local a = {1, 2, 3} local b = a[2]");
}

// PrettyPrinter.test.cpp: index_expr_spaces_around_tokens
#[test]
fn index_expr_spaces_around_tokens() {
    assert_round_trips_with_types("local _ = a[2]");
    assert_round_trips_with_types("local _ = a   [2]");
    assert_round_trips_with_types("local _ = a[   2]");
    assert_round_trips_with_types("local _ = a[2   ]");
}

// PrettyPrinter.test.cpp: prettyPrint_unary
#[test]
fn pretty_print_unary() {
    assert_round_trips_with_types(
        r#"
local a = 1
local b = -1
local c = true
local d = not c
local e = 'hello'
local d = #e
    "#,
    );
}

// PrettyPrinter.test.cpp: unary_spaces_around_tokens
#[test]
fn unary_spaces_around_tokens() {
    assert_round_trips_with_types(
        r#"
local _ =   -1
local _ = -  1
local _ =   not true
local _ = not   true
local _ =   #e
local _ = #  e
    "#,
    );
}

// PrettyPrinter.test.cpp: binary_spaces_around_tokens
#[test]
fn binary_spaces_around_tokens() {
    assert_round_trips_with_types(
        r#"
local _ =    1+1
local _ = 1   +1
local _ = 1+   1
    "#,
    );
}

// PrettyPrinter.test.cpp: prettyPrint_break_continue
#[test]
fn pretty_print_break_continue() {
    assert_round_trips_with_types(
        r#"
local a, b, c
repeat
    if a then break end
    if b then continue end
until c
    "#,
    );
}

// PrettyPrinter.test.cpp: prettyPrint_compound_assignment
#[test]
fn pretty_print_compound_assignment() {
    assert_round_trips_with_types(
        r#"
local a = 1
a += 2
a -= 3
a *= 4
a /= 5
a //= 5
a %= 6
a ^= 7
a ..= ' - result'
    "#,
    );
}

// PrettyPrinter.test.cpp: compound_assignment_spaces_around_tokens
#[test]
fn compound_assignment_spaces_around_tokens() {
    assert_round_trips_with_types(r" a   += 1 ");
    assert_round_trips_with_types(r" a +=   1 ");
}

// PrettyPrinter.test.cpp: prettyPrint_assign_multiple
#[test]
fn pretty_print_assign_multiple() {
    assert_round_trips_with_types("a, b, c = 1, 2, 3");
}

// PrettyPrinter.test.cpp: prettyPrint_assign_spaces_around_tokens
#[test]
fn pretty_print_assign_spaces_around_tokens() {
    assert_round_trips("a = 1");
    assert_round_trips("a    = 1");
    assert_round_trips("a =    1");
    assert_round_trips("a   , b = 1, 2");
    assert_round_trips("a,    b = 1, 2");
    assert_round_trips("a, b = 1   , 2");
    assert_round_trips("a, b = 1,    2");
}

// PrettyPrinter.test.cpp: prettyPrint_generic_function
#[test]
fn pretty_print_generic_function() {
    assert_round_trips_with_types(
        r#"
local function foo<T,S...>(a: T, ...: S...) return 1 end
local f: <T,S...>(T, S...)->(number) = foo
    "#,
    );
}

// PrettyPrinter.test.cpp: prettyPrint_for_in_multiple
#[test]
fn pretty_print_for_in_multiple() {
    assert_round_trips_with_types("for k,v in next,{}do print(k,v) end");
}

// PrettyPrinter.test.cpp: prettyPrint_for_in_multiple_types
#[test]
fn pretty_print_for_in_multiple_types() {
    assert_round_trips_with_types("for k:string,v:boolean in next,{}do end");
}

// PrettyPrinter.test.cpp: prettyPrint_function_attributes
#[test]
fn pretty_print_function_attributes() {
    let _export = flags::LuauExportValueSyntax.scoped(true);
    assert_round_trips_with_types(
        r#"
        @native
        function foo()
        end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        @native
        local function foo()
        end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        @checked local function foo()
        end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        local foo = @native function() end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        @native
        function foo:bar()
        end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        @native   @checked
        function foo:bar()
        end
    "#,
    );
    assert_round_trips_with_types(
        r#"
        @debugnoinline
        local function t() end
        "#,
    );
    assert_round_trips_with_types(
        r#"
    @[deprecated {
        use = "newApi()",
        reason = "newApi is faster and supports all value types.",
    }]
    local function oldApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    @[deprecated {use = "newApi()"}, native]
    local function oldFastApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    @[deprecated({use = "newApi()"})]
    local function oldFastApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    @[deprecated {
        use = "newApi()",
        reason = "newApi is faster and supports all value types.",
    }, native]
    function oldApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    @checked
    @[    deprecated  , native    ]
    function oldApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    @checked
    @[    deprecated  , native    ]
    export function oldApi()
    end
    "#,
    );
    assert_round_trips_with_types(
        r#"
    local foo = @checked
    @[    deprecated  , native    ]
    function()
    end
    "#,
    );
}

// PrettyPrinter.test.cpp: pretty_print_explicit_type_instantiations
#[test]
fn pretty_print_explicit_type_instantiations() {
    let code = "f<<A, B, C...>>() t.f<<A, B, C...>>() t:f<<A, B, C>>()";
    assert_round_trips_with_types(code);
    assert_pretty_prints(
        code,
        "f              () t.f              () t:f           ()",
    );
    assert_round_trips_with_types(
        "f < < A , B , C... > >( ) t.f < < A, B, C... > >  ( )  t:f< < A, B, C > > ( )",
    );
}