Skip to content

Commit

Permalink
Move rule examples below implementation closure (#1832)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored and nicklockwood committed Sep 7, 2024
1 parent ebcf01f commit 8d6e356
Show file tree
Hide file tree
Showing 98 changed files with 1,798 additions and 1,701 deletions.
6 changes: 3 additions & 3 deletions Sources/FormatRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public final class FormatRule: Equatable, Comparable, CustomStringConvertible {
}

init(help: String,
examples: String? = nil,
deprecationMessage: String? = nil,
runOnceOnly: Bool = false,
disabledByDefault: Bool = false,
orderAfter: [FormatRule] = [],
options: [String] = [],
sharedOptions: [String] = [],
_ fn: @escaping (Formatter) -> Void)
_ fn: @escaping (Formatter) -> Void,
examples: (() -> String)? = nil)
{
self.fn = fn
self.help = help
Expand All @@ -73,7 +73,7 @@ public final class FormatRule: Equatable, Comparable, CustomStringConvertible {
self.options = options
self.sharedOptions = sharedOptions
self.deprecationMessage = deprecationMessage
self.examples = examples
self.examples = examples?()
}

public func apply(with formatter: Formatter) {
Expand Down
27 changes: 14 additions & 13 deletions Sources/Rules/Acronyms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ import Foundation
public extension FormatRule {
static let acronyms = FormatRule(
help: "Capitalize acronyms when the first character is capitalized.",
examples: """
```diff
- let destinationUrl: URL
- let urlRouter: UrlRouter
- let screenId: String
- let entityUuid: UUID
+ let destinationURL: URL
+ let urlRouter: URLRouter
+ let screenID: String
+ let entityUUID: UUID
```
""",
disabledByDefault: true,
options: ["acronyms"]
) { formatter in
Expand Down Expand Up @@ -85,5 +72,19 @@ public extension FormatRule {
formatter.replaceToken(at: index, with: updatedToken)
}
}
} examples: {
"""
```diff
- let destinationUrl: URL
- let urlRouter: UrlRouter
- let screenId: String
- let entityUuid: UUID
+ let destinationURL: URL
+ let urlRouter: URLRouter
+ let screenID: String
+ let entityUUID: UUID
```
"""
}
}
45 changes: 23 additions & 22 deletions Sources/Rules/AndOperator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,7 @@ import Foundation
public extension FormatRule {
/// Replace the `&&` operator with `,` where applicable
static let andOperator = FormatRule(
help: "Prefer comma over `&&` in `if`, `guard` or `while` conditions.",
examples: """
```diff
- if true && true {
+ if true, true {
```
```diff
- guard true && true else {
+ guard true, true else {
```
```diff
- if functionReturnsBool() && true {
+ if functionReturnsBool(), true {
```
```diff
- if functionReturnsBool() && variable {
+ if functionReturnsBool(), variable {
```
"""
help: "Prefer comma over `&&` in `if`, `guard` or `while` conditions."
) { formatter in
formatter.forEachToken { i, token in
switch token {
Expand Down Expand Up @@ -102,5 +81,27 @@ public extension FormatRule {
index += 1
}
}
} examples: {
"""
```diff
- if true && true {
+ if true, true {
```
```diff
- guard true && true else {
+ guard true, true else {
```
```diff
- if functionReturnsBool() && true {
+ if functionReturnsBool(), true {
```
```diff
- if functionReturnsBool() && variable {
+ if functionReturnsBool(), variable {
```
"""
}
}
23 changes: 12 additions & 11 deletions Sources/Rules/AnyObjectProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ import Foundation
public extension FormatRule {
/// Prefer `AnyObject` over `class` for class-based protocols
static let anyObjectProtocol = FormatRule(
help: "Prefer `AnyObject` over `class` in protocol definitions.",
examples: """
```diff
- protocol Foo: class {}
+ protocol Foo: AnyObject {}
```
**NOTE:** The guideline to use `AnyObject` instead of `class` was only
introduced in Swift 4.1, so the `anyObjectProtocol` rule is disabled unless the
swift version is set to 4.1 or above.
"""
help: "Prefer `AnyObject` over `class` in protocol definitions."
) { formatter in
formatter.forEach(.keyword("protocol")) { i, _ in
guard formatter.options.swiftVersion >= "4.1",
Expand All @@ -37,5 +27,16 @@ public extension FormatRule {
}
formatter.replaceToken(at: classIndex, with: .identifier("AnyObject"))
}
} examples: {
"""
```diff
- protocol Foo: class {}
+ protocol Foo: AnyObject {}
```
**NOTE:** The guideline to use `AnyObject` instead of `class` was only
introduced in Swift 4.1, so the `anyObjectProtocol` rule is disabled unless the
swift version is set to 4.1 or above.
"""
}
}
33 changes: 17 additions & 16 deletions Sources/Rules/AssertionFailures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,6 @@ public extension FormatRule {
help: """
Changes all instances of assert(false, ...) to assertionFailure(...)
and precondition(false, ...) to preconditionFailure(...).
""",
examples: """
```diff
- assert(false)
+ assertionFailure()
```
```diff
- assert(false, "message", 2, 1)
+ assertionFailure("message", 2, 1)
```
```diff
- precondition(false, "message", 2, 1)
+ preconditionFailure("message", 2, 1)
```
"""
) { formatter in
formatter.forEachToken { i, token in
Expand All @@ -55,5 +39,22 @@ public extension FormatRule {
break
}
}
} examples: {
"""
```diff
- assert(false)
+ assertionFailure()
```
```diff
- assert(false, "message", 2, 1)
+ assertionFailure("message", 2, 1)
```
```diff
- precondition(false, "message", 2, 1)
+ preconditionFailure("message", 2, 1)
```
"""
}
}
23 changes: 12 additions & 11 deletions Sources/Rules/BlankLineAfterImports.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,6 @@ public extension FormatRule {
/// Insert blank line after import statements
static let blankLineAfterImports = FormatRule(
help: "Insert blank line after import statements.",
examples: """
```diff
import A
import B
@testable import D
+
class Foo {
// foo
}
```
""",
sharedOptions: ["linebreaks"]
) { formatter in
formatter.forEach(.keyword("import")) { currentImportIndex, _ in
Expand All @@ -45,5 +34,17 @@ public extension FormatRule {
formatter.insertLinebreak(at: endOfLine)
}
}
} examples: {
"""
```diff
import A
import B
@testable import D
+
class Foo {
// foo
}
```
"""
}
}
45 changes: 23 additions & 22 deletions Sources/Rules/BlankLineAfterSwitchCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,6 @@ public extension FormatRule {
Insert a blank line after multiline switch cases (excluding the last case,
which is followed by a closing brace).
""",
examples: #"""
```diff
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
navigationComputer.destination = targetedDestination
await warpDrive.spinUp()
warpDrive.activate()
+
case let .scanPlanet(planet):
scanner.target = planet
scanner.scanAtmosphere()
scanner.scanBiosphere()
scanner.scanForArticialLife()
+
case .handleIncomingEnergyBlast:
await energyShields.prepare()
energyShields.engage()
}
}
```
"""#,
disabledByDefault: true,
orderAfter: [.redundantBreak]
) { formatter in
Expand All @@ -61,5 +39,28 @@ public extension FormatRule {
}
}
}
} examples: {
#"""
```diff
func handle(_ action: SpaceshipAction) {
switch action {
case .engageWarpDrive:
navigationComputer.destination = targetedDestination
await warpDrive.spinUp()
warpDrive.activate()
+
case let .scanPlanet(planet):
scanner.target = planet
scanner.scanAtmosphere()
scanner.scanBiosphere()
scanner.scanForArticialLife()
+
case .handleIncomingEnergyBlast:
await energyShields.prepare()
energyShields.engage()
}
}
```
"""#
}
}
43 changes: 22 additions & 21 deletions Sources/Rules/BlankLinesAroundMark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@ public extension FormatRule {
/// Adds a blank line around MARK: comments
static let blankLinesAroundMark = FormatRule(
help: "Insert blank line before and after `MARK:` comments.",
examples: """
```diff
func foo() {
// foo
}
// MARK: bar
func bar() {
// bar
}
func foo() {
// foo
}
+
// MARK: bar
+
func bar() {
// bar
}
```
""",
options: ["lineaftermarks"],
sharedOptions: ["linebreaks"]
) { formatter in
Expand All @@ -55,5 +34,27 @@ public extension FormatRule {
formatter.insertLinebreak(at: lastIndex)
}
}
} examples: {
"""
```diff
func foo() {
// foo
}
// MARK: bar
func bar() {
// bar
}
func foo() {
// foo
}
+
// MARK: bar
+
func bar() {
// bar
}
```
"""
}
}
Loading

0 comments on commit 8d6e356

Please sign in to comment.