Print string literals from raw source to preserve escapes - #68
Merged
Conversation
The Literal/StringLiteral case emitted the cooked `node.value` wrapped in
quotes, so escape sequences were destroyed: `'\n'` printed as a real
newline, breaking the (single-quoted) literal, and `\t`, `\uXXXX`, and
escaped quotes were likewise corrupted. Round-tripping any file with an
escaped string (e.g. `assert('\nmessage', ...)`) produced invalid output.
Return `node.raw` (the exact source) when present; fall back to
`JSON.stringify(value)` for synthesized nodes that have no raw, so those
are still quoted and escaped correctly.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
🏎️ Benchmark Comparison
Full mitata output |
Merged
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
The
Literal/StringLiteralcase printed the cookednode.valuewrapped in quotes, destroying escape sequences. Round-tripping any string with an escape produced corrupt/invalid output:\t,\uXXXX, and escaped quotes were corrupted the same way. This is what mangled real files likeassert('\n…', args.style)when a codemod reprinted them.Change
Return
node.raw/node.extra.raw(the exact original source — escapes and quote style intact) when present. Fall back toJSON.stringify(value)for synthesized nodes that have noraw, so those are still safely quoted and escaped.Tests
print.test.js: escape preservation viaraw; synthesized fallback escaping.parse-print-gjs.test.js: round-trip of'\nLine "two"\té'.oxlint+oxfmt --check+publintclean.🤖 Generated with Claude Code