Skip to content

Commit

Permalink
fix(transformer): fix incorrect jsx whitespace text handling (#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Apr 14, 2024
1 parent 5b02ae1 commit 35e3b0f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
16 changes: 7 additions & 9 deletions crates/oxc_transformer/src/react/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,20 +646,18 @@ impl<'a> ReactJsx<'a> {
let mut acc: Option<String> = None;
let mut first_non_whitespace: Option<usize> = Some(0);
let mut last_non_whitespace: Option<usize> = None;
let mut i: usize = 0;
for c in text.chars() {
for (index, c) in text.char_indices() {
if is_line_terminator(c) {
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;
} 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() {
first_non_whitespace.replace(i);
first_non_whitespace.replace(index);
}
}
i += c.len_utf8();
}
if let Some(first) = first_non_whitespace {
Some(Self::add_line_of_jsx_text(acc, &text[first..]))
Expand All @@ -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>
fn decode_entities(s: &str) -> String {
let mut buffer = vec![];
let mut chars = s.bytes().enumerate();
let mut chars = s.char_indices();
let mut prev = 0;
while let Some((i, c)) = chars.next() {
if c == b'&' {
if c == '&' {
let start = i;
let mut end = None;
for (j, c) in chars.by_ref() {
if c == b';' {
if c == ';' {
end.replace(j);
break;
}
Expand Down
4 changes: 2 additions & 2 deletions tasks/transform_conformance/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Passed: 0/0
Passed: 1/1

# All Passed:

* babel-plugin-transform-react-jsx


Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["transform-react-jsx"]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>
🏝️
</h2>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {jsx as _jsx} from 'react/jsx-runtime';
_jsx('h2', {
children: '🏝️'
});

0 comments on commit 35e3b0f

Please sign in to comment.