Skip to content

Commit

Permalink
Increase strictness of grouped extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Nov 13, 2020
1 parent 7e167a4 commit 8fa0e78
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Sources/Rules.swift
Expand Up @@ -5125,9 +5125,28 @@ public struct _FormatRules {
// extension Foo { } // This extension is "grouped" with its extending type
// extension String { } // This extension is standalone (not grouped with any type)
//
let isGroupedWithExtendingType = declarations[..<index].contains(where: {
let isGroupedWithExtendingType: Bool
if let indexOfExtendingType = declarations[..<index].lastIndex(where: {
$0.name == typeName && ["class", "enum", "protocol", "struct", "typealias"].contains($0.keyword)
})
}) {
let declarationsBetweenTypeAndExtension = declarations[indexOfExtendingType + 1 ..< index]
isGroupedWithExtendingType = declarationsBetweenTypeAndExtension.allSatisfy {
// Only treat the type and its extension as grouped if there aren't any other
// types or type-like declaraitons between them
if ["class", "enum", "protocol", "struct", "typealias"].contains($0.keyword) {
return false
}

// Extensions extending other types also break the grouping
if $0.keyword == "extension", $0.name != declaration.name {
return false
}

return true
}
} else {
isGroupedWithExtendingType = false
}

if isGroupedWithExtendingType {
commentTemplate = "// \(formatter.options.groupedExtensionMarkComment)"
Expand Down
54 changes: 54 additions & 0 deletions Tests/RulesTests+Organization.swift
Expand Up @@ -2191,4 +2191,58 @@ extension RulesTests {

testFormatting(for: input, rule: FormatRules.markTypes)
}

func testDoesntUseGroupedMarkTemplateWhenSeparatedByOtherType() {
let input = """
// MARK: - MyComponent
class MyComponent {}
// MARK: - MyComponentContent
struct MyComponentContent {}
// MARK: - MyComponent + ContentConfigurableView
extension MyComponent: ContentConfigurableView {}
"""

testFormatting(for: input, rule: FormatRules.markTypes)
}

func testUsesGroupedMarkTemplateWhenSeparatedByExtensionOfSameType() {
let input = """
// MARK: - MyComponent
class MyComponent {}
// MARK: Equatable
extension MyComponent: Equatable {}
// MARK: ContentConfigurableView
extension MyComponent: ContentConfigurableView {}
"""

testFormatting(for: input, rule: FormatRules.markTypes)
}

func testDoesntUseGroupedMarkTemplateWhenSeparatedByExtensionOfOtherType() {
let input = """
// MARK: - MyComponent
class MyComponent {}
// MARK: - OtherComponent + Equatable
extension OtherComponent: Equatable {}
// MARK: - MyComponent + ContentConfigurableView
extension MyComponent: ContentConfigurableView {}
"""

testFormatting(for: input, rule: FormatRules.markTypes)
}
}
3 changes: 3 additions & 0 deletions Tests/XCTestManifests.swift
Expand Up @@ -688,6 +688,8 @@ extension RulesTests {
("testDoesntUpdateExtensionVisibilityWithInternalDeclarations", testDoesntUpdateExtensionVisibilityWithInternalDeclarations),
("testDoesntUpdateExtensionVisibilityWithoutMajorityBodyVisibility", testDoesntUpdateExtensionVisibilityWithoutMajorityBodyVisibility),
("testDoesntUpdatesExtensionThatHasLowerACLThanBodyDeclarations", testDoesntUpdatesExtensionThatHasLowerACLThanBodyDeclarations),
("testDoesntUseGroupedMarkTemplateWhenSeparatedByExtensionOfOtherType", testDoesntUseGroupedMarkTemplateWhenSeparatedByExtensionOfOtherType),
("testDoesntUseGroupedMarkTemplateWhenSeparatedByOtherType", testDoesntUseGroupedMarkTemplateWhenSeparatedByOtherType),
("testDoesntWrapReturnOnSingleLineFunctionDeclaration", testDoesntWrapReturnOnSingleLineFunctionDeclaration),
("testDontChangePrivateExtensionToFileprivate", testDontChangePrivateExtensionToFileprivate),
("testDontCorruptPartialFragment", testDontCorruptPartialFragment),
Expand Down Expand Up @@ -2005,6 +2007,7 @@ extension RulesTests {
("testUppercaseGroupedHexExponent", testUppercaseGroupedHexExponent),
("testUppercaseHexExponent", testUppercaseHexExponent),
("testUppercaseLiteralConvertedToLower", testUppercaseLiteralConvertedToLower),
("testUsesGroupedMarkTemplateWhenSeparatedByExtensionOfSameType", testUsesGroupedMarkTemplateWhenSeparatedByExtensionOfSameType),
("testUseVoidOptionFalse", testUseVoidOptionFalse),
("testVarAttributeIsNotWrapped", testVarAttributeIsNotWrapped),
("testVarModifiersCorrected", testVarModifiersCorrected),
Expand Down

0 comments on commit 8fa0e78

Please sign in to comment.