Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prettier): support print empty switch #1514

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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