Skip to content

Commit

Permalink
Add organizeDeclarations rule (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Aug 16, 2020
1 parent 628ff49 commit 535afe4
Show file tree
Hide file tree
Showing 11 changed files with 1,625 additions and 3 deletions.
63 changes: 63 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [multilineEnumCases](#multilineEnumCases)
* [multilineSwitchCases](#multilineSwitchCases)
* [numberFormatting](#numberFormatting)
* [organizeDeclarations](#organizeDeclarations)
* [preferKeyPath](#preferKeyPath)
* [ranges *(deprecated)*](#ranges)
* [redundantBackticks](#redundantBackticks)
Expand Down Expand Up @@ -654,6 +655,68 @@ Option | Description
</details>
<br/>

## organizeDeclarations

Organizes declarations within class, struct, and enum bodies.

Option | Description
--- | ---
`--categorymark` | Template for category mark comments (defaults to `MARK: %c`)
`--beforemarks` | Declarations placed before first mark (e.g. `typealias,struct`)
`--lifecycle` | Names of additional Lifecycle methods (e.g. `viewDidLoad`)
`--structthreshold` | Minimum line count to organize struct body (defaults to `nil`)
`--classthreshold` | Minimum line count to organize class body (defaults to `nil`)
`--enumthreshold` | Minimum line count to organize enum body (defaults to `nil`)

<details>
<summary>Examples</summary>

```diff
public class Foo {
- public func c() -> String {}
-
- public let a: Int = 1
- private let g: Int = 2
- let e: Int = 2
- public let b: Int = 3
-
- public func d() {}
- func f() {}
- init() {}
- deinit() {}
}

public class Foo {
+
+ // MARK: Lifecycle
+
+ init() {}
+ deinit() {}
+
+ // MARK: Public
+
+ public let a: Int = 1
+ public let b: Int = 3
+
+ public func c() -> String {}
+ public func d() {}
+
+ // MARK: Internal
+
+ let e: Int = 2
+
+ func f() {}
+
+ // MARK: Private
+
+ private let g: Int = 2
+
}
```

</details>
<br/>

## preferKeyPath

Convert trivial `map { $0.foo }` closures to keyPath-based syntax.
Expand Down
45 changes: 45 additions & 0 deletions Sources/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1128,4 +1128,49 @@ private struct Examples {
+ let barArray = fooArray.map(\\.bar)
```
"""

let organizeDeclarations = """
```diff
public class Foo {
- public func c() -> String {}
-
- public let a: Int = 1
- private let g: Int = 2
- let e: Int = 2
- public let b: Int = 3
-
- public func d() {}
- func f() {}
- init() {}
- deinit() {}
}
public class Foo {
+
+ // MARK: Lifecycle
+
+ init() {}
+ deinit() {}
+
+ // MARK: Public
+
+ public let a: Int = 1
+ public let b: Int = 3
+
+ public func c() -> String {}
+ public func d() {}
+
+ // MARK: Internal
+
+ let e: Int = 2
+
+ func f() {}
+
+ // MARK: Private
+
+ private let g: Int = 2
+
}
```
"""
}
18 changes: 18 additions & 0 deletions Sources/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ public struct FormatOptions: CustomStringConvertible {
public var funcAttributes: AttributeMode
public var typeAttributes: AttributeMode
public var varAttributes: AttributeMode
public var categoryMarkComment: String
public var beforeMarks: Set<String>
public var lifecycleMethods: Set<String>
public var organizeClassThreshold: Int?
public var organizeStructThreshold: Int?
public var organizeEnumThreshold: Int?
public var yodaSwap: YodaMode

// Deprecated
Expand Down Expand Up @@ -369,6 +375,12 @@ public struct FormatOptions: CustomStringConvertible {
funcAttributes: AttributeMode = .preserve,
typeAttributes: AttributeMode = .preserve,
varAttributes: AttributeMode = .preserve,
categoryMarkComment: String = "MARK: %c",
beforeMarks: Set<String> = [],
lifecycleMethods: Set<String> = [],
organizeClassThreshold: Int? = nil,
organizeStructThreshold: Int? = nil,
organizeEnumThreshold: Int? = nil,
yodaSwap: YodaMode = .always,
// Doesn't really belong here, but hard to put elsewhere
fragment: Bool = false,
Expand Down Expand Up @@ -423,6 +435,12 @@ public struct FormatOptions: CustomStringConvertible {
self.funcAttributes = funcAttributes
self.typeAttributes = typeAttributes
self.varAttributes = varAttributes
self.categoryMarkComment = categoryMarkComment
self.beforeMarks = beforeMarks
self.lifecycleMethods = lifecycleMethods
self.organizeClassThreshold = organizeClassThreshold
self.organizeStructThreshold = organizeStructThreshold
self.organizeEnumThreshold = organizeEnumThreshold
self.yodaSwap = yodaSwap
// Doesn't really belong here, but hard to put elsewhere
self.fragment = fragment
Expand Down
74 changes: 74 additions & 0 deletions Sources/OptionsDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ extension FormatOptions.Descriptor {
funcAttributes,
typeAttributes,
varAttributes,
categoryMark,
beforeMarks,
lifecycleMethods,
organizeClassThreshold,
organizeStructThreshold,
organizeEnumThreshold,
yodaSwap,

// Deprecated
Expand Down Expand Up @@ -684,6 +690,74 @@ extension FormatOptions.Descriptor {
keyPath: \.shortOptionals,
options: ["always", "except-properties"]
)
static let categoryMark = FormatOptions.Descriptor(
argumentName: "categorymark",
propertyName: "categoryMarkComment",
displayName: "Category Mark Comment",
help: "Template for category mark comments (defaults to `MARK: %c`)",
keyPath: \.categoryMarkComment,
fromArgument: { $0 },
toArgument: { $0 }
)
static let beforeMarks = FormatOptions.Descriptor(
argumentName: "beforemarks",
propertyName: "beforeMarks",
displayName: "Before Marks",
help: "Declarations placed before first mark (e.g. `typealias,struct`)",
keyPath: \.beforeMarks
)
static let lifecycleMethods = FormatOptions.Descriptor(
argumentName: "lifecycle",
propertyName: "lifecycleMethods",
displayName: "Lifecycle Methods",
help: "Names of additional Lifecycle methods (e.g. `viewDidLoad`)",
keyPath: \.lifecycleMethods
)
static let organizeStructThreshold = FormatOptions.Descriptor(
argumentName: "structthreshold",
propertyName: "organizeStructThreshold",
displayName: "Organize Struct Threshold",
help: "Minimum line count to organize struct body (defaults to `nil`)",
keyPath: \.organizeStructThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
)
static let organizeClassThreshold = FormatOptions.Descriptor(
argumentName: "classthreshold",
propertyName: "organizeClassThreshold",
displayName: "Organize Class Threshold",
help: "Minimum line count to organize class body (defaults to `nil`)",
keyPath: \.organizeClassThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
)
static let organizeEnumThreshold = FormatOptions.Descriptor(
argumentName: "enumthreshold",
propertyName: "organizeEnumThreshold",
displayName: "Organize Enum Threshold",
help: "Minimum line count to organize enum body (defaults to `nil`)",
keyPath: \.organizeEnumThreshold,
fromArgument: { .some(Int($0)) },
toArgument: {
if let lineCount = $0 {
return "\(lineCount)"
} else {
return ""
}
}
)

// MARK: - Internal

Expand Down
Loading

0 comments on commit 535afe4

Please sign in to comment.