nicklockwood / SwiftFormat

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

Bracket Formatting Lacks Trailing Comma #1463

Open PompeiaPaetenari opened 1 year ago

PompeiaPaetenari commented 1 year ago

This issue was noticed back in November, and still remains today, and I have been remiss about reporting it... but here we are:

The problem (this is after I run SwiftFormat on something a bit more messy):

private func getSentence() {
    _ = [firstString(strings: [
      "The fox that jumped over a lazy " +
        "dog was the laziest of them all.",
    ])]
  }

Due to how the trailing brackets and parens are grouped, we end up with an issue that triggers SwiftLint's rule for trailing commas:

Warning Trailing Comma Violation: Multi-line collection literals should have trailing commas (trailing_comma)

A normal array is fine (this is after SwiftFormat has run when the array did not have that final comma):

let multilineArray = [
    "a really long string",
    "another long string",
    "and one final longish string",
  ]

And calling a function that takes an array is fine, too (same as the problem one, but I removed the outer array):

private func getSentence() {
    _ = firstString(strings: [
      "The fox that jumped over a lazy " +
        "dog was the laziest of them all.",
    ])
  }

Current workaround, and how I think it should look once formatted anyhow (all the trailing brackets and parents get their own line):

private func getSentence() {
    _ = [
      firstString(
        strings: [
          "The fox that jumped over a lazy " +
            "dog was the laziest of them all.",
        ]
      ),
    ]
  }

Suggestion from others on the team (these also work, but not really my thing):

private func getSentence() {
    _ = [
      firstString(
        strings: [
          "The fox that jumped over a lazy " +
            "dog was the laziest of them all.",
        ]),
    ]
  }
private func getSentence() {
    _ = [
      firstString(strings: [
        "The fox that jumped over a lazy " +
          "dog was the laziest of them all.",
      ]),
    ]
  }

Hopefully, this is enough to go on for creating a proper solution for both styles (all-man and others).