Skip to content

Commit 9b3a8a8

Browse files
committed
Update on "[compiler] Override type provider for hook-like names"
If an imported name is hook-like but a type provider provides a non-hook type, we now override the returned value and treat it as a generic custom hook. This is meant as an extra check to prevent miscompilation. [ghstack-poisoned]
1 parent 22ea042 commit 9b3a8a8

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-import-object-typed-module-as-hook-name.expect.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ export function Component({a, b}) {
1818
);
1919
}
2020

21+
export const FIXTURE_ENTRYPOINT = {
22+
fn: Component,
23+
params: [{a: 0, b: 0}],
24+
sequentialRenders: [
25+
{a: 0, b: 0},
26+
{a: 1, b: 0},
27+
{a: 1, b: 1},
28+
{a: 1, b: 2},
29+
{a: 2, b: 2},
30+
{a: 3, b: 2},
31+
{a: 0, b: 0},
32+
],
33+
};
34+
2135
```
2236

2337
## Code
@@ -104,7 +118,28 @@ export function Component(t0) {
104118
return t9;
105119
}
106120

121+
export const FIXTURE_ENTRYPOINT = {
122+
fn: Component,
123+
params: [{ a: 0, b: 0 }],
124+
sequentialRenders: [
125+
{ a: 0, b: 0 },
126+
{ a: 1, b: 0 },
127+
{ a: 1, b: 1 },
128+
{ a: 1, b: 2 },
129+
{ a: 2, b: 2 },
130+
{ a: 3, b: 2 },
131+
{ a: 0, b: 0 },
132+
],
133+
};
134+
107135
```
108136
109137
### Eval output
110-
(kind: exception) Fixture not implemented
138+
(kind: ok) [[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
139+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
140+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
141+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
142+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
143+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
144+
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]]
145+
logs: [{ a: 0 },{ b: 0 },{ a: 0 },{ b: 0 },{ a: 1 },{ b: 1 },{ a: 1 },{ b: 2 },{ a: 2 },{ b: 2 },{ a: 3 },{ b: 2 },{ a: 0 },{ b: 0 }]

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-import-object-typed-module-as-hook-name.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ export function Component({a, b}) {
1313
</>
1414
);
1515
}
16+
17+
export const FIXTURE_ENTRYPOINT = {
18+
fn: Component,
19+
params: [{a: 0, b: 0}],
20+
sequentialRenders: [
21+
{a: 0, b: 0},
22+
{a: 1, b: 0},
23+
{a: 1, b: 1},
24+
{a: 1, b: 2},
25+
{a: 2, b: 2},
26+
{a: 3, b: 2},
27+
{a: 0, b: 0},
28+
],
29+
};

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-unknown-import-from-typed-module.expect.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
```javascript
55
import {useMemo} from 'react';
6-
import {useHoook, ValidateMemoization} from 'shared-runtime';
6+
import {useHook, ValidateMemoization} from 'shared-runtime';
77

88
export function Component({a, b}) {
99
const item1 = useMemo(() => ({a}), [a]);
@@ -39,7 +39,7 @@ export const FIXTURE_ENTRYPOINT = {
3939
```javascript
4040
import { c as _c } from "react/compiler-runtime";
4141
import { useMemo } from "react";
42-
import { useHoook, ValidateMemoization } from "shared-runtime";
42+
import { useHook, ValidateMemoization } from "shared-runtime";
4343

4444
export function Component(t0) {
4545
const $ = _c(17);
@@ -135,10 +135,10 @@ export const FIXTURE_ENTRYPOINT = {
135135
```
136136
137137
### Eval output
138-
(kind: ok) [[ (exception in render) ReferenceError: useHook is not defined ]]
139-
[[ (exception in render) ReferenceError: useHook is not defined ]]
140-
[[ (exception in render) ReferenceError: useHook is not defined ]]
141-
[[ (exception in render) ReferenceError: useHook is not defined ]]
142-
[[ (exception in render) ReferenceError: useHook is not defined ]]
143-
[[ (exception in render) ReferenceError: useHook is not defined ]]
144-
[[ (exception in render) ReferenceError: useHook is not defined ]]
138+
(kind: ok) <div>{"inputs":[0],"output":{"a":0}}</div><div>{"inputs":[0],"output":{"b":0}}</div>
139+
<div>{"inputs":[1],"output":{"a":1}}</div><div>{"inputs":[0],"output":{"b":0}}</div>
140+
<div>{"inputs":[1],"output":{"a":1}}</div><div>{"inputs":[1],"output":{"b":1}}</div>
141+
<div>{"inputs":[1],"output":{"a":1}}</div><div>{"inputs":[2],"output":{"b":2}}</div>
142+
<div>{"inputs":[2],"output":{"a":2}}</div><div>{"inputs":[2],"output":{"b":2}}</div>
143+
<div>{"inputs":[3],"output":{"a":3}}</div><div>{"inputs":[2],"output":{"b":2}}</div>
144+
<div>{"inputs":[0],"output":{"a":0}}</div><div>{"inputs":[0],"output":{"b":0}}</div>

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/type-provider-unknown-import-from-typed-module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {useMemo} from 'react';
2-
import {useHoook, ValidateMemoization} from 'shared-runtime';
2+
import {useHook, ValidateMemoization} from 'shared-runtime';
33

44
export function Component({a, b}) {
55
const item1 = useMemo(() => ({a}), [a]);

0 commit comments

Comments
 (0)