Files
BreakEscape/scripts/EXTRACTION_SCRIPT_FIXES.md

192 lines
7.0 KiB
Markdown

# Extraction Script Fixes - Phase 1 Issue Resolution
## Problem Summary
Phase 1 extraction created a working module but had integration issues that required manual fixes:
- Methods used `parent.` instead of `this.parent.`
- Import statement wasn't added to main file
- Method calls weren't updated to use `this.lockConfig`
## Root Causes Identified
### Issue 1: Parent Reference Pattern
**File**: `scripts/extract_lockpicking_methods.py` (line ~47)
**Method**: `MethodExtractor.replace_this_with_parent()`
**Problem**: Replaced `this.` with `parent.` (bare reference) instead of `this.parent.`
```python
# OLD: Bad pattern
modified_line = re.sub(r'\bthis\.', 'parent.', modified_line)
# NEW: Correct pattern
modified_line = re.sub(r'\bthis\.', 'this.parent.', modified_line)
```
**Impact**: Caused `ReferenceError: lockConfig is not defined` because methods couldn't access parent state
### Issue 2: Import Statement Bug
**File**: `scripts/extract_lockpicking_methods.py` (line ~315)
**Method**: `MainFileUpdater.add_import()`
**Problem**: Used JavaScript method `startsWith()` instead of Python method `startswith()`
```python
# OLD: Syntax error (JavaScript syntax in Python)
if line.startsWith('import '):
# NEW: Correct Python syntax
if line.startswith('import '):
```
**Impact**: Import statements weren't being added to main file
### Issue 3: Missing Module Initialization
**File**: `scripts/extract_lockpicking_methods.py` (line ~337)
**Method**: `MainFileUpdater.add_module_initialization()`
**Problem**: Method existed but wasn't being called in auto-integrate flow
**Impact**: `this.lockConfig` was never initialized, causing undefined references
### Issue 4: Incorrect Method Call Replacement
**File**: `scripts/extract_lockpicking_methods.py` (line ~386)
**Method**: `MainFileUpdater.replace_method_calls()`
**Problem**: Replaced `this.method()` with `moduleInstance.method()` instead of `this.moduleInstance.method()`
```python
# OLD: Missing this. prefix
replacement = f'{module_instance}.{method_name}('
# NEW: Include this. prefix
replacement = f'this.{module_instance}.{method_name}('
```
**Impact**: Method calls wouldn't work because they weren't properly scoped to instance
### Issue 5: Auto-Integrate Not Using Updater Methods
**File**: `scripts/extract_lockpicking_methods.py` (lines ~694-720)
**Section**: Main execution auto-integrate block
**Problem**: Implemented import/removal inline instead of calling `MainFileUpdater` methods
**Impact**: Content updates weren't persisted properly between operations
## Fixes Applied
### Fix 1: Parent Reference Pattern ✅
```python
def replace_this_with_parent(self, code: str, use_parent_keyword: bool = True) -> str:
# Replace 'this.' with 'this.parent.' for method bodies
modified_line = re.sub(r'\bthis\.', 'this.parent.', modified_line)
return '\n'.join(modified_lines)
```
### Fix 2: Python Syntax ✅
```python
def add_import(self, class_name: str, module_path: str) -> str:
for i, line in enumerate(lines):
if line.startswith('import '): # Python method, not JavaScript
insert_idx = i + 1
```
### Fix 3: Module Initialization Called ✅
```python
if args.auto_integrate:
# Now properly calls add_module_initialization
main_updater.add_module_initialization(module_instance_name, class_name)
print(f" ✓ Added module initialization in constructor")
```
### Fix 4: Correct Method Call Pattern ✅
```python
def replace_method_calls(self, method_names: List[str], module_instance: str) -> str:
for method_name in method_names:
pattern = rf'this\.{method_name}\('
replacement = f'this.{module_instance}.{method_name}(' # Include this.
updated = re.sub(pattern, replacement, updated)
```
### Fix 5: Auto-Integrate Uses Proper Methods ✅
```python
if args.auto_integrate:
print(f"\n 🔧 Auto-integrating...")
# 1. Add import statement
main_updater.add_import(class_name, f'./{import_path}')
print(f" ✓ Added import statement")
# 2. Add module initialization in constructor
main_updater.add_module_initialization(module_instance_name, class_name)
print(f" ✓ Added module initialization in constructor")
# 3. Remove old methods from main file
main_updater.remove_methods(method_names)
print(f" ✓ Removed {len(method_names)} methods from main file")
# 4. Replace method calls to use module instance
main_updater.replace_method_calls(method_names, module_instance_name)
print(f" ✓ Updated method calls to use this.{module_instance_name}")
```
### Fix 6: Content Persistence ✅
All updater methods now update `self.content` after changes:
```python
# After each operation
self.content = updated_content # Persist for next operation
return self.content
```
## Verification Results
### Documentation Updated
- Module generator now correctly documents `this.parent` pattern
- All comments reference proper instance access
### Pattern Consistency
✅ Constructor: `constructor(parent) { this.parent = parent; }`
✅ Methods: Use `this.parent.property` throughout
✅ Main file: Uses `this.lockConfig.method()` for calls
✅ Initialization: `this.lockConfig = new LockConfiguration(this);`
## Testing - Phase 2 Ready
The fixed script should now:
1. ✅ Replace `this.` with `this.parent.` in extracted methods
2. ✅ Add import statements correctly
3. ✅ Initialize modules in constructor
4. ✅ Replace method calls with `this.moduleInstance.method()`
5. ✅ Remove old methods from main file
6. ✅ Persist all changes properly
## Next Steps
### To Use Fixed Script for Phase 2:
```bash
cd /home/cliffe/Files/Projects/Code/BreakEscape/BreakEscape
python3 scripts/extract_lockpicking_methods.py \
--methods "createLockBackground,createTensionWrench,createHookPick" \
--output-file "js/minigames/lockpicking/lock-graphics.js" \
--class-name "LockGraphics" \
--replace-this --auto-integrate \
--update-main-file "js/minigames/lockpicking/lockpicking-game-phaser.js" \
--module-instance-name "lockGraphics" --show-dependencies
```
### Expected Output:
```
✅ Success! Created: js/minigames/lockpicking/lock-graphics.js
📝 Updating main file: js/minigames/lockpicking/lockpicking-game-phaser.js
🔧 Auto-integrating...
✓ Added import statement
✓ Added module initialization in constructor
✓ Removed 3 methods from main file
✓ Updated method calls to use this.lockGraphics
✅ Updated: js/minigames/lockpicking/lockpicking-game-phaser.js
```
## Files Modified
- `scripts/extract_lockpicking_methods.py`: All 5 fixes applied
- `lock-configuration.js`: Already manually fixed with `this.parent` pattern
- `lockpicking-game-phaser.js`: Already manually fixed with proper initialization and calls
## Safeguards for Future Phases
**Extraction**: Uses `this.parent` pattern automatically
**Initialization**: Added in constructor automatically
**Import**: Added to top of file automatically
**Method Calls**: Replaced with `this.moduleInstance.method()` automatically
**Removal**: Old methods removed from main file automatically
**Result**: No manual fixes needed for Phases 2-12! 🎉