Skip to content

Commit

Permalink
fix: Handle multiple words before trailing space (#572)
Browse files Browse the repository at this point in the history
`/^(\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
  • Loading branch information
kamal authored Jul 1, 2020
1 parent 530ed1f commit e0c082e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/formatter/formatTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ const escape = (s: string) => {
const preserveTrailingSpace = (s: string) => {
let result = s;
if (result.endsWith(' ')) {
result = result.replace(/^(\S*)(\s*)$/, "$1{'$2'}");
result = result.replace(/^(.*?)(\s+)$/, "$1{'$2'}");
}

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

return result;
Expand Down
10 changes: 5 additions & 5 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,19 @@ describe('reactElementToJSXString(ReactElement)', () => {
);
});

it('reactElementToJSXString(<div>\nfoo <span>bar</span> baz\n</div>)', () => {
it('reactElementToJSXString(<div>\nfoo bar <span> baz </span> qux quux\n</div>)', () => {
expect(
reactElementToJSXString(
<div>
foo <span>bar</span> baz
foo bar <span> baz </span> qux quux
</div>
)
).toEqual(`<div>
foo{' '}
foo bar{' '}
<span>
bar
{' '}baz{' '}
</span>
{' '}baz
{' '}qux quux
</div>`);
});

Expand Down

0 comments on commit e0c082e

Please sign in to comment.