Enclose A JSX Comment In Braces

ID

javascript.jsx_comment_not_enclosed

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

jsx, reliability

Description

Reports a comment written among a JSX element’s children without the braces that make it a comment. JSX children are markup, not code: the characters are rendered.

Rationale

Inside JSX children a comment has to be an expression, which means braces. Written bare it is text, and React renders it — the comment shows up on the page, usually pushed into a layout that was not expecting it.

It is a natural mistake to make, because the same characters two lines up, outside the markup, are a comment. That is exactly the distinction the rule has to draw: a comment immediately above a <div> is fine, and a comment immediately above a <span> inside that div is not. Both precede the same token, so the comment’s own position decides — the innermost node that encloses it is found by walking up from the token that follows it, and only a JSX element or fragment answering there means the comment landed among children. A brace pair answers with the child expression and an opening tag answers with itself, so the correct spellings are silent by construction.

One caveat, measured against this parser rather than assumed: the lexer scans // as a line comment wherever it appears, so a single-line comment among JSX children is only visible when the markup survives it. It does when the comment sits on a line of its own; it does not when the comment follows children on the same line, because the rest of that line is swallowed and the file fails to parse. The block form is unaffected.

<div>/* the total */{value}</div>        // FLAW - the reader sees the comment
<ul>
  /* one row per item */                 // FLAW - same, on its own line
  <li>{item}</li>
</ul>

<div>{/* the total */}{value}</div>      // OK - braces make it a comment
/* about the element */
<div>{value}</div>                       // OK - outside the markup

Remediation

Wrap the comment in braces so it becomes an expression: {/* the total */}. Move it above the JSX expression instead when it is about the whole block, where an ordinary comment is fine. Put a comment about one attribute inside the opening tag, which is JavaScript context and needs no braces.

© 2026 Xygeni. All rights reserved