mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
Ensure <fbt> children are not independently memod
This is a Meta-ism, but adding it for now to unblock. We special-case the `<fbt>` element for translation purposes, and have a transform that requires the children of this element to be a limited subset of nodes. Notably, any dynamic translation values must appear as `<fbt:param>` children — we disallow identifiers as children of `<fbt>` nodes. This PR adds a new pass which finds `<fbt>` nodes and ensures their immediate operands are not independently memoized. Note that this still allows the values of `<fbt:param>` to be independently memoized, as demonstrated in the unit test.
This commit is contained in:
1 parent
8dfe06ddba
commit
4e2ef92779
9 files changed
+208
-21
No files matched your search
@@ -12,7 +12,7 @@ import {
|
||||
mergeConsecutiveBlocks,
|
||||
ReactiveFunction,
|
||||
} from "./HIR";
|
||||
import { EnvironmentConfig, Environment } from "./HIR/Environment";
|
||||
import { Environment, EnvironmentConfig } from "./HIR/Environment";
|
||||
import { validateConsistentIdentifiers } from "./HIR/ValidateConsistentIdentifiers";
|
||||
import {
|
||||
analyseFunctions,
|
||||
@@ -29,6 +29,7 @@ import {
|
||||
flattenReactiveLoops,
|
||||
flattenScopesWithHooks,
|
||||
inferReactiveScopeVariables,
|
||||
memoizeFbtOperandsInSameScope,
|
||||
mergeOverlappingReactiveScopes,
|
||||
promoteUsedTemporaries,
|
||||
propagateScopeDependencies,
|
||||
@@ -104,6 +105,13 @@ export function* run(
|
||||
value: reactiveFunction,
|
||||
});
|
||||
|
||||
memoizeFbtOperandsInSameScope(reactiveFunction);
|
||||
yield log({
|
||||
kind: "reactive",
|
||||
name: "MemoizeFbtOperandsInSameScope",
|
||||
value: reactiveFunction,
|
||||
});
|
||||
|
||||
alignReactiveScopesToBlockScopes(reactiveFunction);
|
||||
yield log({
|
||||
kind: "reactive",
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {
|
||||
IdentifierId,
|
||||
makeInstructionId,
|
||||
ReactiveFunction,
|
||||
ReactiveInstruction,
|
||||
} from "../HIR";
|
||||
import { eachInstructionValueOperand } from "../HIR/visitors";
|
||||
import { ReactiveFunctionVisitor, visitReactiveFunction } from "./visitors";
|
||||
|
||||
/**
|
||||
* This is a Meta-ism. We special-case the `<fbt>` element for translation purposes,
|
||||
* and have a transform that requires the children of this element to be a limited
|
||||
* subset of nodes. Notably, any dynamic translation values must appear as
|
||||
* `<fbt:param>` children — we disallow identifiers as children of `<fbt>` nodes.
|
||||
*
|
||||
* This PR adds a new pass which finds `<fbt>` nodes and ensures their immediate
|
||||
* operands are not independently memoized. Note that this still allows the values
|
||||
* of `<fbt:param>` to be independently memoized
|
||||
*/
|
||||
export function memoizeFbtOperandsInSameScope(fn: ReactiveFunction): void {
|
||||
visitReactiveFunction(fn, new Transform(), undefined);
|
||||
}
|
||||
|
||||
class Transform extends ReactiveFunctionVisitor<void> {
|
||||
fbtTags: Set<IdentifierId> = new Set();
|
||||
|
||||
override visitInstruction(
|
||||
instruction: ReactiveInstruction,
|
||||
_state: void
|
||||
): void {
|
||||
const { lvalue, value } = instruction;
|
||||
if (lvalue === null) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
value.kind === "Primitive" &&
|
||||
typeof value.value === "string" &&
|
||||
value.value === "fbt"
|
||||
) {
|
||||
// We don't distinguish between tag names and strings, so record
|
||||
// all `fbt` string literals in case they are used as a jsx tag.
|
||||
this.fbtTags.add(lvalue.identifier.id);
|
||||
} else if (
|
||||
value.kind === "JsxExpression" &&
|
||||
this.fbtTags.has(value.tag.identifier.id)
|
||||
) {
|
||||
// if the JSX element's tag was `fbt`, mark all its operands
|
||||
// to ensure that they end up in the same scope as the jsx element
|
||||
// itself.
|
||||
for (const operand of eachInstructionValueOperand(value)) {
|
||||
operand.identifier.scope = lvalue.identifier.scope;
|
||||
operand.identifier.mutableRange.end =
|
||||
lvalue.identifier.mutableRange.end;
|
||||
|
||||
// Expand the jsx element's range to account for its operands
|
||||
lvalue.identifier.mutableRange.start = makeInstructionId(
|
||||
Math.min(
|
||||
lvalue.identifier.mutableRange.start,
|
||||
operand.identifier.mutableRange.start
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ export { codegenReactiveFunction } from "./CodegenReactiveFunction";
|
||||
export { flattenReactiveLoops } from "./FlattenReactiveLoops";
|
||||
export { flattenScopesWithHooks } from "./FlattenScopesWithHooks";
|
||||
export { inferReactiveScopeVariables } from "./InferReactiveScopeVariables";
|
||||
export { memoizeFbtOperandsInSameScope } from "./MemoizeFbtOperandsInSameScope";
|
||||
export { mergeOverlappingReactiveScopes } from "./MergeOverlappingReactiveScopes";
|
||||
export { printReactiveFunction } from "./PrintReactiveFunction";
|
||||
export { promoteUsedTemporaries } from "./PromoteUsedTemporaries";
|
||||
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
return (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello <fbt:param name="user name">{capitalize(props.name)}</fbt:param>
|
||||
</fbt>
|
||||
);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
const $ = React.unstable_useMemoCache(4);
|
||||
const c_0 = $[0] !== props.name;
|
||||
let t1;
|
||||
if (c_0) {
|
||||
const c_2 = $[2] !== props.name;
|
||||
let t0;
|
||||
if (c_2) {
|
||||
t0 = capitalize(props.name);
|
||||
$[2] = props.name;
|
||||
$[3] = t0;
|
||||
} else {
|
||||
t0 = $[3];
|
||||
}
|
||||
t1 = (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello {<fbt:param name={"user name"}>{t0}</fbt:param>}
|
||||
</fbt>
|
||||
);
|
||||
$[0] = props.name;
|
||||
$[1] = t1;
|
||||
} else {
|
||||
t1 = $[1];
|
||||
}
|
||||
return t1;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
function Component(props) {
|
||||
return (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello <fbt:param name="user name">{capitalize(props.name)}</fbt:param>
|
||||
</fbt>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
return (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello <fbt:param name="user name">{props.name}</fbt:param>
|
||||
</fbt>
|
||||
);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
function Component(props) {
|
||||
const $ = React.unstable_useMemoCache(2);
|
||||
const c_0 = $[0] !== props.name;
|
||||
let t0;
|
||||
if (c_0) {
|
||||
t0 = (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello {<fbt:param name={"user name"}>{props.name}</fbt:param>}
|
||||
</fbt>
|
||||
);
|
||||
$[0] = props.name;
|
||||
$[1] = t0;
|
||||
} else {
|
||||
t0 = $[1];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
function Component(props) {
|
||||
return (
|
||||
<fbt desc={"Dialog to show to user"}>
|
||||
Hello <fbt:param name="user name">{props.name}</fbt:param>
|
||||
</fbt>
|
||||
);
|
||||
}
|
||||
@@ -18,5 +18,7 @@ declare global {
|
||||
let __DEV__: boolean | null | undefined;
|
||||
}
|
||||
|
||||
console.log("loading Forget!!!");
|
||||
|
||||
import ReactForgetBabelPlugin from "./Babel/BabelPlugin";
|
||||
export default ReactForgetBabelPlugin;
|
||||
Reference in new issue
Block a user