Skip to content

Commit

Permalink
Update SwiftLint rules and fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
yvbeek committed Jan 31, 2019
1 parent dd427ad commit 1b35380
Show file tree
Hide file tree
Showing 32 changed files with 168 additions and 124 deletions.
36 changes: 33 additions & 3 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,53 @@ disabled_rules:
- cyclomatic_complexity
- force_cast
- force_try
- large_tuple
- line_length
- valid_docs
- variable_name
- statement_position
opt_in_rules:
- array_init
- closure_body_length
- closure_end_indentation
- closure_spacing
- collection_alignment
- contains_over_first_not_nil
- discouraged_optional_boolean
- empty_count
- empty_string
- explicit_init
- extension_access_modifier
- fatal_error_message
- first_where
- identical_operands
- joined_default_parameter
- last_where
- legacy_random
- literal_expression_end_indentation
- lower_acl_than_parent
- modifier_order
- nimble_operator
- object_literal
- operator_usage_whitespace
- overridden_super_call
- override_in_extension
- private_action
- private_outlet
- prohibited_super_call
- redundant_nil_coalescing
- redundant_type_annotation
- required_enum_case
- sorted_first_last
- toggle_bool
- unneeded_parentheses_in_closure_argument
- unused_import
- unused_private_declaration
- yoda_condition
identifier_name:
excluded:
- x
- y
- at
- ok
- to
excluded:
- Carthage
- Pods
4 changes: 2 additions & 2 deletions Examples/Demo/TelegraphDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class TelegraphDemo: NSObject {
var webSocketClient: WebSocketClient!
}

extension TelegraphDemo {
public func start() {
public extension TelegraphDemo {
func start() {
// Comment out this line if you want HTTP instead of HTTPS
loadCertificates()

Expand Down
12 changes: 6 additions & 6 deletions Examples/macOS Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.55" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13771"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.55"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -600,7 +600,7 @@
<menuItem title="Show Sidebar" keyEquivalent="s" id="kIP-vf-haE">
<modifierMask key="keyEquivalentModifierMask" control="YES" command="YES"/>
<connections>
<action selector="toggleSourceList:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
<action selector="toggleSidebar:" target="Ady-hI-5gd" id="iwa-gc-5KM"/>
</connections>
</menuItem>
<menuItem title="Enter Full Screen" keyEquivalent="f" id="4J7-dP-txa">
Expand Down Expand Up @@ -664,7 +664,7 @@
<scene sceneID="R2V-B0-nI4">
<objects>
<windowController id="B8D-0N-5wS" sceneMemberID="viewController">
<window key="window" title="Telegraph" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" animationBehavior="default" id="IQv-IB-iLA" customClass="AppWindow" customModule="macOS_Example" customModuleProvider="target">
<window key="window" title="Telegraph" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" releasedWhenClosed="NO" animationBehavior="default" id="IQv-IB-iLA" customClass="AppWindow" customModule="macOS_Example" customModuleProvider="target">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="480" height="270"/>
Expand All @@ -679,7 +679,7 @@
</windowController>
<customObject id="Oky-zY-oP4" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="250"/>
<point key="canvasLocation" x="74" y="269"/>
</scene>
<!--View Controller-->
<scene sceneID="hIz-AP-VOD">
Expand All @@ -706,7 +706,7 @@
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="75" y="655"/>
<point key="canvasLocation" x="74" y="683"/>
</scene>
</scenes>
<resources>
Expand Down
8 changes: 4 additions & 4 deletions Sources/Clients/Client+Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

import Foundation

extension HTTPConfig {
public static var clientDefault: HTTPConfig {
public extension HTTPConfig {
static var clientDefault: HTTPConfig {
return HTTPConfig(requestHandlers: [])
}
}

extension WebSocketConfig {
public static var clientDefault: WebSocketConfig {
public extension WebSocketConfig {
static var clientDefault: WebSocketConfig {
var config = WebSocketConfig()
config.maskMessages = true
config.pingInterval = 0
Expand Down
8 changes: 4 additions & 4 deletions Sources/Clients/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,21 +165,21 @@ public extension WebSocketClient {

// MARK: Convenience initializers

extension WebSocketClient {
public extension WebSocketClient {
/// Creates a new WebSocketClient with an url in string form.
public convenience init(_ string: String) throws {
convenience init(_ string: String) throws {
guard let url = URL(string: string) else { throw WebSocketClientError.invalidURL }
try self.init(url: url)
}

/// Creates a new WebSocketClient with an url in string form and certificates to trust.
public convenience init(_ string: String, certificates: [Certificate]) throws {
convenience init(_ string: String, certificates: [Certificate]) throws {
try self.init(string)
self.tlsPolicy = TLSPolicy(certificates: certificates)
}

/// Creates a new WebSocketClient with an url and certificates to trust.
public convenience init(url: URL, certificates: [Certificate]) throws {
convenience init(url: URL, certificates: [Certificate]) throws {
try self.init(url: url)
self.tlsPolicy = TLSPolicy(certificates: certificates)
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/Helpers/Extensions/Array+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import Foundation

extension Array {
public func first<T>(ofType: T.Type) -> T? {
return first(where: { $0 as? T != nil }) as? T
public extension Array {
/// Returns the first item in an array that conforms to the provided type.
func first<T>(ofType: T.Type) -> T? {
return first { $0 as? T != nil } as? T
}

public func filter<T>(ofType: T.Type) -> [T] {
/// Returns only the items that match the provided type.
func filter<T>(ofType: T.Type) -> [T] {
return compactMap { $0 as? T }
}
}
4 changes: 2 additions & 2 deletions Sources/Helpers/Extensions/Data+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public extension Data {
/// Masks the contents of the data with the provided mask bytes.
mutating func mask(with maskBytes: [UInt8]) {
let maskSize = maskBytes.count
for i in 0..<count {
self[i] ^= maskBytes[i % maskSize]
for index in 0..<count {
self[index] ^= maskBytes[index % maskSize]
}
}
}
4 changes: 2 additions & 2 deletions Sources/Helpers/Extensions/FileManager+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import Foundation
import MobileCoreServices
#endif

extension FileManager {
public extension FileManager {
/// Returns the mime type of a file.
public func mimeType(of url: URL) -> String {
func mimeType(of url: URL) -> String {
let fallback = "application/octet-stream"
let fileExt = url.pathExtension as CFString

Expand Down
6 changes: 3 additions & 3 deletions Sources/Helpers/Extensions/NSRegularExpression+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ extension NSRegularExpression {

// MARK: String helpers

extension String {
fileprivate var fullRange: NSRange {
private extension String {
var fullRange: NSRange {
return NSRange(location: 0, length: count)
}

fileprivate func substring(with range: NSRange) -> String? {
func substring(with range: NSRange) -> String? {
guard let range = Range(range) else { return nil }
let from = index(startIndex, offsetBy: range.lowerBound)
let to = index(startIndex, offsetBy: range.upperBound)
Expand Down
13 changes: 8 additions & 5 deletions Sources/Helpers/Extensions/String+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@

import Foundation

extension String {
public var base64: String {
public extension String {
/// Generates a base64 encoded string of this String's contents.
var base64: String {
return utf8Data.base64EncodedString()
}

public var utf8Data: Data {
/// The character bytes in UTF8 encoding.
var utf8Data: Data {
return data(using: .utf8)!
}

public func truncate(count: Int, ellipses: Bool = true) -> String {
if self.count < count { return self }
/// Truncates a string and optionally adds ellipses.
func truncate(count: Int, ellipses: Bool = true) -> String {
if self.count <= count { return self }
return prefix(count) + (ellipses ? "..." : "")
}
}
20 changes: 12 additions & 8 deletions Sources/Helpers/Extensions/UInt+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@

import Foundation

extension UInt8 {
public static var random: UInt8 {
public extension UInt8 {
/// Generates a random UInt8 byte.
static var random: UInt8 {
var number: UInt8 = 0
_ = SecRandomCopyBytes(kSecRandomDefault, 1, &number)
return number
}
}

extension UInt16 {
public var bytes: [UInt8] {
public extension UInt16 {
/// The two UInt8 bytes that make up the UInt16.
var bytes: [UInt8] {
return [
UInt8(truncatingIfNeeded: self >> 8),
UInt8(truncatingIfNeeded: self)
]
}
}

extension UInt32 {
public var bytes: [UInt8] {
public extension UInt32 {
/// The four UInt8 bytes that make up the UInt32.
var bytes: [UInt8] {
return [
UInt8(truncatingIfNeeded: self >> 24),
UInt8(truncatingIfNeeded: self >> 16),
Expand All @@ -36,8 +39,9 @@ extension UInt32 {
}
}

extension UInt64 {
public var bytes: [UInt8] {
public extension UInt64 {
/// The eight UInt8 bytes that make up the UInt64.
var bytes: [UInt8] {
return [
UInt8(truncatingIfNeeded: self >> 56),
UInt8(truncatingIfNeeded: self >> 48),
Expand Down
11 changes: 7 additions & 4 deletions Sources/Helpers/Extensions/URL+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@

import Foundation

extension URL {
public var hasWebSocketScheme: Bool {
public extension URL {
/// Indicates if the URL has a https, http, wss or ws scheme.
var hasWebSocketScheme: Bool {
return scheme == "https" || scheme == "http" || scheme == "wss" || scheme == "ws"
}

public var portBasedOnScheme: Int {
/// A simple port determination based on the scheme, 443 for HTTPS and 80 for HTTP.
var portBasedOnScheme: Int {
return isSchemeSecure ? 443 : 80
}

public var isSchemeSecure: Bool {
/// Indicates if the scheme is HTTPS or WSS.
var isSchemeSecure: Bool {
return scheme == "https" || scheme == "wss"
}
}
6 changes: 3 additions & 3 deletions Sources/Protocols/HTTP/Handlers/HTTPRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public protocol HTTPRequestHandler {
func respond(to request: HTTPRequest, nextHandler: HTTPRequest.Handler) throws -> HTTPResponse?
}

extension HTTPRequest {
public typealias Handler = (HTTPRequest) throws -> HTTPResponse?
public extension HTTPRequest {
typealias Handler = (HTTPRequest) throws -> HTTPResponse?
}

extension Collection where Element == HTTPRequestHandler {
// Creates a closure chain with all of the handlers
func chain(lastHandler: @escaping HTTPRequest.Handler = { _ in return nil }) -> HTTPRequest.Handler {
func chain(lastHandler: @escaping HTTPRequest.Handler = { _ in nil }) -> HTTPRequest.Handler {
return reversed().reduce(lastHandler) { nextHandler, handler in
return { request in try handler.respond(to: request, nextHandler: nextHandler) }
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Protocols/HTTP/Models/HTTPMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ open class HTTPMessage {

// MARK: Helper methods

extension HTTPMessage {
public extension HTTPMessage {
/// Returns a boolean indicating if the connection should be kept open.
var keepAlive: Bool {
guard let connection = headers.connection else { return version.minor != 0 }
Expand Down
4 changes: 2 additions & 2 deletions Sources/Protocols/HTTP/Models/HTTPRequest+Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import Foundation

extension HTTPRequest {
public extension HTTPRequest {
// Sets the host header to the specified host and port.
public func setHostHeader(host: String?, port: Int? = nil) {
func setHostHeader(host: String?, port: Int? = nil) {
var value: String?

if let host = host {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Protocols/HTTP/Models/HTTPRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ open class HTTPRequest: HTTPMessage {

public var method: HTTPMethod
public var uri: URI
public var params: Params = Params()
public var params = Params()

/// Creates a new HTTPRequest.
public init(_ method: HTTPMethod = .GET, uri: URI = .root, version: HTTPVersion = .default,
Expand All @@ -37,7 +37,7 @@ open class HTTPRequest: HTTPMessage {
// MARK: CustomStringConvertible implementation

extension HTTPRequest: CustomStringConvertible {
open var description: String {
public var description: String {
let typeName = type(of: self)
return "<\(typeName): \(method) \(uri) \(version), headers: \(headers.count), body: \(body.count)>"
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Protocols/HTTP/Models/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ open class HTTPResponse: HTTPMessage {

// MARK: Convenience initializers

extension HTTPResponse {
public extension HTTPResponse {
/// Creates an HTTP response to send textual content.
public convenience init(_ status: HTTPStatus = .ok, headers: HTTPHeaders = .empty, content: String) {
convenience init(_ status: HTTPStatus = .ok, headers: HTTPHeaders = .empty, content: String) {
self.init(status, headers: headers, body: content.utf8Data)
}

/// Creates an HTTP response to send an error.
public convenience init(_ status: HTTPStatus = .internalServerError, headers: HTTPHeaders = .empty, error: Error) {
convenience init(_ status: HTTPStatus = .internalServerError, headers: HTTPHeaders = .empty, error: Error) {
var errorHeaders = headers
errorHeaders.connection = "close"

Expand All @@ -62,7 +62,7 @@ extension HTTPResponse {
// MARK: CustomStringConvertible

extension HTTPResponse: CustomStringConvertible {
open var description: String {
public var description: String {
let typeName = type(of: self)
return "<\(typeName): \(version) \(status), headers: \(headers.count), body: \(body.count)>"
}
Expand Down
Loading

0 comments on commit 1b35380

Please sign in to comment.