Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collapse_simple_stmt: check if return expressions are "simple" #923

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `.stylua.toml` config resolution now supports looking up config files next to files being formatted, recursively going
upwards until reaching the current working directory, then stopping (unless `--search-parent-directories` was specified).
For example, for a file `./src/test.lua`, executing `stylua src/` will look for `./src/stylua.toml` and then `./stylua.toml`.
- When `collapse_simple_statement` is enabled, if the enclosing block is a return, we will check if the return expression is "simple" (currently, not containing a function definition) ([#898](https://github.com/JohnnyMorganz/StyLua/issues/898))

### Fixed

Expand Down
44 changes: 43 additions & 1 deletion src/formatters/trivia_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,50 @@ pub fn is_block_empty(block: &Block) -> bool {
block.stmts().next().is_none() && block.last_stmt().is_none()
}

fn is_expression_simple(expression: &Expression) -> bool {
match expression {
Expression::Function(_) => false,
Expression::FunctionCall(function_call) => {
function_call.suffixes().all(|suffix| match suffix {
Suffix::Index(_) => true,
Suffix::Call(call) => match call {
Call::AnonymousCall(function_args) => match function_args {
FunctionArgs::Parentheses { arguments, .. } => {
arguments.iter().all(is_expression_simple)
}
_ => true,
},
Call::MethodCall(method_call) => match method_call.args() {
FunctionArgs::Parentheses { arguments, .. } => {
arguments.iter().all(is_expression_simple)
}
_ => true,
},
other => unreachable!("unknown node: {:?}", other),
},
other => unreachable!("unknown node: {:?}", other),
})
}
_ => true,
}
}

fn is_last_stmt_simple(last_stmt: &LastStmt) -> bool {
match last_stmt {
LastStmt::Break(_) => true,
#[cfg(feature = "luau")]
LastStmt::Continue(_) => true,
LastStmt::Return(r#return) => {
r#return.returns().is_empty() || r#return.returns().iter().all(is_expression_simple)
}
other => unreachable!("unknown node {:?}", other),
}
}

pub fn is_block_simple(block: &Block) -> bool {
(block.stmts().next().is_none() && block.last_stmt().is_some())
(block.stmts().next().is_none()
&& block.last_stmt().is_some()
&& is_last_stmt_simple(block.last_stmt().unwrap()))
|| (block.stmts().count() == 1
&& block.last_stmt().is_none()
&& match block.stmts().next().unwrap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/898

if bar then
return function()
foo()
end
end

if bar then
return Array.filter({}, function()
return true
end)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/tests.rs
expression: "format_code(&contents,\n Config {\n collapse_simple_statement: CollapseSimpleStatement::Always,\n ..Config::default()\n }, None, OutputVerification::None).unwrap()"
input_file: tests/inputs-collapse-single-statement/conditional-with-function-1.lua
---
-- https://github.com/JohnnyMorganz/StyLua/issues/898

if bar then
return function() foo() end
end

if bar then
return Array.filter({}, function() return true end)
end

Loading