Skip to content
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 @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed npm publishing by bumping Node.js from 16 to 22 in CI workflows to support npm trusted publishing
- Luau: Fixed union/intersection type definitions not being hung when the type alone fits within the column width but the full line (including ` = `) exceeds it ([#1104](https://github.com/JohnnyMorganz/StyLua/issues/1104))
- Luau: Fixed stray leading newlines not being removed from `local function` and `const function` declarations that have attributes (e.g. `@native`) at the start of a block ([#1109](https://github.com/JohnnyMorganz/StyLua/issues/1109))

## [2.4.1] - 2026-04-06

Expand Down
54 changes: 42 additions & 12 deletions src/formatters/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use crate::{
},
shape::Shape,
};
#[cfg(feature = "luau")]
use full_moon::ast::luau::LuauAttribute;
use full_moon::ast::{
punctuated::Punctuated, Block, Expression, LastStmt, Prefix, Return, Stmt, Var,
};
Expand Down Expand Up @@ -317,6 +319,20 @@ fn var_remove_leading_newline(var: Var) -> Var {
}
}

#[cfg(feature = "luau")]
fn strip_attribute_leading_newlines<'a>(
attributes: impl Iterator<Item = &'a LuauAttribute>,
) -> Option<Vec<LuauAttribute>> {
let mut cloned = attributes.cloned();
let first = cloned.next()?;
let at_sign = first.at_sign();
let leading_trivia = trivia_remove_leading_newlines(at_sign.leading_trivia().collect());
let new_at_sign = at_sign.update_leading_trivia(FormatTriviaType::Replace(leading_trivia));
let mut result = vec![first.with_at_sign(new_at_sign)];
result.extend(cloned);
Some(result)
}

fn stmt_remove_leading_newlines(stmt: Stmt) -> Stmt {
match stmt {
Stmt::Assignment(assignment) => {
Expand Down Expand Up @@ -363,12 +379,19 @@ fn stmt_remove_leading_newlines(stmt: Stmt) -> Stmt {
local_assignment.local_token(),
with_local_token
),
Stmt::LocalFunction(local_function) => update_first_token!(
LocalFunction,
local_function,
local_function.local_token(),
with_local_token
),
Stmt::LocalFunction(local_function) => {
#[cfg(feature = "luau")]
if let Some(attributes) = strip_attribute_leading_newlines(local_function.attributes())
{
return Stmt::LocalFunction(local_function.with_attributes(attributes));
}
update_first_token!(
LocalFunction,
local_function,
local_function.local_token(),
with_local_token
)
}
Stmt::NumericFor(numeric_for) => update_first_token!(
NumericFor,
numeric_for,
Expand Down Expand Up @@ -404,12 +427,19 @@ fn stmt_remove_leading_newlines(stmt: Stmt) -> Stmt {
with_const_token
),
#[cfg(feature = "luau")]
Stmt::ConstFunction(const_function) => update_first_token!(
ConstFunction,
const_function,
const_function.const_token(),
with_const_token
),
Stmt::ConstFunction(const_function) => {
if let Some(attributes) = strip_attribute_leading_newlines(const_function.attributes())
{
Stmt::ConstFunction(const_function.with_attributes(attributes))
} else {
update_first_token!(
ConstFunction,
const_function,
const_function.const_token(),
with_const_token
)
}
}

#[cfg(feature = "luau")]
Stmt::ExportedTypeDeclaration(exported_type_declaration) => update_first_token!(
Expand Down
13 changes: 13 additions & 0 deletions tests/inputs-luau/attributes-4.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
do

@native
local function foo()
end
end

do

@native
const function bar()
end
end
15 changes: 15 additions & 0 deletions tests/snapshots/tests__luau@attributes-4.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: tests/tests.rs
assertion_line: 36
expression: "format(&contents, LuaVersion::Luau)"
input_file: tests/inputs-luau/attributes-4.lua
---
do
@native
local function foo() end
end

do
@native
const function bar() end
end
Loading