Skip to content

preserve-block-newline-gaps=Preserve inserts trailing whitespace for an empty function #1146

Open

Description

@idbrii

Thanks for adding this option! I much prefer having some newlines in my if statements!

Using preserve-block-newline-gaps=Preserve when a function contains a blank line removes the line but adds a space after the function declaration.

Before:

function FuncWithBlankLine()

end

function FuncEmpty()
end

function FuncOneLiner() end

Run stylua --preserve-block-newline-gaps=Preserve --no-editorconfig --config-path NUL test.lua

After:

function FuncWithBlankLine() 
end

function FuncEmpty() end

function FuncOneLiner() end

FuncWithBlankLine has a space on the end of the line: function FuncWithBlankLine()_

Expected: FuncOneLiner is unchanged as I'd expect, but I would expect FuncWithBlankLine and FuncEmpty to look the same.

function FuncWithBlankLine()
end

function FuncEmpty()
end

function FuncOneLiner() end

Additionally, the output is not stable. Running the same stylua command again removes the space and makes all functions look like FuncOneLiner. This is the same behaviour as using --preserve-block-newline-gaps=Never on the original file, so I think the intent was for the newline to be preserved instead of replacing it with a space.

Activity

  1. changed the title [-]preserve-block-newline-gaps=Preserve leaves trailing whitespace for an empty function[/-] [+]preserve-block-newline-gaps=Preserve inserts trailing whitespace for an empty function[/+] on Aug 13, 2026
  2. JohnnyMorganz commented on Oct 3, 2026

    @JohnnyMorganz
    Owner

    The behaviour implemented in #1159 is slightly different to your expectation here, but I think it's also reasonable - does that match what you expect?

  3. idbrii commented on Oct 19, 2026

    @idbrii
    Author

    I tried v2.6.0 but it gave me the exact same results as above, so I guess it's not yet released.

    From the PR's test it looks like this would be the output:

    function FuncWithBlankLine()
    
    end
    
    function FuncEmpty() end
    
    function FuncOneLiner() end
    

    I expected it would remove blank lines but preserve newlines. The PR preserves blank links but collapses newlines. The PR better matches the description of the feature:

    Specify whether to preserve leading and trailing newline gaps for blocks. Possible options: Never, Preserve.

    However, I'm not sure this behaviour is actually more useful in practice. I don't like one-line functions, so I wouldn't find it helpful to collapse like this. I also wouldn't write functions that only contain a blank line so I wouldn't go the route of adding a blank line before each end just to prevent them from collapsing to one line. But these empty functions are rare, and the output is now stable so it's definitely an improvement. I think you can close as resolved if the current behaviour is satisfactory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions