Skip to content

Commit

Permalink
Cleanup Dangerfile.swift, docs/sample.swift & Downloader.swift.
Browse files Browse the repository at this point in the history
Partial #638

Signed-off-by: Ross Goldberg <[email protected]>
  • Loading branch information
rgoldberg committed Nov 21, 2024
1 parent c6d2ac8 commit c169e88
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
13 changes: 9 additions & 4 deletions Dangerfile.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import Danger

let danger = Danger()

// Thanks other people!
if let github = danger.github {
// Thank non-member submitters
let submitter = github.pullRequest.user.login
if submitter != "phatblat" && submitter != "chris-araman" {
switch submitter {
case "chris-araman",
"phatblat":
break
default:
danger.message(":tada: Thanks for your contribution, \(submitter)!")
}

// Mainly to encourage writing up some reasoning about the PR, rather than just leaving a title
// Encourage writing up some reasoning about the PR
if github.pullRequest.body?.count ?? 0 < 5 {
danger.fail(":memo: Please provide a summary in the Pull Request description.")
}

// Make it more obvious that a PR is a work in progress and shouldn't be merged yet
// Warn that PR marked [WIP] should be a Draft
if github.pullRequest.title.contains("[WIP]") {
danger.warn(":construction: Title includes `[WIP]`. Please convert the pull request to a Draft.")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/mas/AppStore/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func downloadApps(withAppIDs appIDs: [AppID], purchasing: Bool = false) -> Promi
}
}
.done {
if let error = firstError {
throw error
if let firstError {
throw firstError
}
}
}
Expand Down
82 changes: 44 additions & 38 deletions docs/sample.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,99 @@

// MARK: Types and naming

// Types begin with a capital letter
/// Types begin with a capital letter.
struct User {
let name: String

// if the first letter of an acronym is lowercase, the entire thing should
// be lowercase
/// if the first letter of an acronym is lowercase, the entire thing should
/// be lowercase.
let json: Any

// if the first letter of an acronym is uppercase, the entire thing should
// be uppercase
static func decode(from json: JSON) -> User {
return User(json: json)
/// if the first letter of an acronym is uppercase, the entire thing should
/// be uppercase.
static func decode(from json: JSON) -> Self {
Self(json: json)
}
}

// Use () for void arguments and Void for void return types
let f: () -> Void = {}
/// Use () for void arguments and Void for void return types.
let closure: () -> Void = {
// Do nothing
}

// When using classes, default to marking them as final
final class MyViewController: UIViewController {
// Prefer strong IBOutlet references
@IBOutlet var button: UIButton!
/// When using classes, default to marking them as final.
final class MyClass {
// Empty class
}

// Use typealias when closures are referenced in multiple places
/// Use typealias when closures are referenced in multiple places.
typealias CoolClosure = (Int) -> Bool

// Use aliased parameter names when function parameters are ambiguous
/// Use aliased parameter names when function parameters are ambiguous.
func yTown(some: Int, withCallback callback: CoolClosure) -> Bool {
return callback(some)
callback(some)
}

// It's OK to use $ variable references if the closure is very short and
// readability is maintained
/// It's OK to use $ variable references if the closure is very short and
/// readability is maintained.
let cool = yTown(5) { $0 == 6 }

// Use full variable names when closures are more complex
/// Use full variable names when closures are more complex.
let cool = yTown(5) { foo in
if foo > 5, foo < 0 {
return true
} else {
return false
}
max(foo, 0)
// …
}

// Strongify weak references in async closures
APIClient.getAwesomeness { [weak self] result in
guard let `self` = self else { return }
guard let self else {
return
}
self.stopLoadingSpinner()
self.show(result)
}

// If the API you are using has implicit unwrapping you should still use if-let
func someUnauditedAPI(thing: String!) {
if let string = thing {
print(string)
/// Use if-let to check for not `nil` (even if using an implicitly unwrapped variable from an API).
func someUnauditedAPI(thing: String?) {
if let thing {
print(thing)
}
}

// When the type is known you can let the compiler infer
/// When the type is known you can let the compiler infer.
let response: Response = .success(NSData())

func doSomeWork() -> Response {
let data = ...
let data = Data("", .utf8)
return .success(data)
}

switch response {
case let .success(data):
case .success(let data):
print("The response returned successfully \(data)")

case let .failure(error):
case .failure(let error):
print("An error occurred: \(error)")
}

// MARK: Organization

// Group methods into specific extensions for each level of access control
/// Group methods into specific extensions for each level of access control.
private extension MyClass {
func doSomethingPrivate() {}
func doSomethingPrivate() {
// Do something
}
}

// MARK: Breaking up long lines

// One expression to evaluate and short or no return
guard let singleTest = somethingFailable() else { return }
guard statementThatShouldBeTrue else { return }
guard let singleTest = somethingFailable() else {
return
}

guard statementThatShouldBeTrue else {
return
}

// If a guard clause requires multiple lines, chop down, then start `else` new line
// In this case, always chop down else clause.
Expand Down

0 comments on commit c169e88

Please sign in to comment.