Skip to content

Commit

Permalink
Swiftlint refactoring (#2952)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1205842942115003/1207743897886423/f
What kind of version bump will this require?: No version bump

Swiftlint rules disabled: 
- line_length
- function_parameter_count
- function_body_length
- file_length
- type_body_length removed

cyclomatic_complexity disabled only for switch cases and superfluous
disable removed
  • Loading branch information
federicocappelli authored Jul 10, 2024
1 parent f2d4143 commit 98aaee0
Show file tree
Hide file tree
Showing 69 changed files with 89 additions and 191 deletions.
15 changes: 8 additions & 7 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ disabled_rules:
- line_length
- type_name
- implicit_getter
- function_parameter_count
- trailing_comma
- nesting
- opening_brace
- function_parameter_count
- function_body_length
- file_length
- type_body_length
- non_optional_string_data_conversion
- static_over_final_class

opt_in_rules:
- file_header
Expand All @@ -37,15 +42,11 @@ custom_rules:
severity: error

# Rule Config
cyclomatic_complexity:
ignores_case_statements: true
identifier_name:
min_length: 1
max_length: 1000
file_length:
warning: 1200
error: 1200
type_body_length:
warning: 500
error: 500
large_tuple:
warning: 4
error: 5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1530"
version = "1.8">
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
Expand Down
4 changes: 0 additions & 4 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import NetworkProtectionIPC
import DataBrokerProtection
import RemoteMessaging

// @MainActor
// swiftlint:disable:next type_body_length
final class AppDelegate: NSObject, NSApplicationDelegate {

#if DEBUG
Expand Down Expand Up @@ -149,7 +147,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
return firstLaunchDate >= Date.weekAgo
}

// swiftlint:disable:next function_body_length
override init() {
do {
let encryptionKey = NSApplication.runType.requiresEnvironment ? try keyStore.readKey() : nil
Expand Down Expand Up @@ -278,7 +275,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
vpnUninstaller: vpnUninstaller)
}

// swiftlint:disable:next function_body_length
func applicationDidFinishLaunching(_ notification: Notification) {
guard NSApp.runType.requiresEnvironment else { return }
defer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public class LegacyBookmarksStoreMigration {
return results
}

// swiftlint:disable cyclomatic_complexity
// swiftlint:disable function_body_length
// swiftlint:disable:next cyclomatic_complexity
public static func setupAndMigrate(from source: NSManagedObjectContext,
to destination: NSManagedObjectContext) {

Expand Down Expand Up @@ -173,8 +172,6 @@ public class LegacyBookmarksStoreMigration {
}
}
}
// swiftlint:enable cyclomatic_complexity
// swiftlint:enable function_body_length

private static func cleanupOldData(in context: NSManagedObjectContext) {
let allObjects = (try? context.fetch(BookmarkManagedObject.fetchRequest())) ?? []
Expand Down
2 changes: 0 additions & 2 deletions DuckDuckGo/Bookmarks/Services/LocalBookmarkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import Cocoa
import Persistence
import PixelKit

// swiftlint:disable:next type_body_length
final class LocalBookmarkStore: BookmarkStore {

convenience init(bookmarkDatabase: BookmarkDatabase) {
Expand Down Expand Up @@ -685,7 +684,6 @@ final class LocalBookmarkStore: BookmarkStore {
}
}

// swiftlint:disable:next function_body_length
func moveFavorites(with objectUUIDs: [String], toIndex index: Int?, completion: @escaping (Error?) -> Void) {

applyChangesAndSave(changes: { [weak self] context in
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/View/BookmarkListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class BookmarkListViewController: NSViewController {
fatalError("\(type(of: self)): Bad initializer")
}

override func loadView() { // swiftlint:disable:this function_body_length
override func loadView() {
view = ColorView(frame: .zero, backgroundColor: .popoverBackground)

view.addSubview(titleTextField)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ final class BookmarkManagementDetailViewController: NSViewController, NSMenuItem
fatalError("\(type(of: self)): Bad initializer")
}

// swiftlint:disable:next function_body_length
override func loadView() {
view = ColorView(frame: .zero, backgroundColor: .bookmarkPageBackground)
view.translatesAutoresizingMaskIntoConstraints = false
Expand Down
4 changes: 2 additions & 2 deletions DuckDuckGo/Common/Surveys/SurveyRemoteMessaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ final class DefaultSurveyRemoteMessaging: SurveyRemoteMessaging {
}
}

// swiftlint:disable cyclomatic_complexity function_body_length
// swiftlint:disable cyclomatic_complexity

/// Processes the messages received from S3 and returns those which the user is eligible for. This is done by checking each of the attributes against the user's local state.
/// Because the result of the message fetch is cached, it means that they won't be immediately updated if the user suddenly qualifies, but the refresh interval for remote messages is only 1 hour so it
Expand Down Expand Up @@ -239,7 +239,7 @@ final class DefaultSurveyRemoteMessaging: SurveyRemoteMessaging {
}
}

// swiftlint:enable cyclomatic_complexity function_body_length
// swiftlint:enable cyclomatic_complexity

func presentableRemoteMessages() -> [SurveyRemoteMessage] {
let dismissedMessageIDs = messageStorage.dismissedMessageIDs()
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Common/Surveys/SurveyURLBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ final class SurveyURLBuilder {
self.daysSincePIRLastActive = daysSincePIRLastActive
}

// swiftlint:disable:next cyclomatic_complexity function_body_length
// swiftlint:disable:next cyclomatic_complexity
func buildSurveyURL(from originalURLString: String) -> URL? {
guard var components = URLComponents(string: originalURLString) else {
assertionFailure("Could not build components from survey URL")
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/DBP/DataBrokerProtectionDebugMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ final class DataBrokerProtectionDebugMenu: NSMenu {
private let webUISettings = DataBrokerProtectionWebUIURLSettings(.dbp)
private let settings = DataBrokerProtectionSettings(defaults: .dbp)

// swiftlint:disable:next function_body_length
init() {
super.init(title: "Personal Information Removal")

Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/DBP/DataBrokerProtectionPixelsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import Common

public class DataBrokerProtectionPixelsHandler: EventMapping<DataBrokerProtectionPixels> {

// swiftlint:disable:next function_body_length
public init() {
super.init { event, _, _, _ in
switch event {
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/DataImport/Logins/CSV/CSVParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct CSVParser {
hasPrecedingQuote = false
}

// swiftlint:disable:next cyclomatic_complexity
mutating func accept(_ character: Character) throws {
switch (state, character.kind(delimiter: delimiter), precedingQuote: hasPrecedingQuote) {
case (_, .unsupported, _):
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/DataImport/Model/DataImportViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ struct DataImportViewModel {

/// Called with data import task result to update the state by merging the summary with an existing summary
@MainActor
private mutating func mergeImportSummary(with summary: DataImportSummary) { // swiftlint:disable:this cyclomatic_complexity
private mutating func mergeImportSummary(with summary: DataImportSummary) {
self.importTask = nil

log("merging summary \(summary)")
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/DataImport/Model/InstructionsFormatParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ struct InstructionsFormatParser {
currentEscapeSequence = ""
}

// swiftlint:disable:next cyclomatic_complexity function_body_length
mutating func accept(_ character: Character?) throws {
switch (currentEscapeSequence, character) {
// beggining of %-escaped seq
Expand Down
2 changes: 0 additions & 2 deletions DuckDuckGo/DataImport/ThirdPartyBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ enum ThirdPartyBrowser: CaseIterable {
case onePassword7
case onePassword8

// swiftlint:disable:next cyclomatic_complexity
static func browser(for source: DataImport.Source) -> ThirdPartyBrowser? {
switch source {
case .brave: return .brave
Expand Down Expand Up @@ -254,7 +253,6 @@ enum ThirdPartyBrowser: CaseIterable {
}

// Returns the URL to the profiles for a given browser. This directory will contain a list of directories, each representing a profile.
// swiftlint:disable:next cyclomatic_complexity
func profilesDirectories(applicationSupportURL: URL? = nil) -> [URL] {
let applicationSupportURL = applicationSupportURL ?? URL.nonSandboxApplicationSupportDirectoryURL
return switch self {
Expand Down
2 changes: 0 additions & 2 deletions DuckDuckGo/DataImport/View/DataImportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ struct DataImportView: ModalView {
}
}

// swiftlint:disable:next cyclomatic_complexity
private func viewBody() -> some View {
VStack(alignment: .leading, spacing: 0) {
// body
Expand Down Expand Up @@ -452,7 +451,6 @@ extension DataImportViewModel {
self.dataType = dataType
}

// swiftlint:disable:next function_body_length
func importData(types: Set<DataImport.DataType>) -> DataImportTask {
.detachedWithProgress(.initial) { progressUpdate in
func makeProgress(_ op: (Double) throws -> Void) async throws {
Expand Down
3 changes: 0 additions & 3 deletions DuckDuckGo/DataImport/View/FileImportView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Common
import SwiftUI
import UniformTypeIdentifiers

// swiftlint:disable function_body_length cyclomatic_complexity
@InstructionsView.InstructionsBuilder
func fileImportInstructionsBuilder(source: DataImport.Source, dataType: DataImport.DataType, button: @escaping (String) -> AnyView) -> [InstructionsView.InstructionsItem] {

Expand Down Expand Up @@ -452,7 +451,6 @@ func fileImportInstructionsBuilder(source: DataImport.Source, dataType: DataImpo
assertionFailure("Invalid source/dataType")
}
}
// swiftlint:enable function_body_length cyclomatic_complexity

struct FileImportView: View {

Expand Down Expand Up @@ -592,7 +590,6 @@ struct InstructionsView: View {
// View Model
private let instructions: [[InstructionsViewItem]]

// swiftlint:disable:next function_body_length cyclomatic_complexity
init(@InstructionsBuilder builder: () -> [InstructionsItem]) {
var args = builder()

Expand Down
2 changes: 0 additions & 2 deletions DuckDuckGo/Email/EmailManagerRequestDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extension EmailManagerRequestDelegate {
set {}
}

// swiftlint:disable function_parameter_count
func emailManager(_ emailManager: EmailManager, requested url: URL, method: String, headers: [String: String], parameters: [String: String]?, httpBody: Data?, timeoutInterval: TimeInterval) async throws -> Data {
let finalURL = url.appendingParameters(parameters ?? [:])
var request = URLRequest(url: finalURL, timeoutInterval: timeoutInterval)
Expand All @@ -45,7 +44,6 @@ extension EmailManagerRequestDelegate {

return data
}
// swiftlint:enable function_parameter_count

public func emailManagerKeychainAccessFailed(_ emailManager: EmailManager, accessType: EmailKeychainAccessType, error: EmailKeychainAccessError) {
var parameters = [
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/FileDownload/View/DownloadsCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ final class DownloadsCellView: NSTableCellView {
fatalError("\(Self.self): Bad initializer")
}

// swiftlint:disable:next function_body_length
private func setupUI() {
self.imageView = fileIconView
self.wantsLayer = true
Expand Down
5 changes: 1 addition & 4 deletions DuckDuckGo/FileDownload/View/DownloadsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ final class DownloadsViewController: NSViewController {
super.init(coder: coder)
}

override func loadView() { // swiftlint:disable:this function_body_length
override func loadView() {
view = NSView()

view.addSubview(titleLabel)
Expand Down Expand Up @@ -373,7 +373,6 @@ final class DownloadsViewController: NSViewController {

extension DownloadsViewController: NSMenuDelegate {

// swiftlint:disable cyclomatic_complexity
func menuNeedsUpdate(_ menu: NSMenu) {
guard let index = index(for: menu),
let item = viewModel.items[safe: index]
Expand Down Expand Up @@ -412,8 +411,6 @@ extension DownloadsViewController: NSMenuDelegate {
}
}
}
// swiftlint:enable cyclomatic_complexity

}

extension DownloadsViewController: NSTableViewDataSource, NSTableViewDelegate {
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/History/Services/EncryptedHistoryStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ final class EncryptedHistoryStore: HistoryStoring {
}
}

// swiftlint:disable:next function_body_length
func save(entry: HistoryEntry) -> Future<[(id: Visit.ID, date: Date)], Error> {
return Future { [weak self] promise in
self?.context.perform { [weak self] in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ final class RecentlyVisitedSiteModel: ObservableObject {
return Self.feedImage(for: trackerNetwork)
}

// swiftlint:disable:next cyclomatic_complexity
static func feedImage(for trackerNetwork: TrackerNetwork) -> NSImage? {
switch trackerNetwork {
case .adform: .feedAdform
Expand Down
5 changes: 2 additions & 3 deletions DuckDuckGo/Menus/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import NetworkProtection
import Subscription
import SubscriptionUI

// swiftlint:disable:next type_body_length
@MainActor final class MainMenu: NSMenu {
@MainActor
final class MainMenu: NSMenu {

enum Constants {
static let maxTitleLength = 55
Expand Down Expand Up @@ -553,7 +553,6 @@ import SubscriptionUI

let internalUserItem = NSMenuItem(title: "Set Internal User State", action: #selector(MainViewController.internalUserState))

// swiftlint:disable:next function_body_length
private func setupDebugMenu() -> NSMenu {
let debugMenu = NSMenu(title: "Debug") {
NSMenuItem(title: "Open Vanilla Browser", action: #selector(MainViewController.openVanillaBrowser)).withAccessibilityIdentifier("MainMenu.openVanillaBrowser")
Expand Down
5 changes: 0 additions & 5 deletions DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,6 @@ extension MainViewController {

extension MainViewController: NSMenuItemValidation {

// swiftlint:disable cyclomatic_complexity
// swiftlint:disable function_body_length
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
guard fireViewController.fireViewModel.fire.burningData == nil else {
return true
Expand Down Expand Up @@ -1068,9 +1066,6 @@ extension MainViewController: NSMenuItemValidation {
return true
}
}

// swiftlint:enable function_body_length
// swiftlint:enable cyclomatic_complexity
}

extension AppDelegate: NSMenuItemValidation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ protocol AddressBarButtonsViewControllerDelegate: AnyObject {

}

// swiftlint:disable:next type_body_length
final class AddressBarButtonsViewController: NSViewController {

weak var delegate: AddressBarButtonsViewControllerDelegate?
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/NavigationBar/View/AddressBarTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import PixelKit
import Suggestions
import Subscription

// swiftlint:disable:next type_body_length
final class AddressBarTextField: NSTextField {

var tabCollectionViewModel: TabCollectionViewModel! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import NetworkProtectionUI
import Subscription
import SubscriptionUI

// swiftlint:disable:next type_body_length
final class NavigationBarViewController: NSViewController {

enum Constants {
Expand Down
3 changes: 0 additions & 3 deletions DuckDuckGo/NavigationBar/ViewModel/PrivacyIconViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ struct PrivacyIconViewModel {
}()?.cgImage(forProposedRect: nil, context: .current, hints: nil)
}

// swiftlint:disable cyclomatic_complexity
private static func aquaLogo(for trackerNetwork: TrackerNetwork) -> NSImage? {
switch trackerNetwork {
case .adform: .adform
Expand Down Expand Up @@ -216,8 +215,6 @@ struct PrivacyIconViewModel {
case .xaxis: .xaxisDark
}
}
// swiftlint:enable cyclomatic_complexity

}

extension Character {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ final class NetworkProtectionDebugMenu: NSMenu {

private let excludeLocalNetworksMenuItem = NSMenuItem(title: "excludeLocalNetworks", action: #selector(NetworkProtectionDebugMenu.toggleShouldExcludeLocalRoutes))

// swiftlint:disable:next function_body_length
init() {
preferredServerMenu = NSMenu { [preferredServerAutomaticItem] in
preferredServerAutomaticItem
Expand Down
Loading

0 comments on commit 98aaee0

Please sign in to comment.