fix(format): write BOM when only headers are emitted for empty input#1242
Open
abhu85 wants to merge 1 commit into
Open
fix(format): write BOM when only headers are emitted for empty input#1242abhu85 wants to merge 1 commit into
abhu85 wants to merge 1 commit into
Conversation
The BOM was pushed only in `_transform`, which never runs when the input rows array is empty. With `writeBOM: true` and `alwaysWriteHeaders: true` the headers are flushed in `_flush` with no preceding `_transform`, so no BOM was written. Write the BOM in `_flush` too, when `finish` actually produces content.
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.
Problem
Fixes #1148.
When the input rows array is empty,
writeBOM: trueis ignored even though headers are still written viaalwaysWriteHeaders:Cause
CsvFormatterStreampushes the BOM only in_transform:_transformruns once per row, so with an empty input it never fires. The headers are flushed later in_flush(viarowFormatter.finish()underalwaysWriteHeaders), which had no BOM handling — so the headers come out with no BOM.Fix
Write the BOM in
_flushas well, guarded byhasWrittenBOMand only whenfinishactually produces content:The
rows.length > 0guard keeps truly-empty output (no rows, no headers) from emitting a lone BOM, andhasWrittenBOMprevents a double BOM when rows were present.Test plan
CsvFormatterStreamtest:formatRows([], { headers: ['a','b'], alwaysWriteHeaders: true, writeBOM: true })resolves to['', 'a,b'].mainand passes with the fix.jest packages/format— 231 passing;eslint --max-warnings 0+prettier --checkclean.