fix: escape all special characters in identifiers when stringifying#1802
fix: escape all special characters in identifiers when stringifying#1802spokodev wants to merge 2 commits into
Conversation
`charsToEscapeInName` was missing several characters that are special in
selector syntax, so `stringify()` emitted selectors that re-parse to a
different AST or fail to parse. This breaks round-tripping of modern
class names (arbitrary values, CSS nesting), e.g.:
stringify(parse(".x\\&y")) // => ".x&y" (two selectors)
stringify(parse("[data-foo\\=x]")) // => "[data-foo=x]" (name+operator)
stringify(parse(".a\\{b\\}c")) // => ".a{b}c"
Add `=`, `&`, `?`, `@`, `` ` ``, `{` and `}` so every special ASCII
character in an identifier is escaped, matching the existing handling of
`# > < / , ;` etc.
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
ChangesEscape character expansion
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/stringify.spec.ts`:
- Around line 31-35: The test data array starting at line 31 is missing coverage
for the backtick character and specific round-trip selector contexts. Add test
cases to this array for the backtick escape (backtick paired with its escaped
equivalent string), and add three additional test cases for the exact round-trip
selector contexts mentioned in the PR: the class selector with escaped ampersand
(`.x\&y`), the data attribute selector with escaped equals (` [data-foo\=bar]`),
and the class selector with escaped braces (`.a\{b\}c`). Each test case should
follow the existing pattern of providing the input string, the expected escaped
output using String.raw, and a descriptive label.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 902c7b00-7785-47ad-87c4-7401897ac6e0
📒 Files selected for processing (2)
src/stringify.spec.tssrc/stringify.ts
Bug
charsToEscapeInNameis missing several characters that are special in selector syntax, sostringify()produces selectors that re-parse to a different AST (or fail to parse).parse(stringify(x))is no longer equal tox:This breaks round-tripping of modern class names that contain these characters — e.g. Tailwind arbitrary values and CSS-nesting fragments (
grid-cols-[&],data-[x=y], …).Fix
Add
=,&,?,@,`,{and}tocharsToEscapeInName, so every special ASCII character in an identifier is escaped — completing the set alongside the# > < / , ;etc. that are already handled. Escaping is transparent toparse, so existing round-trips are unaffected; only previously-corrupted ones are fixed.Tests
Added cases to the "Stringify CSS spec compliance" block in
stringify.spec.ts(a=b→a\=b,&,{},?,@). They fail onmain(the characters are emitted unescaped) and pass with this change. Full suite: 860 passing, no regressions.Summary by CodeRabbit
Bug Fixes
=,&,?,@, backticks,{, and}are handled correctly.Tests