Skip to content

fix: escape quotes when both quote types are at string boundaries#5987

Open
prql-bot wants to merge 1 commit into
mainfrom
fix/string-quote-boundary-escape
Open

fix: escape quotes when both quote types are at string boundaries#5987
prql-bot wants to merge 1 commit into
mainfrom
fix/string-quote-boundary-escape

Conversation

@prql-bot

@prql-bot prql-bot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Problem

Literal::String's Display (via quote_string in lexer/lr.rs) picks single or double quotes to avoid escaping. When a string contains both quote characters and one is at each boundary — e.g. starts with " and ends with ' — neither bare delimiter round-trips:

  • The lexer counts opening/closing quotes greedily (multi_quoted_string in lexer/mod.rs), so a quote at the string boundary merges with the chosen delimiter.
  • Result: the formatted output fails to lex back to the original (e.g. "x' formatted as '''"x'''', which the lexer rejects).

This was a known gap — there was a TODO in quote_string noting the case was unhandled and untested.

Fix

When both quote characters appear at a boundary so no bare delimiter works, fall back to a double-quoted string with embedded " escaped as \". This only applies to normal strings (raw strings have no escape mechanism; such content has no raw-string representation in the first place).

The common cases that motivate the quote-switching (e.g. he's nice"he's nice", he said "what up"'he said "what up"') are unchanged.

Tests

  • test_string_quoting_both_boundary_quotes — snapshots the new escaped output.
  • test_string_roundtrip_boundary — round-trips the affected strings through the lexer, failing before this fix.

🤖 Generated with Claude Code

`quote_string` chooses single or double quotes to avoid escaping, but
when a string contains both quote characters at its boundaries (e.g.
starts with `"` and ends with `'`), neither bare delimiter round-trips —
the boundary quote merges with the delimiter because the lexer counts
opening/closing quotes greedily. This produced output the lexer could
not parse back.

Fall back to escaping double-quotes in that case (only for non-raw
strings, which support escapes). Adds a regression test that round-trips
the affected strings through the lexer.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

1 participant