-
Notifications
You must be signed in to change notification settings - Fork 47.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Expect components to have hook calls or jsx directly in body
Summary: We can tighten our criteria for what is a component by requiring that a component or hook contain JSX or hook calls directly within its body, excluding nested functions . Currently, if we see them within the body anywhere -- including nested functions -- we treat it as a component if the other requirements are met. This change makes this stricter. We also now expect components (but not necessarily hooks) to have return statements, and those returns must be potential React nodes (we can reject functions that return function or object literals, for example). ghstack-source-id: a1b538c41cad14246a0ba081240b24cabc5fdbe0 Pull Request resolved: #29865
- Loading branch information
Showing
9 changed files
with
203 additions
and
16 deletions.
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
51 changes: 51 additions & 0 deletions
51
...ompiler/src/__tests__/fixtures/compiler/infer-no-component-nested-jsx.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,51 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const result = f(props); | ||
function helper() { | ||
return <foo />; | ||
} | ||
helper(); | ||
return result; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const result = f(props); | ||
function helper() { | ||
return <foo />; | ||
} | ||
helper(); | ||
return result; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) {} |
18 changes: 18 additions & 0 deletions
18
...el-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-no-component-nested-jsx.js
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,18 @@ | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const result = f(props); | ||
function helper() { | ||
return <foo />; | ||
} | ||
helper(); | ||
return result; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
43 changes: 43 additions & 0 deletions
43
...ompiler/src/__tests__/fixtures/compiler/infer-no-component-obj-return.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,43 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const ignore = <foo />; | ||
return { foo: f(props) }; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const ignore = <foo />; | ||
return { foo: f(props) }; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) {"foo":{}} |
14 changes: 14 additions & 0 deletions
14
...el-plugin-react-compiler/src/__tests__/fixtures/compiler/infer-no-component-obj-return.js
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,14 @@ | ||
// @compilationMode(infer) | ||
function Component(props) { | ||
const ignore = <foo />; | ||
return { foo: f(props) }; | ||
} | ||
|
||
function f(props) { | ||
return props; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{}], | ||
}; |
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
1 change: 1 addition & 0 deletions
1
...iler/rules-of-hooks/error.invalid-hook-in-nested-function-expression-object-expression.js
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// @compilationMode(infer) | ||
function Component() { | ||
"use memo"; | ||
const f = () => { | ||
const x = { | ||
outer() { | ||
|
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
1 change: 1 addition & 0 deletions
1
.../__tests__/fixtures/compiler/rules-of-hooks/error.invalid-hook-in-nested-object-method.js
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// @compilationMode(infer) | ||
function Component() { | ||
"use memo"; | ||
const x = { | ||
outer() { | ||
const y = { | ||
|