Files
leocrm/scripts/test_suite/trace_stores.py
T

69 lines
2.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Trace Stores — Find unused store actions/state in frontend."""
from __future__ import annotations
import re, sys, json
from pathlib import Path
from collections import defaultdict
ROOT = Path(__file__).resolve().parent.parent.parent
FRONTEND = ROOT / "frontend" / "src"
def find_store_actions() -> dict[str, list[str]]:
stores = {}
store_dir = FRONTEND / "store"
if not store_dir.exists(): return stores
for ts in store_dir.glob("*.ts"):
try: content = ts.read_text()
except: continue
store_name = ts.stem
actions = []
# Find action names in zustand store definitions
for m in re.finditer(r'(\w+)\s*:', content):
name = m.group(1)
if name not in ('state', 'set', 'get', 'name', 'storage', 'partialize') and len(name) > 3:
actions.append(name)
stores[store_name] = list(set(actions))
return stores
def find_store_usage() -> dict[str, set[str]]:
usage = defaultdict(set)
for ts in FRONTEND.rglob("*.ts"):
try: content = ts.read_text()
except: continue
for store_name in ['authStore', 'uiStore', 'themeStore', 'workspaceStore', 'pluginStore', 'commStore', 'calendarStore', 'commandPaletteStore', 'onboardingStore', 'windowStore', 'aiUIControlStore', 'pluginToolbarStore']:
if store_name in content:
# Find which actions/state are accessed
for m in re.finditer(rf'{store_name}\.*(\w+)', content):
usage[store_name].add(m.group(1))
return usage
def main():
print("=" * 70)
print("TRACE STORES — Unused Store Actions/State")
print("=" * 70)
stores = find_store_actions()
usage = find_store_usage()
unused = {}
total_unused = 0
for store_name, actions in stores.items():
used = usage.get(store_name, set())
unused_actions = [a for a in actions if a not in used]
if unused_actions:
unused[store_name] = unused_actions
total_unused += len(unused_actions)
print(f"\nStores found: {len(stores)}")
for name, actions in stores.items():
used = len(usage.get(name, set()))
print(f" {name}: {len(actions)} defined, {used} used, {len(unused.get(name, []))} unused")
print(f"\nTotal unused store actions: {total_unused}")
if unused:
print("\n--- Unused store actions ---")
for store, actions in unused.items():
for a in actions[:10]: print(f" {store}.{a}")
results = {"stores": stores, "usage": {k: list(v) for k, v in usage.items()}, "unused": unused}
with open(ROOT / "scripts" / "test_suite" / "results_trace_stores.json", "w") as f:
json.dump(results, f, indent=2, default=str)
return total_unused
if __name__ == "__main__": sys.exit(main())