style: satisfy swiftlint

This commit is contained in:
Peter Steinberger 2025-11-12 23:59:58 +00:00
parent 0ca1293248
commit 7aa64423d8
17 changed files with 146 additions and 81 deletions

View File

@ -3,8 +3,6 @@
import CoreGraphics
import Foundation
// swiftlint:disable file_length
// MARK: - AXCommand and Command Structs
/// Enumeration of all supported AXorcist accessibility commands.
@ -501,5 +499,3 @@ public struct AXBatchCommand: Sendable {
/// depending on the configuration.
public let commands: [SubCommandEnvelope]
}
// swiftlint:enable file_length

View File

@ -8,8 +8,6 @@
import ApplicationServices
import Foundation
// swiftlint:disable file_length
/// Centralized manager for AXObserver instances that coordinates accessibility notifications.
///
/// AXObserverCenter provides:
@ -465,5 +463,3 @@ extension AXObserverCenter {
return tempDict
}
}
// swiftlint:enable file_length

View File

@ -34,14 +34,15 @@ extension AXUIElement {
AXAttributeNames.kAXFocusedApplicationAttribute as CFString,
&focusedApp)
guard error == .success, let app = focusedApp, CFGetTypeID(app) == AXUIElementGetTypeID() else {
guard error == .success, let app = focusedApp, CFGetTypeID(app) == AXUIElementGetTypeID(),
let axApp = app as? AXUIElement else {
let errorDetails = "AXError: \(error.rawValue) - \(error.localizedDescription)"
axErrorLog("Failed to get focused application: \(errorDetails)")
throw AccessibilityError.attributeNotReadable(
attribute: AXAttributeNames.kAXFocusedApplicationAttribute,
elementDescription: "SystemWideElement")
}
return app as! AXUIElement
return axApp
}
/// Returns the frontmost application using NSWorkspace
@ -102,12 +103,12 @@ extension AXUIElement {
guard error == .success,
let window = focusedWindow,
CFGetTypeID(window) == AXUIElementGetTypeID()
else {
CFGetTypeID(window) == AXUIElementGetTypeID(),
let axWindow = window as? AXUIElement else {
return nil
}
return (window as! AXUIElement)
return axWindow
}
/// Returns the main window in the frontmost application
@ -125,12 +126,12 @@ extension AXUIElement {
guard error == .success,
let window = mainWindow,
CFGetTypeID(window) == AXUIElementGetTypeID()
else {
CFGetTypeID(window) == AXUIElementGetTypeID(),
let axWindow = window as? AXUIElement else {
return nil
}
return (window as! AXUIElement)
return axWindow
}
/// Returns all windows for the specified application

View File

@ -2,8 +2,6 @@
import Foundation
// swiftlint:disable file_length
// MARK: - Accessibility Action Names
public enum AXActionNames {
@ -439,5 +437,3 @@ public enum AXMiscConstants {
// pathHintAttributeKey was for Element.swift's pathHint property, which is different from
// AXAttributeNames.kAXPathHintAttribute
}
// swiftlint:enable file_length

View File

@ -244,8 +244,7 @@ extension Element {
AXAttributeNames.kAXURLAttribute as CFString,
&value)
guard error == .success,
let cfURL = value as! CFURL?
else {
let cfURL = value as? CFURL else {
return nil
}
return cfURL as URL

View File

@ -55,10 +55,18 @@ extension Element {
private func convertToString(_ cfValue: CFTypeRef, cfTypeID: CFTypeID) -> String? {
if cfTypeID == CFStringGetTypeID() {
let cfString = cfValue as! CFString
guard let cfString = cfValue as? CFString else {
GlobalAXLogger.shared.log(
AXLogEntry(level: .warning, message: "Failed to cast CFTypeRef to CFString"))
return nil
}
return cfString as String
} else if cfTypeID == CFAttributedStringGetTypeID() {
let attrString = cfValue as! NSAttributedString
guard let attrString = cfValue as? NSAttributedString else {
GlobalAXLogger.shared.log(
AXLogEntry(level: .warning, message: "Failed to cast CFTypeRef to NSAttributedString"))
return nil
}
return attrString.string
}
return nil
@ -66,7 +74,11 @@ extension Element {
private func convertToBool(_ cfValue: CFTypeRef, cfTypeID: CFTypeID) -> Bool? {
if cfTypeID == CFBooleanGetTypeID() {
let cfBool = cfValue as! CFBoolean
guard let cfBool = cfValue as? CFBoolean else {
GlobalAXLogger.shared.log(
AXLogEntry(level: .warning, message: "Failed to cast CFTypeRef to CFBoolean"))
return nil
}
return CFBooleanGetValue(cfBool)
}
return nil
@ -74,7 +86,11 @@ extension Element {
private func convertToInt(_ cfValue: CFTypeRef, cfTypeID: CFTypeID) -> Int? {
if cfTypeID == CFNumberGetTypeID() {
let cfNumber = cfValue as! CFNumber
guard let cfNumber = cfValue as? CFNumber else {
GlobalAXLogger.shared.log(
AXLogEntry(level: .warning, message: "Failed to cast CFTypeRef to CFNumber"))
return nil
}
var intValue = 0
if CFNumberGetValue(cfNumber, .sInt64Type, &intValue) {
return intValue
@ -85,7 +101,11 @@ extension Element {
private func convertToAXUIElement(_ cfValue: CFTypeRef, cfTypeID: CFTypeID) -> AXUIElement? {
if cfTypeID == AXUIElementGetTypeID() {
let element = cfValue as! AXUIElement
guard let element = cfValue as? AXUIElement else {
GlobalAXLogger.shared.log(
AXLogEntry(level: .warning, message: "Failed to cast CFTypeRef to AXUIElement"))
return nil
}
return element
}
return nil

View File

@ -1,8 +1,6 @@
import AppKit
import ApplicationServices
// swiftlint:disable file_length
// MARK: - Mouse Button Types
public enum MouseButton: String, Sendable {
@ -582,5 +580,3 @@ public enum UIAutomationError: Error, LocalizedError {
}
}
}
// swiftlint:enable file_length

View File

@ -4,8 +4,6 @@ import AppKit // Added to provide NSRunningApplication and NSWorkspace
@preconcurrency import ApplicationServices // For AXUIElement and other C APIs
import Foundation
// swiftlint:disable file_length
/// A Swift-idiomatic wrapper around macOS AXUIElement for accessibility automation.
///
/// Element provides a modern Swift interface for interacting with UI elements through
@ -417,5 +415,3 @@ public struct Path {
public let components: [String]
}
// swiftlint:enable file_length

View File

@ -22,7 +22,11 @@ func convertCFValueToSwift(_ cfValue: CFTypeRef?) -> Any? {
case CFDictionaryGetTypeID():
return convertCFDictionary(cfValue)
case AXUIElementGetTypeID():
return cfValue as! AXUIElement // Should be safe to force unwrap if type matches
guard let element = cfValue as? AXUIElement else {
axWarningLog("Expected AXUIElement but received \(cfValue)")
return nil
}
return element
// Add other common CF types if necessary, e.g., CFURL, CFDate
default:
axDebugLog("Unhandled CFTypeRef in convertCFValueToSwift: typeID \(typeID). Value: \(cfValue)")

View File

@ -2,8 +2,6 @@
import Foundation
// swiftlint:disable file_length
// MARK: - AXErrorCode
// Error codes for AXorcist operations
@ -537,5 +535,3 @@ public struct CollectAllOutput: Codable {
case message // MODIFIED: CodingKey updated
}
}
// swiftlint:enable file_length

View File

@ -10,16 +10,30 @@ func formatRawCFValueForTextContent(_ rawValue: CFTypeRef?) async -> String {
guard let value = rawValue else { return AXMiscConstants.kAXNotAvailableString }
let typeID = CFGetTypeID(value)
if typeID == CFStringGetTypeID() {
return (value as! String)
guard let stringValue = value as? String else {
return AXMiscConstants.kAXNotAvailableString
}
return stringValue
} else if typeID == CFAttributedStringGetTypeID() {
return (value as! NSAttributedString).string
guard let attributedString = value as? NSAttributedString else {
return AXMiscConstants.kAXNotAvailableString
}
return attributedString.string
} else if typeID == AXValueGetTypeID() {
let axVal = value as! AXValue
return formatAXValue(axVal, option: ValueFormatOption.smart)
guard let axValue = value as? AXValue else {
return AXMiscConstants.kAXNotAvailableString
}
return formatAXValue(axValue, option: ValueFormatOption.smart)
} else if typeID == CFNumberGetTypeID() {
return (value as! NSNumber).stringValue
guard let number = value as? NSNumber else {
return AXMiscConstants.kAXNotAvailableString
}
return number.stringValue
} else if typeID == CFBooleanGetTypeID() {
return CFBooleanGetValue((value as! CFBoolean)) ? "true" : "false"
guard let boolValue = value as? CFBoolean else {
return AXMiscConstants.kAXNotAvailableString
}
return CFBooleanGetValue(boolValue) ? "true" : "false"
} else {
let typeDesc = CFCopyTypeIDDescription(typeID) as String? ?? "ComplexType"
GlobalAXLogger.shared.log(AXLogEntry(

View File

@ -4,8 +4,6 @@ import ApplicationServices
import Foundation
import Logging
// swiftlint:disable file_length
private let logger = Logger(label: "AXorcist.ElementSearch")
private struct PathNavigationResult {
@ -554,5 +552,3 @@ public nonisolated(unsafe) var axorcScanAll: Bool = false
/// Controls whether SearchVisitor should stop at the first element that satisfies the final locator criteria.
/// CLI flag `--no-stop-first` sets this to `false`.
public nonisolated(unsafe) var axorcStopAtFirstMatch: Bool = true
// swiftlint:enable file_length

View File

@ -137,7 +137,7 @@ func castToAXUIElementArray(_ value: Any, attr: String) -> [AXUIElement]? {
let result = anyArray.compactMap { item -> AXUIElement? in
guard let cfItem = item else { return nil }
if CFGetTypeID(cfItem as CFTypeRef) == AXUIElementGetTypeID() {
return (cfItem as! AXUIElement)
return cfItem as? AXUIElement
}
return nil
}
@ -157,7 +157,10 @@ func castToElementArray(_ value: Any, attr: String) -> [Element]? {
let result = anyArray.compactMap { item -> Element? in
guard let cfItem = item else { return nil }
if CFGetTypeID(cfItem as CFTypeRef) == AXUIElementGetTypeID() {
return Element(cfItem as! AXUIElement) // Assumes Element initializer is public/internal
guard let axElement = cfItem as? AXUIElement else {
return nil
}
return Element(axElement) // Assumes Element initializer is public/internal
}
return nil
}
@ -236,7 +239,7 @@ func castToSpecialType<T>(_ value: Any, expectedType: T.Type, attr: String) -> T
@MainActor
func castToAXUIElement(_ value: Any, attr: String) -> AXUIElement? {
if let cfValue = value as CFTypeRef?, CFGetTypeID(cfValue) == AXUIElementGetTypeID() {
return (cfValue as! AXUIElement)
return cfValue as? AXUIElement
}
let typeDescription = String(describing: type(of: value))
let valueDescription = String(describing: value)

View File

@ -39,17 +39,31 @@ private func formatCFTypeByID(
value,
option: option)
case AXValueGetTypeID():
// Map SimpleValueFormatOption concepts to ValueFormatOption if needed, or pass directly if compatible
// Assuming formatAXValue (from AXValueSpecificFormatter) now takes ValueFormatOption directly
return formatAXValue(value as! AXValue, option: option)
guard let axValue = value as? AXValue else {
axWarningLog("formatCFTypeByID: Expected AXValue but received \(value).")
return "<Invalid AXValue>"
}
return formatAXValue(axValue, option: option)
case CFStringGetTypeID():
return "\"\(escapeStringForDisplay(value as! String))\""
guard let stringValue = value as? String else {
return "<Invalid String>"
}
return "\"\(escapeStringForDisplay(stringValue))\""
case CFAttributedStringGetTypeID():
return "\"\(escapeStringForDisplay((value as! NSAttributedString).string))\""
guard let attributedString = value as? NSAttributedString else {
return "<Invalid AttributedString>"
}
return "\"\(escapeStringForDisplay(attributedString.string))\""
case CFBooleanGetTypeID():
return CFBooleanGetValue((value as! CFBoolean)) ? "true" : "false"
guard let boolValue = value as? CFBoolean else {
return "<Invalid Boolean>"
}
return CFBooleanGetValue(boolValue) ? "true" : "false"
case CFNumberGetTypeID():
return (value as! NSNumber).stringValue
guard let number = value as? NSNumber else {
return "<Invalid Number>"
}
return number.stringValue
case CFArrayGetTypeID():
return formatCFArray(value, option: option)
case CFDictionaryGetTypeID():
@ -71,7 +85,11 @@ private func formatAXUIElement(
_ value: CFTypeRef,
option: ValueFormatOption, // Changed from SimpleValueFormatOption
) -> String {
let element = Element(value as! AXUIElement)
guard let axElement = value as? AXUIElement else {
axWarningLog("formatAXUIElement: Failed to cast CFTypeRef to AXUIElement.")
return "<Invalid AXUIElement>"
}
let element = Element(axElement)
// Element.role() and .title() will use GlobalAXLogger internally
let role = element.role() ?? "Unknown"
@ -92,7 +110,9 @@ private func formatCFArray(
_ value: CFTypeRef,
option: ValueFormatOption, // Changed from SimpleValueFormatOption
) -> String {
let cfArray = value as! CFArray
guard let cfArray = value as? CFArray else {
return "<Invalid Array>"
}
let count = CFArrayGetCount(cfArray)
// Adjust logic based on ValueFormatOption cases
@ -119,7 +139,9 @@ private func formatCFDictionary(
_ value: CFTypeRef,
option: ValueFormatOption, // Changed from SimpleValueFormatOption
) -> String {
let cfDict = value as! CFDictionary
guard let cfDict = value as? CFDictionary else {
return "<Invalid Dictionary>"
}
let count = CFDictionaryGetCount(cfDict)
// Adjust logic based on ValueFormatOption cases

View File

@ -4,8 +4,6 @@ import ApplicationServices
import CoreGraphics // For CGPoint, CGSize, CGRect, CFRange
import Foundation
// swiftlint:disable file_length
// GlobalAXLogger is assumed to be available
// Inspired by UIElementInspector's UIElementUtilities.m
@ -45,7 +43,10 @@ public func getAXValueTypeForAttribute(element: Element, attributeName: String)
return nil
}
let axValue = rawValue as! AXValue
guard let axValue = rawValue as? AXValue else {
axWarningLog("getAXValueTypeForAttribute: Expected AXValue but received \(rawValue).")
return nil
}
return AXValueGetType(axValue)
}
@ -84,7 +85,13 @@ private func convertStringValue(
switch typeID {
case AXValueGetTypeID():
let axValue = currentValue as! AXValue
guard let axValue = currentValue as? AXValue else {
let detail = "Attribute '\(attributeName)' reported AXValue type but casting failed."
axErrorLog(detail, file: #file, function: #function, line: #line)
throw AccessibilityError.attributeUnsupported(
attribute: detail,
elementDescription: element.briefDescription())
}
let axValueType = AXValueGetType(axValue)
axDebugLog(
"Attribute '\(attributeName)' is AXValue of type: \(stringFromAXValueType(axValueType))",
@ -406,5 +413,3 @@ private func parseDefaultAXValueType(
}
// stringFromAXValueType is now defined in ValueHelpers.swift
// swiftlint:enable file_length

View File

@ -28,17 +28,37 @@ enum ValueUnwrapper {
{
switch typeID {
case ApplicationServices.AXUIElementGetTypeID():
return value as! AXUIElement
guard let element = value as? AXUIElement else {
axWarningLog("Failed to cast CFTypeRef to AXUIElement.")
return nil
}
return element
case ApplicationServices.AXValueGetTypeID():
return self.unwrapAXValue(value)
case CFStringGetTypeID():
return (value as! CFString) as String
guard let cfString = value as? CFString else {
axWarningLog("Failed to cast CFTypeRef to CFString.")
return nil
}
return cfString as String
case CFAttributedStringGetTypeID():
return (value as! NSAttributedString).string
guard let attributedString = value as? NSAttributedString else {
axWarningLog("Failed to cast CFTypeRef to NSAttributedString.")
return nil
}
return attributedString.string
case CFBooleanGetTypeID():
return CFBooleanGetValue((value as! CFBoolean))
guard let cfBool = value as? CFBoolean else {
axWarningLog("Failed to cast CFTypeRef to CFBoolean.")
return nil
}
return CFBooleanGetValue(cfBool)
case CFNumberGetTypeID():
return value as! NSNumber
guard let number = value as? NSNumber else {
axWarningLog("Failed to cast CFTypeRef to NSNumber.")
return nil
}
return number
case CFArrayGetTypeID():
return self.unwrapCFArray(value)
case CFDictionaryGetTypeID():
@ -55,7 +75,10 @@ enum ValueUnwrapper {
private static func unwrapAXValue(
_ value: CFTypeRef) -> Any?
{
let axVal = value as! AXValue
guard let axVal = value as? AXValue else {
axWarningLog("Failed to cast CFTypeRef to AXValue.")
return nil
}
let axValueType = axVal.valueType
// Log the AXValueType
@ -86,7 +109,10 @@ enum ValueUnwrapper {
private static func unwrapCFArray(
_ value: CFTypeRef) -> [Any?]
{
let cfArray = value as! CFArray
guard let cfArray = value as? CFArray else {
axWarningLog("Failed to cast CFTypeRef to CFArray.")
return []
}
var swiftArray: [Any?] = []
for index in 0..<CFArrayGetCount(cfArray) {
@ -104,7 +130,10 @@ enum ValueUnwrapper {
private static func unwrapCFDictionary(
_ value: CFTypeRef) -> [String: Any?]
{
let cfDict = value as! CFDictionary
guard let cfDict = value as? CFDictionary else {
axWarningLog("Failed to cast CFTypeRef to CFDictionary.")
return [:]
}
var swiftDict: [String: Any?] = [:]
if let nsDict = cfDict as? [String: AnyObject] {

View File

@ -8,8 +8,6 @@ extension Comment {
}
}
// swiftlint:disable file_length
extension Tag {
@Tag static var safe: Self
@Tag static var automation: Self
@ -502,5 +500,3 @@ var productsDirectory: URL {
return Bundle.main.bundleURL
#endif
}
// swiftlint:enable file_length