Replies: 4 comments 3 replies
|
There is not a Prettier option for preserving arbitrary TSX/JSX whitespace exactly as typed. Prettier prints JSX text according to its JSX formatter, and raw source spaces are not a reliable layout primitive in JSX. If the space is semantic, make it explicit: <span>Before{" "}after</span>
<span>Before after</span>If the space is visual layout, use CSS instead: |
|
You can't disable this globally. In TSX/JSX, whitespace between props is normalized by the formatter. Try using a smaller line width: {
"formatter": {
"lineWidth": 80
}
}So: not a bug, expected formatter behavior. |
|
This looks like blank lines between JSX/TSX attributes specifically. For now, the workaround is to ignore the whole JSX node: {/* prettier-ignore */}
<ReactPlayer
muted
onTimeUpdate={...}
onLoadedMetadata={hideLoading}
onLoadedData={hideLoading}
ref={playerRef}
/>or, depending on the surrounding code: // prettier-ignore
<ReactPlayer
...
/>No closing marker is needed because Another workaround is to use comments as prop group separators, so Prettier can still format the component: <ReactPlayer
muted
onTimeUpdate={...}
// Loading handlers
onLoadedMetadata={hideLoading}
onLoadedData={hideLoading}
// Media config
ref={playerRef}
slot="media"
/>@fisker, maintainers: would you be open to a PR that preserves at most one blank line between JSX attributes when it already exists in the source? The idea would not be to add a new formatting option, but to preserve limited intentional grouping, similar to how Prettier already preserves blank lines in some other places. Multiple blank lines could still be collapsed to one. If this behavior is acceptable for Prettier, I will implement it properly and include tests. |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I have a problem with the fact that for convenience, I’m taking whitespaces in the TSX file, but it eats them up when formatting
Before/After


All reactions