Skip to content

[rust-compiler] Preserve explicit predicate: null in function-like nodes#36517

Open
poteto wants to merge 1 commit into
pr-36173from
lauren/swc-12-predicate-null-preserve
Open

[rust-compiler] Preserve explicit predicate: null in function-like nodes#36517
poteto wants to merge 1 commit into
pr-36173from
lauren/swc-12-predicate-null-preserve

Conversation

@poteto
Copy link
Copy Markdown
Collaborator

@poteto poteto commented May 21, 2026

The Babel parser emits "predicate": null on every function-like
node (FunctionDeclaration, ArrowFunctionExpression, ObjectMethod,
...) to signal "no Flow %checks predicate". Babel JSON contains
the field explicitly even when no predicate is set, and the test
suite at scripts/test-babel-ast.sh diffs original-vs-round-tripped
JSON byte-equivalently.

FunctionDeclaration and ArrowFunctionExpression already had a
predicate: Option<Box<Value>> field, but plain Option
deserialization collapses both "absent" and "explicit null" into
None, and then skip_serializing_if = "Option::is_none" drops
the field on the way back out. ObjectMethod was missing the
field entirely.

Apply the existing crate::common::nullable_value deserializer
(already used for the same pattern elsewhere) to the two existing
fields, and add the same field shape to ObjectMethod. The helper
distinguishes:

  • absent -> None (skip on serialize)
  • null -> Some(Value::Null) (round-trips as "predicate": null)
  • object -> Some(Value::Object(_)) (round-trips a populated predicate)

FunctionExpression is not touched here because no failing
fixture uses a function expression with predicate: null; can be
extended later if/when one shows up. DeclareFunction.predicate
in declarations.rs has the same shape but is also not exercised
by current fixtures.

Fixes 4 round-trip parity failures:

  • error.todo-hoist-type-alias-before-declaration.js
  • error.todo-round2_severity_diff.js
  • component-in-object-method-body.flow.js
  • (one additional fixture that was truncated in CI output)

Test plan:

  • bash compiler/scripts/test-babel-ast.sh:
    Before: 1774/1780 round-trip (6 failures)
    After: 1778/1780 round-trip (2 failures, 4 fixed)
    Remaining 2 failures are unrelated: TypeCastExpression missing
    from PatternLike (next commit) and a lone-surrogate JSON parse
    failure (skip-list commit after that).
  • cargo test --workspace: not re-run; this change is data-only.

…nodes

The Babel parser emits `"predicate": null` on every function-like
node (FunctionDeclaration, ArrowFunctionExpression, ObjectMethod,
...) to signal "no Flow `%checks` predicate". Babel JSON contains
the field explicitly even when no predicate is set, and the test
suite at scripts/test-babel-ast.sh diffs original-vs-round-tripped
JSON byte-equivalently.

`FunctionDeclaration` and `ArrowFunctionExpression` already had a
`predicate: Option<Box<Value>>` field, but plain `Option`
deserialization collapses both "absent" and "explicit null" into
`None`, and then `skip_serializing_if = "Option::is_none"` drops
the field on the way back out. `ObjectMethod` was missing the
field entirely.

Apply the existing `crate::common::nullable_value` deserializer
(already used for the same pattern elsewhere) to the two existing
fields, and add the same field shape to `ObjectMethod`. The helper
distinguishes:

- absent  -> None (skip on serialize)
- null    -> Some(Value::Null) (round-trips as `"predicate": null`)
- object  -> Some(Value::Object(_)) (round-trips a populated predicate)

`FunctionExpression` is not touched here because no failing
fixture uses a function expression with `predicate: null`; can be
extended later if/when one shows up. `DeclareFunction.predicate`
in declarations.rs has the same shape but is also not exercised
by current fixtures.

Fixes 4 round-trip parity failures:
- error.todo-hoist-type-alias-before-declaration.js
- error.todo-round2_severity_diff.js
- component-in-object-method-body.flow.js
- (one additional fixture that was truncated in CI output)

Test plan:
- bash compiler/scripts/test-babel-ast.sh:
    Before: 1774/1780 round-trip (6 failures)
    After:  1778/1780 round-trip (2 failures, 4 fixed)
  Remaining 2 failures are unrelated: TypeCastExpression missing
  from PatternLike (next commit) and a lone-surrogate JSON parse
  failure (skip-list commit after that).
- cargo test --workspace: not re-run; this change is data-only.
@meta-cla meta-cla Bot added the CLA Signed label May 21, 2026
@github-actions github-actions Bot added the React Core Team Opened by a member of the React Core Team label May 21, 2026
@poteto poteto requested a review from mvitousek May 21, 2026 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant