[Bug]: TS view prints an unused template element statement without its `;`, so TypeScript adds TS2657 or TS1005
[!NOTE] This is a bug today, with Ripple's pinned
@tsrx/core0.3.3. It doesn't depend on the upgrade to@tsrx/core0.5.0 (#1520) and can be fixed now.
Issue Category
Compilation Bug
Bug Description
A template element or fragment written as a statement whose output isn't used is a compile error: This TSRX template output is unused. Return it, assign it to a value that is rendered, or make it part of the rendered output of a function '@{...}' body. In the editor output (compile_to_volar_mappings), Ripple prints such an element without its ;, so TypeScript reports a second syntax error that has nothing to do with the mistake:
- Two element statements in a row (
<div />;then<><div /></>;) print as adjacent elements, and TypeScript reads them as siblings without a parent: TS2657JSX expressions must have one parent element. if (x) <div />withelse <b />on the next line prints on one line asif (x) <div /> else <b />. The source relied on the line break to end the statement, so TypeScript reports TS1005';' expected.
Only input that is already an error is affected, but the editor shows the TypeScript error next to the real one, at a different place.
Expected: the TS view keeps the ;, as in <div />; and if (x) <div />; else <b />;, so only the compile error is reported.
Reproduction Steps
From the repository root at 83ed5615968364a077cbd39bc26a74e4d8c12b11, after pnpm install:
cd packages/tsrx-ripple
node --input-type=module <<'JS'
import { compile_to_volar_mappings } from './src/index.js';
import ts from 'typescript';
const sources = [
`export function Test() {
<div />;
<><div /></>;
}`,
`export function C({ x }: { x: boolean }) @{
if (x) <div />
else <b />
<span />
}`,
];
for (const source of sources) {
const { code, errors } = compile_to_volar_mappings(source, 'App.tsrx', { loose: true });
const file = ts.createSourceFile('App.tsx', code, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
console.log(code.split('\n').filter((line) => !line.startsWith('import ')).join('\n'));
console.log('compile errors:', errors.length, errors[0]?.message.split('.')[0]);
console.log('TSX syntax errors:', file.parseDiagnostics.map((d) => `TS${d.code} ${d.messageText}`).join('; '));
console.log('---');
}
JSOutput:
export function Test() {
<div />
<><div /></>
}
compile errors: 2 This TSRX template output is unused
TSX syntax errors: TS2657 JSX expressions must have one parent element.
---
export function C({ x }: { x: boolean }) {
if (x) <div /> else <b />
return <span />;
}
compile errors: 2 This TSRX template output is unused
TSX syntax errors: TS1005 ';' expected.
---Cause
@tsrx/core's parser returns a template element that starts a statement as the statement itself (plugin.js#L8083-L8105). So <div />; is a JSXElement followed by an EmptyStatement, and an if without braces has the JSXElement as its consequent.
In the TS view, the client transform's JSXElement visitor returns the element as an expression, and nothing wraps it as a statement, so it stays in the statement list as is. esrap's statement-list printer skips the EmptyStatement and writes no ; after a node that isn't an ExpressionStatement. The JSXFragment visitor's TS-view branch does the same.
@tsrx/core's own targets have the same bug in their editor output: tsrx-org/tsrx#730. Ripple needs its own fix, since it builds the TS view with its own transform and printer.
Suggested fix
In the TS view, emit a template element or fragment in statement position (a statement list, or an if, else, or loop body) as an ExpressionStatement, so it prints with its ;. The server transform already does this for regular JS statement positions with is_regular_js_statement_position and b.stmt(...). The TS view also has to cover if, else, and loop bodies.
A compile_to_volar_mappings test could check that both sources above give no TSX syntax errors and still report the two unused-output errors.
Additional Context
Found while checking the @tsrx/core upgrade (#1520). It isn't an upgrade effect: the pinned 0.3.3 has it too.
Environment
- Ripple
mainat83ed5615968364a077cbd39bc26a74e4d8c12b11,@tsrx/ripple0.2.4,@tsrx/core0.3.3, esrap 2.3.2 - The same TSX syntax errors with tsrx
main(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