Skip to content
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

[compiler][patch] Throw todo when encountering fbt param with unicode or double quote #30384

Closed
wants to merge 1 commit into from
Closed
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 @@ -2205,7 +2205,7 @@ function codegenInstructionValue(
* u00A0 to uFFFF: All non-basic Latin characters
* https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
*/
const STRING_REQUIRES_EXPR_CONTAINER_PATTERN =
export const STRING_REQUIRES_EXPR_CONTAINER_PATTERN =
/[\u{0000}-\u{001F}\u{007F}\u{0080}-\u{FFFF}]|"/u;
function codegenJsxAttribute(
cx: Context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
* LICENSE file in the root directory of this source tree.
*/

import { CompilerError } from "..";
import {
HIRFunction,
IdentifierId,
makeInstructionId,
Place,
ReactiveValue,
} from "../HIR";
import { STRING_REQUIRES_EXPR_CONTAINER_PATTERN } from "./CodegenReactiveFunction";
import { eachReactiveValueOperand } from "./visitors";

/**
Expand Down Expand Up @@ -52,6 +54,25 @@ export function memoizeFbtAndMacroOperandsInSameScope(fn: HIRFunction): void {
break;
}
}

/**
* Hack in a check to ensure that we don't produce invalid ASTs in codegen
* (see https://github.com/facebook/react/pull/29997 and `fbt-param-with-quotes` fixture)
*/
for (const [, block] of fn.body.blocks) {
for (const { lvalue, value } of block.instructions) {
if (fbtValues.has(lvalue.identifier.id) && value.kind === "Primitive") {
if (typeof value.value === "string") {
if (STRING_REQUIRES_EXPR_CONTAINER_PATTERN.test(value.value)) {
CompilerError.throwTodo({
reason: "Handle non-ascii character in fbt-like macro operand",
loc: value.loc,
});
}
}
}
}
}
}

export const FBT_TAGS: Set<string> = new Set([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

## Input

```javascript
import fbt from "fbt";

function Component(props) {
const element = (
<fbt desc={"Dialog to show to user"}>
Hello <fbt:param name='"user" name'>{props.name}</fbt:param>
</fbt>
);
return element.toString();
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ name: "Jason" }],
};

```


## Error

```
4 | const element = (
5 | <fbt desc={"Dialog to show to user"}>
> 6 | Hello <fbt:param name='"user" name'>{props.name}</fbt:param>
| ^^^^^^^^^^^^^ Todo: Handle non-ascii character in fbt-like macro operand (6:6)
7 | </fbt>
8 | );
9 | return element.toString();
```


Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import fbt from "fbt";

function Component(props) {
const element = (
<fbt desc={"Dialog to show to user"}>
Hello <fbt:param name='"user" name'>{props.name}</fbt:param>
</fbt>
);
return element.toString();
}

export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ name: "Jason" }],
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ export const FIXTURE_ENTRYPOINT = {
## Error

```
Property arguments[0] of CallExpression expected node to be of a type ["Expression","SpreadElement","JSXNamespacedName","ArgumentPlaceholder"] but instead got "JSXExpressionContainer"
4 | const element = (
5 | <fbt desc={"Dialog to show to user"}>
> 6 | Hello <fbt:param name="user name ☺">{props.name}</fbt:param>
| ^^^^^^^^^^^^^ Todo: Handle non-ascii character in fbt-like macro operand (6:6)
7 | </fbt>
8 | );
9 | return element.toString();
```


Loading