Skip to content

Commit

Permalink
[compiler] add fire imports
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.

--
  • Loading branch information
jbrown215 committed Dec 17, 2024
1 parent 157410d commit 4d7660a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 2 deletions.
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 @@ -770,6 +770,7 @@ export class Environment {
fnType: ReactFunctionType;
useMemoCacheIdentifier: string;
hasLoweredContextAccess: boolean;
hasFireRewrite: boolean;

#contextIdentifiers: Set<t.Identifier>;
#hoistedIdentifiers: Set<t.Identifier>;
Expand All @@ -794,6 +795,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 @@ -232,6 +231,7 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {

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

Expand Down Expand Up @@ -570,7 +570,7 @@ class Context {
inUseEffectLambda(): boolean {
return this.#inUseEffectLambda;
}

addFunctionExpression(id: IdentifierId, fn: FunctionExpression): void {
this.#functionExpressions.set(id, fn);
}
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 4d7660a

Please sign in to comment.