Skip to content

Commit

Permalink
Merge pull request #900 from preactjs/fix/jsx-whitespace
Browse files Browse the repository at this point in the history
Fix/jsx whitespace
  • Loading branch information
developit committed Jan 19, 2022
2 parents 1a1b871 + 69b0496 commit 2f7c74a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-comics-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Ensures leading newline in text nodes is not converted to whitespace
8 changes: 6 additions & 2 deletions packages/wmr/src/lib/acorn-traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,8 +890,12 @@ const TYPES = {
// We can drop the whole matched string if it only contains
// whitespace characters, because that means we're at the
// beginning or at the end of the JSXText node
const replacement = /[^\s]/.test(value) ? ' ' : '';
value = value.replace(/\s*\n+\s*/g, replacement);
if (/[^\s]/.test(value)) {
value = value.replace(/(^\s*\n\s*|\s*\n\s*$)/g, '');
value = value.replace(/\s*\n+\s*/g, ' ');
} else {
value = value.replace(/\s*\n+\s*/g, '');
}
}
children.push(TYPES.stringLiteral(value));
}
Expand Down
4 changes: 3 additions & 1 deletion packages/wmr/test/acorn-traverse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,7 @@ describe('acorn-traverse', () => {
<p>это
👩‍🚀</p>
, foo
</p>
);
`;
Expand All @@ -1343,14 +1344,15 @@ describe('acorn-traverse', () => {
<p>это
👩‍🚀</p>
, foo
</p>\`
);"
`);

// Should remove the whitespaces between the HTM generated syntax
expect(doTransformWithCompact(expression)).toMatchInlineSnapshot(`
"(
html\`<p>hello world <p>это 👩‍🚀</p></p>\`
html\`<p>hello world<p>это 👩‍🚀</p>, foo</p>\`
);"
`);
});
Expand Down

0 comments on commit 2f7c74a

Please sign in to comment.