Skip to content

Commit 349797b

Browse files
committed
[compiler:codegen] Wrap non-ascii characters in JsxExpressionContainer
This PR extends the previous logic added in #29141 to also account for other kinds of non-ascii characters such as `\n`. Because these control characters are individual special characters (and not 2 characters `\` and `n`) we match based on unicode which was already being checked for non-Latin characters. This allows control characters to continue to be compiled equivalently to its original source if it was provided in a JsxExpressionContainer. However note that this PR does not convert JSX attributes that are StringLiterals to JsxExpressionContainer, to preserve the original source code as it was written. Alternatively we could always emit a JsxExpressionContainer if it was used in the source and not try to down level it to some other node kind. But since we already do this I opted to keep this behavior. Partially addresses #29648. ghstack-source-id: 42a80bb Pull Request resolved: #29997
1 parent 8cf6cb7 commit 349797b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,10 +2157,18 @@ function codegenInstructionValue(
21572157
}
21582158

21592159
/**
2160-
* Due to a bug in earlier Babel versions, JSX string attributes with double quotes or with unicode characters
2161-
* may be escaped unnecessarily. To avoid trigger this Babel bug, we use a JsxExpressionContainer for such strings.
2160+
* Due to a bug in earlier Babel versions, JSX string attributes with double quotes, unicode characters, or special
2161+
* control characters such as \n may be escaped unnecessarily. To avoid trigger this Babel bug, we use a
2162+
* JsxExpressionContainer for such strings.
2163+
*
2164+
* u0000 to u001F: C0 control codes
2165+
* u007F: Delete character
2166+
* u0080 to u009F: C1 control codes
2167+
* u00A0 to uFFFF: All non-basic Latin characters
2168+
* https://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes
21622169
*/
2163-
const STRING_REQUIRES_EXPR_CONTAINER_PATTERN = /[\u{0080}-\u{FFFF}]|"/u;
2170+
const STRING_REQUIRES_EXPR_CONTAINER_PATTERN =
2171+
/[\u{0000}-\u{001F}|\u{007F}|\u{0080}-\u{FFFF}]|"/u;
21642172
function codegenJsxAttribute(
21652173
cx: Context,
21662174
attribute: JsxAttribute
1.14 KB
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function Component() {
2+
return (
3+
<div>
4+
<Text value={"\u0000"} />
5+
<Text value={"A\tE"} />
6+
<Text value={"나은"} />
7+
<Text value={"Sathya"} />
8+
</div>
9+
);
10+
}
11+
12+
function Text({ value }) {
13+
return <span>{value}</span>;
14+
}
15+
16+
export const FIXTURE_ENTRYPOINT = {
17+
fn: Component,
18+
params: [{}],
19+
};

0 commit comments

Comments
 (0)