Detect unknown hooks on React namespace

Handles an edge-case from earlier in the stack. When looking up a property on a 
shape, if the property is defined we return it. But if it isn't defined, and the 
property name is a hook, we treat it like a default custom hook.
This commit is contained in:
Joe Savona
2023-10-03 09:00:11 -07:00
parent a2dd376820
commit b7a14ecc8d
4 changed files with 48 additions and 7 deletions
@@ -374,9 +374,12 @@ export class Environment {
loc: null,
suggestions: null,
});
return (
shape.properties.get(property) ?? shape.properties.get("*") ?? null
);
let value =
shape.properties.get(property) ?? shape.properties.get("*") ?? null;
if (value === null && isHookName(property)) {
value = this.#getCustomHookType();
}
return value;
} else if (isHookName(property)) {
return this.#getCustomHookType();
} else {
@@ -27,21 +27,30 @@ import { unstable_useMemoCache as useMemoCache } from "react";
import * as React from "react";
function Component(props) {
const $ = useMemoCache(2);
const $ = useMemoCache(4);
const c_0 = $[0] !== props.value;
let t0;
if (c_0) {
t0 = (() => {
t0 = () => {
const x = [];
x.push(props.value);
return x;
})();
};
$[0] = props.value;
$[1] = t0;
} else {
t0 = $[1];
}
const x_0 = t0;
const c_2 = $[2] !== t0;
let t1;
if (c_2) {
t1 = t0();
$[2] = t0;
$[3] = t1;
} else {
t1 = $[3];
}
const x_0 = t1;
return x_0;
}
@@ -0,0 +1,22 @@
## Input
```javascript
function Component(props) {
let x = null;
if (props.cond) {
x = React.useNonexistentHook();
}
return x;
}
```
## Error
```
[ReactForget] InvalidReact: Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning) (4:4)
```
@@ -0,0 +1,7 @@
function Component(props) {
let x = null;
if (props.cond) {
x = React.useNonexistentHook();
}
return x;
}