Skip to content

Comments in JSX text render as text on Node (@tsrx/oxc parse), but not in the browser compiler #1328

Closed

Description

@leonidaz

Note

This is a bug today, on Octane main with the published @tsrx/core 0.4.0 and @tsrx/oxc 0.16.0. It doesn't depend on @tsrx/core 0.5.0. The fix is in @tsrx/oxc (tsrx-org/oxc#110). Octane then picks up that release and adds the test.

Summary

On Node, Octane renders two kinds of comment in JSX text as text:

  • a // line comment in template text;
  • a /* … */ block comment in the text of JSX in a plain function.

The browser compiler leaves both out, and so do the other TSRX targets. The TSRX specification says comments between template children "are permitted … and do not render" (4.3 Template Elements).

The difference comes from the parser. On Node, parser.node.js parses with @tsrx/oxc's tsrx-core-compat, which keeps these comments in JSXText.value. The browser compiler (parser.browser.js) and the editor parse with @tsrx/core, which leaves them out:

Source @tsrx/oxc 0.16.0 value @tsrx/core 0.4.0 value
template text with a // note line the // note line is in value the // note line is left out
<p>a /* note */ b</p> in a plain function "a /* note */ b" "a b"

normalizeAuthoredJsxLiterals then cooks whatever value holds, so the comment reaches the template and the SSR string.

@tsrx/oxc 0.16.0 is its latest release. A /* … */ comment in template text is already left out of value by both parsers. The parser side is tsrx-org/oxc#110.

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';

const sources = {
	template: `export function App() @{
	<p>
		a
		// note
		b
	</p>
}`,
	'plain function': `export function App() {
	return <p>a /* note */ b</p>;
}`,
};

for (const [name, source] of Object.entries(sources)) {
	for (const mode of ['client', 'server']) {
		const { code } = compile(source, 'App.tsrx', { mode, hmr: false });
		console.log(name, mode, code.split('\n').find((line) => line.includes('<p>')).trim());
	}
}

Output:

template client const _t$0 = /* @__PURE__ */ _$template("<p>a // note b</p>");
template server return _$ssrHtml(`${`<p>${`a // note b`}</p>`}`);
plain function client const _t$0 = /* @__PURE__ */ _$template("<p>a /* note */ b</p>");
plain function server return _$ssrHtml(`${`<p>${`a /* note */ b`}</p>`}`);

With #octane/compiler-parser resolved to parser.browser.js (checked with a Node resolve hook), the same script prints:

template client const _t$0 = /* @__PURE__ */ _$template("<p>a b</p>");
template server return _$ssrHtml(`${`<p>${`a b`}</p>`}`);
plain function client const _t$0 = /* @__PURE__ */ _$template("<p>a  b</p>");
plain function server return _$ssrHtml(`${`<p>${`a  b`}</p>`}`);

Expected: <p>a b</p> from both compilers.

Suggested fix

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