Repository navigation
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
It intentionally changes the long-standing output semantics of the public sort() API, so a human maintainer should sign off on the behavioral change even though the implementation and tests are sound.
0 open findings
What changed in this PR
This PR fixes a data-loss bug in ShellJS's sort() command. Previously, _sort called contents.trimRight() before splitting into lines, which stripped trailing whitespace from the final line and discarded valid blank lines. The fix splits on \n directly and removes only the single terminal line created by a final newline, preserving line contents and blank lines while keeping the existing empty-file output and final output newline. The numeric parser's regex gains the s (dotAll) flag so retained carriage returns in nonnumeric suffixes don't cause str.match to return null (which would throw) under -n/-rn.
Changes:
- Replace
trimRight().split('\n')withsplit('\n')plus a conditionalpop()when content ends in\n, preserving trailing whitespace and blank lines. - Add the
sflag to theparseNumberregex so numerical sort handles lines containing\r. - Add comprehensive regression tests covering pipes, multiple files, CRLF content, final-newline controls, and reverse/numeric options.
| File | Description |
|---|---|
| src/sort.js | Preserves trailing whitespace/blank lines by removing trimRight() and popping only the terminal newline entry; adds s flag to numeric parser regex to tolerate retained \r. |
| test/sort.js | Adds regression tests for whitespace/blank-line preservation across pipes, multiple files, empty files, CRLF, and -r/-n/-rn options. |
I traced each new test case against the updated logic and confirmed the expected outputs are correct. I also verified the existing test/resources/sort/* fixtures all end with a single \n and no trailing blank lines, so the pre-existing tests remain unaffected, and that the project's node >=18 engine requirement supports the dotAll (s) regex flag. The s flag is genuinely necessary: without it, (.*)$ cannot consume a trailing \r, causing str.match(...) to return null and throw. I found no code defects.
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
sort()currently trims all trailing whitespace before splitting each input into lines. This loses a final line's spaces or tabs and removes valid blank lines:ShellString('b\na ').sort()returns'a\nb\n', andShellString('b\na\n\n').sort()returns'a\nb\n'.Split on
\nand remove only the final split entry when the input ends with a newline. This preserves line contents and blank lines while keeping the existing empty-file output and final output newline. The numeric parser also accepts retained carriage returns in nonnumeric suffixes so preservation does not introduce an exception for-n/-rn. Regression tests cover files, pipes, combined inputs, CRLF content, final-newline controls, and reverse/numeric options.Validation on Node.js 22.22.3 / macOS:
npm test: 654 passed, 6 skipped (includes lint).npm run test-with-coverage,npm run lint,npm run check-node-support,npm run gendocs, andnpm pack --dry-run: passed; documentation unchanged.