Skip to content

Represent JSX component annotations with a parsetree node - #8884

Merged
cknitt merged 2 commits into
masterfrom
jsx-component-node
Oct 17, 2026
Merged

cknitt merged 2 commits into
masterfrom
jsx-component-node

Conversation

@cknitt

@cknitt cknitt commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Part of #8624 (attribute → node: JSX component attributes).

@react.component, @jsx.component and their componentWithProps forms become a node on value bindings and value descriptions (pvb_jsx_component / pval_jsx_component), following the recipe used for @as in #8619:

  • Ast_helper.Vb.mk / Val.mk take the annotation out of the attributes. Vb.binding_attributes / Val.description_attributes put it back where it was written for the printers, mappers/iterators, the v0 PPX bridge and the typed tree.

  • The JSX transform reads the node instead of scanning attribute names. The first @react.component, else the first componentWithProps, becomes the node, the precedence the transform always used.

  • Any component annotation left among the attributes is now an error, which is the only user-facing change:

    • a payload the transform doesn't understand (@react.component("x"), @react.componentWithProps(: t)) used to be accepted, with the payload silently ignored;
    • a second annotation on the same definition used to be dropped silently on let bindings, and rejected with assorted messages on externals and signature items.

    Keeping these as attributes is what lets the formatter preserve them. With no leftovers possible, the transform no longer filters attributes or counts annotations (second commit).

  • Magic numbers bumped: ast, cmi, cmt.

Verification

I compared base and branch bsc and res_parser on 232 generated programs covering every annotation set × position (module/file/local let, let … and, external, signature let/external, module type), including comments and doc comments between attributes:

  • with -bs-jsx 4: identical JS and diagnostics except for the 45 programs with an unsupported payload and the 45 with two annotations (17 of which compiled before), which now hit the new errors;
  • without JSX: 232/232 identical;
  • formatting, plain and with -test-ast-conversion: identical and idempotent.

New tests

  • tests/syntax_tests/data/ast-mapping/JsxComponents.res: the transform still sees every annotation after the v0 round trip. The output is identical to base.
  • ounit (ounit_ast_mapper0_tests.ml): extraction, precedence, unknown payloads, v0 attribute order (source positions and PPX ties), spelling, mapper round trip, value descriptions.
  • super_errors: react_component_invalid_payload.res, react_componentWithProps_invalid_payload.res, react_component_duplicate_annotation.res.

🤖 Generated with Claude Code

https://claude.ai/code/session_014hcyfimX2ss8qj5MZwdMdP

`@react.component`, `@jsx.component` and their `componentWithProps` forms
on a `let`, an `external` or a signature item become `pvb_jsx_component` /
`pval_jsx_component`: the kind (labelled arguments, optionally sharing a
props type given as `(: t)`, or a props record), the spelling, and the
attribute's location. `Ast_helper.Vb.mk` and `Val.mk` take the annotation
out of the attributes, as `Type.field` does for `@as`, and
`Vb.binding_attributes` / `Val.description_attributes` put it back where it
was written for the printers, the mappers and iterators, the v0 PPX bridge
and the typed tree. The JSX transform reads the node instead of scanning
attribute names. An annotation whose payload the transform does not
understand, such as `@react.component("x")`, stays an attribute and is now
reported; it used to be accepted with the payload ignored.

Example (rescript):
    module C = {
      @react.component("Button")
      let make = (~label) => React.string(label)
    }

Diagnostic (text):
    @react.component takes no payload, or the props type to share, as in
    @react.component(: props).

Review: Why a field on the binding, not a structure item of its own?
 The annotation is written on a `let`, an `external` or a signature `let`,
 and the JSX transform expands it in place. A field keeps that shape, and
 bindings the transform leaves alone (local `let`s, `componentWithProps` on
 a signature item, everything when JSX is off) keep their meaning without
 special cases.

Review: Why does the typed tree get the attribute back?
 Nothing downstream reads the node, but `.cmi` attributes and tools that
 read typed attributes saw `@react.component` wherever the JSX transform
 did not consume it. Restoring the written attributes keeps that, and with
 it diagnostics that print those declarations.

Review: Where does the attribute go on the v0 wire?
 At its source position among the other attributes. PPX output carries no
 positions, so everything ties; the annotation then goes first, where it is
 usually written. A PPX that writes it after another attribute sees the
 order swapped after a round trip; `@as` has the same limitation.

Review: Do duplicates behave as before?
 Yes. The first `@react.component`, or failing that the first
 `componentWithProps`, becomes the node, the precedence the transform
 always applied. The rest stay attributes: bindings drop them as before,
 externals and signature items count them for the existing "Only one JSX
 component call" and `componentWithProps` external errors.

Area: syntax printer frontend typechecker analysis tests
Output: unchanged, the test suites' generated JS is byte-identical; 232
 generated programs compile to identical JS without JSX, and with JSX 4
 except the 45 with an unsupported payload, which now fail with the error
 above
Formatting: unchanged, base and branch res_parser print 232 generated and
 comment-heavy files identically, plain and through the v0 AST
Formats: ast cmi cmt
PPX: unchanged wire: PPXs see the attributes at their source positions;
 ast-mapping fixture JsxComponents.res
Editor: unchanged: iterators see the annotation as an attribute, and
 Utils.is_jsx_component reads the node
Errors: none, because the new messages are inline `raise_errorf` calls,
 which the catalog's scope leaves out
Tests: +tests/syntax_tests/data/ast-mapping/JsxComponents.res
 +tests/build_tests/super_errors/fixtures/react_component_invalid_payload.res
 +tests/build_tests/super_errors/fixtures/react_componentWithProps_invalid_payload.res
 ~tests/ounit_tests/ounit_ast_mapper0_tests.ml
Measured: differential harness, base vs branch bsc -bs-jsx 4: 187 of 232
 programs identical, 45 differ by the new payload error; without JSX: 232
 of 232 identical
PR: #8884
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014hcyfimX2ss8qj5MZwdMdP
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the jsx-component-node branch from e5a1dfd to 4f4d9d7 Compare October 17, 2026 07:58
@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.63804% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.04%. Comparing base (4e11b25) to head (ce53d0d).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
tests/ounit_tests/ounit_ast_mapper0_tests.ml 91.76% 7 Missing ⚠️
compiler/syntax/src/jsx_common.ml 66.66% 3 Missing ⚠️
compiler/ml/ast_helper.ml 97.22% 1 Missing ⚠️
compiler/ml/ast_mapper_to0.ml 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8884      +/-   ##
==========================================
+ Coverage   81.01%   81.04%   +0.02%     
==========================================
  Files         454      455       +1     
  Lines       63187    63502     +315     
==========================================
+ Hits        51193    51463     +270     
- Misses      11994    12039      +45     
Files with missing lines Coverage Δ
analysis/src/dump_ast.ml 44.01% <ø> (+0.18%) ⬆️
analysis/src/utils.ml 49.71% <100.00%> (-0.87%) ⬇️
compiler/ext/config.ml 100.00% <ø> (ø)
compiler/frontend/ast_external_mk.ml 100.00% <ø> (ø)
compiler/frontend/ast_tuple_pattern_flatten.ml 96.96% <100.00%> (+0.09%) ⬆️
compiler/frontend/bs_builtin_ppx.ml 90.61% <100.00%> (+0.12%) ⬆️
compiler/ml/ast_iterator.ml 92.70% <100.00%> (-1.44%) ⬇️
compiler/ml/ast_mapper.ml 98.45% <100.00%> (+0.03%) ⬆️
compiler/ml/pprintast.ml 91.59% <100.00%> (-0.29%) ⬇️
compiler/ml/printast.ml 15.45% <ø> (-0.24%) ⬇️
... and 8 more

... and 18 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@8884

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@8884

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@8884

@rescript/darwin-x64

npm i https://pkg.pr.new/@rescript/darwin-x64@8884

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@8884

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@8884

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@8884

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@8884

commit: ce53d0d

@github-actions

Copy link
Copy Markdown

A definition with two component annotations, such as
`@react.component @react.componentWithProps let make = ...`, is now an
error. On `let` bindings the second annotation used to be dropped silently;
on externals and signature items it was already an error, with messages
that depended on the combination. With no annotation left over after the
node takes one, the JSX transform no longer filters attributes or counts
annotations: externals and signature items match on the node directly.

Example (rescript):
    module C = {
      @react.component @react.componentWithProps
      let make = (~label) => React.string(label)
    }

Diagnostic (text):
    A definition can have only one JSX component annotation.

Breaking: a `let` with two component annotations stops compiling; delete
 the extra one. The one that took effect is the first `@react.component`,
 or failing that the first `componentWithProps`.

Review: Which diagnostics change on externals and signature items?
 "Only one JSX component call can exist on a component at one time", and
 the `componentWithProps` external error where `@react.component` was also
 present, become the new message, located at the second annotation.

Area: syntax tests
Output: unchanged, except that the 45 generated programs with two
 annotations now fail with the error above; 17 of them compiled before
Formatting: unchanged, base and branch res_parser print 232 generated files
 identically
Formats: none
PPX: unchanged
Editor: unchanged
Errors: none, because the message is an inline `raise_errorf` call, which
 the catalog's scope leaves out
Tests: +tests/build_tests/super_errors/fixtures/react_component_duplicate_annotation.res
 ~tests/syntax_tests/data/ast-mapping/JsxComponents.res
Measured: differential harness, base vs branch bsc -bs-jsx 4: 142 of 232
 programs identical, 45 differ by the payload error, 45 by this one;
 without JSX: 232 of 232 identical
PR: #8884
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014hcyfimX2ss8qj5MZwdMdP
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

Copy link
Copy Markdown

@cknitt
cknitt marked this pull request as ready for review October 17, 2026 09:47
@cknitt

cknitt commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-17T09:50:01.813054Z ce53d0d Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: ce53d0d039

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt merged commit 58f1bd2 into master Oct 17, 2026
30 checks passed
@cknitt
cknitt deleted the jsx-component-node branch October 17, 2026 12:21
cknitt added a commit that referenced this pull request Oct 21, 2026
…ing (#8944)

#8884 made a payload on @react.componentWithProps an error, breaking code
that compiled before; the payload was always ignored.


Claude-Session: https://claude.ai/code/session_01GPwM4m3vhYgbdzgXvmQ7Te

Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants