Fix incorrect replacement of && operator inside a ternary condition

This commit is contained in:
Nick Lockwood
2026-02-26 09:21:21 +00:00
committed by Cal Stephens
parent fc3a2ccb10
commit a962548ecf
2 changed files with 8 additions and 1 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ public extension FormatRule {
let endOfGroup = formatter.index(of: .delimiter(","), after: index) ?? endIndex
var nextOpIndex = index
while let next = formatter.index(of: .operator, after: nextOpIndex) {
if formatter.tokens[next] == .operator("||", .infix) {
if [.operator("||", .infix), .operator("?", .infix)].contains(formatter.tokens[next]) {
index = endOfGroup
continue outer
}
+7
View File
@@ -134,6 +134,13 @@ final class AndOperatorTests: XCTestCase {
testFormatting(for: input, rule: .andOperator)
}
func testNoReplaceAndInTernaryCondition() {
let input = """
if foo && bar ? baz : quux {}
"""
testFormatting(for: input, rule: .andOperator)
}
func testHandleAndAtStartOfLine() {
let input = """
if a == b