Skip to content

Commit

Permalink
Add SwiftLint
Browse files Browse the repository at this point in the history
We need to enforce clean code.

Add [SwiftLint](https://github.com/realm/SwiftLint) to automatically
check code style.

* Run SwiftLint autocorrect and fix minor violations
* Add .swiftlint.yml config
* Add Xcode build phase to run linter on each build
  • Loading branch information
Donatas Stundys committed Mar 25, 2016
1 parent e9ac77a commit e5fd97c
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 76 deletions.
9 changes: 9 additions & 0 deletions .swiftlint.yml
@@ -0,0 +1,9 @@
included:
- Sources/Swifton
- Tests
disabled_rules:
- force_cast
- force_try
line_length: 120
variable_name:
min_length: 2
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -6,6 +6,7 @@ sudo: required
dist: trusty
osx_image: xcode7.3
install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then ./install_swiftlint.sh; fi
- curl -sL https://gist.github.com/kylef/5c0475ff02b7c7671d2a/raw/621ef9b29bbb852fdfd2e10ed147b321d792c1e4/swiftenv-install.sh | bash
script:
- . ~/.swiftenv/init
Expand All @@ -14,3 +15,4 @@ script:
- rm -rf Packages/*/Tests
- swift build
- swift test
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then swiftlint; fi
21 changes: 10 additions & 11 deletions Sources/Swifton/Controller.swift
Expand Up @@ -12,10 +12,10 @@ public class Controller {
var filters = [String: Filter]()
var beforeFilters = FilterCollection()
var afterFilters = FilterCollection()
public let next:Response? = nil
public let next: Response? = nil

public init() { controller() }

public func controller() {}

public func action(name: String, body: Action) {
Expand All @@ -29,10 +29,10 @@ public class Controller {
public subscript(actionName: String) -> Action {
get {
return { (request) in
guard let action = self.actions[actionName] else {
guard let action = self.actions[actionName] else {
return Response(.NotFound, contentType: "text/plain; charset=utf8", body: "Action Not Found")
}

if let filterResponse = self.runFilters(request, actionName, self.beforeFilters) {
return filterResponse
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public class Controller {
}
}
}
return nil
return nil
}

func runFilter(filter: Filter, _ actionName: String, _ request: Request, _ options: FilterOptions) -> Response? {
Expand Down Expand Up @@ -101,7 +101,7 @@ public class Controller {
}

public func afterAction(filter: String, _ options: FilterOptions = nil) -> Void {
afterFilters[filter] = options
afterFilters[filter] = options
}
}

Expand All @@ -111,7 +111,7 @@ public func render(template: String) -> Response {
}

public func render(template: String, _ object: HTMLRenderable?) -> Response {
var body:String
var body: String
if let obj = object {
body = StencilView(template, obj.renderableAttributes()).render()
} else {
Expand All @@ -121,13 +121,13 @@ public func render(template: String, _ object: HTMLRenderable?) -> Response {
}

public func render(template: String, _ context: [String: Any]) -> Response {
var body:String
var body: String
body = StencilView(template, context).render()
return Response(.Ok, contentType: "text/html; charset=utf8", body: body)
}

public func renderJSON(object: JSONRenderable?) -> Response {
var body:String
var body: String
if let obj = object {
body = JSONView(obj.renderableJSONAttributes()).render()
} else {
Expand All @@ -137,7 +137,7 @@ public func renderJSON(object: JSONRenderable?) -> Response {
}

public func renderJSON(context: [String: Any]? = nil) -> Response {
var body:String
var body: String
body = JSONView(context).render()
return Response(.Ok, contentType: "application/json; charset=utf8", body: body)
}
Expand All @@ -155,4 +155,3 @@ public func respondTo(request: Request, _ responders: [String: () -> Response])
}
return Response(.NotAcceptable)
}

2 changes: 1 addition & 1 deletion Sources/Swifton/CookiesMiddleware.swift
Expand Up @@ -15,5 +15,5 @@ public class CookiesMiddleware: Middleware {

response["Set-Cookie"] = response.cookies.map { $0 + "=" + $1 }.joinWithSeparator(";")
return response
}
}
}
25 changes: 13 additions & 12 deletions Sources/Swifton/Extensions.swift
Expand Up @@ -16,39 +16,40 @@ extension String {
public func split(separator: Character) -> [String] {
return self.characters.split { $0 == separator }.map(String.init)
}

public func split(maxSplit: Int = Int.max, separator: Character) -> [String] {
return self.characters.split(maxSplit) { $0 == separator }.map(String.init)
}

public func replace(old: Character, _ new: Character) -> String {
var buffer = [Character]()
self.characters.forEach { buffer.append($0 == old ? new : $0) }
return String(buffer)
}

public func unquote() -> String {
var scalars = self.unicodeScalars;
var scalars = self.unicodeScalars
if scalars.first == "\"" && scalars.last == "\"" && scalars.count >= 2 {
scalars.removeFirst();
scalars.removeLast();
scalars.removeFirst()
scalars.removeLast()
return String(scalars)
}
return self
}

public func trim() -> String {
var scalars = self.unicodeScalars
while let _ = scalars.first?.asWhitespace() { scalars.removeFirst() }
while let _ = scalars.last?.asWhitespace() { scalars.removeLast() }
return String(scalars)
}

public static func fromUInt8(array: [UInt8]) -> String {
// Apple changes the definition of String(data: .... ) every release so let's stay with 'fromUInt8(...)' wrapper.
// Apple changes the definition of String(data: .... ) every release so let's stay
// with 'fromUInt8(...)' wrapper.
return array.reduce("", combine: { $0.0 + String(UnicodeScalar($0.1)) })
}

public func removePercentEncoding() -> String {
var scalars = self.unicodeScalars
var output = ""
Expand Down Expand Up @@ -84,7 +85,7 @@ extension String {
}

extension UnicodeScalar {

public func asWhitespace() -> UInt8? {
if self.value >= 9 && self.value <= 13 {
return UInt8(self.value)
Expand All @@ -94,7 +95,7 @@ extension UnicodeScalar {
}
return nil
}

public func asAlpha() -> UInt8? {
if self.value >= 48 && self.value <= 57 {
return UInt8(self.value) - 48
Expand Down
8 changes: 4 additions & 4 deletions Sources/Swifton/JSON.swift
@@ -1,4 +1,4 @@
// http://stackoverflow.com/questions/35246542/serialize-stringany-to-json
// http://stackoverflow.com/questions/35246542/serialize-stringany-to-json

protocol JSONSerializable {
func toJSON() -> String?
Expand All @@ -24,7 +24,7 @@ extension Double : JSONSerializable {

extension Array : JSONSerializable {
func toJSON() -> String? {
var out : [String] = []
var out: [String] = []
for element in self {
if let json_element = element as? JSONSerializable, let string = json_element.toJSON() {
out.append(string)
Expand All @@ -33,12 +33,12 @@ extension Array : JSONSerializable {
}
}
return "[\(out.joinWithSeparator(", "))]"
}
}
}

extension Dictionary : JSONSerializable {
func toJSON() -> String? {
var out : [String] = []
var out: [String] = []
for (k, v) in self {
if let json_element = v as? JSONSerializable, let string = json_element.toJSON() {
out.append("\"\(k)\": \(string)")
Expand Down
27 changes: 13 additions & 14 deletions Sources/Swifton/MemoryModel.swift
@@ -1,8 +1,8 @@
public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
static var id = 1
public static var all = [MemoryModel]()
public static var all = [MemoryModel]()
public var attributes = [String: Any]()
public var id:Int {
public var id: Int {
get {
return attributes["id"] as! Int
}
Expand All @@ -12,7 +12,7 @@ public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
}

required public init(_ attributes: [String: Any]) {
self.attributes = attributes
self.attributes = attributes
self.attributes["id"] = self.dynamicType.id
self.dynamicType.id += 1
}
Expand All @@ -24,7 +24,7 @@ public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
set(newValue) {
attributes[name] = newValue
}
}
}

public static func create(attributes: [String: String]) -> Self {
let resolvedAttributes = self.resolveAttributes(attributes)
Expand All @@ -40,7 +40,7 @@ public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
}

public static func find(id: Int?) -> MemoryModel? {
return all.filter{ $0.id == id }.first
return all.filter { $0.id == id }.first
}

public static func destroy(model: MemoryModel?) {
Expand All @@ -52,15 +52,15 @@ public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
public static func allAttributes() -> Any {
var items = [Any]()
for model in all {
var attrs = model.attributes
var attrs = model.attributes
attrs["id"] = String(model.id)
items.append(attrs as Any)
}
return items as Any
}

public static func reset() {
all = [MemoryModel]()
all = [MemoryModel]()
id = 1
}

Expand All @@ -71,28 +71,27 @@ public class MemoryModel: HTMLRenderable, JSONRenderable, Equatable {
static func resolveAttributes(attributes: [String: String]) -> [String: Any] {
var attrs = [String: Any]()
for (key, value) in attributes {
if let integer:Int = Int(value) {
if let integer: Int = Int(value) {
attrs[key] = integer
} else if let double:Double = Double(value) {
} else if let double: Double = Double(value) {
attrs[key] = double
} else {
attrs[key] = value
}
}
return attrs
}

public func renderableAttributes() -> [String: Any] {
return self.attributes
return self.attributes
}

public func renderableJSONAttributes() -> [String: Any] {
return self.attributes
return self.attributes
}

}

public func ==(lhs: MemoryModel, rhs: MemoryModel) -> Bool {
public func == (lhs: MemoryModel, rhs: MemoryModel) -> Bool {
return lhs.id == rhs.id
}

2 changes: 1 addition & 1 deletion Sources/Swifton/Middleware.swift
@@ -1,5 +1,5 @@
import Inquiline

public protocol Middleware {
func call(request: Request, _ closure: Request -> Response) -> Response
func call(request: Request, _ closure: Request -> Response) -> Response
}
5 changes: 3 additions & 2 deletions Sources/Swifton/MimeType.swift
@@ -1,5 +1,6 @@
// swiftlint:disable:next line_length
// https://gist.githubusercontent.com/ngs/918b07f448977789cf69/raw/05bbb2103a41e0ed5d5579b3666d61c0891f91c0/MimeType.swift
internal let DEFAULT_MIME_TYPE = "application/octet-stream"
internal let defaultMimeType = "application/octet-stream"

internal let mimeTypes = [
"html": "text/html",
Expand Down Expand Up @@ -111,7 +112,7 @@ internal func MimeType(ext: String?) -> String {
if ext != nil && mimeTypes.contains({ $0.0 == ext!.lowercaseString }) {
return mimeTypes[ext!.lowercaseString]!
}
return DEFAULT_MIME_TYPE
return defaultMimeType
}

extension String {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Swifton/ParametersMiddleware.swift
Expand Up @@ -3,13 +3,13 @@ import Inquiline
public class ParametersMiddleware: Middleware {
public func call(request: Request, _ closure: Request -> Response) -> Response {
var newRequest = request
var queryString:String = ""
var queryString: String = ""
if Method(rawValue: request.method) == .GET {
let elements = request.path.split(1, separator: "?")
if elements.count > 1 {
queryString = request.path.split(1, separator: "?").last!
}
} else {
} else {
queryString = request.body!
}

Expand All @@ -21,7 +21,7 @@ public class ParametersMiddleware: Middleware {
}
newRequest.method = self.resolveMethod(newRequest)
return closure(newRequest)
}
}

func resolveMethod(request: Request) -> String {
if request.method == "POST" {
Expand Down

0 comments on commit e5fd97c

Please sign in to comment.