Skip to content

Commit

Permalink
Update organizeDeclarations type mode to not treat properties with di…
Browse files Browse the repository at this point in the history
…dSet as computed properties (#1952)
  • Loading branch information
calda authored Dec 26, 2024
1 parent 112174a commit 01d7248
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ Default value for `--typeorder` when using `--organizationmode visibility`:
`nestedType, staticProperty, staticPropertyWithBody, classPropertyWithBody, overriddenProperty, swiftUIPropertyWrapper, instanceProperty, instancePropertyWithBody, swiftUIProperty, swiftUIMethod, overriddenMethod, staticMethod, classMethod, instanceMethod`

Default value for `--typeorder` when using `--organizationmode type`:
`beforeMarks, nestedType, staticProperty, staticPropertyWithBody, classPropertyWithBody, overriddenProperty, swiftUIPropertyWrapper, instanceProperty, instancePropertyWithBody, instanceLifecycle, swiftUIProperty, swiftUIMethod, overriddenMethod, staticMethod, classMethod, instanceMethod`
`beforeMarks, nestedType, staticProperty, staticPropertyWithBody, classPropertyWithBody, overriddenProperty, swiftUIPropertyWrapper, instanceProperty, computedProperty, instanceLifecycle, swiftUIProperty, swiftUIMethod, overriddenMethod, staticMethod, classMethod, instanceMethod`

**NOTE:** The follow declaration types must be included in either `--typeorder` or `--visibilityorder`:
`beforeMarks, nestedType, instanceLifecycle, instanceProperty, instanceMethod`
Expand Down
59 changes: 49 additions & 10 deletions Sources/DeclarationV1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ enum DeclarationType: String, CaseIterable {
case swiftUIPropertyWrapper
case instanceProperty
case instancePropertyWithBody
case computedProperty
case instanceLifecycle
case swiftUIProperty
case swiftUIMethod
Expand Down Expand Up @@ -462,6 +463,8 @@ enum DeclarationType: String, CaseIterable {
case .instanceProperty:
return "Properties"
case .instancePropertyWithBody:
return "Properties with Bodies"
case .computedProperty:
return "Computed Properties"
case .staticMethod:
return "Static Functions"
Expand All @@ -485,13 +488,42 @@ enum DeclarationType: String, CaseIterable {
static func defaultOrdering(for mode: DeclarationOrganizationMode) -> [DeclarationType] {
switch mode {
case .type:
return allCases
return [
.beforeMarks,
.nestedType,
.staticProperty,
.staticPropertyWithBody,
.classPropertyWithBody,
.overriddenProperty,
.swiftUIPropertyWrapper,
.instanceProperty,
.computedProperty,
.instanceLifecycle,
.swiftUIProperty,
.swiftUIMethod,
.overriddenMethod,
.staticMethod,
.classMethod,
.instanceMethod,
]

case .visibility:
return allCases.filter { type in
// Exclude beforeMarks and instanceLifecycle, since by default
// these are instead treated as top-level categories
type != .beforeMarks && type != .instanceLifecycle
}
return [
nestedType,
staticProperty,
staticPropertyWithBody,
classPropertyWithBody,
overriddenProperty,
swiftUIPropertyWrapper,
instanceProperty,
instancePropertyWithBody,
swiftUIProperty,
swiftUIMethod,
overriddenMethod,
staticMethod,
classMethod,
instanceMethod,
]
}
}
}
Expand Down Expand Up @@ -549,11 +581,15 @@ extension Declaration {
before: declarationTypeTokenIndex
) != nil

let isDeclarationWithBody = declarationParser.startOfPropertyBody(
let isPropertyWithBody = declarationParser.startOfPropertyBody(
at: declarationTypeTokenIndex,
endOfPropertyIndex: declarationParser.tokens.count
) != nil

// A property with a body is either a computed property,
// or a stored property with a willSet or didSet.
let isComputedProperty = isPropertyWithBody && !isStoredProperty

let isViewDeclaration: Bool = {
guard let someKeywordIndex = declarationParser.index(
of: .identifier("some"), after: declarationTypeTokenIndex
Expand All @@ -575,7 +611,7 @@ extension Declaration {
if isOverriddenDeclaration, availableTypes.contains(.overriddenProperty) {
return .overriddenProperty
}
if isStaticDeclaration, isDeclarationWithBody, availableTypes.contains(.staticPropertyWithBody) {
if isStaticDeclaration, isPropertyWithBody, availableTypes.contains(.staticPropertyWithBody) {
return .staticPropertyWithBody
}
if isStaticDeclaration, availableTypes.contains(.staticProperty) {
Expand All @@ -590,10 +626,13 @@ extension Declaration {
if isViewDeclaration, availableTypes.contains(.swiftUIProperty) {
return .swiftUIProperty
}
if !isDeclarationWithBody, isSwiftUIPropertyWrapper, availableTypes.contains(.swiftUIPropertyWrapper) {
if !isPropertyWithBody, isSwiftUIPropertyWrapper, availableTypes.contains(.swiftUIPropertyWrapper) {
return .swiftUIPropertyWrapper
}
if isDeclarationWithBody, availableTypes.contains(.instancePropertyWithBody) {
if isComputedProperty, availableTypes.contains(.computedProperty) {
return .computedProperty
}
if isPropertyWithBody, availableTypes.contains(.instancePropertyWithBody) {
return .instancePropertyWithBody
}

Expand Down
8 changes: 4 additions & 4 deletions Tests/Rules/OrganizeDeclarationsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1214,16 +1214,16 @@ class OrganizeDeclarationsTests: XCTestCase {
"closure body"
}()
var d2: CGFloat = 3.141592653589 {
didSet {}
}
// MARK: Computed Properties
var d1: CGFloat {
3.141592653589
}
var d2: CGFloat = 3.141592653589 {
didSet {}
}
// MARK: Lifecycle
init(a: Int) {
Expand Down

0 comments on commit 01d7248

Please sign in to comment.