-
Notifications
You must be signed in to change notification settings - Fork 49.8k
[compiler] Detect known incompatible libraries #34027
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
91 changes: 91 additions & 0 deletions
91
compiler/packages/babel-plugin-react-compiler/src/HIR/DefaultModuleTypeProvider.ts
This file contains hidden or 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,91 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import {Effect, ValueKind} from '..'; | ||
| import {TypeConfig} from './TypeSchema'; | ||
|
|
||
| /** | ||
| * Libraries developed before we officially documented the [Rules of React](https://react.dev/reference/rules) | ||
| * implement APIs which cannot be memoized safely, either via manual or automatic memoization. | ||
| * | ||
| * Any non-hook API that is designed to be called during render (not events/effects) should be safe to memoize: | ||
| * | ||
| * ```js | ||
| * function Component() { | ||
| * const {someFunction} = useLibrary(); | ||
| * // it should always be safe to memoize functions like this | ||
| * const result = useMemo(() => someFunction(), [someFunction]); | ||
| * } | ||
| * ``` | ||
| * | ||
| * However, some APIs implement "interior mutability" — mutating values rather than copying into a new value | ||
| * and setting state with the new value. Such functions (`someFunction()` in the example) could return different | ||
| * values even though the function itself is the same object. This breaks memoization, since React relies on | ||
| * the outer object (or function) changing if part of its value has changed. | ||
| * | ||
| * Given that we didn't have the Rules of React precisely documented prior to the introduction of React compiler, | ||
| * it's understandable that some libraries accidentally shipped APIs that break this rule. However, developers | ||
| * can easily run into pitfalls with these APIs. They may manually memoize them, which can break their app. Or | ||
| * they may try using React Compiler, and think that the compiler has broken their code. | ||
| * | ||
| * To help ensure that developers can successfully use the compiler with existing code, this file teaches the | ||
| * compiler about specific APIs that are known to be incompatible with memoization. We've tried to be as precise | ||
| * as possible. | ||
| * | ||
| * The React team is open to collaborating with library authors to help develop compatible versions of these APIs, | ||
| * and we have already reached out to the teams who own any API listed here to ensure they are aware of the issue. | ||
| */ | ||
| export function defaultModuleTypeProvider( | ||
| moduleName: string, | ||
| ): TypeConfig | null { | ||
| switch (moduleName) { | ||
| case 'react-hook-form': { | ||
| return { | ||
| kind: 'object', | ||
| properties: { | ||
| useForm: { | ||
| kind: 'hook', | ||
| returnType: { | ||
| kind: 'object', | ||
| properties: { | ||
| // Only the `watch()` function returned by react-hook-form's `useForm()` API is incompatible | ||
| watch: { | ||
| kind: 'function', | ||
| positionalParams: [], | ||
| restParam: Effect.Read, | ||
| calleeEffect: Effect.Read, | ||
| returnType: {kind: 'type', name: 'Any'}, | ||
| returnValueKind: ValueKind.Mutable, | ||
| knownIncompatible: `React Hook Form's \`useForm()\` API returns a \`watch()\` function which cannot be memoized safely.`, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| case '@tanstack/react-table': { | ||
| return { | ||
| kind: 'object', | ||
| properties: { | ||
| /* | ||
| * Many of the properties of `useReactTable()`'s return value are incompatible, so we mark the entire hook | ||
| * as incompatible | ||
| */ | ||
| useReactTable: { | ||
| kind: 'hook', | ||
| positionalParams: [], | ||
| restParam: Effect.Read, | ||
| returnType: {kind: 'type', name: 'Any'}, | ||
| knownIncompatible: `TanStack Table's \`useReactTable()\` API returns functions that cannot be memoized safely`, | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
| return null; | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
34 changes: 34 additions & 0 deletions
34
...__tests__/fixtures/compiler/error.invalid-known-incompatible-function.expect.md
This file contains hidden or 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,34 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {knownIncompatible} from 'ReactCompilerKnownIncompatibleTest'; | ||
|
|
||
| function Component() { | ||
| const data = knownIncompatible(); | ||
| return <div>Error</div>; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Compilation Skipped: Use of incompatible library | ||
|
|
||
| This API returns functions which cannot be memoized without leading to stale UI. To prevent this, by default React Compiler will skip memoizing this component/hook. However, you may see issues if values from this API are passed to other components/hooks that are memoized. | ||
|
|
||
| error.invalid-known-incompatible-function.ts:4:15 | ||
| 2 | | ||
| 3 | function Component() { | ||
| > 4 | const data = knownIncompatible(); | ||
| | ^^^^^^^^^^^^^^^^^ useKnownIncompatible is known to be incompatible | ||
| 5 | return <div>Error</div>; | ||
| 6 | } | ||
| 7 | | ||
| ``` | ||
|
|
||
|
|
6 changes: 6 additions & 0 deletions
6
...act-compiler/src/__tests__/fixtures/compiler/error.invalid-known-incompatible-function.js
This file contains hidden or 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,6 @@ | ||
| import {knownIncompatible} from 'ReactCompilerKnownIncompatibleTest'; | ||
|
|
||
| function Component() { | ||
| const data = knownIncompatible(); | ||
| return <div>Error</div>; | ||
| } |
33 changes: 33 additions & 0 deletions
33
...xtures/compiler/error.invalid-known-incompatible-hook-return-property.expect.md
This file contains hidden or 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,33 @@ | ||
|
|
||
| ## Input | ||
|
|
||
| ```javascript | ||
| import {useKnownIncompatibleIndirect} from 'ReactCompilerKnownIncompatibleTest'; | ||
|
|
||
| function Component() { | ||
| const {incompatible} = useKnownIncompatibleIndirect(); | ||
| return <div>{incompatible()}</div>; | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
|
|
||
| ## Error | ||
|
|
||
| ``` | ||
| Found 1 error: | ||
|
|
||
| Compilation Skipped: Use of incompatible library | ||
|
|
||
| This API returns functions which cannot be memoized without leading to stale UI. To prevent this, by default React Compiler will skip memoizing this component/hook. However, you may see issues if values from this API are passed to other components/hooks that are memoized. | ||
|
|
||
| error.invalid-known-incompatible-hook-return-property.ts:5:15 | ||
| 3 | function Component() { | ||
| 4 | const {incompatible} = useKnownIncompatibleIndirect(); | ||
| > 5 | return <div>{incompatible()}</div>; | ||
| | ^^^^^^^^^^^^ useKnownIncompatibleIndirect returns an incompatible() function that is known incompatible | ||
| 6 | } | ||
| 7 | | ||
| ``` | ||
|
|
||
|
|
6 changes: 6 additions & 0 deletions
6
.../src/__tests__/fixtures/compiler/error.invalid-known-incompatible-hook-return-property.js
This file contains hidden or 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,6 @@ | ||
| import {useKnownIncompatibleIndirect} from 'ReactCompilerKnownIncompatibleTest'; | ||
|
|
||
| function Component() { | ||
| const {incompatible} = useKnownIncompatibleIndirect(); | ||
| return <div>{incompatible()}</div>; | ||
| } |
Oops, something went wrong.
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.
This wasn't working in playground bc Zod doesn't seem to populate defaults when they are functions (?). I've tested that this works by building and running the linter on a local project.