mirror of
https://github.com/danbulant/oxc
synced 2026-05-22 05:38:54 +00:00
refactor(prettier): Refactor printing statements (#8269)
Part of #5068 Just for verifying. Almost all changes are cosmetic, do not affect compatibility. --- (Happy new year! 🎍)
This commit is contained in:
parent
b92b2abc2b
commit
0ae2ba4316
1 changed files with 26 additions and 28 deletions
|
|
@ -138,7 +138,7 @@ impl<'a> Format<'a> for IfStatement<'a> {
|
|||
p,
|
||||
[
|
||||
text!("if ("),
|
||||
group!(p, [indent!(p, [softline!(), test_doc]), softline!(),]),
|
||||
group!(p, [indent!(p, [softline!(), test_doc]), softline!()]),
|
||||
text!(")"),
|
||||
consequent
|
||||
]
|
||||
|
|
@ -205,7 +205,7 @@ impl<'a> Format<'a> for ForStatement<'a> {
|
|||
indent!(p, parts_head)
|
||||
};
|
||||
|
||||
group!(p, [text!("for ("), group!(p, [parts_head, softline!()]), text!(")"), body,])
|
||||
group!(p, [text!("for ("), group!(p, [parts_head, softline!()]), text!(")"), body])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -228,8 +228,10 @@ impl<'a> Format<'a> for ForInStatement<'a> {
|
|||
parts.push(text!(" in "));
|
||||
parts.push(self.right.format(p));
|
||||
parts.push(text!(")"));
|
||||
let body = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
let body_doc = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body_doc, false));
|
||||
|
||||
group!(p, parts)
|
||||
})
|
||||
}
|
||||
|
|
@ -248,8 +250,10 @@ impl<'a> Format<'a> for ForOfStatement<'a> {
|
|||
parts.push(text!(" of "));
|
||||
parts.push(self.right.format(p));
|
||||
parts.push(text!(")"));
|
||||
let body = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
let body_doc = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body_doc, false));
|
||||
|
||||
group!(p, parts)
|
||||
})
|
||||
}
|
||||
|
|
@ -268,14 +272,12 @@ impl<'a> Format<'a> for WhileStatement<'a> {
|
|||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
wrap!(p, self, WhileStatement, {
|
||||
let mut parts = Vec::new_in(p.allocator);
|
||||
|
||||
parts.push(text!("while ("));
|
||||
let test_doc = self.test.format(p);
|
||||
parts.push(group!(p, [indent!(p, [softline!(), test_doc]), softline!(),]));
|
||||
parts.push(group!(p, [indent!(p, [softline!(), self.test.format(p)]), softline!()]));
|
||||
parts.push(text!(")"));
|
||||
|
||||
let body = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
let body_doc = self.body.format(p);
|
||||
parts.push(misc::adjust_clause(p, &self.body, body_doc, false));
|
||||
|
||||
group!(p, parts)
|
||||
})
|
||||
|
|
@ -287,10 +289,9 @@ impl<'a> Format<'a> for DoWhileStatement<'a> {
|
|||
wrap!(p, self, DoWhileStatement, {
|
||||
let mut parts = Vec::new_in(p.allocator);
|
||||
|
||||
let clause = self.body.format(p);
|
||||
let clause = misc::adjust_clause(p, &self.body, clause, false);
|
||||
let clause_doc = self.body.format(p);
|
||||
let clause = misc::adjust_clause(p, &self.body, clause_doc, false);
|
||||
let do_body = group!(p, [text!("do"), clause]);
|
||||
|
||||
parts.push(do_body);
|
||||
|
||||
if matches!(self.body, Statement::BlockStatement(_)) {
|
||||
|
|
@ -300,9 +301,9 @@ impl<'a> Format<'a> for DoWhileStatement<'a> {
|
|||
}
|
||||
|
||||
parts.push(text!("while ("));
|
||||
let test_doc = self.test.format(p);
|
||||
parts.push(group!(p, [indent!(p, [softline!(), test_doc]), softline!()]));
|
||||
parts.push(group!(p, [indent!(p, [softline!(), self.test.format(p)]), softline!()]));
|
||||
parts.push(text!(")"));
|
||||
|
||||
if let Some(semi) = p.semi() {
|
||||
parts.push(semi);
|
||||
}
|
||||
|
|
@ -322,6 +323,10 @@ impl<'a> Format<'a> for ContinueStatement<'a> {
|
|||
parts.push(label.format(p));
|
||||
}
|
||||
|
||||
if p.options.semi {
|
||||
parts.push(text!(";"));
|
||||
}
|
||||
|
||||
array!(p, parts)
|
||||
}
|
||||
}
|
||||
|
|
@ -454,13 +459,10 @@ impl<'a> Format<'a> for ReturnStatement<'a> {
|
|||
impl<'a> Format<'a> for LabeledStatement<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
if matches!(self.body, Statement::EmptyStatement(_)) {
|
||||
let label_doc = self.label.format(p);
|
||||
return array!(p, [label_doc, text!(":;")]);
|
||||
return array!(p, [self.label.format(p), text!(":;")]);
|
||||
}
|
||||
|
||||
let label_doc = self.label.format(p);
|
||||
let body_doc = self.body.format(p);
|
||||
array!(p, [label_doc, text!(": "), body_doc])
|
||||
array!(p, [self.label.format(p), text!(": "), self.body.format(p)])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -516,13 +518,12 @@ impl<'a> Format<'a> for ThrowStatement<'a> {
|
|||
|
||||
impl<'a> Format<'a> for WithStatement<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let object_doc = self.object.format(p);
|
||||
let body_doc = self.body.format(p);
|
||||
group!(
|
||||
p,
|
||||
[
|
||||
text!("with ("),
|
||||
object_doc,
|
||||
self.object.format(p),
|
||||
text!(")"),
|
||||
misc::adjust_clause(p, &self.body, body_doc, false)
|
||||
]
|
||||
|
|
@ -1269,7 +1270,7 @@ impl<'a> Format<'a> for BinaryExpression<'a> {
|
|||
&self.right,
|
||||
);
|
||||
if misc::in_parentheses(p.parent_kind(), p.source_text, self.span) {
|
||||
group!(p, [indent!(p, [softline!(), doc]), softline!(),])
|
||||
group!(p, [indent!(p, [softline!(), doc]), softline!()])
|
||||
} else {
|
||||
doc
|
||||
}
|
||||
|
|
@ -1601,10 +1602,7 @@ impl<'a> Format<'a> for AccessorProperty<'a> {
|
|||
|
||||
impl<'a> Format<'a> for PrivateIdentifier<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = Vec::new_in(p.allocator);
|
||||
parts.push(text!("#"));
|
||||
parts.push(dynamic_text!(p, self.name.as_str()));
|
||||
array!(p, parts)
|
||||
array!(p, [text!("#"), dynamic_text!(p, self.name.as_str())])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue