Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 18, 2024
1 parent 2dad0e4 commit 0f73f94
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 51 deletions.
15 changes: 13 additions & 2 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ only_rules:
- discouraged_direct_init
- discouraged_none_name
- discouraged_object_literal
- discouraged_optional_boolean
- discouraged_optional_collection
- duplicate_conditions
- duplicate_enum_cases
Expand Down Expand Up @@ -79,6 +80,7 @@ only_rules:
- no_extension_access_modifier
- no_fallthrough_only
- no_space_in_method_call
- non_overridable_class_declaration
- notification_center_detachment
- ns_number_init_as_function_reference
- nsobject_prefer_isequal
Expand All @@ -93,6 +95,7 @@ only_rules:
- private_action
- private_outlet
- private_subject
- private_swiftui_state
- private_unit_test
- prohibited_super_call
- protocol_property_accessors_order
Expand Down Expand Up @@ -132,6 +135,7 @@ only_rules:
- unavailable_condition
- unavailable_function
- unneeded_break_in_switch
- unneeded_override
- unneeded_parentheses_in_closure_argument
- unowned_variable_capture
- untyped_error_in_catch
Expand Down Expand Up @@ -191,9 +195,16 @@ custom_rules:
regex: '\bNSPoint\b'
match_kinds: typeidentifier
message: 'Use CGPoint instead of NSPoint'
no_cgfloat:
regex: '\bCGFloat\b'
match_kinds: typeidentifier
message: 'Use Double instead of CGFloat'
no_cgfloat2:
regex: '\bCGFloat\('
message: 'Use Double instead of CGFloat'
swiftui_state_private:
regex: '@(State|StateObject|ObservedObject|EnvironmentObject)\s+var'
message: 'SwiftUI @State/@StateObject/@ObservedObject/@EnvironmentObject properties should be private'
regex: '@(ObservedObject|EnvironmentObject)\s+var'
message: 'SwiftUI @ObservedObject/@EnvironmentObject properties should be private'
swiftui_environment_private:
regex: '@Environment\(\\\.\w+\)\s+var'
message: 'SwiftUI @Environment properties should be private'
Expand Down
4 changes: 2 additions & 2 deletions Sources/Defaults/Defaults+AnySerializable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ extension Defaults.AnySerializable: Hashable {
hasher.combine(value)
case let value as Double:
hasher.combine(value)
case let value as CGFloat:
case let value as CGFloat: // swiftlint:disable:this no_cgfloat
hasher.combine(value)
case let value as String:
hasher.combine(value)
Expand Down Expand Up @@ -135,7 +135,7 @@ extension Defaults.AnySerializable: Equatable {
lhs == rhs
case (let lhs as Double, let rhs as Double):
lhs == rhs
case (let lhs as CGFloat, let rhs as CGFloat):
case (let lhs as CGFloat, let rhs as CGFloat): // swiftlint:disable:this no_cgfloat
lhs == rhs
case (let lhs as String, let rhs as String):
lhs == rhs
Expand Down
2 changes: 1 addition & 1 deletion Sources/Defaults/Defaults+Bridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ extension Defaults {
let object = object as? [Any],
let rawColorspace = object[0] as? String,
let colorspace = CGColorSpace(name: rawColorspace as CFString),
let components = object[1] as? [CGFloat],
let components = object[1] as? [CGFloat], // swiftlint:disable:this no_cgfloat
let cgColor = CGColor(colorSpace: colorspace, components: components)
else {
return nil
Expand Down
1 change: 1 addition & 0 deletions Sources/Defaults/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extension String: Defaults.Serializable {
public static let isNativelySupportedType = true
}

// swiftlint:disable:next no_cgfloat
extension CGFloat: Defaults.Serializable {
public static let isNativelySupportedType = true
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Defaults/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension Defaults {
final class Observable<Value: Serializable>: ObservableObject {
private var cancellable: AnyCancellable?
private var task: Task<Void, Never>?

var key: Defaults.Key<Value> {
didSet {
if key != oldValue {
Expand All @@ -24,14 +24,14 @@ extension Defaults {

init(_ key: Key<Value>) {
self.key = key

observe()
}

deinit {
task?.cancel()
}

func observe() {
// We only use this on the latest OSes (as of adding this) since the backdeploy library has a lot of bugs.
if #available(macOS 13, iOS 16, tvOS 16, watchOS 9, *) {
Expand Down
10 changes: 5 additions & 5 deletions Tests/DefaultsTests/DefaultsAnySeriliazableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ final class DefaultsAnySerializableTests: XCTestCase {
XCTAssertEqual(Defaults[any].get()!, URL(string: "https://example.com")!)
#if os(macOS)
// Test NSColor
Defaults[any].set(NSColor(red: CGFloat(103) / CGFloat(0xFF), green: CGFloat(132) / CGFloat(0xFF), blue: CGFloat(255) / CGFloat(0xFF), alpha: 0.987))
Defaults[any].set(NSColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 0.987))
XCTAssertEqual(Defaults[any].get(NSColor.self)?.alphaComponent, 0.987)
#else
// Test UIColor
Defaults[any].set(UIColor(red: CGFloat(103) / CGFloat(0xFF), green: CGFloat(132) / CGFloat(0xFF), blue: CGFloat(255) / CGFloat(0xFF), alpha: 0.654))
Defaults[any].set(UIColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 0.654))
XCTAssertEqual(Defaults[any].get(UIColor.self)?.cgColor.alpha, 0.654)
#endif
// Test Codable type
Expand Down Expand Up @@ -256,7 +256,7 @@ final class DefaultsAnySerializableTests: XCTestCase {
Defaults[key].insert(float)
XCTAssertTrue(Defaults[key].contains(float))

let cgFloat = Defaults.AnySerializable(CGFloat(12_131.415))
let cgFloat = Defaults.AnySerializable(CGFloat(12_131.415)) // swiftlint:disable:this no_cgfloat2
Defaults[key].insert(cgFloat)
XCTAssertTrue(Defaults[key].contains(cgFloat))

Expand All @@ -278,7 +278,7 @@ final class DefaultsAnySerializableTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[Defaults.AnySerializable]?>("testArrayOptionalAnyKey")
let key = Defaults.Key<[Defaults.AnySerializable]?>("testArrayOptionalAnyKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [123]
Defaults[key]?.append(456)
Expand Down Expand Up @@ -315,7 +315,7 @@ final class DefaultsAnySerializableTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Defaults.AnySerializable]?>("independentDictionaryOptionalAnyKey")
let key = Defaults.Key<[String: Defaults.AnySerializable]?>("independentDictionaryOptionalAnyKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["unicorn": "🦄"]
XCTAssertEqual(Defaults[key]?["unicorn"], "🦄")
Expand Down
6 changes: 3 additions & 3 deletions Tests/DefaultsTests/DefaultsArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class DefaultsArrayTests: XCTestCase {
}

func testOptionalKey() {
let key = Defaults.Key<[String]?>("independentArrayOptionalStringKey")
let key = Defaults.Key<[String]?>("independentArrayOptionalStringKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = fixtureArray
XCTAssertEqual(Defaults[key]?[0], fixtureArray[0])
Expand Down Expand Up @@ -108,7 +108,7 @@ final class DefaultsArrayTests: XCTestCase {
}

func testObserveOptionalKeyCombine() {
let key = Defaults.Key<[String]?>("observeArrayOptionalKeyCombine")
let key = Defaults.Key<[String]?>("observeArrayOptionalKeyCombine") // swiftlint:disable:this discouraged_optional_collection
let newName = ["Chen"]
let expect = expectation(description: "Observation closure being called")

Expand Down Expand Up @@ -157,7 +157,7 @@ final class DefaultsArrayTests: XCTestCase {
}

func testObserveOptionalKey() {
let key = Defaults.Key<[String]?>("observeArrayOptionalKey")
let key = Defaults.Key<[String]?>("observeArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
let expect = expectation(description: "Observation closure being called")

var observation: Defaults.Observation!
Expand Down
4 changes: 2 additions & 2 deletions Tests/DefaultsTests/DefaultsCodableEnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DefaultsCodableEnumTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[FixtureCodableEnum]?>("independentCodableEnumArrayOptionalKey")
let key = Defaults.Key<[FixtureCodableEnum]?>("independentCodableEnumArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [.halfHour]
}
Expand Down Expand Up @@ -86,7 +86,7 @@ final class DefaultsCodableEnumTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: FixtureCodableEnum]?>("independentCodableEnumDictionaryOptionalKey")
let key = Defaults.Key<[String: FixtureCodableEnum]?>("independentCodableEnumDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": .tenMinutes]
Defaults[key]?["1"] = .halfHour
Expand Down
6 changes: 3 additions & 3 deletions Tests/DefaultsTests/DefaultsCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ final class DefaultsCodableTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[Unicorn]?>("independentCodableArrayOptionalKey")
let key = Defaults.Key<[Unicorn]?>("independentCodableArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [fixtureCodable]
Defaults[key]?.append(Unicorn(isUnicorn: false))
Expand Down Expand Up @@ -111,7 +111,7 @@ final class DefaultsCodableTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Unicorn]?>("independentCodableDictionaryOptionalKey")
let key = Defaults.Key<[String: Unicorn]?>("independentCodableDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": fixtureCodable]
Defaults[key]?["1"] = Unicorn(isUnicorn: false)
Expand Down Expand Up @@ -209,7 +209,7 @@ final class DefaultsCodableTests: XCTestCase {
.map { ($0.oldValue?.isUnicorn, $0.newValue?.isUnicorn) }
.collect(2)

let expectedValue: [(Bool?, Bool?)] = [(nil, true), (true, nil)]
let expectedValue: [(Bool?, Bool?)] = [(nil, true), (true, nil)] // swiftlint:disable:this discouraged_optional_boolean

let cancellable = publisher.sink { tuples in
for (index, expected) in expectedValue.enumerated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ final class DefaultsCollectionCustomElementTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[Bag<Item>]?>("independentCollectionCustomElementArrayOptionalKey")
let key = Defaults.Key<[Bag<Item>]?>("independentCollectionCustomElementArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
Defaults[key] = [.init(items: [fixtureCustomCollection])]
Defaults[key]?[0].insert(element: fixtureCustomCollection1, at: 1)
Defaults[key]?.append(Bag(items: [fixtureCustomCollection2]))
Expand Down Expand Up @@ -126,7 +126,7 @@ final class DefaultsCollectionCustomElementTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Bag<Item>]?>("independentCollectionCustomElementDictionaryOptionalKey")
let key = Defaults.Key<[String: Bag<Item>]?>("independentCollectionCustomElementDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
Defaults[key] = ["0": .init(items: [fixtureCustomCollection])]
Defaults[key]?["0"]?.insert(element: fixtureCustomCollection1, at: 1)
Defaults[key]?["1"] = .init(items: [fixtureCustomCollection2])
Expand Down
4 changes: 2 additions & 2 deletions Tests/DefaultsTests/DefaultsCollectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ final class DefaultsCollectionTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[Bag<String>]?>("independentCollectionArrayOptionalKey")
let key = Defaults.Key<[Bag<String>]?>("independentCollectionArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [Bag(items: [fixtureCollection[0]])]
Defaults[key]?.append(Bag(items: [fixtureCollection[1]]))
Expand Down Expand Up @@ -118,7 +118,7 @@ final class DefaultsCollectionTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: Bag<String>]?>("independentCollectionDictionaryOptionalKey")
let key = Defaults.Key<[String: Bag<String>]?>("independentCollectionDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": Bag(items: [fixtureCollection[0]])]
Defaults[key]?["0"]?.insert(element: fixtureCollection[1], at: 1)
Expand Down
4 changes: 2 additions & 2 deletions Tests/DefaultsTests/DefaultsCustomBridgeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ final class DefaultsCustomBridge: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[User]?>("independentCustomBridgeArrayOptionalKey")
let key = Defaults.Key<[User]?>("independentCustomBridgeArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
let newUser = User(username: "hank121314", password: "123456")
Defaults[key] = [newUser]
Expand Down Expand Up @@ -174,7 +174,7 @@ final class DefaultsCustomBridge: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: User]?>("independentCustomBridgeDictionaryOptionalKey")
let key = Defaults.Key<[String: User]?>("independentCustomBridgeDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": fixtureCustomBridge]
XCTAssertEqual(Defaults[key]?["0"], fixtureCustomBridge)
Expand Down
6 changes: 3 additions & 3 deletions Tests/DefaultsTests/DefaultsDictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ final class DefaultsDictionaryTests: XCTestCase {
}

func testOptionalKey() {
let key = Defaults.Key<[String: String]?>("independentDictionaryOptionalKey")
let key = Defaults.Key<[String: String]?>("independentDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = fixtureDictionary
XCTAssertEqual(Defaults[key]?["0"], fixtureDictionary["0"])
Expand Down Expand Up @@ -100,7 +100,7 @@ final class DefaultsDictionaryTests: XCTestCase {
}

func testObserveOptionalKeyCombine() {
let key = Defaults.Key<[String: String]?>("observeDictionaryOptionalKeyCombine")
let key = Defaults.Key<[String: String]?>("observeDictionaryOptionalKeyCombine") // swiftlint:disable:this discouraged_optional_collection
let expect = expectation(description: "Observation closure being called")
let newName = ["0": "John"]
let publisher = Defaults
Expand Down Expand Up @@ -148,7 +148,7 @@ final class DefaultsDictionaryTests: XCTestCase {
}

func testObserveOptionalKey() {
let key = Defaults.Key<[String: String]?>("observeDictionaryOptionalKey")
let key = Defaults.Key<[String: String]?>("observeDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
let expect = expectation(description: "Observation closure being called")

var observation: Defaults.Observation!
Expand Down
4 changes: 2 additions & 2 deletions Tests/DefaultsTests/DefaultsEnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class DefaultsEnumTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[FixtureEnum]?>("independentEnumArrayOptionalKey")
let key = Defaults.Key<[FixtureEnum]?>("independentEnumArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [.tenMinutes]
Defaults[key]?.append(.halfHour)
Expand Down Expand Up @@ -83,7 +83,7 @@ final class DefaultsEnumTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: FixtureEnum]?>("independentEnumDictionaryOptionalKey")
let key = Defaults.Key<[String: FixtureEnum]?>("independentEnumDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": .tenMinutes]
XCTAssertEqual(Defaults[key]?["0"], .tenMinutes)
Expand Down
10 changes: 5 additions & 5 deletions Tests/DefaultsTests/DefaultsNSColorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Defaults
import XCTest
import AppKit

private let fixtureColor = NSColor(red: CGFloat(103) / CGFloat(0xFF), green: CGFloat(132) / CGFloat(0xFF), blue: CGFloat(255) / CGFloat(0xFF), alpha: 1)
private let fixtureColor1 = NSColor(red: CGFloat(255) / CGFloat(0xFF), green: CGFloat(241) / CGFloat(0xFF), blue: CGFloat(180) / CGFloat(0xFF), alpha: 1)
private let fixtureColor2 = NSColor(red: CGFloat(255) / CGFloat(0xFF), green: CGFloat(180) / CGFloat(0xFF), blue: CGFloat(194) / CGFloat(0xFF), alpha: 1)
private let fixtureColor = NSColor(red: Double(103) / Double(0xFF), green: Double(132) / Double(0xFF), blue: Double(255) / Double(0xFF), alpha: 1)
private let fixtureColor1 = NSColor(red: Double(255) / Double(0xFF), green: Double(241) / Double(0xFF), blue: Double(180) / Double(0xFF), alpha: 1)
private let fixtureColor2 = NSColor(red: Double(255) / Double(0xFF), green: Double(180) / Double(0xFF), blue: Double(194) / Double(0xFF), alpha: 1)

extension Defaults.Keys {
fileprivate static let color = Defaults.Key<NSColor>("NSColor", default: fixtureColor)
Expand Down Expand Up @@ -57,7 +57,7 @@ final class DefaultsNSColorTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[NSColor]?>("independentNSColorOptionalKey")
let key = Defaults.Key<[NSColor]?>("independentNSColorOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [fixtureColor]
Defaults[key]?.append(fixtureColor1)
Expand Down Expand Up @@ -91,7 +91,7 @@ final class DefaultsNSColorTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: NSColor]?>("independentNSColorDictionaryOptionalKey")
let key = Defaults.Key<[String: NSColor]?>("independentNSColorDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": fixtureColor]
Defaults[key]?["1"] = fixtureColor1
Expand Down
6 changes: 3 additions & 3 deletions Tests/DefaultsTests/DefaultsNSSecureCodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private final class ExamplePersistentHistory: NSPersistentHistoryToken, Defaults
coder.encode(value, forKey: "value")
}

override class var supportsSecureCoding: Bool { true }
override class var supportsSecureCoding: Bool { true } // swiftlint:disable:this non_overridable_class_declaration
}

// NSSecureCoding
Expand Down Expand Up @@ -77,7 +77,7 @@ final class DefaultsNSSecureCodingTests: XCTestCase {
}

func testArrayOptionalKey() {
let key = Defaults.Key<[ExamplePersistentHistory]?>("independentNSSecureCodingArrayOptionalKey")
let key = Defaults.Key<[ExamplePersistentHistory]?>("independentNSSecureCodingArrayOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = [persistentHistoryValue]
XCTAssertEqual(Defaults[key]?[0].value, persistentHistoryValue.value)
Expand Down Expand Up @@ -120,7 +120,7 @@ final class DefaultsNSSecureCodingTests: XCTestCase {
}

func testDictionaryOptionalKey() {
let key = Defaults.Key<[String: ExamplePersistentHistory]?>("independentNSSecureCodingDictionaryOptionalKey")
let key = Defaults.Key<[String: ExamplePersistentHistory]?>("independentNSSecureCodingDictionaryOptionalKey") // swiftlint:disable:this discouraged_optional_collection
XCTAssertNil(Defaults[key])
Defaults[key] = ["0": persistentHistoryValue]
XCTAssertEqual(Defaults[key]?["0"]?.value, persistentHistoryValue.value)
Expand Down
Loading

0 comments on commit 0f73f94

Please sign in to comment.