Fix fbt (again)

This commit is contained in:
Joe Savona
2023-05-04 15:19:41 -07:00
parent 407e7b0c62
commit f75cf6fe38
3 changed files with 86 additions and 8 deletions
@@ -65,20 +65,20 @@ class Transform extends ReactiveFunctionVisitor<void> {
(value.kind === "CallExpression" &&
this.fbtValues.has(value.callee.identifier.id))
) {
const fbtScope = lvalue.identifier.scope;
if (fbtScope === null) {
return;
}
// 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 eachReactiveValueOperand(value)) {
operand.identifier.scope = lvalue.identifier.scope;
operand.identifier.mutableRange.end =
lvalue.identifier.mutableRange.end;
operand.identifier.scope = fbtScope;
// 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
)
fbtScope.range.start = makeInstructionId(
Math.min(fbtScope.range.start, operand.identifier.mutableRange.start)
);
}
}
@@ -0,0 +1,61 @@
## Input
```javascript
import fbt from "fbt";
export function Component(props) {
let count = 0;
if (props.items) {
count = props.items.length;
}
return (
<View>
{fbt(
`for ${fbt.param("count", count)} experiences`,
`Label for the number of items`,
{ project: "public" }
)}
</View>
);
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import fbt from "fbt";
export function Component(props) {
const $ = useMemoCache(4);
let count = 0;
if (props.items) {
count = props.items.length;
}
const c_0 = $[0] !== count;
let t0;
if (c_0) {
t0 = fbt._("for {count} experiences", [fbt._param("count", count)], {
hk: "nmYpm",
});
$[0] = count;
$[1] = t0;
} else {
t0 = $[1];
}
const c_2 = $[2] !== t0;
let t1;
if (c_2) {
t1 = <View>{t0}</View>;
$[2] = t0;
$[3] = t1;
} else {
t1 = $[3];
}
return t1;
}
```
@@ -0,0 +1,17 @@
import fbt from "fbt";
export function Component(props) {
let count = 0;
if (props.items) {
count = props.items.length;
}
return (
<View>
{fbt(
`for ${fbt.param("count", count)} experiences`,
`Label for the number of items`,
{ project: "public" }
)}
</View>
);
}