nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

Wrapping / spacing issue with < and > operators in conditional statement breaking build #1225

Closed calda closed 2 years ago

calda commented 2 years ago

We've ran in to an interesting issue where running SwiftFormat (e.g. just swiftformat . --indent 2) on the following code causes it to no longer compile:

Before

if
  previousScrollOffsetY < scrollView.contentOffset.y,
  (scrollView.contentOffset.y + scrollView.bounds.size.height) > (threshold * scrollView.contentSize.height)
{ 
  /...
}

After (swiftformat . --indent 2)

if
  previousScrollOffsetY<scrollView.contentOffset.y,
    scrollView.contentOffset.y + scrollView.bounds.size.height>(threshold * scrollView.contentSize.height)
{
  /...
}

which no longer compilers.

Not sure exactly what is causing this, but I was able to work around it by wrapping this block in // swiftformat:disable:all and // swiftformat:enable:all comments.

calda commented 2 years ago

seems like it thinks the < and > tokens are generic parameters on a type named previousScrollOffsetY

calda commented 2 years ago

This popped up when we enabled the andOperators rule. Previously it was written like this, which didn't cause any issues:

if
  previousScrollOffsetY < scrollView.contentOffset.y
  && (scrollView.contentOffset.y + scrollView.bounds.size.height) > (threshold * scrollView.contentSize.height)
{ 
  /...
}
nicklockwood commented 2 years ago

Thanks - I think your diagnosis is correct. I'll fix it asap.

nicklockwood commented 2 years ago

@calda fixed in 0.49.15