Skip to content

Commit

Permalink
realm#2749 Fix file_types_order in extension-only files
Browse files Browse the repository at this point in the history
Prevents extensions being flagged as in the wrong order when a file only contains extensions and nothing else (enums, classes, structs). In this case it's not intuitive that the longest extension be considered the "main" one so the file can be considered as having no "main" type.
  • Loading branch information
samrayner committed May 13, 2019
1 parent af72c06 commit 7b82250
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
[alvarhansen](https://github.com/alvarhansen)
[#2746](https://github.com/realm/SwiftLint/issues/2746)

* Don't trigger `file_types_order` violations in files only containing
extensions.
[Sam Rayner](https://github.com/samrayner)
[#2749](https://github.com/realm/SwiftLint/issues/2749)

## 0.32.0: Wash-N-Fold-N-Reduce

#### Breaking
Expand Down
7 changes: 7 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -7812,6 +7812,13 @@ extension TestViewController: UITableViewDataSource {
}
```

```swift
// Only extensions
extension Foo {}
extension Bar {
}
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
11 changes: 1 addition & 10 deletions Source/SwiftLintFramework/Rules/Style/FileTypesOrderRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,7 @@ public struct FileTypesOrderRule: ConfigurationProviderRule, OptInRule {
return (lhs.bodyLength ?? 0) > (rhs.bodyLength ?? 0)
}

guard let mainTypeSubstructure = substructuresSortedByBodyLength.first else {
let substructuresSortedByBodyLength = dict.substructure.sorted { lhs, rhs in
return (lhs.bodyLength ?? 0) > (rhs.bodyLength ?? 0)
}

// specify substructure with longest body as main type if existent
return substructuresSortedByBodyLength.first
}

// specify class, enum or struct with longest body as main type
return mainTypeSubstructure
return substructuresSortedByBodyLength.first
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ internal struct FileTypesOrderRuleExamples {
"""
]

static let nonTriggeringExamples = [FileTypesOrderRuleExamples.defaultOrderParts.joined(separator: "\n\n")]
static let nonTriggeringExamples = [
FileTypesOrderRuleExamples.defaultOrderParts.joined(separator: "\n\n"),
"""
// Only extensions
extension Foo {}
extension Bar {
}
"""
]

static let triggeringExamples = [
"""
Expand Down

0 comments on commit 7b82250

Please sign in to comment.