@samitouri / QOS-React / commits / f75cf6fe38

Fix fbt (again)

Joe Savona committed May 4, 2023 at 15:19 UTC f75cf6fe386ef46cfa6b3fd100a287db31b05a98
3 files changed +86 -8
compiler/forget/src/ReactiveScopes/MemoizeFbtOperandsInSameScope.ts
+8 -8
@@ -65,20 +65,20 @@ class Transform extends ReactiveFunctionVisitor<void> {
65 (value.kind === "CallExpression" &&
66 this.fbtValues.has(value.callee.identifier.id))
67 ) {
68 + const fbtScope = lvalue.identifier.scope;
69 + if (fbtScope === null) {
70 + return;
71 + }
72 +
73 // if the JSX element's tag was `fbt`, mark all its operands
74 // to ensure that they end up in the same scope as the jsx element
75 // itself.
76 for (const operand of eachReactiveValueOperand(value)) {
72 - operand.identifier.scope = lvalue.identifier.scope;
73 - operand.identifier.mutableRange.end =
74 - lvalue.identifier.mutableRange.end;
77 + operand.identifier.scope = fbtScope;
78
79 // Expand the jsx element's range to account for its operands
77 - lvalue.identifier.mutableRange.start = makeInstructionId(
78 - Math.min(
79 - lvalue.identifier.mutableRange.start,
80 - operand.identifier.mutableRange.start
81 - )
80 + fbtScope.range.start = makeInstructionId(
81 + Math.min(fbtScope.range.start, operand.identifier.mutableRange.start)
82 );
83 }
84 }
compiler/forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.expect.md new
+61
@@ -0,0 +1,61 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import fbt from "fbt";
6 +
7 +export function Component(props) {
8 + let count = 0;
9 + if (props.items) {
10 + count = props.items.length;
11 + }
12 + return (
13 + <View>
14 + {fbt(
15 + `for ${fbt.param("count", count)} experiences`,
16 + `Label for the number of items`,
17 + { project: "public" }
18 + )}
19 + </View>
20 + );
21 +}
22 +
23 +```
24 +
25 +## Code
26 +
27 +```javascript
28 +import { unstable_useMemoCache as useMemoCache } from "react";
29 +import fbt from "fbt";
30 +
31 +export function Component(props) {
32 + const $ = useMemoCache(4);
33 + let count = 0;
34 + if (props.items) {
35 + count = props.items.length;
36 + }
37 + const c_0 = $[0] !== count;
38 + let t0;
39 + if (c_0) {
40 + t0 = fbt._("for {count} experiences", [fbt._param("count", count)], {
41 + hk: "nmYpm",
42 + });
43 + $[0] = count;
44 + $[1] = t0;
45 + } else {
46 + t0 = $[1];
47 + }
48 + const c_2 = $[2] !== t0;
49 + let t1;
50 + if (c_2) {
51 + t1 = <View>{t0}</View>;
52 + $[2] = t0;
53 + $[3] = t1;
54 + } else {
55 + t1 = $[3];
56 + }
57 + return t1;
58 +}
59 +
60 +```
61 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/fbt-template-string-same-scope.js new
+17
@@ -0,0 +1,17 @@
1 +import fbt from "fbt";
2 +
3 +export function Component(props) {
4 + let count = 0;
5 + if (props.items) {
6 + count = props.items.length;
7 + }
8 + return (
9 + <View>
10 + {fbt(
11 + `for ${fbt.param("count", count)} experiences`,
12 + `Label for the number of items`,
13 + { project: "public" }
14 + )}
15 + </View>
16 + );
17 +}