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:codegen] Wrap non-ascii characters in JsxExpressionContainer #29997

Merged
merged 6 commits into from
Jun 21, 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
8 changes: 7 additions & 1 deletion compiler/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ module.exports = {

"multiline-comment-style": ["error", "starred-block"],

/**
* We sometimes need to check for control characters in regexes for things like preserving input
* strings
*/
"no-control-regex": "off",

"@typescript-eslint/no-empty-function": "off",

/*
Expand Down Expand Up @@ -82,7 +88,7 @@ module.exports = {
],
"@typescript-eslint/array-type": ["error", { default: "generic" }],
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-var-requires": "off"
"@typescript-eslint/no-var-requires": "off",
},
parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2157,10 +2157,18 @@ function codegenInstructionValue(
}

/**
* Due to a bug in earlier Babel versions, JSX string attributes with double quotes or with unicode characters
* may be escaped unnecessarily. To avoid trigger this Babel bug, we use a JsxExpressionContainer for such strings.
* Due to a bug in earlier Babel versions, JSX string attributes with double quotes, unicode characters, or special
* control characters such as \n may be escaped unnecessarily. To avoid trigger this Babel bug, we use a
* JsxExpressionContainer for such strings.
*
* u0000 to u001F: C0 control codes
* u007F : Delete character
* u0080 to u009F: C1 control codes
* 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 = /[\u{0080}-\u{FFFF}]|"/u;
const STRING_REQUIRES_EXPR_CONTAINER_PATTERN =
/[\u{0000}-\u{001F}|\u{007F}|\u{0080}-\u{FFFF}]|"/u;
function codegenJsxAttribute(
cx: Context,
attribute: JsxAttribute
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

## Input

```javascript
function Component() {
return (
<div>
<Text value={"\n"} />
<Text value={"A\tE"} />
<Text value={"나은"} />
<Text value={"Lauren"} />
<Text value={"சத்யா"} />
<Text value={"Sathya"} />
</div>
);
}

function Text({ value }) {
return <span>{value}</span>;
}

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

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = (
<div>
<Text value={"\n"} />
<Text value={"A\tE"} />
<Text value={"\uB098\uC740"} />
<Text value="Lauren" />
<Text value={"\u0B9A\u0BA4\u0BCD\u0BAF\u0BBE"} />
<Text value="Sathya" />
</div>
);
$[0] = t0;
} else {
t0 = $[0];
}
return t0;
}

function Text(t0) {
const $ = _c(2);
const { value } = t0;
let t1;
if ($[0] !== value) {
t1 = <span>{value}</span>;
$[0] = value;
$[1] = t1;
} else {
t1 = $[1];
}
return t1;
}

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

```
### Eval output
(kind: ok) <div><span>
</span><span>A E</span><span>나은</span><span>Lauren</span><span>சத்யா</span><span>Sathya</span></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Component() {
return (
<div>
<Text value={"\n"} />
<Text value={"A\tE"} />
<Text value={"나은"} />
<Text value={"Lauren"} />
<Text value={"சத்யா"} />
<Text value={"Sathya"} />
</div>
);
}

function Text({ value }) {
return <span>{value}</span>;
}

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