Skip to content

JSX inside a dynamic tag name is left in the compiled output, so the module is not valid JavaScript #1329

Closed

Description

@leonidaz

Warning

Wait for tsrx-org/tsrx#737 before fixing this. TSRX decided on 2026-09-26 that a dynamic tag expression may only be an identifier, a member access or a string; anything more is computed above the element (const Tag = …;). @tsrx/core will report arrow functions and elements in a tag as errors, so every input below stops compiling once Octane picks up the @tsrx/core release with that fix. Re-check then whether anything is left, for example in .tsx files, if they don't go through @tsrx/core's check. @tsrx/oxc will follow the same rule.

Today it's still a bug, on Octane main with the published @tsrx/core 0.4.0, with both parsers.

Summary

JSX inside a dynamic tag name is copied into the compiled output as JSX, in client and server mode, so the module isn't valid JavaScript. That covers an arrow function used as an inline component (<{() => <b>x</b>} />), a conditional that picks one (<{c ? () => <b>x</b> : 'i'} />), and a logical expression (<{c || <b>x</b>} />). An arrow function with a @{ … } body there makes compile() throw Not implemented: JSXCodeBlock.

The same arrow function as a prop value compiles correctly: its JSX becomes _$createElement('b', {}, 'x').

jsxNameToExpr returns a dynamic tag's expression as written:

// `<{expr}/>` — dynamic tag carries the expression directly.
if (name.type === 'JSXExpressionContainer') return name.expression;

Of its callers, extractFragmentComponent passes the result through rewriteJsxValues, which lowers JSX in prop values. The value-position lowering (L22286) and tagExprNode don't. It happens with both parsers: @tsrx/oxc on Node, and @tsrx/core in the browser compiler. A dynamic tag in value position (const el = <{c ? () => <b>x</b> : 'i'} />;) and in a .tsx file give invalid output too.

Reproduction

At e813fc0e6d (current main), after pnpm install --frozen-lockfile --ignore-scripts, save this as packages/octane/repro.mjs and run node repro.mjs from packages/octane:

import { compile } from 'octane/compiler';
import { transformSync } from 'esbuild';

const elements = {
	'tag: a conditional with an arrow function': `<{c ? () => <b>x</b> : 'i'} />`,
	'tag: an arrow function': `<{() => <b>x</b>} />`,
	'tag: a logical expression': `<{c || <b>x</b>} />`,
	'tag: an arrow function with a @{ } body': `<{() => @{ <b>x</b> }} />`,
	'control, a prop value': `<Slot render={c ? () => <b>x</b> : 'i'} />`,
};

for (const [name, element] of Object.entries(elements)) {
	const source = `export function App({ c, Slot }) @{
	<div>
		${element}
	</div>
}`;
	console.log(name);
	for (const mode of ['client', 'server']) {
		try {
			const { code } = compile(source, 'App.tsrx', { mode, hmr: false });
			let result = 'valid JavaScript';
			try {
				transformSync(code, { loader: 'js', format: 'esm' });
			} catch (error) {
				result = `invalid JavaScript: ${error.errors[0].text}`;
			}
			const line = code.split('\n').find((l) => l.includes('<b>') || l.includes("'b'"));
			console.log(`  ${mode}: ${result}`);
			console.log(`    ${line.trim()}`);
		} catch (error) {
			console.log(`  ${mode}: compile throws: ${error.message.split('\n')[0]}`);
		}
	}
}

Output:

tag: a conditional with an arrow function
  client: invalid JavaScript: The JSX syntax extension is not currently enabled
    _$componentSlot(__s, 1, _b.a, c ? () => <b>x</b> : 'i', {}, undefined, undefined, undefined, undefined, undefined, 'c:8682a381');
  server: invalid JavaScript: The JSX syntax extension is not currently enabled
    return _$ssrHtml(`${`<div>${`${_$ssrComponent(__s, c ? () => <b>x</b> : 'i', {}, false, undefined, undefined, 'c:8682a381')}`}</div>`}`);
tag: an arrow function
  client: invalid JavaScript: The JSX syntax extension is not currently enabled
    _$componentSlot(__s, 1, _b.a, () => <b>x</b>, {}, undefined, undefined, undefined, undefined, undefined, 'c:8682a381');
  server: invalid JavaScript: The JSX syntax extension is not currently enabled
    return _$ssrHtml(`${`<div>${`${_$ssrComponent(__s, () => <b>x</b>, {}, false, undefined, undefined, 'c:8682a381')}`}</div>`}`);
tag: a logical expression
  client: invalid JavaScript: The JSX syntax extension is not currently enabled
    _$componentSlot(__s, 1, _b.a, c || <b>x</b>, {}, undefined, undefined, undefined, undefined, undefined, 'c:8682a381');
  server: invalid JavaScript: The JSX syntax extension is not currently enabled
    return _$ssrHtml(`${`<div>${`${_$ssrComponent(__s, c || <b>x</b>, {}, false, undefined, undefined, 'c:8682a381')}`}</div>`}`);
tag: an arrow function with a @{ } body
  client: compile throws: Not implemented: JSXCodeBlock (consider using 'esrap/languages/tsx')
  server: compile throws: Not implemented: JSXCodeBlock (consider using 'esrap/languages/tsx')
control, a prop value
  client: valid JavaScript
    _$componentSlot(__s, 1, _b.a, Slot, { 'render': c ? () => _$createElement('b', {}, 'x') : 'i' }, undefined, undefined, undefined, undefined, undefined, 'c:8682a381');
  server: valid JavaScript
    return _$ssrHtml(`${`<div>${`${_$ssrComponent(__s, Slot, { "render": c ? () => _$createElement('b', {}, 'x') : 'i' }, false, undefined, undefined, 'c:8682a381')}`}</div>`}`);

The JSX syntax extension is not currently enabled is esbuild reporting the <b>x</b> left in the output.

Expected: each dynamic tag compiles as the prop value does, with its JSX lowered (c ? () => _$createElement('b', {}, 'x') : 'i'), and the @{ … } body lowered as it is in a prop value.

Suggested fix

  • Lower the dynamic tag expression as a prop value is lowered, wherever the tag becomes an expression: the callers of jsxNameToExpr at L22286 and L30940 (tagExprNode), and the .tsx path, which fails even though L21317 wraps it.
  • Add client and server tests for a dynamic tag holding an arrow function with an element body, one with a @{ … } body, a conditional, and a logical expression, in template and value position, checking that the output parses and renders.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions