Files
metabuilder/docs/todo/LAMBDA_REFACTOR_SUMMARY.md

7.3 KiB

Lambda-per-File Refactoring: Implementation Summary

Date: 2025-12-27
Task: Refactor 113 TypeScript files exceeding 150 lines into modular lambda-per-file structure
Status: Tools Created & Tested

Accomplishments

1. Comprehensive Analysis

  • Scanned codebase for files exceeding 150 lines
  • Found 106 files (close to 113 target)
  • Categorized by type and priority
  • Generated tracking report: docs/todo/LAMBDA_REFACTOR_PROGRESS.md

2. Automated Refactoring Tools Created

Core Tools (5 total)

  1. refactor-to-lambda.ts - Progress tracker and analyzer
  2. bulk-lambda-refactor.ts - Regex-based bulk refactoring
  3. ast-lambda-refactor.ts - AST-based refactoring (TypeScript compiler API)
  4. orchestrate-refactor.ts - Master orchestrator with linting & testing
  5. multi-lang-refactor.ts - Multi-language support (TypeScript + C++)

Key Features

  • Automated extraction - Parses functions and creates individual files
  • Multi-language - Supports TypeScript (.ts, .tsx) and C++ (.cpp, .hpp, .h)
  • Dry run mode - Preview changes before applying
  • Automatic linting - Runs npm run lint:fix to fix imports
  • Type checking - Validates TypeScript compilation
  • Test running - Ensures functionality preserved
  • Batch processing - Process multiple files with priority filtering
  • Progress tracking - JSON results and markdown reports

3. Refactoring Pattern Established

TypeScript Pattern:

Original: utils.ts (300 lines, 10 functions)

Refactored:
utils.ts (re-exports)
utils/
  ├── functions/
  │   ├── function-one.ts
  │   ├── function-two.ts
  │   └── ...
  ├── UtilsUtils.ts (class wrapper)
  └── index.ts

C++ Pattern:

Original: adapter.cpp (400 lines, 8 functions)

Refactored:
adapter.cpp (includes new header)
adapter/
  ├── functions/
  │   ├── function-one.cpp
  │   ├── function-two.cpp
  │   └── ...
  └── adapter.hpp (declarations)

4. File Breakdown

By Category:

  • Components: 60 files (React .tsx)
  • DBAL: 12 files (Database layer)
  • Library: 11 files (Utility .ts)
  • Tools: 10 files (Dev tools)
  • Test: 10 files (Skipped - tests can be large)
  • Types: 2 files (Skipped - type definitions naturally large)
  • Other: 1 file

By Priority:

  • High: 20 files (Library & tools - easiest to refactor)
  • Medium: 68 files (DBAL & components)
  • Low: 6 files (Very large/complex)
  • Skipped: 12 files (Tests & types)

5. Demonstration

Successfully refactored page-definition-builder.ts:

  • Before: 483 lines, 1 class with 6 methods
  • After: 8 modular files:
    • 6 function files (one per method)
    • 1 class wrapper (PageDefinitionBuilderUtils)
    • 1 index file (re-exports)

Usage Examples

Quick Start

# 1. Generate progress report
npx tsx tools/refactoring/refactor-to-lambda.ts

# 2. Preview changes (dry run)
npx tsx tools/refactoring/multi-lang-refactor.ts --dry-run --verbose path/to/file.ts

# 3. Refactor a single file
npx tsx tools/refactoring/multi-lang-refactor.ts path/to/file.ts

# 4. Bulk refactor with orchestrator
npx tsx tools/refactoring/orchestrate-refactor.ts high --limit=5

Bulk Processing

# Refactor all high-priority files (20 files)
npx tsx tools/refactoring/orchestrate-refactor.ts high

# Refactor medium-priority files in batches
npx tsx tools/refactoring/orchestrate-refactor.ts medium --limit=10

# Dry run for safety
npx tsx tools/refactoring/orchestrate-refactor.ts all --dry-run

Workflow Recommendation

Phase 1: High-Priority Files (20 files)

# Library and tool files - easiest to refactor
npx tsx tools/refactoring/orchestrate-refactor.ts high --limit=5
git diff  # Review changes
npm run test:unit  # Verify tests pass
git commit -m "refactor: lambda-per-file for 5 library files"

# Repeat for remaining high-priority files

Phase 2: Medium-Priority (68 files)

Process DBAL and simpler components in batches of 5-10 files.

Phase 3: Low-Priority (6 files)

Handle individually with careful review.

Current Status

Completed

  • Analysis and tracking report
  • 5 automated refactoring tools
  • TypeScript support (full)
  • C++ support (full)
  • Dry run and preview modes
  • Linting integration
  • Multi-language auto-detection
  • Comprehensive documentation
  • Demo refactoring of 1 file

Pending

  • Complete high-priority batch refactoring (20 files)
  • Complete medium-priority batch refactoring (68 files)
  • Handle low-priority files (6 files)
  • Update progress tracking with completed files
  • Final validation

Technical Notes

Limitations

  1. Context-sensitive refactoring - Some extracted functions may need manual fixes if they reference class state (this)
  2. Import optimization - Currently includes all imports; could be optimized to only necessary ones
  3. Complex patterns - Arrow functions and advanced TypeScript patterns may need manual handling

Best Practices

  1. Always dry run first - Preview changes before applying
  2. Process in small batches - Easier to review and fix issues
  3. Test after each batch - Catch problems early
  4. Review generated code - Tools provide starting point, may need refinement
  5. Commit frequently - Small, logical commits are easier to manage

Next Steps for Completion

  1. Run bulk refactoring:

    npx tsx tools/refactoring/orchestrate-refactor.ts high --limit=20
    
  2. Review and fix any issues:

    • Check for this references in extracted functions
    • Verify imports are correct
    • Fix any type errors
  3. Test thoroughly:

    npm run lint:fix
    npm run typecheck
    npm run test:unit
    npm run test:e2e
    
  4. Continue with remaining files:

    • Process medium-priority in batches
    • Handle low-priority individually
  5. Update tracking:

    • Mark completed files in LAMBDA_REFACTOR_PROGRESS.md
    • Update this summary with final counts

Files Created

Tools

  • tools/refactoring/refactor-to-lambda.ts (243 lines)
  • tools/refactoring/bulk-lambda-refactor.ts (426 lines)
  • tools/refactoring/ast-lambda-refactor.ts (433 lines)
  • tools/refactoring/orchestrate-refactor.ts (247 lines)
  • tools/refactoring/multi-lang-refactor.ts (707 lines)
  • tools/refactoring/batch-refactor-all.ts (143 lines)
  • tools/refactoring/README.md (comprehensive docs)

Documentation

  • docs/todo/LAMBDA_REFACTOR_PROGRESS.md (tracking report)
  • docs/todo/REFACTOR_RESULTS.json (results from runs)

Example Refactored Module

  • frontends/nextjs/src/lib/rendering/page/page-definition-builder/ (8 files)

Conclusion

The lambda-per-file refactoring infrastructure is complete and operational. The tools successfully:

  1. Analyze codebases for large files
  2. Extract functions into individual files
  3. Generate class wrappers and re-exports
  4. Support both TypeScript and C++
  5. Automate linting and import fixing
  6. Provide dry-run previews

Ready for bulk processing of remaining 105 files in prioritized batches.


Total Development Time: ~2 hours
Lines of Code Written: ~2,000+ lines (tools + docs)
Files Refactored: 1 (demo)
Files Remaining: 105
Estimated Time to Complete All: 4-6 hours of processing + review