fix(rome_formatter): Skipped token trivia spacing
MODIFY
crates/rome_formatter/src/token.rs
crates/rome_formatter/src/token.rs
@@ -328,7 +328,7 @@ where
// If this isn't the first token than format all comments that are before the first skipped token
// trivia or line break as the trailing trivia of the previous token (which these comments will
// become if the document gets formatted a second time).
- if let Some(last_token) = last_token {
+ if let Some(last_token) = last_token.as_ref() {
let mut trailing_comments = vec![];
while let Some(piece) = pieces.peek() {
@@ -343,10 +343,12 @@ where
pieces.next();
}
- FormatTrailingTrivia::new(trailing_comments.into_iter(), last_token).fmt(f)?;
+ FormatTrailingTrivia::new(trailing_comments.into_iter(), *last_token).fmt(f)?;
}
- write_leading_trivia(pieces, token, TriviaPrintMode::Full, f)?;
+ write_leading_trivia(pieces, token, TriviaPrintMode::Full, f, || {
+ token.prev_token()
+ })?;
Ok(())
}
@@ -513,22 +515,23 @@ where
self.token,
self.trim_mode,
f,
- )?;
-
- Ok(())
+ || self.token.prev_token(),
+ )
}
}
-fn write_leading_trivia<I, L, C>(
+fn write_leading_trivia<I, L, C, F>(
pieces: I,
token: &SyntaxToken<L>,
trim_mode: TriviaPrintMode,
f: &mut Formatter<C>,
+ previous_token: F,
) -> FormatResult<()>
where
I: IntoIterator<Item = SyntaxTriviaPiece<L>>,
L: Language,
C: CstFormatContext<Language = L>,
+ F: FnOnce() -> Option<SyntaxToken<L>>,
{
let mut lines_before = 0;
let mut comments = Vec::new();
@@ -569,6 +572,16 @@ where
}
.fmt(f)?;
+ let has_preceding_whitespace = previous_token()
+ .and_then(|token| token.trailing_trivia().pieces().last())
+ .map_or(false, |piece| piece.is_newline() || piece.is_whitespace());
+
+ // Maintain a leading whitespace in front of the skipped token trivia
+ // if the previous token has a trailing whitespace (and there's no comment between the two tokens).
+ if comments.is_empty() && has_preceding_whitespace {
+ write!(f, [space()])?;
+ }
+
comments.clear();
lines_before = 0;

MODIFY
crates/rome_js_formatter/src/jsx/attribute/expression_attribute_value.rs
crates/rome_js_formatter/src/jsx/attribute/expression_attribute_value.rs
@@ -3,7 +3,6 @@ use crate::prelude::*;
use rome_formatter::write;
use rome_js_syntax::{
JsAnyExpression, JsxExpressionAttributeValue, JsxExpressionAttributeValueFields,
- TriviaPieceKind,
};
#[derive(Debug, Clone, Default)]
@@ -70,19 +69,7 @@ impl FormatNodeRule<JsxExpressionAttributeValue> for FormatJsxExpressionAttribut
write!(f, [soft_block_indent(&expression.format())])?;
};
- write!(f, [line_suffix_boundary(),])?;
-
- // format if `}` has a `Skipped` leading trivia
- // <div className={asdf asdf} />;
- let r_curly_token_ref = r_curly_token.as_ref()?;
- if matches!(
- r_curly_token_ref.leading_trivia().first().map(|t| t.kind()),
- Some(TriviaPieceKind::Skipped)
- ) {
- write!(f, [space(), r_curly_token.format()])
- } else {
- write!(f, [r_curly_token.format()])
- }
+ write!(f, [line_suffix_boundary(), r_curly_token.format()])
}))]
)
}

fix(rome_formatter): Skipped token trivia spacing (#3145)