nicklockwood / SwiftFormat

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

Ternary operator in string interpolation forces single-line string to become multi-line and breaks build #1248

Closed orchetect closed 2 years ago

orchetect commented 2 years ago

Issue

  1. When a single-line string has interpolation containing a ternary expression, the line will be broken up into multiple lines according to the wrap rule.
  2. In this example it also seems to put the ? operator on its own line which is also not ideal.

Conditions

SwiftFormat 0.49.14 Xcode 14 beta 5

This is an example using default SwiftFormat options, with the following customizations: --maxwidth 100 --wrapternary before-operators

Original

let str = "\(true ? "A really long string here it's really long again I cannot emphasize enough how long this string is" : "Some other string")"

SwiftFormat formatted

let str = "\(true
    ?
    "A really long string here it's really long again I cannot emphasize enough how long this string is"
    : "Some other string")"

string-interpolation-ternary-bug

nicklockwood commented 2 years ago

@orchetect fixed in 0.49.15