Skip to content

[compiler] Bail out on local variables named 'fbt' #30524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2133,9 +2133,10 @@ function lowerExpression(
const tagIdentifier = openingIdentifier.isJSXIdentifier()
? builder.resolveIdentifier(openingIdentifier)
: null;
if (tagIdentifier != null && tagIdentifier.kind === 'Identifier') {
CompilerError.throwTodo({
reason: `Support <${tagName}> tags where '${tagName}' is a local variable instead of a global`,
if (tagIdentifier != null) {
// This is already checked in builder.resolveIdentifier
CompilerError.invariant(tagIdentifier.kind !== 'Identifier', {
reason: `<${tagName}> tags should be module-level imports`,
loc: openingIdentifier.node.loc ?? GeneratedSource,
description: null,
suggestions: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,12 @@ export default class HIRBuilder {
}

resolveBinding(node: t.Identifier): Identifier {
if (node.name === 'fbt') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also bail out when fbt is not directly imported, but that's not necessary as we only rename locals within functions the compiler processes

CompilerError.throwTodo({
reason: 'Support local variables named "fbt"',
loc: node.loc ?? null,
});
}
const originalName = node.name;
let name = originalName;
let index = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

## Input

```javascript
import fbt from 'fbt';
import {identity} from 'shared-runtime';

/**
* Note that the fbt transform looks for callsites with a `fbt`-named callee.
* This is incompatible with react-compiler as we rename local variables in
* HIRBuilder + RenameVariables.
*
* See evaluator error:
* Found differences in evaluator results
* Non-forget (expected):
* (kind: ok) <div>Hello, Sathya!Goodbye, Sathya!</div>
* Forget:
* (kind: exception) fbt$0.param is not a function
*/

function Foo(props) {
const getText1 = fbt =>
fbt(
`Hello, ${fbt.param('(key) name', identity(props.name))}!`,
'(description) Greeting'
);

const getText2 = fbt =>
fbt(
`Goodbye, ${fbt.param('(key) name', identity(props.name))}!`,
'(description) Greeting2'
);

return (
<div>
{getText1(fbt)}
{getText2(fbt)}
</div>
);
}

export const FIXTURE_ENTRYPOINT = {
fn: Foo,
params: [{name: 'Sathya'}],
};

```


## Error

```
16 |
17 | function Foo(props) {
> 18 | const getText1 = fbt =>
| ^^^ Todo: Support local variables named "fbt" (18:18)
19 | fbt(
20 | `Hello, ${fbt.param('(key) name', identity(props.name))}!`,
21 | '(description) Greeting'
```


Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ function Component(props) {
## Error

```
2 | const fbt = require('fbt');
1 | function Component(props) {
> 2 | const fbt = require('fbt');
| ^^^ Todo: Support local variables named "fbt" (2:2)
3 |
> 4 | return <fbt desc="Description">{'Text'}</fbt>;
| ^^^ Todo: Support <fbt> tags where 'fbt' is a local variable instead of a global (4:4)
4 | return <fbt desc="Description">{'Text'}</fbt>;
5 | }
6 |
```


This file was deleted.

1 change: 0 additions & 1 deletion compiler/packages/snap/src/SproutTodoFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ const skipFilter = new Set([
'rules-of-hooks/rules-of-hooks-69521d94fa03',

// bugs
'fbt/todo-fbt-as-local',
'bug-invalid-hoisting-functionexpr',
'original-reactive-scopes-fork/bug-nonmutating-capture-in-unsplittable-memo-block',
'original-reactive-scopes-fork/bug-hoisted-declaration-with-scope',
Expand Down