-
Notifications
You must be signed in to change notification settings - Fork 657
fix(rome_js_formatter): restrict export line break for single specifier #3726
fix(rome_js_formatter): restrict export line break for single specifier #3726
Conversation
chore: add space to format
✅ Deploy Preview for docs-rometools canceled.Built without sensitive environment variables
|
crates/rome_js_formatter/src/js/module/export_named_from_clause.rs
Outdated
Show resolved
Hide resolved
let first_specifier = specifiers.elements().next().unwrap(); | ||
match (first_specifier.node(), first_specifier.trailing_separator()) { | ||
(Ok(specifier), Ok(_separator)) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've referenced import_named_clause
.
// SAFETY: we know that the `specifiers.specifiers().len() == 1`, so unwrap `iter().next()` is safe. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fall back to the default implementation if specifier
or separator
isn't ok, and we have to call format_removed
if the separator
is not none.
We should also check that the specifier has no comments
It can then be written like this (you have to make the node
and trailing_separator
fields pub
on AstSeparatedElement
match specifiers.elements().next() {
Some(AstSeparatedElement {
node: Ok(node),
trailing_separator: Ok(separator),
}) if specifiers.len() == 1 && !f.comments().has_comments(node.syntax()) => {
write!(f, [space(), node.format()])?;
if let Some(separator) = separator {
write!(f, [format_removed(&separator)])?;
}
write!(f, [space()])?;
}
_ => {
if node_has_leading_newline(specifiers.syntax()) {
write!(f, [block_indent(&specifiers.format()),])?;
} else {
write!(
f,
[group(&soft_space_or_block_indent(&specifiers.format())),]
)?;
};
}
}
Thanks for pointing to the import specifier code. I noticed that I missed a check when refactoring how Rome handles comments.
Would you mind changing
if specifier.syntax().has_comments_direct()
|| separator
.map(|sep| {
sep.has_leading_comments() || sep.has_trailing_comments()
})
.unwrap_or(false)
{
write!(f, [named_import.format()])
} else {
to
if f.comments().has_comments(specifier.syntax()) {
write!(f, [named_import.format()])
} else {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MichaReiser
Thanks for reviewing and clear explanation! I've updated the code.
I also edited import specifier code and its test.
crates/rome_js_formatter/tests/specs/js/module/export/named_from_clause.js
Outdated
Show resolved
Hide resolved
…om_clause.js Co-authored-by: Micha Reiser <micha@reiser.io>
…nvalley/tools into fix/restrict-export-line-break
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Summary
fix
this comment for prettier compatibilityexport line break
export
is longTest Plan
js/module/export/named_from_clause.js
snapshotprettier/js/export-extension/export.js
snapshot