-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[compiler] Handle member expr as computed property (#31344)
This PR loosens the restriction on the types of computed properties we can handle. Previously, we would disallow anything that is not an identifier because non-identifiers could be mutating. But member expressions are not mutating so we can treat them similar to identifiers.
- Loading branch information
Showing
5 changed files
with
122 additions
and
1 deletion.
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
37 changes: 37 additions & 0 deletions
37
...sts__/fixtures/compiler/error.todo-object-expression-member-expr-call.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,37 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {identity, mutate, mutateAndReturn} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const obj = {mutateAndReturn}; | ||
const key = {}; | ||
const context = { | ||
[obj.mutateAndReturn(key)]: identity([props.value]), | ||
}; | ||
mutate(key); | ||
return context; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{value: 42}], | ||
}; | ||
|
||
``` | ||
|
||
|
||
## Error | ||
|
||
``` | ||
5 | const key = {}; | ||
6 | const context = { | ||
> 7 | [obj.mutateAndReturn(key)]: identity([props.value]), | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ Todo: (BuildHIR::lowerExpression) Expected Identifier, got CallExpression key in ObjectExpression (7:7) | ||
8 | }; | ||
9 | mutate(key); | ||
10 | return context; | ||
``` | ||
16 changes: 16 additions & 0 deletions
16
...compiler/src/__tests__/fixtures/compiler/error.todo-object-expression-member-expr-call.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,16 @@ | ||
import {identity, mutate, mutateAndReturn} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const obj = {mutateAndReturn}; | ||
const key = {}; | ||
const context = { | ||
[obj.mutateAndReturn(key)]: identity([props.value]), | ||
}; | ||
mutate(key); | ||
return context; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{value: 42}], | ||
}; |
53 changes: 53 additions & 0 deletions
53
...ler/src/__tests__/fixtures/compiler/object-expression-computed-member.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,53 @@ | ||
|
||
## Input | ||
|
||
```javascript | ||
import {identity, mutate, mutateAndReturn} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const key = {a: 'key'}; | ||
const context = { | ||
[key.a]: identity([props.value]), | ||
}; | ||
mutate(key); | ||
return context; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{value: 42}], | ||
}; | ||
|
||
``` | ||
|
||
## Code | ||
|
||
```javascript | ||
import { c as _c } from "react/compiler-runtime"; | ||
import { identity, mutate, mutateAndReturn } from "shared-runtime"; | ||
|
||
function Component(props) { | ||
const $ = _c(2); | ||
let context; | ||
if ($[0] !== props.value) { | ||
const key = { a: "key" }; | ||
context = { [key.a]: identity([props.value]) }; | ||
|
||
mutate(key); | ||
$[0] = props.value; | ||
$[1] = context; | ||
} else { | ||
context = $[1]; | ||
} | ||
return context; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{ value: 42 }], | ||
}; | ||
|
||
``` | ||
### Eval output | ||
(kind: ok) {"key":[42]} |
15 changes: 15 additions & 0 deletions
15
...lugin-react-compiler/src/__tests__/fixtures/compiler/object-expression-computed-member.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,15 @@ | ||
import {identity, mutate, mutateAndReturn} from 'shared-runtime'; | ||
|
||
function Component(props) { | ||
const key = {a: 'key'}; | ||
const context = { | ||
[key.a]: identity([props.value]), | ||
}; | ||
mutate(key); | ||
return context; | ||
} | ||
|
||
export const FIXTURE_ENTRYPOINT = { | ||
fn: Component, | ||
params: [{value: 42}], | ||
}; |