mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier): format with and if statements (#1425)
This commit is contained in:
parent
c5adda81c2
commit
902d4d0bab
4 changed files with 45 additions and 43 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use oxc_allocator::Vec;
|
||||
use oxc_ast::{ast::*, AstKind};
|
||||
|
||||
use crate::{doc::Doc, format::array, hardline, indent, ss, Prettier};
|
||||
use crate::{doc::Doc, hardline, indent, ss, Prettier};
|
||||
|
||||
use super::statement;
|
||||
|
||||
|
|
@ -66,20 +66,3 @@ pub(super) fn print_block_body<'a>(
|
|||
|
||||
Some(Doc::Array(parts))
|
||||
}
|
||||
|
||||
pub(super) fn adjust_clause<'a>(
|
||||
p: &Prettier<'a>,
|
||||
node: &Statement<'a>,
|
||||
clause: Doc<'a>,
|
||||
force_space: bool,
|
||||
) -> Doc<'a> {
|
||||
if matches!(node, Statement::EmptyStatement(_)) {
|
||||
return ss!(";");
|
||||
}
|
||||
|
||||
if matches!(node, Statement::BlockStatement(_)) || force_space {
|
||||
return array![p, ss!(" "), clause];
|
||||
}
|
||||
|
||||
indent![p, Doc::Line, clause]
|
||||
}
|
||||
|
|
|
|||
20
crates/oxc_prettier/src/format/misc.rs
Normal file
20
crates/oxc_prettier/src/format/misc.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{array, doc::Doc, indent, ss, Prettier};
|
||||
|
||||
pub(super) fn adjust_clause<'a>(
|
||||
p: &Prettier<'a>,
|
||||
node: &Statement<'a>,
|
||||
clause: Doc<'a>,
|
||||
force_space: bool,
|
||||
) -> Doc<'a> {
|
||||
if matches!(node, Statement::EmptyStatement(_)) {
|
||||
return ss!(";");
|
||||
}
|
||||
|
||||
if matches!(node, Statement::BlockStatement(_)) || force_space {
|
||||
return array![p, ss!(" "), clause];
|
||||
}
|
||||
|
||||
indent![p, Doc::Line, clause]
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ mod call_expression;
|
|||
mod class;
|
||||
mod function;
|
||||
mod function_parameters;
|
||||
mod misc;
|
||||
mod module;
|
||||
mod object;
|
||||
mod statement;
|
||||
|
|
@ -34,7 +35,6 @@ use crate::{
|
|||
use self::{
|
||||
array::Array,
|
||||
binaryish::{BinaryishLeft, BinaryishOperator},
|
||||
block::adjust_clause,
|
||||
};
|
||||
|
||||
pub trait Format<'a> {
|
||||
|
|
@ -135,7 +135,7 @@ impl<'a> Format<'a> for IfStatement<'a> {
|
|||
let mut parts = p.vec();
|
||||
|
||||
let consequent = format!(p, self.consequent);
|
||||
let consequent = adjust_clause(p, &self.consequent, consequent, false);
|
||||
let consequent = misc::adjust_clause(p, &self.consequent, consequent, false);
|
||||
|
||||
let opening = group![
|
||||
p,
|
||||
|
|
@ -147,8 +147,19 @@ impl<'a> Format<'a> for IfStatement<'a> {
|
|||
parts.push(opening);
|
||||
|
||||
if let Some(alternate) = &self.alternate {
|
||||
let else_on_same_line = matches!(alternate, Statement::BlockStatement(_));
|
||||
parts.push(if else_on_same_line { ss!(" ") } else { hardline!() });
|
||||
parts.push(ss!("else"));
|
||||
parts.push(group!(p, format!(p, alternate)));
|
||||
let alternate_doc = format!(p, alternate);
|
||||
parts.push(group!(
|
||||
p,
|
||||
misc::adjust_clause(
|
||||
p,
|
||||
alternate,
|
||||
alternate_doc,
|
||||
matches!(alternate, Statement::IfStatement(_))
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
Doc::Array(parts)
|
||||
|
|
@ -191,7 +202,7 @@ impl<'a> Format<'a> for ForStatement<'a> {
|
|||
parts.push(ss!(")"));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
})
|
||||
|
|
@ -219,7 +230,7 @@ impl<'a> Format<'a> for ForInStatement<'a> {
|
|||
parts.push(ss!(")"));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
@ -241,7 +252,7 @@ impl<'a> Format<'a> for ForOfStatement<'a> {
|
|||
parts.push(ss!(")"));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
@ -267,7 +278,7 @@ impl<'a> Format<'a> for WhileStatement<'a> {
|
|||
parts.push(ss!(")"));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
parts.push(misc::adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
})
|
||||
|
|
@ -280,7 +291,7 @@ impl<'a> Format<'a> for DoWhileStatement<'a> {
|
|||
let mut parts = p.vec();
|
||||
|
||||
let clause = format!(p, self.body);
|
||||
let clause = adjust_clause(p, &self.body, clause, false);
|
||||
let clause = misc::adjust_clause(p, &self.body, clause, false);
|
||||
let do_body = group!(p, ss!("do"), clause);
|
||||
|
||||
parts.push(do_body);
|
||||
|
|
@ -451,20 +462,14 @@ impl<'a> Format<'a> for ThrowStatement<'a> {
|
|||
|
||||
impl<'a> Format<'a> for WithStatement<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
let body = group![p, hardline!(), format!(p, self.body), hardline!()];
|
||||
let with_stmt = group![
|
||||
let body_doc = self.body.format(p);
|
||||
group![
|
||||
p,
|
||||
ss!("with ("),
|
||||
format!(p, self.object),
|
||||
ss!(")"),
|
||||
ss!(" "),
|
||||
ss!("{"),
|
||||
indent!(p, body),
|
||||
hardline!()
|
||||
];
|
||||
parts.push(with_stmt);
|
||||
Doc::Array(parts)
|
||||
misc::adjust_clause(p, &self.body, body_doc, false)
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 109/838 (13.01%)
|
||||
Compatibility: 114/838 (13.60%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -507,7 +507,6 @@ Compatibility: 109/838 (13.01%)
|
|||
* functional-composition/pipe-function-calls.js
|
||||
* functional-composition/ramda_compose.js
|
||||
* functional-composition/ramda_pipe.js
|
||||
* functional-composition/redux_compose.js
|
||||
* functional-composition/redux_connect.js
|
||||
* functional-composition/reselect_createselector.js
|
||||
* functional-composition/rxjs_pipe.js
|
||||
|
|
@ -552,7 +551,6 @@ Compatibility: 109/838 (13.01%)
|
|||
* import/comments.js
|
||||
* import/empty-import.js
|
||||
* import/inline.js
|
||||
* import/long-line.js
|
||||
* import/multiple_standalones.js
|
||||
* import/same-local-and-imported.js
|
||||
|
||||
|
|
@ -588,7 +586,6 @@ Compatibility: 109/838 (13.01%)
|
|||
* import-reflection/valid-default-import.mjs
|
||||
* import-reflection/valid-from-as-default-module-binding-escaped.mjs
|
||||
* import-reflection/valid-from-as-default-module-binding.mjs
|
||||
* import-reflection/valid-module-as-default-binding.mjs
|
||||
|
||||
### label
|
||||
* label/comment.js
|
||||
|
|
@ -1001,9 +998,6 @@ Compatibility: 109/838 (13.01%)
|
|||
### while
|
||||
* while/indent.js
|
||||
|
||||
### with
|
||||
* with/indent.js
|
||||
|
||||
### yield
|
||||
* yield/arrow.js
|
||||
* yield/conditional.js
|
||||
|
|
|
|||
Loading…
Reference in a new issue