benjamn / recast

JavaScript syntax tree transformer, nondestructive pretty-printer, and automatic source map generator
MIT License
4.91k stars 347 forks source link

printer: Support \n\n while printing JSXElement children #1396

Open JHilker opened 4 months ago

JHilker commented 4 months ago

The printer seems to treat any number of newlines within a JSXText as a single newline. This causes any transform touching these elements to remove newlines that tooling such as Prettier would otherwise not remove.

Though I'm a little surprised to not see the same formatting difference in ASTExplorer, though it's running on 0.21.1 instead of the latest version (0.23.6), so maybe this was a regression at some point? I can reproduce it both within the test suite, and transforms using 0.23.6.

Edit: Oh, this was because by default the transform itself was using parsers.esprima. It's reproducible if you swap to parsers.babel, example.

Current behavior:

Before After
```jsx const SomeComponent = () => { return (
); } ```
```jsx const SomeComponent = () => { return (
); } ```

This PR updates the printer for JSXElement / JSXFragment to return two newlines instead of a single newline for any string children which include two adjacent newlines.

Proposed behavior:

Before After
```jsx const SomeComponent = () => { return (
); } ```
```jsx const SomeComponent = () => { return (
); } ```

Thanks in advance for any review. This seems like an innocent enough change, but I'd entirely understand if it's more complicated than I'm imagining. Open to feedback and happy to iterate on this PR!