Skill: Analyze ViewModels
Purpose
Analyze iOS/Swift ViewModels for architecture compliance, pattern correctness, memory safety, and anti-patterns. Generates a report with actionable findings categorized by severity.
Execution Flow — 6 Checks
Check 1: @MainActor Compliance
- Scan all ViewModel files for
@MainActorannotation - Flag any ViewModel class missing
@MainActor - Check for
nonisolatedproperties — they must be justified (e.g., constants) - Verify
ObservableObjectconformance is present - Flag any direct
DispatchQueue.main.asynccalls — should use@MainActorinstead
Check 2: @Published State Management
- List all
@Publishedproperties per ViewModel - Flag ViewModels with more than 8
@Publishedproperties (suggest decomposition) - Verify
isLoading,errorMessagepatterns exist for API-connected ViewModels - Check for redundant state (e.g.,
isLoadingANDloadingStateenum) - Verify no
@Publishedproperties are set from outside the ViewModel
Check 3: Combine Publisher Usage
- Verify
cancellables: Set<AnyCancellable>exists - Check that all
.sink()subscriptions store incancellables - Flag orphaned subscriptions (not stored)
- Verify
.receive(on: DispatchQueue.main)is used before UI-bound sinks - Check for overly complex publisher chains (>5 operators) — suggest extraction
Check 4: Memory Leak Detection
- Scan all closures for
[weak self]usage on capture lists - Flag
self.references in closures without[weak self] - Check for strong reference cycles between ViewModel and Coordinator
- Verify
cancellablesis properly cleaned up (or ViewModel deinit is handled) - Flag retain cycles in nested closures
Check 5: Coordinator Delegation
- Verify navigation actions go through Coordinator (not direct
NavigationLinkpushes) - Check that Coordinator reference is injected via Factory (
@Injected) - Flag any
UIApplication.sharedorUINavigationControllerdirect access - Verify Coordinator protocol conformance
Check 6: Error Handling Patterns
- Check that all Combine
receiveCompletionhandlers process.failure - Verify errors are mapped to user-friendly messages
- Flag raw error descriptions exposed to UI
- Check for empty catch blocks or ignored errors
- Verify error state is reset before retrying operations
Output
Generate a report with:
# ViewModel Analysis Report
## Summary
- Total ViewModels scanned: X
- Compliant: X
- Issues found: X (Critical: X, Warning: X, Info: X)
## Critical Issues
[List with file:line references]
## Warnings
[List with file:line references]
## Recommendations
[Actionable improvement suggestions]
Auto-Shielding
- ABORT if no ViewModel files found in the project — wrong project type
- WARN if more than 50% of ViewModels fail @MainActor check — systemic issue
Rules
- Never modify code during analysis — this is read-only
- Report all findings with exact file paths and line numbers
- Categorize findings: Critical (breaks at runtime), Warning (potential issue), Info (improvement)
- @MainActor missing is always Critical
- Memory leak indicators are always Critical
- Missing error handling is Warning
- Style/pattern suggestions are Info
Source: juankmvanegas/factoria-powers — distributed by TomeVault.