Skip to content

Commit

Permalink
SwiftLint: Add custom rule for placing each let on its own line
Browse files Browse the repository at this point in the history
Summary:
This rule will flag when there are 2 let conditions on the same line. For example:

```
if let lhs = lhs, let rhs = rhs {
```

However one limitation is that it is unable to flag boolean conditions since this is difficult to detect via a regex

```
if let lhs = lhs, a == b {
if let lhs = lhs, isBooleanCondition {
```

Reviewed By: samodom

Differential Revision: D33844926

fbshipit-source-id: 4f629131ed22d6d7446c1500bcefb787ec4b7861
  • Loading branch information
jawwad authored and facebook-github-bot committed Jan 28, 2022
1 parent f420c86 commit e329438
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ custom_rules:
swiftlint_disable_without_this_or_next:
regex: "// swiftlint:disable (?!implicitly_unwrapped_optional|force_unwrapping)"
message: "Prefer using swiftlint:disable:this or swiftlint:disable:next over swiftlint:disable"
one_let_per_line:
regex: " let [^\n]*?, let "
message: "Place each let statement and condition on its own line"

only_rules:
- array_init
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,8 @@ class ServerConfigurationTests: XCTestCase {
file: StaticString = #filePath,
line: UInt = #line
) {
if let lhs = lhs, let rhs = rhs {
if let lhs = lhs,
let rhs = rhs {
let dict1 = NSDictionary(dictionary: lhs)
let dict2 = NSDictionary(dictionary: rhs)
XCTAssertEqual(dict1, dict2, message(), file: file, line: line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ class GamingServiceController: NSObject {

func isValidCallbackURL(_ url: URL, forService service: String) -> Bool {
// verify the URL is intended as a callback for the SDK's friend finder
guard let appID = settings.appID, let scheme = url.scheme else { return false }
guard let appID = settings.appID,
let scheme = url.scheme else { return false }

return scheme.hasPrefix("fb\(appID)") && url.host == service
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public class ShareTournamentDialog: NSObject, URLOpening {
}

func isTournamentURL(url: URL) -> Bool {
if let scheme = url.scheme, let host = url.host, let appID = Settings.shared.appID {
if let scheme = url.scheme,
let host = url.host,
let appID = Settings.shared.appID {
return scheme.hasPrefix("fb\(appID)") && host.elementsEqual("instant_tournament")
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ class ShareMediaContentTests: XCTestCase {
let media = ShareModelTestUtility.media

for (item1, item2) in zip(media, mediaContentMedia) {
if let photo1 = item1 as? SharePhoto, let photo2 = item2 as? SharePhoto {
if let photo1 = item1 as? SharePhoto,
let photo2 = item2 as? SharePhoto {
XCTAssertEqual(photo1, photo2)
} else if let video1 = item1 as? ShareVideo, let video2 = item2 as? ShareVideo {
} else if let video1 = item1 as? ShareVideo,
let video2 = item2 as? ShareVideo {
XCTAssertEqual(video1, video2)
} else {
XCTFail("Unexpected type implementing the ShareMedia protocol. Item1: \(item1), Item2: \(item2)")
Expand Down

0 comments on commit e329438

Please sign in to comment.