Add UTF-8 BOM support for package.json files#39
Merged
Conversation
Detect and preserve UTF-8 BOM when sorting package.json files. Files with BOM (like Vite playground examples) now maintain the BOM after sorting, ensuring compatibility with tools that expect it. - Add BOM detection and stripping before JSON parsing - Preserve BOM in output if present in input - Add dedicated BOM test fixture (tests/fixtures/package-bom.json) - Add comprehensive test coverage for BOM handling Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Replace String::insert(0, BOM) with pre-allocated String to avoid expensive byte shifting. Use idiomatic strip_prefix for BOM detection. - Use strip_prefix() instead of manual slicing - Pre-allocate exact capacity when BOM is needed - Push BOM first then append result (faster than insert at 0) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Combine three BOM tests into one that covers: - BOM preservation when present - No BOM added when not present - Idempotency of BOM sorting Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Motivation
Some tools and editors (like those used in Vite playground) create package.json files with UTF-8 BOM markers. When sorting these files, the BOM should be preserved to maintain compatibility with tools that expect it.
Reference: https://github.com/vitejs/vite/blob/main/playground/resolve/utf8-bom-package/package.json
Implementation
strip_prefix()to detect and remove BOM (\u{FEFF}) before JSON parsinginsert(0)operation)Test Coverage
tests/fixtures/package-bom.jsonwith actual UTF-8 BOMTest Plan
🤖 Generated with Claude Code