Support method call when validating useEffect

Treat MethodCall similarly to CallExpression when validation useEffect
This commit is contained in:
Sathya Gunasekaran
2024-03-22 23:42:27 +00:00
parent 1f45b13fda
commit db7e7c7fae
3 changed files with 62 additions and 2 deletions
@@ -1190,7 +1190,9 @@ function inferBlock(
const effects =
signature !== null ? getFunctionEffects(instrValue, signature) : null;
let hasCaptureArgument = false;
let isUseEffect = isEffectHook(instrValue.property.identifier);
for (let i = 0; i < instrValue.args.length; i++) {
const argumentEffects: Array<FunctionEffect> = [];
const arg = instrValue.args[i];
const place = arg.kind === "Identifier" ? arg : arg.place;
if (effects !== null) {
@@ -1200,18 +1202,28 @@ function inferBlock(
*/
state.reference(
place,
functionEffects,
argumentEffects,
effects[i],
ValueReason.Other
);
} else {
state.reference(
place,
functionEffects,
argumentEffects,
Effect.ConditionallyMutate,
ValueReason.Other
);
}
/*
* Join the effects of the argument with the effects of the enclosing function,
* unless the we're detecting a global mutation inside a useEffect hook
*/
functionEffects.push(
...argumentEffects.filter(
(argEffect) =>
!isUseEffect || i !== 0 || argEffect.kind !== "GlobalMutation"
)
);
hasCaptureArgument ||= place.effect === Effect.Capture;
}
if (signature !== null) {
@@ -0,0 +1,37 @@
## Input
```javascript
let x = {};
function Component() {
React.useEffect(() => {
x.foo = 1;
});
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
};
```
## Code
```javascript
let x = {};
function Component() {
React.useEffect(() => {
x.foo = 1;
});
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
};
```
### Eval output
(kind: ok)
@@ -0,0 +1,11 @@
let x = {};
function Component() {
React.useEffect(() => {
x.foo = 1;
});
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [],
};