dillon1000/react

Commit

[rust-compiler] Carry uninspected AST subtrees as raw JSON text (#36730)

Stacked on #36729 (upstream rejects cross-fork base branches, so this
targets main as a draft; the first commit belongs to the parent PR.
Review the last three commits. Will rebase and mark ready when #36729
lands.)

Unmodeled AST subtrees (type annotations, class bodies, unknown
statements) were stored as `serde_json::Value` trees: every node
allocated through a `Map<String, Value>`, and pass-through subtrees were
repeatedly traversed by code that never looks inside them. They are now
`RawNode`, a newtype over `Box<RawValue>` holding the original JSON text
verbatim.

Design notes, since two obvious alternatives fail:
- Bare `RawValue` fields break under `#[serde(tag = "type")]` enums:
internally-tagged deserialization buffers content into serde's private
`Content` tree, which `RawValue` cannot read from.
`RawNode::deserialize` instead streams whatever deserializer it is
handed through `serde_transcode` into a fresh JSON string, which works
behind tagged enums, `flatten`, and `from_value` alike.
- Default-limit reparses break deep ASTs: internal `RawNode` reparse
sites use `from_json_str_unbounded` (disables serde_json's 128-level
recursion limit, matching how the top-level parse is configured);
regression-tested with a 400-deep statement chain.

`parse_value` fails loudly on malformed text rather than masking
corruption with `Value::Null`; RawNode holds valid JSON by construction.

Size-neutral in the shipped binary; the win is structural (no
speculative `Value` trees on the hot path, pass-through subtrees stay
untouched text).

Verified on this exact tree: cargo workspace tests, both snap channels
1804/1804.

---------

Co-authored-by: Boshen <[email protected]>
Browse files
Changed paths17 files
First-parent comparison
M compiler/Cargo.lock ModifiedM compiler/crates/react_compiler/src/entrypoint/program.rs ModifiedM compiler/crates/react_compiler_ast/Cargo.toml ModifiedM compiler/crates/react_compiler_ast/src/common.rs ModifiedM compiler/crates/react_compiler_ast/src/declarations.rs ModifiedM compiler/crates/react_compiler_ast/src/expressions.rs ModifiedM compiler/crates/react_compiler_ast/src/jsx.rs ModifiedM compiler/crates/react_compiler_ast/src/patterns.rs ModifiedM compiler/crates/react_compiler_ast/src/statements.rs ModifiedA compiler/crates/react_compiler_ast/tests/deep_nesting.rs AddedM compiler/crates/react_compiler_ast/tests/scope_resolution.rs ModifiedM compiler/crates/react_compiler_lowering/src/build_hir.rs ModifiedM compiler/crates/react_compiler_lowering/src/identifier_loc_index.rs ModifiedM compiler/crates/react_compiler_oxc/src/convert_ast.rs ModifiedM compiler/crates/react_compiler_reactive_scopes/src/codegen_reactive_function.rs ModifiedM compiler/crates/react_compiler_swc/src/convert_ast.rs ModifiedM compiler/crates/react_compiler_swc/src/convert_ast_reverse.rs Modified
Patch

Files changed

Rendering syntax-highlighted changes…