Skip to content

fix(csv-stringify): skip escaping empty quote strings - #563

Open
MirandaCavalie wants to merge 3 commits into
adaltas:masterfrom
MirandaCavalie:codex/fix-stringify-empty-quote
Open

MirandaCavalie wants to merge 3 commits into
adaltas:masterfrom
MirandaCavalie:codex/fix-stringify-empty-quote

Conversation

@MirandaCavalie

@MirandaCavalie MirandaCavalie commented Nov 15, 2026 •

Copy link
Copy Markdown

Fix the regression introduced by #559: disabled quoting inserts escape characters between every character when a quoting selector is active.

stringify([["abc", "def"]], { quote: false, quoted: true });
// master: '"a"b"c","d"e"f"\n'
// fixed: 'abc,def\n'

Skip quote escaping when quote is empty. Checking the quote directly also preserves escaping of the apostrophe added by escape_formulas when using a custom quote, as identified in review.

Two minimal regression tests cover disabled quoting and formula escaping with an apostrophe quote. The formula test fails on the previous PR implementation and passes with this change; the original disabled-quote test remains passing.

Validation:

  • Node 24: npm run build passes for all 7 projects; npm test passes for all 14 projects (1,119 passing, 5 existing pending).
  • Node 20 and 22: npm test --workspace=csv-stringify passes on each (245 passing, 1 existing pending).
  • Changed-file ESLint, Prettier and git diff --check pass.

Full build and tests ran on macOS in a checkout without spaces, with the three PR files verified identical to the submitted files.

@wdavidw

wdavidw commented Nov 15, 2026

Copy link
Copy Markdown
Member

Hi @MirandaCavalie could you reduce the number of tests to the minimum. Thank you

@MirandaCavalie

Copy link
Copy Markdown
Author

Hi @MirandaCavalie could you reduce the number of tests to the minimum. Thank you

Thanks, David! Reduced this to a single regression test and confirmed it fails without the fix and passes with it. The full suite also passes locally

Comment thread packages/csv-stringify/lib/api/index.js Outdated
}
// Escape all quote characters if quoted
if (shouldQuote === true) {
if (shouldQuote === true && containsQuote === true) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

containsQuote is computed before the escape_formulas block, which can add a ' in front of the value. When the quote char is ', that new quote is no longer escaped, so the output can't be parsed back:

const opts = { quote: "'", escape: "'", quoted: true, escape_formulas: true };
stringify([["=1", "b"]], opts);
// master:   "'''=1','b'\n"  → parse gives [["'=1","b"]]
// this PR:  "''=1','b'\n"   → parse throws "Invalid Closing Quote: got \"=\""

Same with quoted_match instead of quoted. Checking the empty quote directly fixes the quote: false case without depending on the earlier flag:

Suggested change
if (shouldQuote === true && containsQuote === true) {
if (shouldQuote === true && quote !== "") {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this! Applied your suggestion and added a regression test that fails before the fix and passes afterward. The full suite passes locally

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked a45082b: =1 with quote: "'" now round-trips through csv-parse for both quoted and quoted_match, and quote: false / quote: "" with quoted: true give abc,d with no stray quotes. The 160 stringify tests pass for me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants