Skip to content

Commit 8cfc48a

Browse files
author
gaoxiang
committed
build: 新增 swiftlint
1 parent 6e2f211 commit 8cfc48a

15 files changed

Lines changed: 75 additions & 41 deletions

.swiftlint.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
included:
3+
excluded:
4+
- Carthage
5+
- Pods
6+
line_length:
7+
warning: 120
8+
ignores_function_declarations: true
9+
ignores_comments: true
10+
disabled_rules:
11+
- empty_enum_arguments
12+
- trailing_whitespace
13+
- cyclomatic_complexity
14+
- file_length
15+
- type_name
16+
- inclusive_language
17+
- function_parameter_count
18+
- identifier_name
19+
- type_body_length
20+
- function_body_length
21+
- nesting
22+
- unneeded_break_in_switch
23+
identifier_name:
24+
excluded:
25+
- i
26+
- idx
27+
- h
28+
- w
29+
- x
30+
- y
31+
- id
32+
- url
33+
- min
34+
- max
35+
- r
36+
- g
37+
- b
38+
- a

Example/CleanJSON/AppDelegate.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1313

1414
var window: UIWindow?
1515

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

44-
4543
}
46-

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let package = Package(
99
// Products define the executables and libraries produced by a package, and make them visible to other packages.
1010
.library(
1111
name: "CleanJSON",
12-
targets: ["CleanJSON"]),
12+
targets: ["CleanJSON"])
1313
],
1414
dependencies: [
1515
// Dependencies declare other packages that this package depends on.
@@ -23,6 +23,6 @@ let package = Package(
2323
dependencies: []),
2424
.testTarget(
2525
name: "CleanJSONTests",
26-
dependencies: ["CleanJSON"]),
26+
dependencies: ["CleanJSON"])
2727
]
2828
)

Sources/CleanJSON/CaseDefaultable.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@ private extension _CleanJSONDecoder {
3333
func decodeCase<T>(_ type: T.Type) throws -> T
3434
where T: CaseDefaultable,
3535
T: Decodable,
36-
T.RawValue: Decodable
37-
{
36+
T.RawValue: Decodable {
3837
guard !decodeNil(), !storage.containers.isEmpty, storage.topContainer is T.RawValue else {
3938
return T.defaultCase
4039
}
4140

4241
if let number = storage.topContainer as? NSNumber,
43-
(number === kCFBooleanTrue || number === kCFBooleanFalse) {
42+
number === kCFBooleanTrue || number === kCFBooleanFalse {
4443
guard let rawValue = number.boolValue as? T.RawValue else {
4544
return T.defaultCase
4645
}

Sources/CleanJSON/CleanJSONDecoder.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open class CleanJSONDecoder: JSONDecoder {
3232
let valueNotFoundDecodingStrategy: ValueNotFoundDecodingStrategy
3333
let nestedContainerDecodingStrategy: NestedContainerDecodingStrategy
3434
let jsonStringDecodingStrategy: JSONStringDecodingStrategy
35-
let userInfo: [CodingUserInfoKey : Any]
35+
let userInfo: [CodingUserInfoKey: Any]
3636
}
3737

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

105105
private extension CleanJSONDecoder {
106106

107-
func decode<T : Decodable>(
107+
func decode<T: Decodable>(
108108
_ type: T.Type,
109109
from container: Any
110110
) throws -> T {

Sources/CleanJSON/CleanJSONKey.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
struct CleanJSONKey : CodingKey {
11+
struct CleanJSONKey: CodingKey {
1212

1313
public var stringValue: String
1414

Sources/CleanJSON/CleanJSONKeyedDecodingContainer.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
struct CleanJSONKeyedDecodingContainer<K : CodingKey>: KeyedDecodingContainerProtocol {
11+
struct CleanJSONKeyedDecodingContainer<K: CodingKey>: KeyedDecodingContainerProtocol {
1212

1313
typealias Key = K
1414

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

2020
/// A reference to the container we're reading from.
21-
private let container: [String : Any]
21+
private let container: [String: Any]
2222

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

2626
// MARK: - Initialization
2727

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

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

490-
guard let dictionary = value as? [String : Any] else {
490+
guard let dictionary = value as? [String: Any] else {
491491
switch decoder.options.nestedContainerDecodingStrategy.typeMismatch {
492492
case .throw:
493493
throw DecodingError._typeMismatch(
494494
at: self.codingPath,
495-
expectation: [String : Any].self,
495+
expectation: [String: Any].self,
496496
reality: value)
497497
case .useEmptyContainer:
498498
return nestedContainer()
@@ -863,7 +863,7 @@ extension CleanJSONKeyedDecodingContainer {
863863
}
864864

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

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

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

950950
// Do a cheap isEmpty check before creating and appending potentially empty strings
951-
let result : String
952-
if (leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty) {
951+
let result: String
952+
if leadingUnderscoreRange.isEmpty && trailingUnderscoreRange.isEmpty {
953953
result = joinedString
954-
} else if (!leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty) {
954+
} else if !leadingUnderscoreRange.isEmpty && !trailingUnderscoreRange.isEmpty {
955955
// Both leading and trailing underscores
956956
result = String(stringKey[leadingUnderscoreRange]) + joinedString + String(stringKey[trailingUnderscoreRange])
957-
} else if (!leadingUnderscoreRange.isEmpty) {
957+
} else if !leadingUnderscoreRange.isEmpty {
958958
// Just leading
959959
result = String(stringKey[leadingUnderscoreRange]) + joinedString
960960
} else {

Sources/CleanJSON/CleanJSONUnkeyedDecodingContainer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
11+
struct CleanJSONUnkeyedDecodingContainer: UnkeyedDecodingContainer {
1212
// MARK: Properties
1313

1414
/// A reference to the decoder we're reading from.
@@ -394,7 +394,7 @@ struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
394394
}
395395
}
396396

397-
public mutating func decode<T : Decodable>(_ type: T.Type) throws -> T {
397+
public mutating func decode<T: Decodable>(_ type: T.Type) throws -> T {
398398
guard !self.isAtEnd else {
399399
throw DecodingError.valueNotFound(
400400
type,
@@ -451,12 +451,12 @@ struct CleanJSONUnkeyedDecodingContainer : UnkeyedDecodingContainer {
451451
}
452452
}
453453

454-
guard let dictionary = value as? [String : Any] else {
454+
guard let dictionary = value as? [String: Any] else {
455455
switch decoder.options.nestedContainerDecodingStrategy.typeMismatch {
456456
case .throw:
457457
throw DecodingError._typeMismatch(
458458
at: self.codingPath,
459-
expectation: [String : Any].self,
459+
expectation: [String: Any].self,
460460
reality: value
461461
)
462462
case .useEmptyContainer:

Sources/CleanJSON/DecodingError+CleanJSON.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension DecodingError {
3434
return "a string/data"
3535
} else if value is [Any] {
3636
return "an array"
37-
} else if value is [String : Any] {
37+
} else if value is [String: Any] {
3838
return "a dictionary"
3939
} else {
4040
return "\(type(of: value))"

Sources/CleanJSON/_CleanJSONDecoder+Decode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extension _CleanJSONDecoder {
1919
return bool
2020
} else if let int = Int.defaultValue as? T {
2121
return int
22-
}else if let double = Double.defaultValue as? T {
22+
} else if let double = Double.defaultValue as? T {
2323
return double
2424
} else if let date = Date.defaultValue(for: options.dateDecodingStrategy) as? T {
2525
return date

0 commit comments

Comments
 (0)