Skip to content

fix(formatting): trailling space #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/formatter/formatProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default (
formattedPropValue === '{false}' &&
!hasDefaultValue
) {
// If a boolean is false an is not different from it's default, we do not render the attribute
// If a boolean is false and not different from it's default, we do not render the attribute
attributeFormattedInline = '';
attributeFormattedMultiline = '';
} else if (useBooleanShorthandSyntax && formattedPropValue === '{true}') {
Expand Down
17 changes: 16 additions & 1 deletion src/formatter/formatTreeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ const escape = (s: string) => {
return `{\`${s}\`}`;
};

const preserveTrailingSpace = (s: string) => {
let result = s;
if (result.endsWith(' ')) {
result = result.replace(/^(\S*)(\s*)$/, "$1{'$2'}");
}

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

return result;
};

export default (
node: TreeNode,
inline: boolean,
Expand All @@ -27,7 +40,9 @@ export default (
}

if (node.type === 'string') {
return node.value ? escape(String(node.value)) : '';
return node.value
? `${preserveTrailingSpace(escape(String(node.value)))}`
: '';
}

if (node.type === 'ReactElement') {
Expand Down
20 changes: 19 additions & 1 deletion src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,22 @@ describe('reactElementToJSXString(ReactElement)', () => {
);
});

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

it('reactElementToJSXString(<div a={[1, 2, 3, 4]} />', () => {
expect(reactElementToJSXString(<div a={[1, 2, 3, 4]} />)).toEqual(
`<div
Expand Down Expand Up @@ -722,7 +738,9 @@ describe('reactElementToJSXString(ReactElement)', () => {

it('reactElementToJSXString(<div> {false} </div>)', () => {
expect(reactElementToJSXString(<div> {false} </div>)).toEqual(
'<div>\n \n</div>'
`<div>
{' '}
</div>`
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like this output. But the previous one were also not pretty... so I guess we have to look at it in a dedicated PR.

Copy link
Contributor

@vvo vvo Oct 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can always provide an option to remove trailing whitespace no?, wait no, that's different. It should be kept I guess.

);
});

Expand Down