Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
35e58360cb |
@@ -451,6 +451,18 @@ function* generateInstructionTypes(
|
|||||||
|
|
||||||
case 'JsxExpression':
|
case 'JsxExpression':
|
||||||
case 'JsxFragment': {
|
case 'JsxFragment': {
|
||||||
|
if (env.config.enableTreatRefLikeIdentifiersAsRefs) {
|
||||||
|
if (value.kind === 'JsxExpression') {
|
||||||
|
for (const prop of value.props) {
|
||||||
|
if (prop.kind === 'JsxAttribute' && prop.name === 'ref') {
|
||||||
|
yield equation(prop.place.identifier.type, {
|
||||||
|
kind: 'Object',
|
||||||
|
shapeId: BuiltInUseRefId,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
yield equation(left, {kind: 'Object', shapeId: BuiltInJsxId});
|
yield equation(left, {kind: 'Object', shapeId: BuiltInJsxId});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -407,15 +407,28 @@ function validateNoRefAccessInRenderImpl(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* If we already reported an error on this instruction, don't report
|
||||||
|
* duplicate errors
|
||||||
|
*/
|
||||||
if (!didError) {
|
if (!didError) {
|
||||||
/*
|
const isRefLValue = isUseRefType(instr.lvalue.identifier);
|
||||||
* If we already reported an error on this instruction, don't report
|
|
||||||
* duplicate errors
|
|
||||||
*/
|
|
||||||
for (const operand of eachInstructionValueOperand(instr.value)) {
|
for (const operand of eachInstructionValueOperand(instr.value)) {
|
||||||
if (hookKind != null) {
|
if (hookKind != null) {
|
||||||
validateNoDirectRefValueAccess(errors, operand, env);
|
validateNoDirectRefValueAccess(errors, operand, env);
|
||||||
} else {
|
} else if (!isRefLValue) {
|
||||||
|
/**
|
||||||
|
* In general passing a ref to a function may access that ref
|
||||||
|
* value during render, so we disallow it.
|
||||||
|
*
|
||||||
|
* The main exception is the "mergeRefs" pattern, ie a function
|
||||||
|
* that accepts multiple refs as arguments (or an array of refs)
|
||||||
|
* and returns a new, aggregated ref. If the lvalue is a ref,
|
||||||
|
* we assume that the user is doing this pattern and allow passing
|
||||||
|
* refs.
|
||||||
|
*
|
||||||
|
* Eg `const mergedRef = mergeRefs(ref1, ref2)`
|
||||||
|
*/
|
||||||
validateNoRefPassedToFunction(
|
validateNoRefPassedToFunction(
|
||||||
errors,
|
errors,
|
||||||
env,
|
env,
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
## Input
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
// @enableTreatRefLikeIdentifiersAsRefs @validateRefAccessDuringRender
|
||||||
|
|
||||||
|
import {useRef} from 'react';
|
||||||
|
|
||||||
|
function Component() {
|
||||||
|
const ref = useRef(null);
|
||||||
|
const ref2 = useRef(null);
|
||||||
|
const mergedRef = mergeRefs([ref], ref2);
|
||||||
|
|
||||||
|
return <Stringify ref={mergedRef} />;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Code
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
import { c as _c } from "react/compiler-runtime"; // @enableTreatRefLikeIdentifiersAsRefs @validateRefAccessDuringRender
|
||||||
|
|
||||||
|
import { useRef } from "react";
|
||||||
|
|
||||||
|
function Component() {
|
||||||
|
const $ = _c(1);
|
||||||
|
const ref = useRef(null);
|
||||||
|
const ref2 = useRef(null);
|
||||||
|
const mergedRef = mergeRefs([ref], ref2);
|
||||||
|
let t0;
|
||||||
|
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||||
|
t0 = <Stringify ref={mergedRef} />;
|
||||||
|
$[0] = t0;
|
||||||
|
} else {
|
||||||
|
t0 = $[0];
|
||||||
|
}
|
||||||
|
return t0;
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### Eval output
|
||||||
|
(kind: exception) Fixture not implemented
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
// @enableTreatRefLikeIdentifiersAsRefs @validateRefAccessDuringRender
|
||||||
|
|
||||||
|
import {useRef} from 'react';
|
||||||
|
|
||||||
|
function Component() {
|
||||||
|
const ref = useRef(null);
|
||||||
|
const ref2 = useRef(null);
|
||||||
|
const mergedRef = mergeRefs([ref], ref2);
|
||||||
|
|
||||||
|
return <Stringify ref={mergedRef} />;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user