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

fix(rome_js_formatter): Incorrect formatting of interfaces declared inside functions #4253 #4256

Merged
merged 2 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,37 +67,41 @@ impl FormatNodeRule<TsInterfaceDeclaration> for FormatTsInterfaceDeclaration {
Ok(())
});

write![f, [interface_token.format(), space()]]?;
let content = format_with(|f| {
write![f, [interface_token.format(), space()]]?;

let id_has_trailing_comments = f.comments().has_trailing_comments(id.syntax());
if id_has_trailing_comments || extends_clause.is_some() {
if should_indent_extends_only {
write!(
f,
[group(&format_args!(format_id, indent(&format_extends)))]
)?;
let id_has_trailing_comments = f.comments().has_trailing_comments(id.syntax());
if id_has_trailing_comments || extends_clause.is_some() {
if should_indent_extends_only {
write!(
f,
[group(&format_args!(format_id, indent(&format_extends)))]
)?;
} else {
write!(
f,
[group(&indent(&format_args!(format_id, format_extends)))]
)?;
}
} else {
write!(f, [format_id, format_extends])?;
}

write!(f, [space(), l_curly_token.format()])?;

if members.is_empty() {
write!(
f,
[group(&indent(&format_args!(format_id, format_extends)))]
[format_dangling_comments(node.syntax()).with_block_indent()]
)?;
} else {
write!(f, [block_indent(&members.format())])?;
}
} else {
write!(f, [format_id, format_extends])?;
}

write!(f, [space(), l_curly_token.format()])?;

if members.is_empty() {
write!(
f,
[format_dangling_comments(node.syntax()).with_block_indent()]
)?;
} else {
write!(f, [block_indent(&members.format())])?;
}
write!(f, [r_curly_token.format()])
});

write!(f, [r_curly_token.format()])
write![f, [group(&content)]]
}

fn fmt_dangling_comments(
Expand Down
25 changes: 24 additions & 1 deletion crates/rome_js_formatter/tests/specs/ts/declaration/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,27 @@ interface D extends B<string, symbol>, F<string, symbol>, G<string, number, symb
// @ts-ignore
interface D extends B<string, symbol>, F<string, symbol> {

}
}

interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp
}

f(() => {
interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp
}
});

x.y(() => {
interface Wrong {
a: {
b: string;
};
p: {
q: string;
};
}
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info:
test_file: ts/declaration/interface.ts
info: ts/declaration/interface.ts
---

# Input
Expand Down Expand Up @@ -29,6 +28,30 @@ interface D extends B<string, symbol>, F<string, symbol>, G<string, number, symb
interface D extends B<string, symbol>, F<string, symbol> {

}

interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp
}

f(() => {
interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp
}
});

x.y(() => {
interface Wrong {
a: {
b: string;
};
p: {
q: string;
};
}
});

```


Expand Down Expand Up @@ -80,6 +103,29 @@ interface D
}
// @ts-ignore
interface D extends B<string, symbol>, F<string, symbol> {}

interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp;
}

f(() => {
interface Wrong {
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
ppppppp: ppppppp;
}
});

x.y(() => {
interface Wrong {
a: {
b: string;
};
p: {
q: string;
};
}
});
```