From e0c082eb1d1e9fe2ed2918db157079d17af3af09 Mon Sep 17 00:00:00 2001 From: Kamal Fariz Mahyuddin Date: Wed, 1 Jul 2020 00:00:08 -0700 Subject: [PATCH] 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 --- src/formatter/formatTreeNode.js | 4 ++-- src/index.spec.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/formatter/formatTreeNode.js b/src/formatter/formatTreeNode.js index 0a24d7909..0d5ea3429 100644 --- a/src/formatter/formatTreeNode.js +++ b/src/formatter/formatTreeNode.js @@ -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; diff --git a/src/index.spec.js b/src/index.spec.js index a88060855..c2b4c288c 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -559,19 +559,19 @@ describe('reactElementToJSXString(ReactElement)', () => { ); }); - it('reactElementToJSXString(
\nfoo bar baz\n
)', () => { + it('reactElementToJSXString(
\nfoo bar baz qux quux\n
)', () => { expect( reactElementToJSXString(
- foo bar baz + foo bar baz qux quux
) ).toEqual(`
- foo{' '} + foo bar{' '} - bar + {' '}baz{' '} - {' '}baz + {' '}qux quux
`); });