Skip to content

Stylua reaches outside the given range if there are semicolons #1147

Description

@ivanalejandro0

Formatting or checking lua code with a given range, stylua tackles the given range but also any other part of the file where there is a semicolon.

This happens with latest version (stylua 2.5.2).

Here's a small code sample used to show the problem:

example.lua

for i = 1, 5 do
  print("count: " .. i)
end

for i = 1, 10, 2 do
  print("count: " .. i)
end

-- unnecessary semicolons, for stylua to catch
local a = 1;
local b = 2;

Example use cases showing the issue

Checking style on first loop:

$ stylua --check --range-start 0 --range-end 43 example.lua
Diff in example.lua:
1   1    | for i = 1, 5 do
2        |-  print("count: " .. i)
    2    |+	print("count: " .. i)
3   3    | end
4   4    | 
5   5    | for i = 1, 10, 2 do
--------------------------------------------------------------------------------
7   7    | end
8   8    | 
9   9    | -- unnecessary semicolons, for stylua to catch
10       |-local a = 1;
11       |-local b = 2;
    10   |+local a = 1
    11   |+local b = 2

Checking style on second loop:

$ stylua --check --range-start 46 --range-end 94 example.lua
Diff in example.lua:
3   3    | end
4   4    | 
5   5    | for i = 1, 10, 2 do
6        |-  print("count: " .. i)
    6    |+	print("count: " .. i)
7   7    | end
8   8    | 
9   9    | -- unnecessary semicolons, for stylua to catch
10       |-local a = 1;
11       |-local b = 2;
    10   |+local a = 1
    11   |+local b = 2

Checking style on an "empty" range:

$ stylua --check --range-start 0 --range-end 0 example.lua
Diff in example.lua:
7   7    | end
8   8    |
9   9    | -- unnecessary semicolons, for stylua to catch
10       |-local a = 1;
11       |-local b = 2;
    10   |+local a = 1
    11   |+local b = 2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions