From 22c2bb689ea1dfec61b4cb060cf9eeccd758e7b9 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sat, 21 Feb 2026 10:15:04 -0800 Subject: [PATCH] Remove spaces around `::` module selection operator in `spaceAroundOperators` (#2402) Co-authored-by: calda <1811727+calda@users.noreply.github.com> --- Sources/Rules/SpaceAroundOperators.swift | 7 ++++++ Tests/Rules/SpaceAroundOperatorsTests.swift | 24 +++++++++++++++++++++ Tests/Rules/WrapTests.swift | 14 ++++++------ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Sources/Rules/SpaceAroundOperators.swift b/Sources/Rules/SpaceAroundOperators.swift index 7cbbe87e..7934f40f 100644 --- a/Sources/Rules/SpaceAroundOperators.swift +++ b/Sources/Rules/SpaceAroundOperators.swift @@ -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) diff --git a/Tests/Rules/SpaceAroundOperatorsTests.swift b/Tests/Rules/SpaceAroundOperatorsTests.swift index 9ae4dc19..187159f4 100644 --- a/Tests/Rules/SpaceAroundOperatorsTests.swift +++ b/Tests/Rules/SpaceAroundOperatorsTests.swift @@ -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) + } } diff --git a/Tests/Rules/WrapTests.swift b/Tests/Rules/WrapTests.swift index 046d8443..69d403c8 100644 --- a/Tests/Rules/WrapTests.swift +++ b/Tests/Rules/WrapTests.swift @@ -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) } }