fix(vite): don't inject source attrs into angle brackets inside strings/comments - #8
Conversation
The regex JSX scanner matched `<Tag` anywhere in the source, including
inside string literals, template-literal text, and comments. A `<` that
sits after an operator-like char (e.g. the `/` in a `<owner>/<repo>`
placeholder) passed isLikelyJsx() and got a `data-solid-source="..."`
attribute injected straight into the string. The injected `"` terminated
the JS string and crashed the downstream Babel parse with
`Unexpected token, expected ","`.
Add buildLiteralMask(): a small linear scanner that marks every character
inside a string, template-literal text, or comment, and skip any regex
match whose `<` falls in a masked region. Template-literal interpolations
(`${ ... }`) are still treated as code, so real JSX there keeps its source
attributes. Regex literals remain a known (rarer) edge case.
Adds regression tests for the placeholder-in-template case plus string,
comment, and interpolation coverage.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughAdds a ChangesLiteral-aware masking for JSX injection
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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 |
## [1.1.3](v1.1.2...v1.1.3) (2026-06-22) ### Bug Fixes * **vite:** don't inject into angle brackets inside strings/comments ([#8](#8)) ([38886d8](38886d8))
The bug
The Vite plugin's JSX scanner (
transformJsx) matches<Tagwith a raw-textregex over the whole file — it doesn't know about string/comment context. When
a
<happens to sit after an operator-like character,isLikelyJsx()waves itthrough and a
data-solid-source="..."attribute gets injected into whatever itlanded on, even inside a string.
A real-world trigger is a CLI command built with a template literal containing a
<owner>/<repo>placeholder:<repo>matches the regex;isLikelyJsx()walks back, sees the/before it(treated as an operator), and decides it's JSX. The plugin rewrites the string to:
The injected
"terminates the JS string and the downstream Babel parse dies:The fix
Add
buildLiteralMask()— a small linear scanner that marks every characterliving inside a string literal, template-literal text, or comment. The match
loop now skips any
<whose position is masked.Template-literal interpolations (
${ ... }) are treated as code, so genuineJSX inside an interpolation still gets its source/component attributes. Handles
single/double quotes, escapes, line/block comments, and nested
${}.Regex literals are intentionally not tracked — disambiguating regex from
division needs a real tokenizer, and
<tag>inside a regex literal is far rarerthan inside strings/comments. Noted as a known limitation in the code.
Tests
bun test→ 62 pass. New regression cases intests/vite-plugin.test.ts:<owner>/<repo>placeholder inside a template literal → string leftintact, sibling real JSX still gets a source attr
//and/* */comments → no injection${ }interpolation → still injectedVerified end-to-end against the file that surfaced this: the rebuilt plugin
leaves the placeholder untouched and the output parses cleanly.
Summary by CodeRabbit
Bug Fixes
Tests