Commits
Click on a commit to change the comparison rangefix(codegen): correct `CRLF` handling in comment processing (#13169)
The oxc-transform's isolated declaration feature was incorrectly handling Windows line endings (`\r\n`) in comments, generating double newlines in the output. Consider:
```typescript
import { isolatedDeclaration } from 'oxc-transform'
console.log([
isolatedDeclaration('a.ts',
// Simulates windows file line-ending.
[
'/**',
' * Comment.',
' */',
'export const a: number = 1;'
].join('\r\n')
).code
])
// Before: [ '/**\n\n* Comment.\n\n*/\nexport declare const a: number;\n' ]
// After: [ '/**\n* Comment.\n*/\nexport declare const a: number;\n' ]
```
## Root Cause
The issue was in the comment processing logic in `oxc_codegen/src/comment.rs`. The `split(is_line_terminator)` function treats both `\r` and `\n` as separate line terminators, causing `\r\n` sequences to split incorrectly with empty strings between them, leading to double newlines in the output.
## Solution
Implemented an allocation-free approach using a custom `LineTerminatorSplitter` iterator that:
1. Handles CRLF sequences correctly by treating `\r\n` as a single line terminator
2. Eliminates performance overhead from string allocations during comment processing
3. Processes line terminators on-demand without creating temporary strings
This approach provides better performance compared to the previous normalization function while correctly handling Windows line endings.
## Additional Changes
- Converted all single-line block comments (`/** comment */`) to multiline format in test fixtures to better verify the CRLF fix
- Updated corresponding test snapshots to reflect the improved comment formatting
## Testing
- Added comprehensive tests covering Windows line endings, mixed line endings, and standalone CR handling
- All existing tests continue to pass (codegen, isolated declarations, transform)
- Verified the exact reproduction case from the issue works correctly
- Confirmed zero allocation overhead in comment processing
Fixes #13059.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey.