# Code Size Limit Enforcement & Refactoring Guide ## ๐ŸŽฏ Overview This document outlines the code size limit enforcement strategy and refactoring approach for the MetaBuilder codebase. ## ๐Ÿ“ Size Limits Based on the project's architectural principles, the following limits are enforced: ### TypeScript/React Files (`.ts`, `.tsx`) | Metric | Limit | Severity | |--------|-------|----------| | Lines of Code (LOC) | 150 | โŒ Error | | Total Lines | 200 | โš ๏ธ Warning | | Max Nesting Depth | 3 | โš ๏ธ Warning | | Exports per File | 5 (components), 10 (utilities) | โš ๏ธ Warning | | Function Parameters | 5 | โš ๏ธ Warning | ### JavaScript Files (`.js`, `.jsx`) Same limits as TypeScript files. ## ๐Ÿ› ๏ธ Enforcement Tool ### Location ```bash scripts/enforce-size-limits.ts ``` ### Usage ```bash # Run the size limit checker npx tsx scripts/enforce-size-limits.ts # Output # - Console report with violations # - JSON report (size-limits-report.json) for CI/CD ``` ### Features - Scans all TypeScript/JavaScript files (excludes node_modules, build, .next, etc.) - Counts actual lines of code (excludes comments and blank lines) - Detects nesting depth issues - Identifies function parameter count violations - Generates both human-readable and machine-readable reports ## โ™ป๏ธ Refactoring Strategy ### Core Principles 1. **Separation of Concerns** - Each component should have ONE primary responsibility - Extract business logic into custom hooks - Move persistent state to database 2. **Hook-Based Architecture** - `useGitHubFetcher`: API integration and data fetching - `useAutoRefresh`: Polling and timer management - `useFileTree`: Tree data structure operations - `useCodeEditor`: Editor state and operations - Custom hooks for domain-specific logic 3. **Component Hierarchy** - Small, reusable UI components (<100 LOC) - Container components that use hooks (<150 LOC) - Avoid deeply nested JSX 4. **Data Flow** ``` Persistent Database โ†“ Custom Hooks (logic) โ†“ Container Components (state management) โ†“ UI Components (rendering) ``` ## ๐Ÿ“‚ Refactoring Artifacts ### New Hooks Created ``` src/hooks/ โ”œโ”€โ”€ useGitHubFetcher.ts # GitHub API integration โ”œโ”€โ”€ useAutoRefresh.ts # Auto-refresh polling โ”œโ”€โ”€ useCodeEditor.ts # Editor state management โ”œโ”€โ”€ useFileTree.ts # File tree operations โ””โ”€โ”€ index.ts # Barrel export ``` ### New Components Created ``` src/components/ โ”œโ”€โ”€ WorkflowRunCard.tsx # Extracted from GitHubActionsFetcher โ”œโ”€โ”€ GitHubActionsFetcher.refactored.tsx # Refactored version โ””โ”€โ”€ ... (more to follow) ``` ## ๐Ÿ“‹ Refactoring Checklist ### Phase 1: Foundation (Week 1) - [x] Create size limit enforcement tool - [x] Create custom hooks for common patterns - [x] Create small, reusable UI components - [ ] Replace original GitHubActionsFetcher with refactored version - [ ] Add unit tests for hooks ### Phase 2: Large Components (Weeks 2-3) - [ ] Refactor NerdModeIDE.tsx (733 LOC) - [ ] Refactor LuaEditor.tsx (686 LOC) - [ ] Refactor PackageImportExport.tsx (638 LOC) ### Phase 3: Medium Components (Week 4) - [ ] Refactor PackageManager.tsx (558 LOC) - [ ] Refactor WorkflowEditor.tsx (498 LOC) - [ ] Refactor ComponentHierarchyEditor.tsx (477 LOC) ### Phase 4: Verification & Tooling (Ongoing) - [ ] Run size limit checker on all files - [ ] Set up pre-commit hook to prevent new violations - [ ] Add CI/CD check for size limits - [ ] Document refactored components ## ๐Ÿ”— Hook Usage Examples ### useGitHubFetcher ```tsx function MyComponent() { const { data, isLoading, error, fetch } = useGitHubFetcher() useEffect(() => { fetch() }, []) return
{data ? 'Loaded' : 'Loading...'}
} ``` ### useAutoRefresh ```tsx function MyComponent() { const { isAutoRefreshing, toggleAutoRefresh } = useAutoRefresh({ intervalMs: 30000, onRefresh: () => fetchData(), }) return } ``` ### useFileTree ```tsx function FileExplorer() { const { files, addChild, deleteNode, toggleFolder } = useFileTree() return files.map(file => (
toggleFolder(file.id)}> {file.name}
)) } ``` ### useCodeEditor ```tsx function CodeEditor() { const { activeFile, openFile, updateContent, saveFile } = useCodeEditor() return ( <> ) } ``` ## ๐Ÿงช Testing Strategy ### Unit Tests - Test each hook with various inputs - Mock external APIs - Verify state transitions ### Component Tests - Test refactored components in isolation - Verify props are passed correctly - Test user interactions ### Integration Tests - Test hooks working together - Verify data flows through component hierarchy ### E2E Tests - Test complete workflows - Verify UI behavior end-to-end ## ๐Ÿš€ Deployment Steps 1. **Create Feature Branch** ```bash git checkout -b refactor/enforce-size-limits ``` 2. **Add Tests** - Create tests for new hooks - Create tests for new components 3. **Run Checks** ```bash npm run lint npm run test npm run test:e2e npx tsx scripts/enforce-size-limits.ts ``` 4. **Create Pull Request** - Link to issue - Describe refactoring strategy - List breaking changes (if any) 5. **Deploy** - Merge when approved - Monitor for errors - Update documentation ## ๐Ÿ“Š Current Violations Run `npx tsx scripts/enforce-size-limits.ts` to see current violations. As of the initial scan: - โŒ Configuration files have deep nesting (max 3 required) - โœ… No source files exceed size limits yet ## ๐Ÿ”ง Next Steps 1. Add pre-commit hook to enforce limits automatically 2. Add GitHub Actions workflow for CI/CD checks 3. Create migration guide for large components 4. Document best practices for staying under limits --- **Last Updated**: December 25, 2025 **Status**: ๐ŸŸข In Progress