[Bug]: TS view prints template text decoded, so `>`, `<`, `>` and `{x}` reach TypeScript bare
[!NOTE] A bug today. The fix needs
@tsrx/core0.5.0 or later, for tsrx-org/tsrx#711'srawwithout comments. 0.5.0 and 0.5.1 are published. Fix it together with the core bump in #1520, which now goes straight to 0.5.2 (updated 2026-09-29).
Issue Category
Compilation Bug
Bug Description
Ripple's TS view (compile_to_volar_mappings) prints template text as the string Ripple decoded for rendering, not as it's written. A character that JSX text can't hold reaches TypeScript bare, and a character reference turns into the character it stands for:
<div><span /> > 5</div>prints a bare>(TS1382).<span><3</span>prints a bare<(TS1003).>prints as>(TS1382), and<as<.{x}prints as{x}, an expression that reads an undeclaredx.- A
<script>body is printed the same way.const value = 1 < 2 ? 'a' : 'b';inpackages/ripple/tests/client/head.test.tsrxandpackages/ripple/tests/server/head.test.tsrxgets a bare<(TS1003).
The client and server builds are correct. Only the editor output is wrong. It happens on every @tsrx/core version I tried: 0.3.3 (pinned), 0.4.0, tsrx main, and tsrx-org/tsrx#711.
Expected: the TS view writes the text as written, with <, >, { and } as <, >, { and }. @tsrx/core's own printer does this for the other targets from tsrx-org/tsrx#711 on (escape_jsx_text).
Reproduction Steps
From the repository root at 83ed5615968364a077cbd39bc26a74e4d8c12b11, after pnpm install:
cd packages/tsrx-ripple
node --input-type=module <<'JS'
import { readFileSync } from 'node:fs';
import { compile_to_volar_mappings } from './src/index.js';
import ts from 'typescript';
const sources = {
'a `>` in text': `export function App() @{
<div><span /> > 5</div>
}`,
'character references': `export function App() @{
<p>{x} > 1</p>
}`,
'head.test.tsrx': readFileSync('../ripple/tests/client/head.test.tsrx', 'utf8'),
};
for (const [name, source] of Object.entries(sources)) {
const { code } = compile_to_volar_mappings(source, 'App.tsrx', { loose: true });
const file = ts.createSourceFile('App.tsx', code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
const [first] = file.parseDiagnostics;
console.log(`${name}: ${file.parseDiagnostics.map((d) => `TS${d.code}`).join(', ') || 'no syntax errors'}`);
if (first) console.log(` ${code.split('\n')[file.getLineAndCharacterOfPosition(first.start).line].trim()}`);
}
JSOutput:
a `>` in text: TS1382
return <div><span /> > 5</div>;
character references: TS1382
return <p>{x} > 1</p>;
head.test.tsrx: TS1003
const value = 1 < 2 ? 'a' : 'b';Cause
transform_tsrx_tsx_child builds the TS view's text with b.jsx_text(value, value). Its value comes from get_template_text_value, which runs Ripple's decode_jsx_text_entities, so the node's raw is the decoded text. The TS view is printed with esrap's tsx language (create_tsx_with_typescript_support). Its JSXText visitor writes raw ?? value without escaping.
tsrx-org/tsrx#711 fixes the same bug for @tsrx/core's targets in core's printer, which Ripple doesn't use.
Suggested fix
Keep the text as written on the node, and escape <, >, { and } when the TS view prints it:
--- a/packages/tsrx-ripple/src/template-ast.js
+++ b/packages/tsrx-ripple/src/template-ast.js
@@ -60,6 +60,19 @@ export function get_template_text_value(node, to_ts) {
return decode_jsx_text_entities(normalized);
}
+/** @type {Record<string, string>} */
+const JSX_TEXT_ESCAPES = { '<': '<', '>': '>', '{': '{', '}': '}' };
+
+/**
+ * JSX text as the TypeScript view prints it: the text as written, with the
+ * characters JSX text can't hold written as character references.
+ * @param {string} raw
+ * @returns {string}
+ */
+export function escape_jsx_text(raw) {
+ return raw.replace(/[<>{}]/g, (ch) => JSX_TEXT_ESCAPES[ch]);
+}
+
--- a/packages/tsrx-ripple/src/transform/client/index.js
+++ b/packages/tsrx-ripple/src/transform/client/index.js
@@ -5842,7 +5843,10 @@ function transform_tsrx_tsx_child(node, context) {
if (node.type === 'JSXText') {
const value = get_template_text_value(node, true);
- return setLocation(b.jsx_text(value, value), /** @type {AST.NodeWithLocation} */ (node));
+ return setLocation(
+ b.jsx_text(value, node.raw ?? value),
+ /** @type {AST.NodeWithLocation} */ (node),
+ );
}
@@ -8279,6 +8283,9 @@ function create_tsx_with_typescript_support(comments) {
return /** @type {Visitors<AST.Node, TransformClientState>} */ ({
...base_tsx,
+ JSXText(node, context) {
+ context.write(escape_jsx_text(node.raw ?? node.value), node);
+ },
_(node, context, visit) {escape_jsx_text also needs importing from ../../template-ast.js in the client transform.
Two requirements:
- It needs
@tsrx/corewith tsrx-org/tsrx#711. Before #711, template text'srawis the source slice, comments between children included:<p>a /* c */ b</p>hasrawa /* c */ b, whilevalueleaves the comment out. #711 makesrawthe text as written without comments. It also makes core'swrapEdgeWhitespace, which Ripple calls on the TS view's children, keeprawinstead of rebuilding each text asjsx_text(value, value). So this change belongs with the@tsrx/coreupgrade that brings #711. - Escape at print time, not in
raw. #711's completion mapping for text that starts with@(segments.js) matches the node'srawagainst the source. When I stored the escaped text inrawinstead, the mapping for<div>@if > x</div>covered@if > x</din the source for@if > xin the output. With the escaping in the printer, it maps@ifto@if.
I checked this against #711's core. I compiled 2,191 sources: Ripple's and tsrx's .tsrx files and test sources, and generated text shapes in 20 template places. The change removes the TS-view syntax errors from 260 of the 269 sources that had them. The other 9 don't involve text: invalid inputs from error tests, an expected-HTML string, #1517, and #1518. Client and server output don't change, and the full test suite is unchanged: the only failures are the 4 from #1516.
Additional Context
- tsrx-org/tsrx#711:
escape_jsx_text, core'sJSXTextprinter, template textraw, andwrapEdgeWhitespace. - tsrx-org/tsrx#710 makes template text's
valuedecoded, in@tsrx/core0.5.2 (tsrx-org/tsrx#972). With this change in place, the TS view already printsraw, so #710 doesn't affect it. Ripple's runtime decoding is a separate matter, covered in #1520, the upgrade issue. - #1514 changes the same printer's root visitor. The new
JSXTextvisitor is independent of it.
Environment
- Ripple
mainat83ed5615968364a077cbd39bc26a74e4d8c12b11,@tsrx/ripple0.2.4,@tsrx/core0.3.3, esrap 2.3.2 - The same TS view with
@tsrx/core0.4.0, tsrxmain(f73b6760), and tsrx-org/tsrx#711 (c71ebf37) - Node 24.18.0, macOS 26.6.2
Checklist
- I've searched for existing issues
Source: Ripple-TS/ripple