Skip to content

Commit

Permalink
[compiler] add fire imports (facebook#31797)
Browse files Browse the repository at this point in the history
Summary:

Adds import {useFire} from 'react' when fire syntax is used.

This is experimentation and may not become a stable feature in the
compiler.

--
---
[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31797).
* facebook#31811
* facebook#31798
* __->__ facebook#31797
  • Loading branch information
jbrown215 authored Dec 20, 2024
1 parent 03297e0 commit ab27231
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,11 @@ export function compileProgram(
if (environment.enableChangeDetectionForDebugging != null) {
externalFunctions.push(environment.enableChangeDetectionForDebugging);
}

const hasFireRewrite = compiledFns.some(c => c.compiledFn.hasFireRewrite);
if (environment.enableFire && hasFireRewrite) {
externalFunctions.push({source: 'react', importSpecifierName: 'useFire'});
}
} catch (err) {
handleError(err, pass, null);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ export class Environment {
fnType: ReactFunctionType;
useMemoCacheIdentifier: string;
hasLoweredContextAccess: boolean;
hasFireRewrite: boolean;

#contextIdentifiers: Set<t.Identifier>;
#hoistedIdentifiers: Set<t.Identifier>;
Expand All @@ -811,6 +812,7 @@ export class Environment {
this.#shapes = new Map(DEFAULT_SHAPES);
this.#globals = new Map(DEFAULT_GLOBALS);
this.hasLoweredContextAccess = false;
this.hasFireRewrite = false;

if (
config.disableMemoizationForDebugging &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export type CodegenFunction = {
* This is true if the compiler has the lowered useContext calls.
*/
hasLoweredContextAccess: boolean;

/**
* This is true if the compiler has compiled a fire to a useFire call
*/
hasFireRewrite: boolean;
};

export function codegenFunction(
Expand Down Expand Up @@ -355,6 +360,7 @@ function codegenReactiveFunction(
prunedMemoValues: countMemoBlockVisitor.prunedMemoValues,
outlined: [],
hasLoweredContextAccess: fn.env.hasLoweredContextAccess,
hasFireRewrite: fn.env.hasFireRewrite,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {BuiltInFireId, DefaultNonmutatingHook} from '../HIR/ObjectShape';
/*
* TODO(jmbrown):
* In this stack:
* - Insert useFire import
* - Assert no lingering fire calls
* - Ensure a fired function is not called regularly elsewhere in the same effect
*
Expand Down Expand Up @@ -226,6 +225,7 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {

if (rewriteInstrs.size > 0 || deleteInstrs.size > 0) {
hasRewrite = true;
fn.env.hasFireRewrite = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function Component(props) {
## Code

```javascript
import { useFire } from "react";
import { c as _c } from "react/compiler-runtime"; // @enableFire
import { fire } from "react";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function Component(props) {
## Code

```javascript
import { useFire } from "react";
import { c as _c } from "react/compiler-runtime"; // @enableFire
import { fire } from "react";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function Component(props) {
## Code

```javascript
import { useFire } from "react";
import { c as _c } from "react/compiler-runtime"; // @enableFire
import { fire } from "react";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function Component(props) {
## Code

```javascript
import { useFire } from "react";
import { c as _c } from "react/compiler-runtime"; // @enableFire
import { fire } from "react";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function Component({bar, baz}) {
## Code

```javascript
import { useFire } from "react";
import { c as _c } from "react/compiler-runtime"; // @enableFire
import { fire } from "react";

Expand Down

0 comments on commit ab27231

Please sign in to comment.