-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Rewrite effect dep arrays that use fire (#31811)
If an effect uses a dep array, also rewrite the dep array to use the fire binding --
- Loading branch information
Showing
9 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...s/compiler/transform-fire/error.invalid-rewrite-deps-no-array-literal.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
|
||
const deps = [foo, props]; | ||
|
||
useEffect(() => { | ||
fire(foo(props)); | ||
}, deps); | ||
|
||
return null; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
11 | useEffect(() => { | ||
12 | fire(foo(props)); | ||
> 13 | }, deps); | ||
| ^^^^ Invariant: Cannot compile `fire`. You must use an array literal for an effect dependency array when that effect uses `fire()` (13:13) | ||
14 | | ||
15 | return null; | ||
16 | } | ||
``` | ||
16 changes: 16 additions & 0 deletions
16
...__tests__/fixtures/compiler/transform-fire/error.invalid-rewrite-deps-no-array-literal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
|
||
const deps = [foo, props]; | ||
|
||
useEffect(() => { | ||
fire(foo(props)); | ||
}, deps); | ||
|
||
return null; | ||
} |
37 changes: 37 additions & 0 deletions
37
...__/fixtures/compiler/transform-fire/error.invalid-rewrite-deps-spread.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
|
||
const deps = [foo, props]; | ||
|
||
useEffect(() => { | ||
fire(foo(props)); | ||
}, ...deps); | ||
|
||
return null; | ||
} | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
11 | useEffect(() => { | ||
12 | fire(foo(props)); | ||
> 13 | }, ...deps); | ||
| ^^^^ Invariant: Cannot compile `fire`. You must use an array literal for an effect dependency array when that effect uses `fire()` (13:13) | ||
14 | | ||
15 | return null; | ||
16 | } | ||
``` | ||
19 changes: 19 additions & 0 deletions
19
...piler/src/__tests__/fixtures/compiler/transform-fire/error.invalid-rewrite-deps-spread.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
|
||
const deps = [foo, props]; | ||
|
||
useEffect( | ||
() => { | ||
fire(foo(props)); | ||
}, | ||
...deps | ||
); | ||
|
||
return null; | ||
} |
60 changes: 60 additions & 0 deletions
60
...iler/src/__tests__/fixtures/compiler/transform-fire/fire-and-autodeps.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableFire @inferEffectDependencies | ||
import {fire, useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = arg => { | ||
console.log(arg, props.bar); | ||
}; | ||
useEffect(() => { | ||
fire(foo(props)); | ||
}); | ||
|
||
return null; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { useFire } from "react/compiler-runtime"; | ||
import { c as _c } from "react/compiler-runtime"; // @enableFire @inferEffectDependencies | ||
import { fire, useEffect } from "react"; | ||
|
||
function Component(props) { | ||
const $ = _c(5); | ||
let t0; | ||
if ($[0] !== props.bar) { | ||
t0 = (arg) => { | ||
console.log(arg, props.bar); | ||
}; | ||
$[0] = props.bar; | ||
$[1] = t0; | ||
} else { | ||
t0 = $[1]; | ||
} | ||
const foo = t0; | ||
const t1 = useFire(foo); | ||
let t2; | ||
if ($[2] !== props || $[3] !== t1) { | ||
t2 = () => { | ||
t1(props); | ||
}; | ||
$[2] = props; | ||
$[3] = t1; | ||
$[4] = t2; | ||
} else { | ||
t2 = $[4]; | ||
} | ||
useEffect(t2, [t1, props]); | ||
return null; | ||
} | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Fixture not implemented |
13 changes: 13 additions & 0 deletions
13
...plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/fire-and-autodeps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @enableFire @inferEffectDependencies | ||
import {fire, useEffect} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = arg => { | ||
console.log(arg, props.bar); | ||
}; | ||
useEffect(() => { | ||
fire(foo(props)); | ||
}); | ||
|
||
return null; | ||
} |
57 changes: 57 additions & 0 deletions
57
...-compiler/src/__tests__/fixtures/compiler/transform-fire/rewrite-deps.expect.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
useEffect(() => { | ||
fire(foo(props)); | ||
}, [foo, props]); | ||
|
||
return null; | ||
} | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { useFire } from "react/compiler-runtime"; | ||
import { c as _c } from "react/compiler-runtime"; // @enableFire | ||
import { fire } from "react"; | ||
|
||
function Component(props) { | ||
const $ = _c(4); | ||
const foo = _temp; | ||
const t0 = useFire(foo); | ||
let t1; | ||
let t2; | ||
if ($[0] !== props || $[1] !== t0) { | ||
t1 = () => { | ||
t0(props); | ||
}; | ||
t2 = [t0, props]; | ||
$[0] = props; | ||
$[1] = t0; | ||
$[2] = t1; | ||
$[3] = t2; | ||
} else { | ||
t1 = $[2]; | ||
t2 = $[3]; | ||
} | ||
useEffect(t1, t2); | ||
return null; | ||
} | ||
function _temp(props_0) { | ||
console.log(props_0); | ||
} | ||
|
||
``` | ||
### Eval output | ||
(kind: exception) Fixture not implemented |
13 changes: 13 additions & 0 deletions
13
...abel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/rewrite-deps.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// @enableFire | ||
import {fire} from 'react'; | ||
|
||
function Component(props) { | ||
const foo = props => { | ||
console.log(props); | ||
}; | ||
useEffect(() => { | ||
fire(foo(props)); | ||
}, [foo, props]); | ||
|
||
return null; | ||
} |