Skip to content

Commit db1a65a

Browse files
committed
[compiler] Bail out on local variables named 'fbt'
[ghstack-poisoned]
1 parent f9259fb commit db1a65a

File tree

7 files changed

+76
-144
lines changed

7 files changed

+76
-144
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/BuildHIR.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,9 +2133,10 @@ function lowerExpression(
21332133
const tagIdentifier = openingIdentifier.isJSXIdentifier()
21342134
? builder.resolveIdentifier(openingIdentifier)
21352135
: null;
2136-
if (tagIdentifier != null && tagIdentifier.kind === 'Identifier') {
2137-
CompilerError.throwTodo({
2138-
reason: `Support <${tagName}> tags where '${tagName}' is a local variable instead of a global`,
2136+
if (tagIdentifier != null) {
2137+
// This is already checked in builder.resolveIdentifier
2138+
CompilerError.invariant(tagIdentifier.kind !== 'Identifier', {
2139+
reason: `<${tagName}> tags should be module-level imports`,
21392140
loc: openingIdentifier.node.loc ?? GeneratedSource,
21402141
description: null,
21412142
suggestions: null,

compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,12 @@ export default class HIRBuilder {
311311
}
312312

313313
resolveBinding(node: t.Identifier): Identifier {
314+
if (node.name === 'fbt') {
315+
CompilerError.throwTodo({
316+
reason: 'Support local variables named "fbt"',
317+
loc: node.loc ?? null,
318+
});
319+
}
314320
const originalName = node.name;
315321
let name = originalName;
316322
let index = 0;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
## Input
3+
4+
```javascript
5+
import fbt from 'fbt';
6+
import {identity} from 'shared-runtime';
7+
8+
/**
9+
* Note that the fbt transform looks for callsites with a `fbt`-named callee.
10+
* This is incompatible with react-compiler as we rename local variables in
11+
* HIRBuilder + RenameVariables.
12+
*
13+
* See evaluator error:
14+
* Found differences in evaluator results
15+
* Non-forget (expected):
16+
* (kind: ok) <div>Hello, Sathya!Goodbye, Sathya!</div>
17+
* Forget:
18+
* (kind: exception) fbt$0.param is not a function
19+
*/
20+
21+
function Foo(props) {
22+
const getText1 = fbt =>
23+
fbt(
24+
`Hello, ${fbt.param('(key) name', identity(props.name))}!`,
25+
'(description) Greeting'
26+
);
27+
28+
const getText2 = fbt =>
29+
fbt(
30+
`Goodbye, ${fbt.param('(key) name', identity(props.name))}!`,
31+
'(description) Greeting2'
32+
);
33+
34+
return (
35+
<div>
36+
{getText1(fbt)}
37+
{getText2(fbt)}
38+
</div>
39+
);
40+
}
41+
42+
export const FIXTURE_ENTRYPOINT = {
43+
fn: Foo,
44+
params: [{name: 'Sathya'}],
45+
};
46+
47+
```
48+
49+
50+
## Error
51+
52+
```
53+
16 |
54+
17 | function Foo(props) {
55+
> 18 | const getText1 = fbt =>
56+
| ^^^ Todo: Support local variables named "fbt" (18:18)
57+
19 | fbt(
58+
20 | `Hello, ${fbt.param('(key) name', identity(props.name))}!`,
59+
21 | '(description) Greeting'
60+
```
61+
62+

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/error.todo-locally-require-fbt.expect.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ function Component(props) {
1414
## Error
1515

1616
```
17-
2 | const fbt = require('fbt');
17+
1 | function Component(props) {
18+
> 2 | const fbt = require('fbt');
19+
| ^^^ Todo: Support local variables named "fbt" (2:2)
1820
3 |
19-
> 4 | return <fbt desc="Description">{'Text'}</fbt>;
20-
| ^^^ Todo: Support <fbt> tags where 'fbt' is a local variable instead of a global (4:4)
21+
4 | return <fbt desc="Description">{'Text'}</fbt>;
2122
5 | }
22-
6 |
2323
```
2424
2525

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/fbt/todo-fbt-as-local.expect.md

Lines changed: 0 additions & 136 deletions
This file was deleted.

compiler/packages/snap/src/SproutTodoFilter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ const skipFilter = new Set([
484484
'rules-of-hooks/rules-of-hooks-69521d94fa03',
485485

486486
// bugs
487-
'fbt/todo-fbt-as-local',
488487
'bug-invalid-hoisting-functionexpr',
489488
'original-reactive-scopes-fork/bug-nonmutating-capture-in-unsplittable-memo-block',
490489
'original-reactive-scopes-fork/bug-hoisted-declaration-with-scope',

0 commit comments

Comments
 (0)