Skip to content

Conversation

alangpierce
Copy link
Contributor

See benjamn/recast#365 for more context.

It looks like the recast printer includes all JSX on the same line by default,
which can lead to really ugly code with deeply nested React elements. However,
if we put a '\n' text node around all adjacent children, it will format the
JSX in the typical multiline way, properly indented, so this commit changes the
script to do that.

Ideally, we might want to keep some expressions inline, e.g. by determining
whether the child expressions will fit nicely on one line, but that seems sort
of nontrivial with just the AST, so we just use multiline JSX always for now.

To keep strings correct, we need to be a little more careful about translating
child string literals. Since each child will be on its own line, leading and
trailing whitespace will be removed, so if the string literal starts or ends
with whitespace, or is an empty string, we fall back to an expression container
with a normal JS string.

See benjamn/recast#365 for more context. It looks like the recast printer includes all JSX on the same line by default, which can lead to really ugly code with deeply nested React elements. However, if we put a `'\n'` text node around all adjacent children, it will format the JSX in the typical multiline way, properly indented, so this commit changes the script to do that. Ideally, we might want to keep some expressions inline, e.g. by determining whether the child expressions will fit nicely on one line, but that seems sort of nontrivial with just the AST, so we just use multiline JSX always for now. To keep strings correct, we need to be a little more careful about translating child string literals. Since each child will be on its own line, leading and trailing whitespace will be removed, so if the string literal starts or ends with whitespace, or is an empty string, we fall back to an expression container with a normal JS string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants