Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/autofmt/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,8 @@ impl<'a> Writer<'a> {
if src_lines.peek().is_none() {
out = pretty;
} else {
for line in pretty.lines() {
let mut pretty_iter = pretty.lines().peekable();
while let Some(line) = pretty_iter.next() {
let trimmed = line.trim();
let compacted = line.replace(" ", "").replace(",", "");

Expand Down Expand Up @@ -1062,6 +1063,29 @@ impl<'a> Writer<'a> {
out.push('\n');
}
}

// Skip pretty continuation lines whose content was already
// emitted as part of the verbatim multi-line source.
if acc.len() > compacted.len() {
let mut consumed_compact = compacted.clone();
while consumed_compact.len() < acc.len() {
let Some(next_pretty) = pretty_iter.peek() else {
break;
};
let next_trimmed = next_pretty.trim();
if next_trimmed.is_empty() || next_trimmed.starts_with("//") {
break;
}
let next_compact = next_pretty.replace(" ", "").replace(",", "");
let candidate = format!("{consumed_compact}{next_compact}");
if acc.starts_with(&candidate) {
consumed_compact = candidate;
pretty_iter.next();
continue;
}
break;
}
}
} else {
// Single line - output pretty line and capture inline comments
out.push_str(line);
Expand Down
6 changes: 6 additions & 0 deletions packages/autofmt/tests/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ twoway![
commented_rsx_block_only,
commented_rsx_block_between,
commented_rsx_block_deep,
cfg_let_event_block,
];

fn assert_idempotent(src: &str) {
Expand Down Expand Up @@ -109,3 +110,8 @@ fn comments_attr_expr_blocks_is_idempotent() {
fn comments_expr_with_strings_is_idempotent() {
assert_idempotent(include_str!("./samples/comments_expr_with_strings.rsx"));
}

#[test]
fn cfg_let_event_block_is_idempotent() {
assert_idempotent(include_str!("./samples/cfg_let_event_block.rsx"));
}
24 changes: 24 additions & 0 deletions packages/autofmt/tests/samples/cfg_let_event_block.rsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
rsx! {
button {
onclick: {
#[cfg(target_os = "android")]
let create_photo_collection_gallery = create_photo_collection.clone();

move |_| {
uploading.set(true);
error.set(String::new());

#[cfg(target_os = "android")]
let create_photo_collection_gallery_call =
create_photo_collection_gallery.clone();

if let Err(err) = Err::<(), _>("x") {
error.set(format!("{}: {}", "err", err));
uploading.set(false);
return;
}
}
},
"x"
}
}