feat(prettier): format empty try catch (#1430)

This commit is contained in:
Boshen 2023-11-19 18:01:49 +08:00 committed by GitHub
parent 18fa8e45a4
commit cf388ae79e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 12 deletions

View file

@ -25,7 +25,9 @@ pub(super) fn print_block<'a>(
| AstKind::ForStatement(_)
| AstKind::WhileStatement(_)
| AstKind::DoWhileStatement(_)
) || matches!(p.current_kind(), AstKind::StaticBlock(_)))
) || (matches!(parent, AstKind::CatchClause(_))
&& !matches!(p.parent_parent_kind(), Some(AstKind::TryStatement(stmt)) if stmt.finalizer.is_some()))
|| matches!(p.current_kind(), AstKind::StaticBlock(_)))
{
parts.push(hardline!());
}

View file

@ -474,17 +474,19 @@ impl<'a> Format<'a> for TryStatement<'a> {
impl<'a> Format<'a> for CatchClause<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = p.vec();
wrap!(p, self, CatchClause, {
let mut parts = p.vec();
parts.push(ss!("catch "));
if let Some(param) = &self.param {
parts.push(ss!("("));
parts.push(format!(p, param));
parts.push(ss!(") "));
}
parts.push(format!(p, self.body));
parts.push(ss!("catch "));
if let Some(param) = &self.param {
parts.push(ss!("("));
parts.push(format!(p, param));
parts.push(ss!(") "));
}
parts.push(format!(p, self.body));
Doc::Array(parts)
Doc::Array(parts)
})
}
}

View file

@ -76,6 +76,11 @@ impl<'a> Prettier<'a> {
self.nodes[self.nodes.len() - 2]
}
fn parent_parent_kind(&self) -> Option<AstKind<'a>> {
let len = self.nodes.len();
(len >= 3).then(|| self.nodes[len - 3])
}
/// A hack for erasing the lifetime requirement.
#[allow(clippy::unused_self)]
fn alloc<T>(&self, t: &T) -> &'a T {

View file

@ -1,4 +1,4 @@
Compatibility: 121/838 (14.44%)
Compatibility: 122/838 (14.56%)
# Failed
@ -954,7 +954,6 @@ Compatibility: 121/838 (14.44%)
### try
* try/catch.js
* try/empty.js
* try/try.js
### tuple