Skip to content

Commit

Permalink
feat(prettier): support print empty switch (#1514)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing authored Nov 23, 2023
1 parent ef59f54 commit 635008a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 18 additions & 8 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,18 +408,17 @@ impl<'a> Format<'a> for SwitchStatement<'a> {
header_parts.push(softline!());
header_parts.push(ss!(")"));

parts.push(group!(p, Doc::Array(header_parts)));
parts.push(Doc::Group(Group::new(header_parts, false)));

parts.push(ss!(" {"));

let mut cases_parts = p.vec();

for case in &self.cases {
cases_parts.push(hardline!());
cases_parts.push(format!(p, case));
cases_parts.push(indent!(p, hardline!(), format!(p, case)));
}

parts.push(indent!(p, hardline!(), group!(p, Doc::Array(cases_parts))));
parts.extend(cases_parts);

parts.push(hardline!());
parts.push(ss!("}"));
Expand All @@ -441,12 +440,23 @@ impl<'a> Format<'a> for SwitchCase<'a> {
}

let mut consequent_parts = p.vec();
for stmt in &self.consequent {
consequent_parts.push(hardline!());
consequent_parts.push(format!(p, stmt));

if !(self.consequent.len() == 1
&& matches!(self.consequent[0], Statement::EmptyStatement(_)))
{
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))));
if !consequent_parts.is_empty() {
parts.push(indent!(
p,
hardline!(),
Doc::Group(Group { contents: consequent_parts, should_break: false })
));
}

Doc::Array(parts)
}
Expand Down
3 changes: 1 addition & 2 deletions tasks/prettier_conformance/prettier.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Compatibility: 145/597 (24.29%)
Compatibility: 146/597 (24.46%)

# Failed

Expand Down Expand Up @@ -577,7 +577,6 @@ Compatibility: 145/597 (24.29%)
* switch/comments2.js
* switch/empty_lines.js
* switch/empty_statement.js
* switch/empty_switch.js
* switch/switch.js

### template
Expand Down

0 comments on commit 635008a

Please sign in to comment.