diff --git a/Rules.md b/Rules.md index 35fa10b2..b18331a2 100644 --- a/Rules.md +++ b/Rules.md @@ -26,7 +26,6 @@ * [numberFormatting](#numberFormatting) * [organizeDeclarations](#organizeDeclarations) * [preferKeyPath](#preferKeyPath) -* [ranges *(deprecated)*](#ranges) * [redundantBackticks](#redundantBackticks) * [redundantBreak](#redundantBreak) * [redundantExtensionACL](#redundantExtensionACL) @@ -789,12 +788,6 @@ Convert trivial `map { $0.foo }` closures to keyPath-based syntax.
-## ranges - -Add or remove space around range operators. - -*Note: ranges rule is deprecated. Use spaceAroundOperators instead.* - ## redundantBackticks Remove redundant backticks around identifiers. @@ -1331,6 +1324,7 @@ Option | Description --- | --- `--operatorfunc` | Spacing for operator funcs: "spaced" (default) or "no-space" `--nospaceoperators` | Comma-delimited list of operators without surrounding space +`--ranges` | Spacing for ranges: "spaced" (default) or "no-space"
Examples diff --git a/Sources/Examples.swift b/Sources/Examples.swift index df950c58..4fb5be38 100644 --- a/Sources/Examples.swift +++ b/Sources/Examples.swift @@ -382,18 +382,6 @@ private struct Examples { ``` """ - let ranges = """ - ```diff - - for i in 0..<5 {} - + for i in 0 ..< 5 {} - ``` - - ```diff - - if (0...5).contains(i) {} - + if (0 ... 5).contains(i) {} - ``` - """ - let redundantBackticks = """ ```diff - let `infix` = bar diff --git a/Sources/Options.swift b/Sources/Options.swift index 6247c5b6..e6697668 100644 --- a/Sources/Options.swift +++ b/Sources/Options.swift @@ -266,6 +266,7 @@ public struct FormatOptions: CustomStringConvertible { public var indent: String public var linebreak: String public var allowInlineSemicolons: Bool + public var spaceAroundRangeOperators: Bool public var spaceAroundOperatorDeclarations: Bool public var useVoid: Bool public var indentCase: Bool @@ -318,7 +319,6 @@ public struct FormatOptions: CustomStringConvertible { // Deprecated public var indentComments: Bool - public var spaceAroundRangeOperators: Bool // Doesn't really belong here, but hard to put elsewhere public var fragment: Bool diff --git a/Sources/OptionsDescriptor.swift b/Sources/OptionsDescriptor.swift index 6dc9b6b6..9ac3c8d2 100644 --- a/Sources/OptionsDescriptor.swift +++ b/Sources/OptionsDescriptor.swift @@ -672,6 +672,15 @@ extension FormatOptions.Descriptor { } } ) + static let spaceAroundRangeOperators = FormatOptions.Descriptor( + argumentName: "ranges", + propertyName: "spaceAroundRangeOperators", + displayName: "Ranges", + help: "Spacing for ranges: \"spaced\" (default) or \"no-space\"", + keyPath: \.spaceAroundRangeOperators, + trueValues: ["spaced", "space", "spaces"], + falseValues: ["no-space", "nospace"] + ) static let noWrapOperators = FormatOptions.Descriptor( argumentName: "nowrapoperators", propertyName: "noWrapOperators", @@ -820,7 +829,6 @@ extension FormatOptions.Descriptor { hexLiterals.argumentName: "--hexliterals option is deprecated. Use --hexliteralcase instead.", wrapElements.argumentName: "--wrapelements option is deprecated. Use --wrapcollections instead.", experimentalRules.argumentName: "--experimentalRules option is deprecated. Use --enable to opt-in to rules individually.", - spaceAroundRangeOperators.argumentName: "--ranges option is deprecated. Use --nospaceoperators instead.", specifierOrder.argumentName: "--specifierorder option is deprecated. Use --modifierorder instead.", ] @@ -886,15 +894,6 @@ extension FormatOptions.Descriptor { trueValues: ["enabled", "true"], falseValues: ["disabled", "false"] ) - static let spaceAroundRangeOperators = FormatOptions.Descriptor( - argumentName: "ranges", - propertyName: "spaceAroundRangeOperators", - displayName: "Ranges", - help: "Spacing for ranges: \"spaced\" (default) or \"no-space\"", - keyPath: \.spaceAroundRangeOperators, - trueValues: ["spaced", "space", "spaces"], - falseValues: ["no-space", "nospace"] - ) static let specifierOrder = FormatOptions.Descriptor( argumentName: "specifierorder", propertyName: "modifierOrder", diff --git a/Sources/Rules.swift b/Sources/Rules.swift index 1a89f77d..de12cffb 100644 --- a/Sources/Rules.swift +++ b/Sources/Rules.swift @@ -79,7 +79,6 @@ public final class FormatRule: Equatable, Comparable { } static let deprecatedMessage = [ - "ranges": "ranges rule is deprecated. Use spaceAroundOperators instead.", "specifiers": "specifiers rule is deprecated. Use modifierOrder instead.", ] } @@ -448,7 +447,7 @@ public struct _FormatRules { /// preceded by a space, unless it appears at the beginning of a line. public let spaceAroundOperators = FormatRule( help: "Add or remove space around operators or delimiters.", - options: ["operatorfunc", "nospaceoperators"] + options: ["operatorfunc", "nospaceoperators", "ranges"] ) { formatter in formatter.forEachToken { i, token in switch token { @@ -644,38 +643,6 @@ public struct _FormatRules { } } - /// Deprecated - public let ranges = FormatRule( - help: "Add or remove space around range operators.", - options: ["ranges"], - sharedOptions: ["nospaceoperators"] - ) { formatter in - formatter.forEach(.rangeOperator) { i, token in - guard case let .operator(name, .infix) = token else { return } - if !formatter.options.spaceAroundRangeOperators { - if formatter.token(at: i + 1)?.isSpace == true, - formatter.token(at: i - 1)?.isSpace == true, - let nextToken = formatter.next(.nonSpace, after: i), - !nextToken.isCommentOrLinebreak, !nextToken.isOperator(ofType: .prefix), - let prevToken = formatter.last(.nonSpace, before: i), - !prevToken.isCommentOrLinebreak, !prevToken.isOperator(ofType: .postfix) - { - formatter.removeToken(at: i + 1) - formatter.removeToken(at: i - 1) - } - } else if formatter.options.spaceAroundRangeOperators, - !formatter.options.noSpaceOperators.contains(name) - { - if formatter.token(at: i + 1)?.isSpaceOrLinebreak == false { - formatter.insert(.space(" "), at: i + 1) - } - if formatter.token(at: i - 1)?.isSpaceOrLinebreak == false { - formatter.insert(.space(" "), at: i) - } - } - } - } - /// Removes explicit type declarations from initialization declarations public let redundantType = FormatRule( help: "Remove redundant type from variable declarations." diff --git a/Tests/RulesTests.swift b/Tests/RulesTests.swift index d5a62d90..dd328c67 100644 --- a/Tests/RulesTests.swift +++ b/Tests/RulesTests.swift @@ -871,26 +871,112 @@ class RulesTests: XCTestCase { func testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved() { let input = "let range = 0 +\n4" let options = FormatOptions(noSpaceOperators: ["+"]) - testFormatting(for: input, rule: FormatRules.ranges, options: options, exclude: ["indent"]) + testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options, + exclude: ["indent"]) } func testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved2() { let input = "let range = 0\n+ 4" let options = FormatOptions(noSpaceOperators: ["+"]) - testFormatting(for: input, rule: FormatRules.ranges, options: options, exclude: ["indent"]) + testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options, + exclude: ["indent"]) } func testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved() { let input = "let range = 0 + \n4" let options = FormatOptions(noSpaceOperators: ["+"]) - testFormatting(for: input, rule: FormatRules.ranges, options: options, + testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options, exclude: ["indent", "trailingSpace"]) } func testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved2() { let input = "let range = 0\n + 4" let options = FormatOptions(noSpaceOperators: ["+"]) - testFormatting(for: input, rule: FormatRules.ranges, options: options, exclude: ["indent"]) + testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options, + exclude: ["indent"]) + } + + // spaceAroundRangeOperators = false + + func testNoSpaceAroundRangeOperatorsWithCustomOptions() { + let input = "foo ..< bar" + let output = "foo..