-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
[compiler] Override type provider for hook-like names #30868
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45c27a1
[compiler] Override type provider for hook-like names
josephsavona 22ea042
Update on "[compiler] Override type provider for hook-like names"
josephsavona 9b3a8a8
Update on "[compiler] Override type provider for hook-like names"
josephsavona c2dc0d5
Update on "[compiler] Override type provider for hook-like names"
josephsavona 006fc0b
Update on "[compiler] Override type provider for hook-like names"
josephsavona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
123 changes: 123 additions & 0 deletions
123
...ixtures/compiler/type-provider-hooklike-name-not-typed-as-hook-import.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,123 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useMemo} from 'react'; | ||
import { | ||
useArrayConcatNotTypedAsHook, | ||
ValidateMemoization, | ||
} from 'shared-runtime'; | ||
|
||
export function Component({a, b}) { | ||
const item1 = useMemo(() => [a], [a]); | ||
const item2 = useMemo(() => [b], [b]); | ||
const item3 = useArrayConcatNotTypedAsHook(item1, item2); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[a, b]} output={item3} /> | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{a: 0, b: 0}], | ||
sequentialRenders: [ | ||
{a: 0, b: 0}, | ||
{a: 1, b: 0}, | ||
{a: 1, b: 1}, | ||
{a: 1, b: 2}, | ||
{a: 2, b: 2}, | ||
{a: 3, b: 2}, | ||
{a: 0, b: 0}, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useMemo } from "react"; | ||
import { | ||
useArrayConcatNotTypedAsHook, | ||
ValidateMemoization, | ||
} from "shared-runtime"; | ||
|
||
export function Component(t0) { | ||
const $ = _c(10); | ||
const { a, b } = t0; | ||
let t1; | ||
let t2; | ||
if ($[0] !== a) { | ||
t2 = [a]; | ||
$[0] = a; | ||
$[1] = t2; | ||
} else { | ||
t2 = $[1]; | ||
} | ||
t1 = t2; | ||
const item1 = t1; | ||
let t3; | ||
let t4; | ||
if ($[2] !== b) { | ||
t4 = [b]; | ||
$[2] = b; | ||
$[3] = t4; | ||
} else { | ||
t4 = $[3]; | ||
} | ||
t3 = t4; | ||
const item2 = t3; | ||
const item3 = useArrayConcatNotTypedAsHook(item1, item2); | ||
let t5; | ||
if ($[4] !== a || $[5] !== b) { | ||
t5 = [a, b]; | ||
$[4] = a; | ||
$[5] = b; | ||
$[6] = t5; | ||
} else { | ||
t5 = $[6]; | ||
} | ||
let t6; | ||
if ($[7] !== t5 || $[8] !== item3) { | ||
t6 = ( | ||
<> | ||
<ValidateMemoization inputs={t5} output={item3} /> | ||
</> | ||
); | ||
$[7] = t5; | ||
$[8] = item3; | ||
$[9] = t6; | ||
} else { | ||
t6 = $[9]; | ||
} | ||
return t6; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ a: 0, b: 0 }], | ||
sequentialRenders: [ | ||
{ a: 0, b: 0 }, | ||
{ a: 1, b: 0 }, | ||
{ a: 1, b: 1 }, | ||
{ a: 1, b: 2 }, | ||
{ a: 2, b: 2 }, | ||
{ a: 3, b: 2 }, | ||
{ a: 0, b: 0 }, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) <div>{"inputs":[0,0],"output":[0,0]}</div> | ||
<div>{"inputs":[1,0],"output":[1,0]}</div> | ||
<div>{"inputs":[1,1],"output":[1,1]}</div> | ||
<div>{"inputs":[1,2],"output":[1,2]}</div> | ||
<div>{"inputs":[2,2],"output":[2,2]}</div> | ||
<div>{"inputs":[3,2],"output":[3,2]}</div> | ||
<div>{"inputs":[0,0],"output":[0,0]}</div> |
31 changes: 31 additions & 0 deletions
31
.../src/__tests__/fixtures/compiler/type-provider-hooklike-name-not-typed-as-hook-import.tsx
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,31 @@ | ||
import {useMemo} from 'react'; | ||
import { | ||
useArrayConcatNotTypedAsHook, | ||
ValidateMemoization, | ||
} from 'shared-runtime'; | ||
|
||
export function Component({a, b}) { | ||
const item1 = useMemo(() => [a], [a]); | ||
const item2 = useMemo(() => [b], [b]); | ||
const item3 = useArrayConcatNotTypedAsHook(item1, item2); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[a, b]} output={item3} /> | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{a: 0, b: 0}], | ||
sequentialRenders: [ | ||
{a: 0, b: 0}, | ||
{a: 1, b: 0}, | ||
{a: 1, b: 1}, | ||
{a: 1, b: 2}, | ||
{a: 2, b: 2}, | ||
{a: 3, b: 2}, | ||
{a: 0, b: 0}, | ||
], | ||
}; |
145 changes: 145 additions & 0 deletions
145
...xtures/compiler/type-provider-import-object-typed-module-as-hook-name.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,145 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {useMemo} from 'react'; | ||
import useHook from 'shared-runtime'; | ||
|
||
export function Component({a, b}) { | ||
const item1 = useMemo(() => ({a}), [a]); | ||
const item2 = useMemo(() => ({b}), [b]); | ||
useHook(item1, item2); | ||
|
||
return ( | ||
<> | ||
<ValidateMemoization inputs={[a]} output={item1} /> | ||
<ValidateMemoization inputs={[b]} output={item2} /> | ||
</> | ||
); | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{a: 0, b: 0}], | ||
sequentialRenders: [ | ||
{a: 0, b: 0}, | ||
{a: 1, b: 0}, | ||
{a: 1, b: 1}, | ||
{a: 1, b: 2}, | ||
{a: 2, b: 2}, | ||
{a: 3, b: 2}, | ||
{a: 0, b: 0}, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { useMemo } from "react"; | ||
import useHook from "shared-runtime"; | ||
|
||
export function Component(t0) { | ||
const $ = _c(17); | ||
const { a, b } = t0; | ||
let t1; | ||
let t2; | ||
if ($[0] !== a) { | ||
t2 = { a }; | ||
$[0] = a; | ||
$[1] = t2; | ||
} else { | ||
t2 = $[1]; | ||
} | ||
t1 = t2; | ||
const item1 = t1; | ||
let t3; | ||
let t4; | ||
if ($[2] !== b) { | ||
t4 = { b }; | ||
$[2] = b; | ||
$[3] = t4; | ||
} else { | ||
t4 = $[3]; | ||
} | ||
t3 = t4; | ||
const item2 = t3; | ||
useHook(item1, item2); | ||
let t5; | ||
if ($[4] !== a) { | ||
t5 = [a]; | ||
$[4] = a; | ||
$[5] = t5; | ||
} else { | ||
t5 = $[5]; | ||
} | ||
let t6; | ||
if ($[6] !== t5 || $[7] !== item1) { | ||
t6 = <ValidateMemoization inputs={t5} output={item1} />; | ||
$[6] = t5; | ||
$[7] = item1; | ||
$[8] = t6; | ||
} else { | ||
t6 = $[8]; | ||
} | ||
let t7; | ||
if ($[9] !== b) { | ||
t7 = [b]; | ||
$[9] = b; | ||
$[10] = t7; | ||
} else { | ||
t7 = $[10]; | ||
} | ||
let t8; | ||
if ($[11] !== t7 || $[12] !== item2) { | ||
t8 = <ValidateMemoization inputs={t7} output={item2} />; | ||
$[11] = t7; | ||
$[12] = item2; | ||
$[13] = t8; | ||
} else { | ||
t8 = $[13]; | ||
} | ||
let t9; | ||
if ($[14] !== t6 || $[15] !== t8) { | ||
t9 = ( | ||
<> | ||
{t6} | ||
{t8} | ||
</> | ||
); | ||
$[14] = t6; | ||
$[15] = t8; | ||
$[16] = t9; | ||
} else { | ||
t9 = $[16]; | ||
} | ||
return t9; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ a: 0, b: 0 }], | ||
sequentialRenders: [ | ||
{ a: 0, b: 0 }, | ||
{ a: 1, b: 0 }, | ||
{ a: 1, b: 1 }, | ||
{ a: 1, b: 2 }, | ||
{ a: 2, b: 2 }, | ||
{ a: 3, b: 2 }, | ||
{ a: 0, b: 0 }, | ||
], | ||
}; | ||
|
||
``` | ||
|
||
### Eval output | ||
(kind: ok) [[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
[[ (exception in render) ReferenceError: ValidateMemoization is not defined ]] | ||
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 }] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 might produce invalid compilation output on false positives (i.e. functions that are hook-named but not hooks) as hook calls freeze args and returned results whereas regular function calls don't. Imports being renamed nonhook <--> hook look a bit suspicious, but we haven't had any practical issues.
Seems reasonable as our internal use is currently pretty limited. It might be a good idea to warn / bailout here in the future if we find cases of calls being incorrectly inferred as hooks.