-
Notifications
You must be signed in to change notification settings - Fork 49.7k
[compiler] Fix false negatives and add data flow tree to compiler error for no-deriving-state-in-effects
#34995
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
4af24bf to
308f659
Compare
no-deriving-state-in-effectsno-deriving-state-in-effects
6e29244 to
b762183
Compare
1e2ab32 to
4a8789c
Compare
| This setState call is setting a derived value that depends on the following reactive sources: | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait Prop: [props] missing here seems like a bug
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
…d instead update typeOfValue and fix infinite loops
Summary:
With this we are now comparing a snapshot of the derivationCache with the new changes every time we are done recording the derivations happening in the HIR.
We have to do this after recording everything since we still do some mutations on the cache when recording mutations.
Test Plan:
Test the following in playground:
```
// @validateNoDerivedComputationsInEffects_exp
function Component({ value }) {
const [checked, setChecked] = useState('');
useEffect(() => {
setChecked(value === '' ? [] : value.split(','));
}, [value]);
return (
<div>{checked}</div>
)
}
```
This no longer causes an infinite loop.
Added a test case in the next PR in the stack
…or for `no-deriving-state-in-effects` Summary: Revamped the derivationCache graph. This fixes a bunch of bugs where sometimes we fail to track from which props/state we derived values from. Also, it is more intuitive and allows us to easily implement a Data Flow Tree. We can print this tree which gives insight on how the data is derived and should facilitate error resolution in complicated components Test Plan: Added a test case where we were failing to track derivations. Also updated the test cases with the new error containing the data flow tree
| Props: [props] | ||
| Data Flow Tree: | ||
| └── computed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yesss
|
|
||
| if (newValue.sourcesIds.size === 0) { | ||
| newValue.sourcesIds.add(derivedVar.identifier.id); | ||
| if (isNamedIdentifier(sourceMetadata.place)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CompilerError.invariant(isNamedIdentifier(sourceMetadata.place), {
reason: "Compiler internal error: expected derivation entries to always reference named variables),
| } else { | ||
| newValue.sourcesIds.add(sourcePlace.identifier.id); | ||
| } | ||
| if (sourceMetadata === undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We checked if all calls to addDerivationEntry only add known-sourceIds, so this should be safe to remove (or assert with CompilerError.invariant)
| context.derivationCache.cache.set(param.identifier.id, { | ||
| place: param, | ||
| sourcesIds: new Set([param.identifier.id]), | ||
| sourcesIds: new Set( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a todo comment and/or fixture for what might be missed by this isNamedIdentifier check?
(pseudocode)
hook useMyHook({x, y, z}) {
const fromPropValue = x + 2;
useEffect(() => setState(fromPropValue), [fromPropValue]);
}
Summary:
Revamped the derivationCache graph.
This fixes a bunch of bugs where sometimes we fail to track from which props/state we derived values from.
Also, it is more intuitive and allows us to easily implement a Data Flow Tree.
We can print this tree which gives insight on how the data is derived and should facilitate error resolution in complicated components
Test Plan:
Added a test case where we were failing to track derivations. Also updated the test cases with the new error containing the data flow tree
Stack created with Sapling. Best reviewed with ReviewStack.
no-deriving-state-in-effects#34995