From f4bcd7265c588141c0ac9d1312c57308d8e941eb Mon Sep 17 00:00:00 2001 From: JohnnyMorganz Date: Sat, 27 Jan 2024 14:05:06 +0100 Subject: [PATCH] Support Lua version customisation --- src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++------- src/verify_ast.rs | 30 ++++++++++++++++++++++------ tests/tests.rs | 34 +++++++++++++++++-------------- 3 files changed, 87 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 746497c2..5f1264b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 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)] @@ -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. @@ -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(), @@ -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) { @@ -427,7 +464,7 @@ pub fn format_code( range: Option, verify_output: OutputVerification, ) -> Result { - 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)); diff --git a/src/verify_ast.rs b/src/verify_ast.rs index 689a348a..3c8af158 100644 --- a/src/verify_ast.rs +++ b/src/verify_ast.rs @@ -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)); @@ -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)); @@ -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)); diff --git a/tests/tests.rs b/tests/tests.rs index 187b8007..ad5a8f18 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -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)); }) } @@ -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)); }) } @@ -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)); }) } @@ -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)); }) } @@ -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)); }) } @@ -66,7 +69,7 @@ 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)); }) } @@ -74,7 +77,7 @@ fn test_lua54() { 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)); }) } @@ -107,6 +110,7 @@ fn test_collapse_single_statement_lua_52() { end "###, Config { + lua_version: LuaVersion::Lua52, collapse_simple_statement: CollapseSimpleStatement::Always, ..Config::default() }, @@ -152,7 +156,7 @@ local x = 1 "#; let code_crlf = code.lines().collect::>().join("\r\n"); - let output = format(&code_crlf); + let output = format(&code_crlf, LuaVersion::Lua51); assert_eq!(output.find("\r\n"), None); } @@ -170,6 +174,6 @@ local x = 1 "###; let code_crlf = code.lines().collect::>().join("\r\n"); - let output = format(&code_crlf); + let output = format(&code_crlf, LuaVersion::Lua51); assert_eq!(output.find("\r\n"), None); }