Skip to content

Commit

Permalink
Support Lua version customisation
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Jan 27, 2024
1 parent 239eb8b commit f4bcd72
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 28 deletions.
51 changes: 44 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@ mod shape;
mod sort_requires;
mod verify_ast;

/// The Lua syntax version to use
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Deserialize)]
#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
#[cfg_attr(feature = "fromstr", derive(strum::EnumString))]
pub enum LuaVersion {
#[default]
Lua51,
#[cfg(feature = "lua52")]
Lua52,
#[cfg(feature = "lua53")]
Lua53,
#[cfg(feature = "lua54")]
Lua54,
#[cfg(feature = "luau")]
Luau,
}

impl From<LuaVersion> for full_moon::LuaVersion {
fn from(val: LuaVersion) -> Self {
match val {
LuaVersion::Lua51 => full_moon::LuaVersion::lua51(),
#[cfg(feature = "lua52")]
LuaVersion::Lua52 => full_moon::LuaVersion::lua52(),
#[cfg(feature = "lua53")]
LuaVersion::Lua53 => full_moon::LuaVersion::lua53(),
#[cfg(feature = "lua54")]
LuaVersion::Lua54 => full_moon::LuaVersion::lua54(),
#[cfg(feature = "luau")]
LuaVersion::Luau => full_moon::LuaVersion::luau(),
}
}
}

/// The type of indents to use when indenting
#[derive(Debug, Default, Copy, Clone, PartialEq, Eq, Deserialize)]
#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
Expand Down Expand Up @@ -145,6 +179,7 @@ impl SortRequiresConfig {
#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen"), wasm_bindgen)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize))]
pub struct Config {
pub lua_version: LuaVersion,
/// The approximate line length to use when printing the code.
/// This is used as a guide to determine when to wrap lines, but note
/// that this is not a hard upper bound.
Expand Down Expand Up @@ -337,6 +372,7 @@ impl Default for Config {
fn default() -> Self {
#[allow(deprecated)]
Self {
lua_version: LuaVersion::default(),
column_width: 120,
line_endings: LineEndings::default(),
indent_type: IndentType::default(),
Expand Down Expand Up @@ -403,12 +439,13 @@ pub fn format_ast(
// If we are verifying, reparse the output then check it matches the original input
if let Some(input_ast) = input_ast_for_verification {
let output = full_moon::print(&ast);
let reparsed_output = match full_moon::parse(&output) {
Ok(ast) => ast,
Err(error) => {
return Err(Error::VerificationAstError(error));
}
};
let reparsed_output =
match full_moon::parse_fallible(&output, config.lua_version.into()).into_result() {
Ok(ast) => ast,
Err(error) => {
return Err(Error::VerificationAstError(error));
}
};

let mut ast_verifier = verify_ast::AstVerifier::new();
if !ast_verifier.compare(input_ast, reparsed_output) {
Expand All @@ -427,7 +464,7 @@ pub fn format_code(
range: Option<Range>,
verify_output: OutputVerification,
) -> Result<String, Error> {
let input_ast = match full_moon::parse(code) {
let input_ast = match full_moon::parse_fallible(code, config.lua_version.into()).into_result() {
Ok(ast) => ast,
Err(error) => {
return Err(Error::ParseError(error));
Expand Down
30 changes: 24 additions & 6 deletions src/verify_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,14 @@ mod tests {
#[test]
#[cfg(feature = "luau")]
fn test_equivalent_binary_numbers() {
let input_ast = full_moon::parse("local x = 0B10101").unwrap();
let output_ast = full_moon::parse("local x = 0b10101").unwrap();
use full_moon::LuaVersion;

let input_ast = full_moon::parse_fallible("local x = 0B10101", LuaVersion::luau())
.into_result()
.unwrap();
let output_ast = full_moon::parse_fallible("local x = 0b10101", LuaVersion::luau())
.into_result()
.unwrap();

let mut ast_verifier = AstVerifier::new();
assert!(ast_verifier.compare(input_ast, output_ast));
Expand All @@ -287,8 +293,14 @@ mod tests {
#[test]
#[cfg(feature = "luau")]
fn test_different_binary_numbers() {
let input_ast = full_moon::parse("local x = 0b1111").unwrap();
let output_ast = full_moon::parse("local x = 0b1110").unwrap();
use full_moon::LuaVersion;

let input_ast = full_moon::parse_fallible("local x = 0b1111", LuaVersion::luau())
.into_result()
.unwrap();
let output_ast = full_moon::parse_fallible("local x = 0b1110", LuaVersion::luau())
.into_result()
.unwrap();

let mut ast_verifier = AstVerifier::new();
assert!(!ast_verifier.compare(input_ast, output_ast));
Expand All @@ -297,8 +309,14 @@ mod tests {
#[test]
#[cfg(feature = "lua52")]
fn test_equivalent_luajit_numbers() {
let input_ast = full_moon::parse("local x = 2 ^ 63LL").unwrap();
let output_ast = full_moon::parse("local x = 2 ^ 63").unwrap();
use full_moon::LuaVersion;

let input_ast = full_moon::parse_fallible("local x = 2 ^ 63LL", LuaVersion::lua52())
.into_result()
.unwrap();
let output_ast = full_moon::parse_fallible("local x = 2 ^ 63", LuaVersion::lua52())
.into_result()
.unwrap();

let mut ast_verifier = AstVerifier::new();
assert!(ast_verifier.compare(input_ast, output_ast));
Expand Down
34 changes: 19 additions & 15 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
use stylua_lib::{
format_code, CollapseSimpleStatement, Config, OutputVerification, SortRequiresConfig,
format_code, CollapseSimpleStatement, Config, LuaVersion, OutputVerification,
SortRequiresConfig,
};

fn format(input: &str) -> String {
format_code(input, Config::default(), None, OutputVerification::None).unwrap()
fn format(input: &str, lua_version: LuaVersion) -> String {
let config = Config {
lua_version,
..Config::default()
};
format_code(input, config, None, OutputVerification::None).unwrap()
}

#[test]
#[cfg_attr(feature = "luau", ignore)]
fn test_standard() {
insta::glob!("inputs/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua51));
})
}

#[test]
#[cfg_attr(feature = "lua52", ignore)] // A test case has `goto` as an identifier, which is not allowed in Lua 5.2
fn test_full_moon_test_suite() {
insta::glob!("inputs-full_moon/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua51));
})
}

Expand All @@ -30,7 +33,7 @@ fn test_luau() {
insta::glob!("inputs-luau/*.lua", |path| {
dbg!(path);
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Luau));
})
}

Expand All @@ -39,7 +42,7 @@ fn test_luau() {
fn test_luau_full_moon() {
insta::glob!("inputs-luau-full_moon/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Luau));
})
}

Expand All @@ -48,7 +51,7 @@ fn test_luau_full_moon() {
fn test_lua52() {
insta::glob!("inputs-lua52/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua52));
})
}

Expand All @@ -57,7 +60,7 @@ fn test_lua52() {
fn test_lua53() {
insta::glob!("inputs-lua53/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua53));
})
}

Expand All @@ -66,15 +69,15 @@ fn test_lua53() {
fn test_lua54() {
insta::glob!("inputs-lua54/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua54));
})
}

#[test]
fn test_ignores() {
insta::glob!("inputs-ignore/*.lua", |path| {
let contents = std::fs::read_to_string(path).unwrap();
insta::assert_snapshot!(format(&contents));
insta::assert_snapshot!(format(&contents, LuaVersion::Lua51));
})
}

Expand Down Expand Up @@ -107,6 +110,7 @@ fn test_collapse_single_statement_lua_52() {
end
"###,
Config {
lua_version: LuaVersion::Lua52,
collapse_simple_statement: CollapseSimpleStatement::Always,
..Config::default()
},
Expand Down Expand Up @@ -152,7 +156,7 @@ local x = 1
"#;

let code_crlf = code.lines().collect::<Vec<_>>().join("\r\n");
let output = format(&code_crlf);
let output = format(&code_crlf, LuaVersion::Lua51);
assert_eq!(output.find("\r\n"), None);
}

Expand All @@ -170,6 +174,6 @@ local x = 1
"###;

let code_crlf = code.lines().collect::<Vec<_>>().join("\r\n");
let output = format(&code_crlf);
let output = format(&code_crlf, LuaVersion::Lua51);
assert_eq!(output.find("\r\n"), None);
}

0 comments on commit f4bcd72

Please sign in to comment.