use crate::pretty_printer::{
assert_pretty_prints, assert_round_trips, assert_round_trips_with_types, with_parse,
};
use luau_common::ByteSlice;
use luau_common::flags;
use luau_syntax::parser::ParseOptions;
#[test]
fn test_1() {
assert_round_trips(
r#"
local function isPortal(element)
if type(element)~='table'then
return false
end
return element.component == Core.Portal
end
"#,
);
}
#[test]
fn if_stmt_spaces_around_tokens() {
assert_round_trips(r" if This then Once() end");
assert_round_trips(r" if This then Once() end");
assert_round_trips(r" if This then Once() end");
assert_round_trips(r" if This then Once() end");
assert_round_trips(r" if This then Once() else Other() end");
assert_round_trips(r" if This then Once() else Other() end");
assert_round_trips(r" if This then Once() elseif true then Other() end");
assert_round_trips(r" if This then Once() elseif true then Other() end");
assert_round_trips(r" if This then Once() elseif true then Other() end");
}
#[test]
fn elseif_chains_indent_sensibly() {
assert_round_trips(
r#"
if This then
Once()
elseif That then
Another()
elseif SecondLast then
Third()
else
IfAllElseFails()
end
"#,
);
}
#[test]
fn strips_type_annotations() {
assert_pretty_prints(
r" local s: string= 'hello there' ",
r" local s = 'hello there' ",
);
}
#[test]
fn function_taking_ellipsis() {
assert_round_trips(r" function F(...) end ");
}
#[test]
fn local_assignment() {
assert_round_trips(r" local x = 1 ");
assert_round_trips(r" local x, y, z = 1, 2, 3 ");
assert_round_trips(r" local x ");
}
#[test]
fn local_assignment_spaces_around_tokens() {
assert_round_trips(r" local x = 1 ");
assert_round_trips(r" local x = 1 ");
assert_round_trips(r" local x = 1 ");
assert_round_trips(r" local x , y = 1, 2 ");
assert_round_trips(r" local x, y = 1, 2 ");
assert_round_trips(r" local x, y = 1 , 2 ");
assert_round_trips(r" local x, y = 1, 2 ");
}
#[test]
fn local_function() {
assert_round_trips(r" local function p(o, m, g) return 77 end ");
assert_round_trips(r" local function p(o, m, g,...) return 77 end ");
}
#[test]
fn local_function_spaces_around_tokens() {
assert_round_trips(r" local function p(o, m, ...) end ");
assert_round_trips(r" local function p(o, m, ...) end ");
}
#[test]
fn export() {
let _export = flags::LuauExportValueSyntax.scoped(true);
let source = r#"
export local version = "1.0.0"
export const tabbed = ...
export const TAU = math.pi * 2
export local settings: Settings = getSettings()
export local a, b, c = 1, 2, 3
export local d
"#;
with_parse(
source,
ParseOptions::default().with_cst_data(true),
|result| {
assert_eq!(
luau_syntax::pretty_printer::pretty_print_with_types_and_cst(
result.root,
&result.metadata.cst_nodes
)
.as_bytes(),
source.as_bytes()
);
},
);
let source = r#"
export function add(a: number, b: number): number
return a + b
end
export function greet(name: string): string
return "Hello, " .. name
end
export function noop()
end
export function tabbed(): number
return 1
end
"#;
with_parse(
source,
ParseOptions::default().with_cst_data(true),
|result| {
assert_eq!(
luau_syntax::pretty_printer::pretty_print_with_types_and_cst(
result.root,
&result.metadata.cst_nodes
)
.as_bytes(),
source.as_bytes()
);
},
);
let source = r#"
@native
export function foo()
end
@native
export function tabbed_attribute()
end
"#;
with_parse(
source,
ParseOptions::default().with_cst_data(true),
|result| {
assert_eq!(
luau_syntax::pretty_printer::pretty_print_with_types_and_cst(
result.root,
&result.metadata.cst_nodes
)
.as_bytes(),
source.as_bytes()
);
},
);
let source = r#"
export local f, g
function f()
return g()
end
function g()
return 42
end
"#;
with_parse(
source,
ParseOptions::default().with_cst_data(true),
|result| {
assert_eq!(
luau_syntax::pretty_printer::pretty_print_with_types_and_cst(
result.root,
&result.metadata.cst_nodes
)
.as_bytes(),
source.as_bytes()
);
},
);
let source = r#"
export type Config = {
debug: boolean,
timeout: number,
}
export local currentConfig: Config
export function createConfig(debug: boolean, timeout: number): Config
return {
debug = debug,
timeout = timeout,
}
end
"#;
with_parse(
source,
ParseOptions::default().with_cst_data(true),
|result| {
assert_eq!(
luau_syntax::pretty_printer::pretty_print_with_types_and_cst(
result.root,
&result.metadata.cst_nodes
)
.as_bytes(),
source.as_bytes()
);
},
);
}
#[test]
fn function() {
assert_round_trips(r" function p(o, m, g) return 77 end ");
assert_round_trips(r" function p(o, m, g,...) return 77 end ");
}
#[test]
fn function_spaces_around_tokens() {
assert_round_trips(r" function p(o, m, ...) end ");
assert_round_trips(r" function p( o, m, ...) end ");
assert_round_trips(r" function p(o , m, ...) end ");
assert_round_trips(r" function p(o, m, ...) end ");
assert_round_trips(r" function p(o, m , ...) end ");
assert_round_trips(r" function p(o, m, ...) end ");
assert_round_trips(r" function p(o, m, ... ) end ");
assert_round_trips(r" function p(o, m, ...) end ");
}
#[test]
fn function_with_types_spaces_around_tokens() {
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p <X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X , Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z ...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z... >(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...> (o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o : string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string , m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ... : any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any ): string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any) :string end ",
);
assert_round_trips_with_types(
r" function p<X, Y, Z...>(o: string, m: number, ...: any): string end ",
);
}
#[test]
fn returns_spaces_around_tokens() {
assert_round_trips(r" return 1 ");
assert_round_trips(r" return 1 , 2 ");
assert_round_trips(r" return 1, 2 ");
}
#[test]
fn for_loop() {
assert_round_trips(r" for i=1,10 do end ");
assert_round_trips(r" for i=5,6,7 do end ");
}
#[test]
fn for_loop_spaces_around_tokens() {
assert_round_trips(r" for index = 1, 10 do call(index) end ");
assert_round_trips(r" for index = 1 , 10 do call(index) end ");
assert_round_trips(r" for index = 1, 10 , 3 do call(index) end ");
assert_round_trips(r" for index = 1, 10 do call(index) end ");
assert_round_trips(r" for index = 1, 10 do call(index) end ");
}
#[test]
fn for_in_loop() {
assert_round_trips(r" for k, v in ipairs(x)do end ");
}
#[test]
fn for_in_loop_spaces_around_tokens() {
assert_round_trips(r" for k, v in ipairs(x) do end ");
assert_round_trips(r" for k, v in ipairs(x) do end ");
assert_round_trips(r" for k , v in ipairs(x) do end ");
assert_round_trips(r" for k, v in next , t do end ");
assert_round_trips(r" for k, v in ipairs(x) do end ");
}
#[test]
fn for_in_single_variable() {
assert_round_trips(r" for key in pairs(x) do end ");
}
#[test]
fn while_loop() {
assert_round_trips(r" while f(x)do print() end ");
}
#[test]
fn while_loop_spaces_around_tokens() {
assert_round_trips(r" while f(x) do print() end ");
assert_round_trips(r" while f(x) do print() end ");
assert_round_trips(r" while f(x) do print() end ");
assert_round_trips(r" while f(x) do print() end ");
}
#[test]
fn repeat_until_loop() {
assert_round_trips(r" repeat print() until f(x) ");
}
#[test]
fn repeat_until_loop_condition_on_new_line() {
assert_round_trips(
r#"
repeat
print()
until
f(x) "#,
);
}
#[test]
fn spaces_between_keywords_even_if_it_pushes_the_line_estimation_off() {
assert_round_trips(r" if math.abs(raySlope) < .01 then return 0 end ");
}