From 8cfc48a79934e772f7d8ed36eb95916cfed8156c Mon Sep 17 00:00:00 2001 From: gaoxiang Date: Wed, 27 Sep 2023 16:03:48 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E6=96=B0=E5=A2=9E=20swiftlint?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .swiftlint.yml | 38 +++++++++++++++++++ Example/CleanJSON/AppDelegate.swift | 3 -- Package.swift | 4 +- Sources/CleanJSON/CaseDefaultable.swift | 5 +-- Sources/CleanJSON/CleanJSONDecoder.swift | 8 ++-- Sources/CleanJSON/CleanJSONKey.swift | 2 +- .../CleanJSONKeyedDecodingContainer.swift | 24 ++++++------ .../CleanJSONUnkeyedDecodingContainer.swift | 8 ++-- .../CleanJSON/DecodingError+CleanJSON.swift | 2 +- .../CleanJSON/_CleanJSONDecoder+Decode.swift | 2 +- ...Decoder+SingleValueDecodingContainer.swift | 4 +- .../CleanJSON/_CleanJSONDecoder+Unbox.swift | 6 +-- Sources/CleanJSON/_CleanJSONDecoder.swift | 6 +-- Tests/CleanJSONTests/CleanJSONTests.swift | 2 +- Tests/CleanJSONTests/XCTestManifests.swift | 2 +- 15 files changed, 75 insertions(+), 41 deletions(-) create mode 100644 .swiftlint.yml diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..84d7a73 --- /dev/null +++ b/.swiftlint.yml @@ -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 diff --git a/Example/CleanJSON/AppDelegate.swift b/Example/CleanJSON/AppDelegate.swift index b6ae68a..ee71091 100644 --- a/Example/CleanJSON/AppDelegate.swift +++ b/Example/CleanJSON/AppDelegate.swift @@ -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 @@ -41,6 +40,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } - } - diff --git a/Package.swift b/Package.swift index 480988c..9b6b2a4 100644 --- a/Package.swift +++ b/Package.swift @@ -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. @@ -23,6 +23,6 @@ let package = Package( dependencies: []), .testTarget( name: "CleanJSONTests", - dependencies: ["CleanJSON"]), + dependencies: ["CleanJSON"]) ] ) diff --git a/Sources/CleanJSON/CaseDefaultable.swift b/Sources/CleanJSON/CaseDefaultable.swift index 1fb34e5..94a6587 100644 --- a/Sources/CleanJSON/CaseDefaultable.swift +++ b/Sources/CleanJSON/CaseDefaultable.swift @@ -33,14 +33,13 @@ private extension _CleanJSONDecoder { func decodeCase(_ 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 } diff --git a/Sources/CleanJSON/CleanJSONDecoder.swift b/Sources/CleanJSON/CleanJSONDecoder.swift index 12d485b..82fa3da 100644 --- a/Sources/CleanJSON/CleanJSONDecoder.swift +++ b/Sources/CleanJSON/CleanJSONDecoder.swift @@ -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. @@ -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(_ type: T.Type, from data: Data) throws -> T { + open override func decode(_ type: T.Type, from data: Data) throws -> T { let topLevel: Any do { topLevel = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) @@ -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( + open func decode( _ type: T.Type, from convertible: JSONContainerConvertible ) throws -> T { @@ -104,7 +104,7 @@ open class CleanJSONDecoder: JSONDecoder { private extension CleanJSONDecoder { - func decode( + func decode( _ type: T.Type, from container: Any ) throws -> T { diff --git a/Sources/CleanJSON/CleanJSONKey.swift b/Sources/CleanJSON/CleanJSONKey.swift index cda19e6..e5147e2 100644 --- a/Sources/CleanJSON/CleanJSONKey.swift +++ b/Sources/CleanJSON/CleanJSONKey.swift @@ -8,7 +8,7 @@ import Foundation -struct CleanJSONKey : CodingKey { +struct CleanJSONKey: CodingKey { public var stringValue: String diff --git a/Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift b/Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift index 939ee70..b0a0851 100644 --- a/Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift +++ b/Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift @@ -8,7 +8,7 @@ import Foundation -struct CleanJSONKeyedDecodingContainer: KeyedDecodingContainerProtocol { +struct CleanJSONKeyedDecodingContainer: KeyedDecodingContainerProtocol { typealias Key = K @@ -18,7 +18,7 @@ struct CleanJSONKeyedDecodingContainer: 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] @@ -26,7 +26,7 @@ struct CleanJSONKeyedDecodingContainer: KeyedDecodingContainerPro // 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: @@ -416,7 +416,7 @@ struct CleanJSONKeyedDecodingContainer: KeyedDecodingContainerPro } @inline(__always) - public func decode(_ type: T.Type, forKey key: Key) throws -> T { + public func decode(_ type: T.Type, forKey key: Key) throws -> T { guard let entry = container[key.stringValue] else { switch decoder.options.keyNotFoundDecodingStrategy { case .throw: @@ -487,12 +487,12 @@ struct CleanJSONKeyedDecodingContainer: 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() @@ -863,7 +863,7 @@ extension CleanJSONKeyedDecodingContainer { } @inline(__always) - func decodeIfPresent(_ type: T.Type, forKey key: K) throws -> T? where T : Decodable { + func decodeIfPresent(_ 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) @@ -939,7 +939,7 @@ private extension CleanJSONDecoder.KeyDecodingStrategy { let trailingUnderscoreRange = stringKey.index(after: lastNonUnderscore)..(_ type: T.Type) throws -> T { + public mutating func decode(_ type: T.Type) throws -> T { guard !self.isAtEnd else { throw DecodingError.valueNotFound( type, @@ -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: diff --git a/Sources/CleanJSON/DecodingError+CleanJSON.swift b/Sources/CleanJSON/DecodingError+CleanJSON.swift index 1bc1c22..44050b1 100644 --- a/Sources/CleanJSON/DecodingError+CleanJSON.swift +++ b/Sources/CleanJSON/DecodingError+CleanJSON.swift @@ -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))" diff --git a/Sources/CleanJSON/_CleanJSONDecoder+Decode.swift b/Sources/CleanJSON/_CleanJSONDecoder+Decode.swift index 76c4de1..56a2951 100644 --- a/Sources/CleanJSON/_CleanJSONDecoder+Decode.swift +++ b/Sources/CleanJSON/_CleanJSONDecoder+Decode.swift @@ -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 diff --git a/Sources/CleanJSON/_CleanJSONDecoder+SingleValueDecodingContainer.swift b/Sources/CleanJSON/_CleanJSONDecoder+SingleValueDecodingContainer.swift index 246ca02..944ed2d 100644 --- a/Sources/CleanJSON/_CleanJSONDecoder+SingleValueDecodingContainer.swift +++ b/Sources/CleanJSON/_CleanJSONDecoder+SingleValueDecodingContainer.swift @@ -8,7 +8,7 @@ import Foundation -extension _CleanJSONDecoder : SingleValueDecodingContainer { +extension _CleanJSONDecoder: SingleValueDecodingContainer { // MARK: SingleValueDecodingContainer Methods public func decodeNil() -> Bool { @@ -197,7 +197,7 @@ extension _CleanJSONDecoder : SingleValueDecodingContainer { } } - public func decode(_ type: T.Type) throws -> T { + public func decode(_ 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 { diff --git a/Sources/CleanJSON/_CleanJSONDecoder+Unbox.swift b/Sources/CleanJSON/_CleanJSONDecoder+Unbox.swift index 645f227..3f1daba 100644 --- a/Sources/CleanJSON/_CleanJSONDecoder+Unbox.swift +++ b/Sources/CleanJSON/_CleanJSONDecoder+Unbox.swift @@ -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 @@ -412,7 +412,7 @@ extension _CleanJSONDecoder { return result as? T } - func unbox(_ value: Any, as type: T.Type) throws -> T? { + func unbox(_ value: Any, as type: T.Type) throws -> T? { return try unbox_(value, as: type) as? T } @@ -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 } } diff --git a/Sources/CleanJSON/_CleanJSONDecoder.swift b/Sources/CleanJSON/_CleanJSONDecoder.swift index af3f20e..3d97cf6 100644 --- a/Sources/CleanJSON/_CleanJSONDecoder.swift +++ b/Sources/CleanJSON/_CleanJSONDecoder.swift @@ -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 } @@ -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: diff --git a/Tests/CleanJSONTests/CleanJSONTests.swift b/Tests/CleanJSONTests/CleanJSONTests.swift index dd024fe..773e331 100644 --- a/Tests/CleanJSONTests/CleanJSONTests.swift +++ b/Tests/CleanJSONTests/CleanJSONTests.swift @@ -10,6 +10,6 @@ final class CleanJSONTests: XCTestCase { } static var allTests = [ - ("testExample", testExample), + ("testExample", testExample) ] } diff --git a/Tests/CleanJSONTests/XCTestManifests.swift b/Tests/CleanJSONTests/XCTestManifests.swift index 95f825a..49ba8a5 100644 --- a/Tests/CleanJSONTests/XCTestManifests.swift +++ b/Tests/CleanJSONTests/XCTestManifests.swift @@ -3,7 +3,7 @@ import XCTest #if !canImport(ObjectiveC) public func allTests() -> [XCTestCaseEntry] { return [ - testCase(CleanJSONTests.allTests), + testCase(CleanJSONTests.allTests) ] } #endif