Remove spaces around :: module selection operator in spaceAroundOperators (#2402)

Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
Copilot
2026-02-21 10:15:04 -08:00
committed by Cal Stephens
parent db93709863
commit 22c2bb689e
3 changed files with 37 additions and 8 deletions
+7
View File
@@ -44,6 +44,13 @@ public extension FormatRule {
{
formatter.insert(.space(" "), at: i + 1)
}
case .operator("::", _):
if formatter.token(at: i + 1)?.isSpace == true {
formatter.removeToken(at: i + 1)
}
if formatter.token(at: i - 1)?.isSpace == true {
formatter.removeToken(at: i - 1)
}
case .operator(".", _):
if formatter.token(at: i + 1)?.isSpace == true {
formatter.removeToken(at: i + 1)
@@ -1170,4 +1170,28 @@ final class SpaceAroundOperatorsTests: XCTestCase {
options: options
)
}
func testNoSpaceAroundDoubleColonOperator() {
let input = """
let x = Module::TypeName
"""
testFormatting(for: input, rule: .spaceAroundOperators)
}
func testSpaceRemovedAroundDoubleColonOperator() {
let input = """
let x = Module :: TypeName
"""
let output = """
let x = Module::TypeName
"""
testFormatting(for: input, output, rule: .spaceAroundOperators)
}
func testNoSpaceAroundDoubleColonInMemberAccess() {
let input = """
let x = foo.Module::functionName()
"""
testFormatting(for: input, rule: .spaceAroundOperators)
}
}
+6 -8
View File
@@ -839,18 +839,12 @@ final class WrapTests: XCTestCase {
let input = """
NationalAeronauticsAndSpaceAdministration::RocketEngine
"""
// wrap-only output (no spaceAroundOperators)
let output = """
NationalAeronauticsAndSpaceAdministration
::RocketEngine
"""
// all-rules output (spaceAroundOperators adds space after ::)
let output2 = """
NationalAeronauticsAndSpaceAdministration
:: RocketEngine
"""
let options = FormatOptions(maxWidth: 50)
testFormatting(for: input, [output, output2], rules: [.wrap], options: options)
testFormatting(for: input, output, rule: .wrap, options: options)
}
func testWrapDoubleColonWithSpacesBreaksBeforeOperator() {
@@ -861,7 +855,11 @@ final class WrapTests: XCTestCase {
NationalAeronauticsAndSpaceAdministration
:: RocketEngine
"""
let output2 = """
NationalAeronauticsAndSpaceAdministration
::RocketEngine
"""
let options = FormatOptions(maxWidth: 50)
testFormatting(for: input, output, rule: .wrap, options: options)
testFormatting(for: input, [output, output2], rules: [.wrap], options: options)
}
}