mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 20:32:10 +00:00
feat(printer) Print SwitchStatement (#1281)
This commit is contained in:
parent
7182cc6ebe
commit
b528af1b93
1 changed files with 48 additions and 3 deletions
|
|
@ -10,7 +10,7 @@ use oxc_allocator::{Box, Vec};
|
|||
use oxc_ast::ast::*;
|
||||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
|
||||
|
||||
use crate::{array, doc::Doc, format, group, indent, softline, string, Prettier};
|
||||
use crate::{array, doc::Doc, format, group, hardline, indent, softline, string, Prettier};
|
||||
|
||||
pub trait Format<'a> {
|
||||
#[must_use]
|
||||
|
|
@ -187,13 +187,58 @@ impl<'a> Format<'a> for BreakStatement {
|
|||
|
||||
impl<'a> Format<'a> for SwitchStatement<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
let mut parts = p.vec();
|
||||
|
||||
let mut header_parts = p.vec();
|
||||
|
||||
header_parts.push(string!(p, "switch ("));
|
||||
|
||||
header_parts.push(indent!(p, softline!(), format!(p, self.discriminant)));
|
||||
|
||||
header_parts.push(softline!());
|
||||
header_parts.push(string!(p, ")"));
|
||||
|
||||
parts.push(group!(p, Doc::Array(header_parts)));
|
||||
|
||||
parts.push(p.str(" {"));
|
||||
|
||||
let mut cases_parts = p.vec();
|
||||
|
||||
for case in &self.cases {
|
||||
cases_parts.push(hardline!());
|
||||
cases_parts.push(format!(p, case));
|
||||
}
|
||||
|
||||
parts.push(indent!(p, hardline!(), group!(p, Doc::Array(cases_parts))));
|
||||
|
||||
parts.push(hardline!());
|
||||
parts.push(p.str("}"));
|
||||
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Format<'a> for SwitchCase<'a> {
|
||||
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
|
||||
Doc::Line
|
||||
let mut parts = p.vec();
|
||||
|
||||
if let Some(test) = &self.test {
|
||||
parts.push(string!(p, "case "));
|
||||
parts.push(format!(p, test));
|
||||
parts.push(string!(p, ":"));
|
||||
} else {
|
||||
parts.push(string!(p, "default:"));
|
||||
}
|
||||
|
||||
let mut consequent_parts = p.vec();
|
||||
for stmt in &self.consequent {
|
||||
consequent_parts.push(hardline!());
|
||||
consequent_parts.push(format!(p, stmt));
|
||||
}
|
||||
|
||||
parts.push(indent!(p, hardline!(), group!(p, Doc::Array(consequent_parts))));
|
||||
|
||||
Doc::Array(parts)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue