| name | swift-code-quality |
| description | Swift code quality rules for mach-mono — 300-line file limit, @Observable, no singletons in views, no direct Defaults access, protocol-based services. Auto-loaded when editing .swift files. |
| user-invocable | false |
| paths | **/*.swift |
Swift Code Quality Rules
Full reference: docs/AGENT-GUIDELINES.md → Swift Code Quality Rules.
File Size
| Limit | Rule |
|---|
| 300 lines | Hard limit — files exceeding this must be split |
| 200 lines | Target — prefer smaller, focused files |
When approaching 300 lines: extract cohesive responsibilities into separate files, use extensions in separate files for protocol conformances.
No Singletons in Views or Services
@Bindable var coordinator = NotchViewCoordinator.shared
let manager = SomeManager.shared
@Environment(NotchViewModel.self) private var viewModel
@Environment(\.pluginManager) private var pluginManager
init(service: SomeServiceProtocol) { ... }
Allowed .shared exceptions: NSWorkspace, NSApplication, URLSession, URLCache, XPCHelperClient, FullScreenMonitor, QLThumbnailGenerator, QLPreviewPanel, NSScreenUUIDCache, SkyLightOperator, DefaultsNotchSettings (injection root only), ScreenDisplayRegistry (system-level screen cache).
No Direct Defaults Access
Defaults[.someSetting]
@Default(.someSetting) var setting
settings.someSetting
@Environment(\.bindableSettings) private var settings
Exception: NotchSettings.swift is the only file allowed direct Defaults access.
Observable State
@Observable
@MainActor
final class SomeViewModel { ... }
class SomeViewModel: ObservableObject {
@Published var state: State
}
All UI-related protocols and classes must be @MainActor.
Protocol-Based Services
protocol SomeServiceProtocol: Sendable {
func doThing() async throws
}
final class SomeService: SomeServiceProtocol { ... }
Never depend on concrete service types — always use the protocol.
File Organization Order
- Imports
- Type declaration + stored properties
- Initializers
- Public/internal methods
- Private methods
- Extensions (prefer separate files for large protocol conformances)
Commit Checklist Before Submitting Swift Changes