fix: correct after_line=-1 prepend position in append_file #16#20
Conversation
|
Caution Review failedThe pull request is closed. Recent review infoRun configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: Files selected for processing (2)
WalkthroughWalkthroughThe ChangesPrepend Mode Negative Index Fix
Estimated code review effort3 (Moderate) | ~15 minutes Possibly related issues
Finishing TouchesGenerate docstrings
Generate unit tests (beta)
Simplify code
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install timed out. The project may have too many dependencies for the sandbox. Comment |
Closes #16
Bug
When
mode=prependandafter_line=-1, content inserted at the wrong position (end of file instead of before the last line).Root Causes
split('\n')trailing newline inflation — Files ending with\nproduce a trailing empty string in the split array, makinglines.lengthone larger than the true line countMath.max(0, lines.length - 2)only worked for files without trailing newlines, and only for -1 specificallyFix
split('\n')before computation, then restore it after-1= before last line,-2= before second-to-last, etc.insertPos = Math.max(0, lines.length + afterLine)for negative valuesTests Added
after_line=-1on a 3-line file (before last line)after_line=-1on a 2-line file (edge case where old formula broke)All 8 tests pass.
Summary by cubic
Fix incorrect insert position in
append_filewhenmode=prependandafter_line=-1. Now inserts before the last line (and supports other negative indexes) while preserving trailing newlines.after_linevalues (-1before last line,-2before second-to-last, etc.).after_line=-1.Written for commit d55f68f. Summary will update on new commits.
Summary by CodeRabbit
Bug Fixes
Tests