fix(transformer): fix incorrect jsx whitespace text handling (#2969)

This commit is contained in:
Boshen 2024-04-14 18:40:40 +08:00 committed by GitHub
parent 5b02ae1175
commit 35e3b0f1cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 19 additions and 11 deletions

View file

@ -646,20 +646,18 @@ impl<'a> ReactJsx<'a> {
let mut acc: Option<String> = None; let mut acc: Option<String> = None;
let mut first_non_whitespace: Option<usize> = Some(0); let mut first_non_whitespace: Option<usize> = Some(0);
let mut last_non_whitespace: Option<usize> = None; let mut last_non_whitespace: Option<usize> = None;
let mut i: usize = 0; for (index, c) in text.char_indices() {
for c in text.chars() {
if is_line_terminator(c) { if is_line_terminator(c) {
if let (Some(first), Some(last)) = (first_non_whitespace, last_non_whitespace) { if let (Some(first), Some(last)) = (first_non_whitespace, last_non_whitespace) {
acc = Some(Self::add_line_of_jsx_text(acc, &text[first..=last])); acc = Some(Self::add_line_of_jsx_text(acc, &text[first..last]));
} }
first_non_whitespace = None; first_non_whitespace = None;
} else if c != ' ' && !is_irregular_whitespace(c) { } else if c != ' ' && !is_irregular_whitespace(c) {
last_non_whitespace = Some(i); last_non_whitespace = Some(index + c.len_utf8());
if first_non_whitespace.is_none() { if first_non_whitespace.is_none() {
first_non_whitespace.replace(i); first_non_whitespace.replace(index);
} }
} }
i += c.len_utf8();
} }
if let Some(first) = first_non_whitespace { if let Some(first) = first_non_whitespace {
Some(Self::add_line_of_jsx_text(acc, &text[first..])) Some(Self::add_line_of_jsx_text(acc, &text[first..]))
@ -682,14 +680,14 @@ impl<'a> ReactJsx<'a> {
/// Code adapted from <https://github.com/microsoft/TypeScript/blob/514f7e639a2a8466c075c766ee9857a30ed4e196/src/compiler/transformers/jsx.ts#L617C1-L635> /// Code adapted from <https://github.com/microsoft/TypeScript/blob/514f7e639a2a8466c075c766ee9857a30ed4e196/src/compiler/transformers/jsx.ts#L617C1-L635>
fn decode_entities(s: &str) -> String { fn decode_entities(s: &str) -> String {
let mut buffer = vec![]; let mut buffer = vec![];
let mut chars = s.bytes().enumerate(); let mut chars = s.char_indices();
let mut prev = 0; let mut prev = 0;
while let Some((i, c)) = chars.next() { while let Some((i, c)) = chars.next() {
if c == b'&' { if c == '&' {
let start = i; let start = i;
let mut end = None; let mut end = None;
for (j, c) in chars.by_ref() { for (j, c) in chars.by_ref() {
if c == b';' { if c == ';' {
end.replace(j); end.replace(j);
break; break;
} }

View file

@ -1,6 +1,6 @@
Passed: 0/0 Passed: 1/1
# All Passed: # All Passed:
* babel-plugin-transform-react-jsx

View file

@ -0,0 +1,3 @@
{
"plugins": [["transform-react-jsx"]]
}

View file

@ -0,0 +1,3 @@
<h2>
🏝
</h2>

View file

@ -0,0 +1,4 @@
import {jsx as _jsx} from 'react/jsx-runtime';
_jsx('h2', {
children: '🏝️'
});