diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba1fdf0..a64329db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed -- Fix formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992)) +- Fixed formatting of index containing brackets string in parentheses ([#992](https://github.com/JohnnyMorganz/StyLua/pull/992)) +- Fixed goto not being recognised for LuaJIT ([#986](https://github.com/JohnnyMorganz/StyLua/issues/986)) ## [2.1.0] - 2025-04-21 diff --git a/src/formatters/block.rs b/src/formatters/block.rs index 6d19a86e..3b6b7a37 100644 --- a/src/formatters/block.rs +++ b/src/formatters/block.rs @@ -425,9 +425,9 @@ fn stmt_remove_leading_newlines(stmt: Stmt) -> Stmt { type_function.type_token(), with_type_token ), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(goto) => update_first_token!(Goto, goto, goto.goto_token(), with_goto_token), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Label(label) => { update_first_token!(Label, label, label.left_colons(), with_left_colons) } diff --git a/src/formatters/lua52.rs b/src/formatters/goto.rs similarity index 100% rename from src/formatters/lua52.rs rename to src/formatters/goto.rs diff --git a/src/formatters/mod.rs b/src/formatters/mod.rs index 57ce20ec..8aa6e41b 100644 --- a/src/formatters/mod.rs +++ b/src/formatters/mod.rs @@ -9,8 +9,8 @@ pub mod expression; #[cfg(any(feature = "luau", feature = "cfxlua"))] pub mod compound_assignment; pub mod functions; -#[cfg(feature = "lua52")] -pub mod lua52; +#[cfg(any(feature = "lua52", feature = "luajit"))] +pub mod goto; #[cfg(feature = "lua54")] pub mod lua54; #[cfg(feature = "luau")] diff --git a/src/formatters/stmt.rs b/src/formatters/stmt.rs index 7cbe2171..5ee9d12f 100644 --- a/src/formatters/stmt.rs +++ b/src/formatters/stmt.rs @@ -1,7 +1,7 @@ #[cfg(any(feature = "luau", feature = "cfxlua"))] use crate::formatters::compound_assignment::format_compound_assignment; -#[cfg(feature = "lua52")] -use crate::formatters::lua52::{format_goto, format_goto_no_trivia, format_label}; +#[cfg(any(feature = "lua52", feature = "luajit"))] +use crate::formatters::goto::{format_goto, format_goto_no_trivia, format_label}; #[cfg(feature = "luau")] use crate::formatters::luau::{ format_exported_type_declaration, format_exported_type_function, format_type_declaration_stmt, @@ -1089,9 +1089,9 @@ pub(crate) mod stmt_block { Stmt::TypeFunction(type_function) => { Stmt::TypeFunction(format_type_function_block(ctx, type_function, block_shape)) } - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(node) => Stmt::Goto(node.to_owned()), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Label(node) => Stmt::Label(node.to_owned()), other => panic!("unknown node {:?}", other), } @@ -1124,8 +1124,8 @@ pub fn format_stmt(ctx: &Context, stmt: &Stmt, shape: Shape) -> Stmt { #[cfg(feature = "luau")] TypeDeclaration = format_type_declaration_stmt, #[cfg(feature = "luau")] ExportedTypeFunction = format_exported_type_function, #[cfg(feature = "luau")] TypeFunction = format_type_function_stmt, - #[cfg(feature = "lua52")] Goto = format_goto, - #[cfg(feature = "lua52")] Label = format_label, + #[cfg(any(feature = "lua52", feature = "luajit"))] Goto = format_goto, + #[cfg(any(feature = "lua52", feature = "luajit"))] Label = format_label, }) } @@ -1141,7 +1141,7 @@ pub fn format_stmt_no_trivia(ctx: &Context, stmt: &Stmt, shape: Shape) -> Stmt { } Stmt::Assignment(stmt) => Stmt::Assignment(format_assignment_no_trivia(ctx, stmt, shape)), Stmt::FunctionCall(stmt) => Stmt::FunctionCall(format_function_call(ctx, stmt, shape)), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(goto) => Stmt::Goto(format_goto_no_trivia(ctx, goto, shape)), _ => unreachable!("format_stmt_no_trivia: node != assignment/function call/goto"), } diff --git a/src/formatters/trivia.rs b/src/formatters/trivia.rs index 1febb699..0041c696 100644 --- a/src/formatters/trivia.rs +++ b/src/formatters/trivia.rs @@ -696,13 +696,13 @@ define_update_trivia!(Stmt, |this, leading, trailing| { } #[cfg(feature = "luau")] Stmt::TypeFunction(stmt) => Stmt::TypeFunction(stmt.update_trivia(leading, trailing)), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(stmt) => Stmt::Goto( stmt.to_owned() .with_goto_token(stmt.goto_token().update_leading_trivia(leading)) .with_label_name(stmt.label_name().update_trailing_trivia(trailing)), ), - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Label(stmt) => Stmt::Label( stmt.to_owned() .with_left_colons(stmt.left_colons().update_leading_trivia(leading)) diff --git a/src/formatters/trivia_util.rs b/src/formatters/trivia_util.rs index c7139224..5b0639a3 100644 --- a/src/formatters/trivia_util.rs +++ b/src/formatters/trivia_util.rs @@ -193,7 +193,7 @@ pub fn is_block_simple(block: &Block) -> bool { true } Stmt::FunctionCall(_) => true, - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(_) => true, _ => false, }) @@ -948,7 +948,7 @@ pub fn get_stmt_trailing_trivia(stmt: Stmt) -> (Stmt, Vec) { let (type_declaration, trailing_trivia) = take_trailing_trivia(&stmt); (Stmt::TypeFunction(type_declaration), trailing_trivia) } - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Goto(stmt) => { let trailing_trivia = stmt .label_name() @@ -963,7 +963,7 @@ pub fn get_stmt_trailing_trivia(stmt: Stmt) -> (Stmt, Vec) { trailing_trivia, ) } - #[cfg(feature = "lua52")] + #[cfg(any(feature = "lua52", feature = "luajit"))] Stmt::Label(stmt) => { let trailing_trivia = stmt .right_colons() diff --git a/tests/inputs-luajit/goto.lua b/tests/inputs-luajit/goto.lua new file mode 100644 index 00000000..97e5c2f7 --- /dev/null +++ b/tests/inputs-luajit/goto.lua @@ -0,0 +1,4 @@ +if opaque then + goto label +end +::label:: diff --git a/tests/snapshots/tests__luajit.snap b/tests/snapshots/tests__luajit.snap new file mode 100644 index 00000000..b2a02c45 --- /dev/null +++ b/tests/snapshots/tests__luajit.snap @@ -0,0 +1,9 @@ +--- +source: tests/tests.rs +expression: "format(&contents, LuaVersion::LuaJIT)" +input_file: tests/inputs-luajit/goto.lua +--- +if opaque then + goto label +end +::label:: diff --git a/tests/tests.rs b/tests/tests.rs index dd2ff45e..1266bb4d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -73,6 +73,15 @@ fn test_lua54() { }) } +#[test] +#[cfg(feature = "luajit")] +fn test_luajit() { + insta::glob!("inputs-luajit/*.lua", |path| { + let contents = std::fs::read_to_string(path).unwrap(); + insta::assert_snapshot!(format(&contents, LuaVersion::LuaJIT)); + }) +} + #[test] #[cfg(feature = "cfxlua")] fn test_cfxlua() {