Remove ranges rule & undeprecate ranges option

This commit is contained in:
Nick Lockwood
2020-08-28 10:35:36 +01:00
parent b4c048205c
commit 3003170c10
7 changed files with 102 additions and 219 deletions
+1 -7
View File
@@ -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.
</details>
<br/>
## 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"
<details>
<summary>Examples</summary>
-12
View File
@@ -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
+1 -1
View File
@@ -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
+9 -10
View File
@@ -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",
+1 -34
View File
@@ -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."
+90 -146
View File
@@ -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..<bar"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, output, rule: FormatRules.spaceAroundOperators, options: options)
}
func testSpaceNotRemovedBeforeLeadingRangeOperatorWithSpaceAroundRangeOperatorsFalse() {
let input = "let range = ..<foo.endIndex"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options)
}
func testSpaceOnOneSideOfRangeMatchedByCommentNotRemoved() {
let input = "let range = 0 .../* foo */4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceOnOneSideOfRangeMatchedByCommentNotRemoved2() {
let input = "let range = 0/* foo */... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceAroundRangeWithCommentOnOneSideNotRemoved() {
let input = "let range = 0 ... /* foo */4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceAroundRangeWithCommentOnOneSideNotRemoved2() {
let input = "let range = 0/* foo */ ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceOnOneSideOfRangeMatchedByLinebreakNotRemoved() {
let input = "let range = 0 ...\n4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["indent"])
}
func testSpaceOnOneSideOfRangeMatchedByLinebreakNotRemoved2() {
let input = "let range = 0\n... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["indent"])
}
func testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved() {
let input = "let range = 0 ... \n4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["indent", "trailingSpace"])
}
func testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved2() {
let input = "let range = 0\n ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options,
exclude: ["indent"])
}
func testSpaceNotRemovedAroundRangeFollowedByPrefixOperator() {
let input = "let range = 0 ... -4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options)
}
func testSpaceNotRemovedAroundRangePreceededByPostfixOperator() {
let input = "let range = 0>> ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.spaceAroundOperators, options: options)
}
// MARK: - spaceAroundComments
@@ -4650,148 +4736,6 @@ class RulesTests: XCTestCase {
testFormatting(for: input, output, rule: FormatRules.semicolons)
}
// MARK: - ranges
func testSpaceAroundRangeOperatorsWithDefaultOptions() {
let input = "foo..<bar"
let output = "foo ..< bar"
testFormatting(for: input, output, rule: FormatRules.ranges)
}
func testNoSpaceAroundRangeOperatorsWithExplicitNoSpace() {
let input = "foo..<bar"
let options = FormatOptions(noSpaceOperators: ["..<"])
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
// spaceAroundRangeOperators = true
func testNoSpaceAddedAroundVariadic() {
let input = "foo(bar: Int...)"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testNoSpaceAddedAroundVariadicWithComment() {
let input = "foo(bar: Int.../* one or more */)"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["spaceAroundComments", "spaceAroundOperators"])
}
func testNoSpaceAddedAroundVariadicThatIsntLastArg() {
let input = "foo(bar: Int..., baz: Int)"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testNoSpaceAddedAroundSplitLineVariadic() {
let input = "foo(\n bar: Int...\n)"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testNoSpaceAddedAroundTrailingRangeOperator() {
let input = "foo[bar...]"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testNoSpaceAddedBeforeLeadingRangeOperator() {
let input = "foo[...bar]"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testSpaceNotRemovedBeforeLeadingRangeOperator() {
let input = "let range = ..<foo.endIndex"
let options = FormatOptions(spaceAroundRangeOperators: true)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
// spaceAroundRangeOperators = false
func testNoSpaceAroundRangeOperatorsWithCustomOptions() {
let input = "foo ..< bar"
let output = "foo..<bar"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, output, rule: FormatRules.ranges, options: options)
}
func testSpaceNotRemovedBeforeLeadingRangeOperatorWithSpaceAroundRangeOperatorsFalse() {
let input = "let range = ..<foo.endIndex"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testSpaceOnOneSideOfRangeMatchedByCommentNotRemoved() {
let input = "let range = 0 .../* foo */4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceOnOneSideOfRangeMatchedByCommentNotRemoved2() {
let input = "let range = 0/* foo */... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceAroundRangeWithCommentOnOneSideNotRemoved() {
let input = "let range = 0 ... /* foo */4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceAroundRangeWithCommentOnOneSideNotRemoved2() {
let input = "let range = 0/* foo */ ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["spaceAroundComments"])
}
func testSpaceOnOneSideOfRangeMatchedByLinebreakNotRemoved() {
let input = "let range = 0 ...\n4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["indent"])
}
func testSpaceOnOneSideOfRangeMatchedByLinebreakNotRemoved2() {
let input = "let range = 0\n... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["indent"])
}
func testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved() {
let input = "let range = 0 ... \n4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["indent", "trailingSpace"])
}
func testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved2() {
let input = "let range = 0\n ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options,
exclude: ["indent"])
}
func testSpaceNotRemovedAroundRangeFollowedByPrefixOperator() {
let input = "let range = 0 ... -4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
func testSpaceNotRemovedAroundRangePreceededByPostfixOperator() {
let input = "let range = 0>> ... 4"
let options = FormatOptions(spaceAroundRangeOperators: false)
testFormatting(for: input, rule: FormatRules.ranges, options: options)
}
// MARK: - modifierOrder
func testVarModifiersCorrected() {
-9
View File
@@ -1229,12 +1229,6 @@ extension RulesTests {
("testNoReplaceRepeatWhileAnd", testNoReplaceRepeatWhileAnd),
("testNoReplaceWhileCaseLetAnd", testNoReplaceWhileCaseLetAnd),
("testNoSpaceAddedAfterColonInSelector", testNoSpaceAddedAfterColonInSelector),
("testNoSpaceAddedAroundSplitLineVariadic", testNoSpaceAddedAroundSplitLineVariadic),
("testNoSpaceAddedAroundTrailingRangeOperator", testNoSpaceAddedAroundTrailingRangeOperator),
("testNoSpaceAddedAroundVariadic", testNoSpaceAddedAroundVariadic),
("testNoSpaceAddedAroundVariadicThatIsntLastArg", testNoSpaceAddedAroundVariadicThatIsntLastArg),
("testNoSpaceAddedAroundVariadicWithComment", testNoSpaceAddedAroundVariadicWithComment),
("testNoSpaceAddedBeforeLeadingRangeOperator", testNoSpaceAddedBeforeLeadingRangeOperator),
("testNoSpaceAddedBetweenDoublebraces", testNoSpaceAddedBetweenDoublebraces),
("testNoSpaceAddedInOptionalChaining", testNoSpaceAddedInOptionalChaining),
("testNoSpaceAddedInsideEmptybraces", testNoSpaceAddedInsideEmptybraces),
@@ -1256,7 +1250,6 @@ extension RulesTests {
("testNoSpaceAroundMultipleOptionalChaining", testNoSpaceAroundMultipleOptionalChaining),
("testNoSpaceAroundPrefixMinus", testNoSpaceAroundPrefixMinus),
("testNoSpaceAroundRangeOperatorsWithCustomOptions", testNoSpaceAroundRangeOperatorsWithCustomOptions),
("testNoSpaceAroundRangeOperatorsWithExplicitNoSpace", testNoSpaceAroundRangeOperatorsWithExplicitNoSpace),
("testNoSpaceBeforeColon", testNoSpaceBeforeColon),
("testNoSpaceBeforeComma", testNoSpaceBeforeComma),
("testNoSpaceBetweenArrayLiteralAndParen", testNoSpaceBetweenArrayLiteralAndParen),
@@ -1678,7 +1671,6 @@ extension RulesTests {
("testSpaceAroundPlusBeforeHash", testSpaceAroundPlusBeforeHash),
("testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved", testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved),
("testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved2", testSpaceAroundPlusWithLinebreakOnOneSideNotRemoved2),
("testSpaceAroundRangeOperatorsWithDefaultOptions", testSpaceAroundRangeOperatorsWithDefaultOptions),
("testSpaceAroundRangeWithCommentOnOneSideNotRemoved", testSpaceAroundRangeWithCommentOnOneSideNotRemoved),
("testSpaceAroundRangeWithCommentOnOneSideNotRemoved2", testSpaceAroundRangeWithCommentOnOneSideNotRemoved2),
("testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved", testSpaceAroundRangeWithLinebreakOnOneSideNotRemoved),
@@ -1729,7 +1721,6 @@ extension RulesTests {
("testSpaceNotInsertedAfterClosureBeforeUnwrap", testSpaceNotInsertedAfterClosureBeforeUnwrap),
("testSpaceNotRemovedAroundRangeFollowedByPrefixOperator", testSpaceNotRemovedAroundRangeFollowedByPrefixOperator),
("testSpaceNotRemovedAroundRangePreceededByPostfixOperator", testSpaceNotRemovedAroundRangePreceededByPostfixOperator),
("testSpaceNotRemovedBeforeLeadingRangeOperator", testSpaceNotRemovedBeforeLeadingRangeOperator),
("testSpaceNotRemovedBeforeLeadingRangeOperatorWithSpaceAroundRangeOperatorsFalse", testSpaceNotRemovedBeforeLeadingRangeOperatorWithSpaceAroundRangeOperatorsFalse),
("testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved", testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved),
("testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved2", testSpaceOnOneSideOfPlusMatchedByLinebreakNotRemoved2),