feat(prettier): print directives (#1497)

This commit is contained in:
Boshen 2023-11-22 19:39:25 +08:00 committed by GitHub
parent cf803d379a
commit 567c6ed757
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 23 deletions

View file

@ -62,15 +62,18 @@ impl<'a> Parser<'a> {
if expecting_diretives { if expecting_diretives {
if let Statement::ExpressionStatement(expr) = &stmt { if let Statement::ExpressionStatement(expr) = &stmt {
if let Expression::StringLiteral(string) = &expr.expression { if let Expression::StringLiteral(string) = &expr.expression {
let src = &self.source_text // span start will mismatch if they are parenthesized when `preserve_parens = false`
[string.span.start as usize + 1..string.span.end as usize - 1]; if expr.span.start == string.span.start {
let directive = self.ast.directive( let src = &self.source_text[string.span.start as usize + 1
expr.span, ..string.span.end as usize - 1];
(*string).clone(), let directive = self.ast.directive(
Atom::from(src), expr.span,
); (*string).clone(),
directives.push(directive); Atom::from(src),
continue; );
directives.push(directive);
continue;
}
} }
} }
expecting_diretives = false; expecting_diretives = false;

View file

@ -1,4 +1,3 @@
use oxc_allocator::Vec;
use oxc_ast::{ast::*, AstKind}; use oxc_ast::{ast::*, AstKind};
use crate::{doc::Doc, hardline, indent, ss, Prettier}; use crate::{doc::Doc, hardline, indent, ss, Prettier};
@ -7,8 +6,8 @@ use super::statement;
pub(super) fn print_block<'a>( pub(super) fn print_block<'a>(
p: &mut Prettier<'a>, p: &mut Prettier<'a>,
stmts: &Vec<'a, Statement<'a>>, stmts: &[Statement<'a>],
directives: Option<&Vec<'a, Directive>>, directives: Option<&[Directive]>,
) -> Doc<'a> { ) -> Doc<'a> {
let mut parts = p.vec(); let mut parts = p.vec();
parts.push(ss!("{")); parts.push(ss!("{"));
@ -38,10 +37,10 @@ pub(super) fn print_block<'a>(
pub(super) fn print_block_body<'a>( pub(super) fn print_block_body<'a>(
p: &mut Prettier<'a>, p: &mut Prettier<'a>,
stmts: &Vec<'a, Statement<'a>>, stmts: &[Statement<'a>],
directives: Option<&Vec<'a, Directive>>, directives: Option<&[Directive]>,
remove_last_statement_hardline: bool, remove_last_statement_hardline: bool,
is_program: bool, is_root: bool,
) -> Option<Doc<'a>> { ) -> Option<Doc<'a>> {
let has_directives = directives.is_some_and(|directives| !directives.is_empty()); let has_directives = directives.is_some_and(|directives| !directives.is_empty());
let has_body = stmts.iter().any(|stmt| !matches!(stmt, Statement::EmptyStatement(_))); let has_body = stmts.iter().any(|stmt| !matches!(stmt, Statement::EmptyStatement(_)));
@ -62,7 +61,7 @@ pub(super) fn print_block_body<'a>(
parts.extend(statement::print_statement_sequence(p, stmts, remove_last_statement_hardline)); parts.extend(statement::print_statement_sequence(p, stmts, remove_last_statement_hardline));
} }
if is_program { if is_root {
parts.push(hardline!()); parts.push(hardline!());
} }

View file

@ -64,9 +64,13 @@ impl<'a> Format<'a> for Program<'a> {
parts.push(hardline!()); parts.push(hardline!());
} }
} }
if let Some(doc) = if let Some(doc) = block::print_block_body(
block::print_block_body(p, &self.body, Some(&self.directives), false, true) p,
{ &self.body,
Some(&self.directives),
false,
/* is_root */ true,
) {
parts.push(doc); parts.push(doc);
} }
p.leave_node(); p.leave_node();
@ -82,7 +86,15 @@ impl<'a> Format<'a> for Hashbang {
impl<'a> Format<'a> for Directive { impl<'a> Format<'a> for Directive {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> { fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
Doc::Line let mut parts = p.vec();
parts.push(
p.str(&string::print_string(self.expression.value.as_str(), p.options.single_quote)),
);
if p.options.semi {
parts.push(ss!(";"));
}
parts.push(hardline!());
Doc::Array(parts)
} }
} }

View file

@ -10,7 +10,7 @@ use super::Format;
pub(super) fn print_statement_sequence<'a, F: Format<'a> + GetSpan>( pub(super) fn print_statement_sequence<'a, F: Format<'a> + GetSpan>(
p: &mut Prettier<'a>, p: &mut Prettier<'a>,
stmts: &Vec<'a, F>, stmts: &[F],
remove_last_statement_hardline: bool, remove_last_statement_hardline: bool,
) -> Vec<'a, Doc<'a>> { ) -> Vec<'a, Doc<'a>> {
let mut parts = p.vec(); let mut parts = p.vec();

View file

@ -1,4 +1,4 @@
Compatibility: 139/597 (23.28%) Compatibility: 140/597 (23.45%)
# Failed # Failed
@ -289,7 +289,6 @@ Compatibility: 139/597 (23.28%)
* directives/last-line-1.js * directives/last-line-1.js
* directives/last-line-2.js * directives/last-line-2.js
* directives/newline.js * directives/newline.js
* directives/no-newline.js
* directives/test.js * directives/test.js
### empty-paren-comment ### empty-paren-comment