feat(prettier): filter out EmptyStatement in BlockStatement (#1546)

This commit is contained in:
Dunqing 2023-11-27 22:45:45 +08:00 committed by GitHub
parent afeed17be9
commit 0e1abae757
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 13 deletions

View file

@ -1,12 +1,11 @@
use oxc_ast::{ast::*, AstKind};
use super::{statement, Format};
use crate::{
doc::{Doc, DocBuilder},
hardline, indent, ss, Prettier,
};
use super::statement;
pub(super) fn print_block<'a>(
p: &mut Prettier<'a>,
stmts: &[Statement<'a>],
@ -61,11 +60,11 @@ pub(super) fn print_block_body<'a>(
if has_directives {
if let Some(directives) = directives {
parts.extend(statement::print_statement_sequence(p, directives, false));
parts.extend(directives.iter().map(|d| d.format(p)));
}
}
if !stmts.is_empty() {
if has_body {
parts.extend(statement::print_statement_sequence(p, stmts, remove_last_statement_hardline));
}

View file

@ -1,4 +1,5 @@
use oxc_allocator::Vec;
use oxc_ast::ast::Statement;
use crate::{
doc::{Doc, DocBuilder, Group},
@ -8,17 +9,26 @@ use oxc_span::GetSpan;
use super::Format;
pub(super) fn print_statement_sequence<'a, F: Format<'a> + GetSpan>(
pub(super) fn print_statement_sequence<'a>(
p: &mut Prettier<'a>,
stmts: &[F],
stmts: &[Statement<'a>],
remove_last_statement_hardline: bool,
) -> Vec<'a, Doc<'a>> {
let mut parts = p.vec();
let mut len = stmts.len();
for (i, stmt) in stmts.iter().enumerate() {
if i < len - 1 && matches!(stmts[i + 1], Statement::EmptyStatement(_)) {
len -= 1;
}
if matches!(stmt, Statement::EmptyStatement(_)) {
continue;
}
let mut docs = stmt.format(p);
if remove_last_statement_hardline && i == stmts.len() - 1 {
if remove_last_statement_hardline && i == len - 1 {
match docs {
Doc::Array(ref mut docs) | Doc::Group(Group { contents: ref mut docs, .. }) => {
if matches!(docs.last(), Some(Doc::Hardline)) {
@ -31,7 +41,7 @@ pub(super) fn print_statement_sequence<'a, F: Format<'a> + GetSpan>(
parts.push(docs);
if i < stmts.len() - 1 {
if i < len - 1 {
parts.push(hardline!());
if p.is_next_line_empty(stmt.span().end) {

View file

@ -1,4 +1,4 @@
Compatibility: 171/591 (28.93%)
Compatibility: 173/591 (29.27%)
# Failed
@ -132,7 +132,6 @@ Compatibility: 171/591 (28.93%)
### class-static-block
* class-static-block/class-static-block.js
* class-static-block/with-line-breaks.js
### classes
* classes/asi.js
@ -277,9 +276,6 @@ Compatibility: 171/591 (28.93%)
* empty-paren-comment/class.js
* empty-paren-comment/empty_paren_comment.js
### empty-statement
* empty-statement/no-newline.js
### end-of-line
* end-of-line/example.js