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 @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed spaces around brackets string (`[[string]]`) used as an index or table key (i.e. `[ [[string]] ]`) being removed, leading to a syntax error. ([#293](https://github.com/JohnnyMorganz/StyLua/issues/293))
- Fixed incorrect shape calculation leading to arguments incorrectly expanding when under column width. ([#298](https://github.com/JohnnyMorganz/StyLua/issues/298))
- Fixed incorrect shape calculation for singleline table at the column width boundary. ([#296](https://github.com/JohnnyMorganz/StyLua/issues/296))
- Fixed IfExpression syntax containing extra/abnormal trailing whitespace when currently formatting as-is under the `luau` feature flag. ([#297](https://github.com/JohnnyMorganz/StyLua/issues/297))

## [0.11.1] - 2021-11-08
### Changed
Expand Down
11 changes: 10 additions & 1 deletion src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,16 @@ fn format_if_expression(
_shape: Shape,
) -> IfExpression {
// TODO: Apply actual formatting here
if_expression.to_owned()
// because we are just returning as-is, we need to remove the trailing whitespace trivia if present, otherwise stuff
// gets weird (https://github.com/JohnnyMorganz/StyLua/issues/297)

let (else_expression, trailing_comments) =
trivia_util::take_expression_trailing_comments(if_expression.else_expression());

let else_expression =
else_expression.update_trailing_trivia(FormatTriviaType::Replace(trailing_comments));

if_expression.to_owned().with_else(else_expression)
}

/// Formats a Value Node
Expand Down
12 changes: 12 additions & 0 deletions tests/inputs-luau/if-expression-2.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- from full-moon tests: https://github.com/Kampfkarren/full-moon/blob/main/full-moon/tests/roblox_cases/pass/if_expression/source.lua
local x = if foo then foo.x else 5
local y = (if x then x.indices else create()):update(if shouldUpdate then information else defaults)
local z = (if bar then foo.y else 5) :: number

local a = if foo then foo.x elseif bar then bar.x else 5
local b = if foo then if bar then bar else foo else 5
local c = if foo then (foo.x :: number) elseif bar then bar.x()() else 5
local d = if foo then 5 else baz :: number

if if foo then bar else baz then
end
11 changes: 11 additions & 0 deletions tests/inputs-luau/if-expression-3.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- https://github.com/JohnnyMorganz/StyLua/issues/297
it("should work", function()
local foo = 1

local bar = if foo > 1 then 1 else 2
bar = if foo > 1 then 1 else 2
end)

Autocomplete = function(player)
return { if player then player.Name else nil }
end
18 changes: 18 additions & 0 deletions tests/snapshots/tests__luau@if-expression-2.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
source: tests/tests.rs
expression: format(&contents)

---
-- from full-moon tests: https://github.com/Kampfkarren/full-moon/blob/main/full-moon/tests/roblox_cases/pass/if_expression/source.lua
local x = if foo then foo.x else 5
local y = (if x then x.indices else create()):update(if shouldUpdate then information else defaults)
local z = if bar then foo.y else 5 :: number

local a = if foo then foo.x elseif bar then bar.x else 5
local b = if foo then if bar then bar else foo else 5
local c = if foo then (foo.x :: number) elseif bar then bar.x()() else 5
local d = if foo then 5 else baz :: number

if if foo then bar else baz then
end

17 changes: 17 additions & 0 deletions tests/snapshots/tests__luau@if-expression-3.lua.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: tests/tests.rs
expression: format(&contents)

---
-- https://github.com/JohnnyMorganz/StyLua/issues/297
it("should work", function()
local foo = 1

local bar = if foo > 1 then 1 else 2
bar = if foo > 1 then 1 else 2
end)

Autocomplete = function(player)
return { if player then player.Name else nil }
end

3 changes: 0 additions & 3 deletions tests/snapshots/tests__luau@if-expression.lua.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ do
console.error(
"guitarFuzz design functions accept exactly two parameters: guiter and fuzz. %s",
if argumentCount == 1 then "Did you forget to use the fuzz parameter?" else "Any additional parameter will be undefined."

)
end
end
Expand All @@ -26,7 +25,6 @@ do
(initialState :: (() -> S))()
else initialState


local state: S = if hook ~= nil then hook.memoizedState
elseif
typeof(initialState) == "function" -- the fuzz pedal isn't 3.3V
Expand All @@ -35,6 +33,5 @@ do
-- Luau needs a little help, even with the generic function
(initialState :: (() -> S))()
else initialState

end