mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(prettier) port adjust clause (#1388)
This commit is contained in:
parent
5cb6ff2895
commit
b025213795
3 changed files with 36 additions and 13 deletions
|
|
@ -2,7 +2,7 @@ use oxc_allocator::Vec;
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use oxc_ast::ast::*;
|
||||
|
||||
use crate::{doc::Doc, hardline, indent, ss, Prettier};
|
||||
use crate::{doc::Doc, format::array, hardline, indent, ss, Prettier};
|
||||
|
||||
use super::statement;
|
||||
|
||||
|
|
@ -57,3 +57,20 @@ 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]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ use crate::{
|
|||
use self::{
|
||||
array::Array,
|
||||
binaryish::{BinaryishLeft, BinaryishOperator},
|
||||
block::adjust_clause,
|
||||
};
|
||||
|
||||
pub trait Format<'a> {
|
||||
|
|
@ -113,13 +114,15 @@ impl<'a> Format<'a> for IfStatement<'a> {
|
|||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
let mut parts = p.vec();
|
||||
|
||||
let consequent = format!(p, self.consequent);
|
||||
let consequent = adjust_clause(p, &self.consequent, consequent, false);
|
||||
|
||||
let opening = group![
|
||||
p,
|
||||
ss!("if ("),
|
||||
group!(p, indent!(p, softline!(), format!(p, self.test), softline!())),
|
||||
ss!(")"),
|
||||
ss!(" "),
|
||||
format!(p, self.consequent)
|
||||
consequent
|
||||
];
|
||||
parts.push(opening);
|
||||
|
||||
|
|
@ -165,7 +168,9 @@ impl<'a> Format<'a> for ForStatement<'a> {
|
|||
parts.push(group!(p, parts_head));
|
||||
|
||||
parts.push(ss!(")"));
|
||||
parts.push(format!(p, self.body));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
@ -190,7 +195,9 @@ impl<'a> Format<'a> for ForInStatement<'a> {
|
|||
parts.push(ss!(" in "));
|
||||
parts.push(format!(p, self.right));
|
||||
parts.push(ss!(")"));
|
||||
parts.push(format!(p, self.body));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
@ -210,7 +217,9 @@ impl<'a> Format<'a> for ForOfStatement<'a> {
|
|||
parts.push(ss!(" of "));
|
||||
parts.push(format!(p, self.right));
|
||||
parts.push(ss!(")"));
|
||||
parts.push(format!(p, self.body));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
@ -231,11 +240,11 @@ impl<'a> Format<'a> for WhileStatement<'a> {
|
|||
let mut parts = p.vec();
|
||||
|
||||
parts.push(ss!("while ("));
|
||||
|
||||
parts.push(group!(p, indent!(p, softline!(), format!(p, self.test), softline!())));
|
||||
|
||||
parts.push(ss!(")"));
|
||||
parts.push(format!(p, self.body));
|
||||
|
||||
let body = format!(p, self.body);
|
||||
parts.push(adjust_clause(p, &self.body, body, false));
|
||||
|
||||
Doc::Group(parts)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
Compatibility: 59/881 (6.70%)
|
||||
Compatibility: 62/881 (7.04%)
|
||||
|
||||
# Failed
|
||||
|
||||
|
|
@ -490,9 +490,7 @@ Compatibility: 59/881 (6.70%)
|
|||
* explicit-resource-management/valid-for-using-declaration.js
|
||||
* explicit-resource-management/valid-module-block-top-level-await-using-binding.js
|
||||
* explicit-resource-management/valid-module-block-top-level-using-binding.js
|
||||
* explicit-resource-management/valid-using-as-identifier-computed-member.js
|
||||
* explicit-resource-management/valid-using-as-identifier-expression-statement.js
|
||||
* explicit-resource-management/valid-using-as-identifier-for-await-of.js
|
||||
* explicit-resource-management/valid-using-as-identifier-for-in.js
|
||||
* explicit-resource-management/valid-using-as-identifier-for-init.js
|
||||
* explicit-resource-management/valid-using-as-identifier-for-of.js
|
||||
|
|
@ -987,7 +985,6 @@ Compatibility: 59/881 (6.70%)
|
|||
* shebang/shebang.js
|
||||
|
||||
### sloppy-mode
|
||||
* sloppy-mode/function-declaration-in-while.js
|
||||
* sloppy-mode/labeled-function-declaration.js
|
||||
|
||||
### source-phase-imports
|
||||
|
|
|
|||
Loading…
Reference in a new issue