Skip to content

Commit e0c082e

Browse files
authored
fix: Handle multiple words before trailing space (#572)
`/^(\S*)(\s*)$/` will not match if there are multiple words before the trailing space due to it trying to match non-space characters before the trailing space
1 parent 530ed1f commit e0c082e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/formatter/formatTreeNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ const escape = (s: string) => {
2020
const preserveTrailingSpace = (s: string) => {
2121
let result = s;
2222
if (result.endsWith(' ')) {
23-
result = result.replace(/^(\S*)(\s*)$/, "$1{'$2'}");
23+
result = result.replace(/^(.*?)(\s+)$/, "$1{'$2'}");
2424
}
2525

2626
if (result.startsWith(' ')) {
27-
result = result.replace(/^(\s*)(\S*)$/, "{'$1'}$2");
27+
result = result.replace(/^(\s+)(.*)$/, "{'$1'}$2");
2828
}
2929

3030
return result;

src/index.spec.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -559,19 +559,19 @@ describe('reactElementToJSXString(ReactElement)', () => {
559559
);
560560
});
561561

562-
it('reactElementToJSXString(<div>\nfoo <span>bar</span> baz\n</div>)', () => {
562+
it('reactElementToJSXString(<div>\nfoo bar <span> baz </span> qux quux\n</div>)', () => {
563563
expect(
564564
reactElementToJSXString(
565565
<div>
566-
foo <span>bar</span> baz
566+
foo bar <span> baz </span> qux quux
567567
</div>
568568
)
569569
).toEqual(`<div>
570-
foo{' '}
570+
foo bar{' '}
571571
<span>
572-
bar
572+
{' '}baz{' '}
573573
</span>
574-
{' '}baz
574+
{' '}qux quux
575575
</div>`);
576576
});
577577

0 commit comments

Comments
 (0)