Skip to content

Commit

Permalink
build: 新增 swiftlint
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoxiang committed Sep 27, 2023
1 parent 6e2f211 commit 8cfc48a
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 41 deletions.
38 changes: 38 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
included:
excluded:
- Carthage
- Pods
line_length:
warning: 120
ignores_function_declarations: true
ignores_comments: true
disabled_rules:
- empty_enum_arguments
- trailing_whitespace
- cyclomatic_complexity
- file_length
- type_name
- inclusive_language
- function_parameter_count
- identifier_name
- type_body_length
- function_body_length
- nesting
- unneeded_break_in_switch
identifier_name:
excluded:
- i
- idx
- h
- w
- x
- y
- id
- url
- min
- max
- r
- g
- b
- a
3 changes: 0 additions & 3 deletions Example/CleanJSON/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
Expand Down Expand Up @@ -41,6 +40,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


}

4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let package = Package(
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "CleanJSON",
targets: ["CleanJSON"]),
targets: ["CleanJSON"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -23,6 +23,6 @@ let package = Package(
dependencies: []),
.testTarget(
name: "CleanJSONTests",
dependencies: ["CleanJSON"]),
dependencies: ["CleanJSON"])
]
)
5 changes: 2 additions & 3 deletions Sources/CleanJSON/CaseDefaultable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,13 @@ private extension _CleanJSONDecoder {
func decodeCase<T>(_ type: T.Type) throws -> T
where T: CaseDefaultable,
T: Decodable,
T.RawValue: Decodable
{
T.RawValue: Decodable {
guard !decodeNil(), !storage.containers.isEmpty, storage.topContainer is T.RawValue else {
return T.defaultCase
}

if let number = storage.topContainer as? NSNumber,
(number === kCFBooleanTrue || number === kCFBooleanFalse) {
number === kCFBooleanTrue || number === kCFBooleanFalse {
guard let rawValue = number.boolValue as? T.RawValue else {
return T.defaultCase
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/CleanJSON/CleanJSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ open class CleanJSONDecoder: JSONDecoder {
let valueNotFoundDecodingStrategy: ValueNotFoundDecodingStrategy
let nestedContainerDecodingStrategy: NestedContainerDecodingStrategy
let jsonStringDecodingStrategy: JSONStringDecodingStrategy
let userInfo: [CodingUserInfoKey : Any]
let userInfo: [CodingUserInfoKey: Any]
}

/// The options set on the top-level decoder.
Expand Down Expand Up @@ -71,7 +71,7 @@ open class CleanJSONDecoder: JSONDecoder {
/// - returns: A value of the requested type.
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
/// - throws: An error if any value throws an error during decoding.
open override func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {
open override func decode<T: Decodable>(_ type: T.Type, from data: Data) throws -> T {
let topLevel: Any
do {
topLevel = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
Expand All @@ -94,7 +94,7 @@ open class CleanJSONDecoder: JSONDecoder {
/// - parameter convertible: The container to decode from.
/// - returns: A value of the requested type.
/// - throws: An error if any value throws an error during decoding.
open func decode<T : Decodable>(
open func decode<T: Decodable>(
_ type: T.Type,
from convertible: JSONContainerConvertible
) throws -> T {
Expand All @@ -104,7 +104,7 @@ open class CleanJSONDecoder: JSONDecoder {

private extension CleanJSONDecoder {

func decode<T : Decodable>(
func decode<T: Decodable>(
_ type: T.Type,
from container: Any
) throws -> T {
Expand Down
2 changes: 1 addition & 1 deletion Sources/CleanJSON/CleanJSONKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

struct CleanJSONKey : CodingKey {
struct CleanJSONKey: CodingKey {

public var stringValue: String

Expand Down
24 changes: 12 additions & 12 deletions Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerProtocol {
struct CleanJSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {

typealias Key = K

Expand All @@ -18,15 +18,15 @@ struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPro
private let decoder: _CleanJSONDecoder

/// A reference to the container we're reading from.
private let container: [String : Any]
private let container: [String: Any]

/// The path of coding keys taken to get to this point in decoding.
private(set) public var codingPath: [CodingKey]

// MARK: - Initialization

/// Initializes `self` by referencing the given decoder and container.
init(referencing decoder: _CleanJSONDecoder, wrapping container: [String : Any]) {
init(referencing decoder: _CleanJSONDecoder, wrapping container: [String: Any]) {
self.decoder = decoder
switch decoder.options.keyDecodingStrategy {
case .useDefaultKeys:
Expand Down Expand Up @@ -416,7 +416,7 @@ struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPro
}

@inline(__always)
public func decode<T : Decodable>(_ type: T.Type, forKey key: Key) throws -> T {
public func decode<T: Decodable>(_ type: T.Type, forKey key: Key) throws -> T {
guard let entry = container[key.stringValue] else {
switch decoder.options.keyNotFoundDecodingStrategy {
case .throw:
Expand Down Expand Up @@ -487,12 +487,12 @@ struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerPro
}
}

guard let dictionary = value as? [String : Any] else {
guard let dictionary = value as? [String: Any] else {
switch decoder.options.nestedContainerDecodingStrategy.typeMismatch {
case .throw:
throw DecodingError._typeMismatch(
at: self.codingPath,
expectation: [String : Any].self,
expectation: [String: Any].self,
reality: value)
case .useEmptyContainer:
return nestedContainer()
Expand Down Expand Up @@ -863,7 +863,7 @@ extension CleanJSONKeyedDecodingContainer {
}

@inline(__always)
func decodeIfPresent<T>(_ type: T.Type, forKey key: K) throws -> T? where T : Decodable {
func decodeIfPresent<T>(_ type: T.Type, forKey key: K) throws -> T? where T: Decodable {
guard contains(key), let entry = container[key.stringValue] else { return nil }

decoder.codingPath.append(key)
Expand Down Expand Up @@ -939,7 +939,7 @@ private extension CleanJSONDecoder.KeyDecodingStrategy {
let trailingUnderscoreRange = stringKey.index(after: lastNonUnderscore)..<stringKey.endIndex

let components = stringKey[keyRange].split(separator: "_")
let joinedString : String
let joinedString: String
if components.count == 1 {
// No underscores in key, leave the word as is - maybe already camel cased
joinedString = String(stringKey[keyRange])
Expand All @@ -948,13 +948,13 @@ private extension CleanJSONDecoder.KeyDecodingStrategy {
}

// Do a cheap isEmpty check before creating and appending potentially empty strings
let result : String
if (leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty) {
let result: String
if leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty {
result = joinedString
} else if (!leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty) {
} else if !leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty {
// Both leading and trailing underscores
result = String(stringKey[leadingUnderscoreRange]) + joinedString + String(stringKey[trailingUnderscoreRange])
} else if (!leadingUnderscoreRange.isEmpty) {
} else if !leadingUnderscoreRange.isEmpty {
// Just leading
result = String(stringKey[leadingUnderscoreRange]) + joinedString
} else {
Expand Down
8 changes: 4 additions & 4 deletions Sources/CleanJSON/CleanJSONUnkeyedDecodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
struct CleanJSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
// MARK: Properties

/// A reference to the decoder we're reading from.
Expand Down Expand Up @@ -394,7 +394,7 @@ struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
}
}

public mutating func decode<T : Decodable>(_ type: T.Type) throws -> T {
public mutating func decode<T: Decodable>(_ type: T.Type) throws -> T {
guard !self.isAtEnd else {
throw DecodingError.valueNotFound(
type,
Expand Down Expand Up @@ -451,12 +451,12 @@ struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
}
}

guard let dictionary = value as? [String : Any] else {
guard let dictionary = value as? [String: Any] else {
switch decoder.options.nestedContainerDecodingStrategy.typeMismatch {
case .throw:
throw DecodingError._typeMismatch(
at: self.codingPath,
expectation: [String : Any].self,
expectation: [String: Any].self,
reality: value
)
case .useEmptyContainer:
Expand Down
2 changes: 1 addition & 1 deletion Sources/CleanJSON/DecodingError+CleanJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension DecodingError {
return "a string/data"
} else if value is [Any] {
return "an array"
} else if value is [String : Any] {
} else if value is [String: Any] {
return "a dictionary"
} else {
return "\(type(of: value))"
Expand Down
2 changes: 1 addition & 1 deletion Sources/CleanJSON/_CleanJSONDecoder+Decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension _CleanJSONDecoder {
return bool
} else if let int = Int.defaultValue as? T {
return int
}else if let double = Double.defaultValue as? T {
} else if let double = Double.defaultValue as? T {
return double
} else if let date = Date.defaultValue(for: options.dateDecodingStrategy) as? T {
return date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

extension _CleanJSONDecoder : SingleValueDecodingContainer {
extension _CleanJSONDecoder: SingleValueDecodingContainer {
// MARK: SingleValueDecodingContainer Methods

public func decodeNil() -> Bool {
Expand Down Expand Up @@ -197,7 +197,7 @@ extension _CleanJSONDecoder : SingleValueDecodingContainer {
}
}

public func decode<T : Decodable>(_ type: T.Type) throws -> T {
public func decode<T: Decodable>(_ type: T.Type) throws -> T {
if type == Date.self || type == NSDate.self {
return try decode(as: Date.self) as! T
} else if type == Data.self || type == NSData.self {
Expand Down
6 changes: 3 additions & 3 deletions Sources/CleanJSON/_CleanJSONDecoder+Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ extension _CleanJSONDecoder {

guard let dict = value as? NSDictionary else { return nil }

var result = [String : Any]()
var result = [String: Any]()
let elementType = type.elementType
for (key, value) in dict {
let key = key as! String
Expand All @@ -412,7 +412,7 @@ extension _CleanJSONDecoder {
return result as? T
}

func unbox<T : Decodable>(_ value: Any, as type: T.Type) throws -> T? {
func unbox<T: Decodable>(_ value: Any, as type: T.Type) throws -> T? {
return try unbox_(value, as: type) as? T
}

Expand Down Expand Up @@ -446,7 +446,7 @@ protocol _JSONStringDictionaryDecodableMarker {
}
#endif

extension Dictionary : _JSONStringDictionaryDecodableMarker where Key == String, Value: Decodable {
extension Dictionary: _JSONStringDictionaryDecodableMarker where Key == String, Value: Decodable {
static var elementType: Decodable.Type { return Value.self }
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/CleanJSON/_CleanJSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class _CleanJSONDecoder: CleanDecoder {
public var codingPath: [CodingKey]

/// Contextual user-provided information for use during encoding.
public var userInfo: [CodingUserInfoKey : Any] {
public var userInfo: [CodingUserInfoKey: Any] {
return self.options.userInfo
}

Expand Down Expand Up @@ -54,12 +54,12 @@ final class _CleanJSONDecoder: CleanDecoder {
}
}

guard let topContainer = self.storage.topContainer as? [String : Any] else {
guard let topContainer = self.storage.topContainer as? [String: Any] else {
switch options.nestedContainerDecodingStrategy.typeMismatch {
case .throw:
throw DecodingError._typeMismatch(
at: codingPath,
expectation: [String : Any].self,
expectation: [String: Any].self,
reality: storage.topContainer
)
case .useEmptyContainer:
Expand Down
2 changes: 1 addition & 1 deletion Tests/CleanJSONTests/CleanJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ final class CleanJSONTests: XCTestCase {
}

static var allTests = [
("testExample", testExample),
("testExample", testExample)
]
}
2 changes: 1 addition & 1 deletion Tests/CleanJSONTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest
#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(CleanJSONTests.allTests),
testCase(CleanJSONTests.allTests)
]
}
#endif

0 comments on commit 8cfc48a

Please sign in to comment.