Skip to content

fix: escape string literals in generated output - #101

Merged
OrKoN merged 2 commits into
mainfrom
escape-string-literals
Nov 10, 2026
Merged

OrKoN merged 2 commits into
mainfrom
escape-string-literals

Conversation

@OrKoN

@OrKoN OrKoN commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Text and byte string values are now escaped before cddlconv writes them between double quotes in Zod and TypeScript output.

Changes

  • Add escape_js_string() in src/util.rs:
    • Keeps valid escape sequences from the CDDL source (\", \\, \n, \u00e9, \u{...}, and others).
    • Escapes raw double quotes. CDDL byte strings ('...') can contain them.
    • Escapes unpaired backslashes, line terminators, and control characters.
    • Escapes / after *, so values such as src/**/*.ts stay valid in doc comments.
  • Use the helper for all text and byte values in both engines: literals, enums, map keys, defaults, and comment text.
  • Add unit tests and an it_works_with_string_literals snapshot test for both engines.

Example

Input:

quoted-bytes = 'say "hello"'

Zod output before this change:

z.literal("'say "hello"'")

This output is not valid TypeScript. After this change:

z.literal("'say \"hello\"'")

Output for existing examples (including WebDriver BiDi) does not change.

Fixed: b/583870769

Text and byte string values are now escaped before they are written
between double quotes in Zod and TypeScript output. Valid escape
sequences from the CDDL source are kept as-is. Raw double quotes,
unpaired backslashes, control characters, and "*/" in doc comments
are escaped.
@OrKoN
OrKoN requested a review from sadym-chromium November 10, 2026 08:10

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Escaping at the single point where values are written between quotes is the right fix, and running the existing examples (including WebDriver BiDi) through unchanged is good evidence that nothing regressed. The unit tests cover the tricky interactions well, such as \\". One edge in the \u{...} acceptance check:

Comment thread src/util.rs Resolved
Accept any number of hex digits when the value is at most U+10FFFF.
@OrKoN
OrKoN merged commit c9d1230 into main Nov 10, 2026
7 checks passed
@OrKoN
OrKoN deleted the escape-string-literals branch November 10, 2026 12:59
OrKoN added a commit that referenced this pull request Nov 11, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.2.1](cddlconv-v0.2.0...cddlconv-v0.2.1)
(2026-11-10)


### Bug Fixes

* escape string literals in generated output
([#101](#101))
([c9d1230](c9d1230))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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