Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
fix(rome_formatter): Skipped token trivia spacing (#3145)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Sep 1, 2022
1 parent 1bf83c5 commit f016baf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
27 changes: 20 additions & 7 deletions crates/rome_formatter/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::prelude::*;
use rome_formatter::write;
use rome_js_syntax::{
JsAnyExpression, JsxExpressionAttributeValue, JsxExpressionAttributeValueFields,
TriviaPieceKind,
};

#[derive(Debug, Clone, Default)]
Expand Down Expand Up @@ -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()])
}))]
)
}
Expand Down

0 comments on commit f016baf

Please sign in to comment.