Skip to content

Commit

Permalink
feat(prettier): Print directive (#8066)
Browse files Browse the repository at this point in the history
Part of #5068 

- [x] Directive
  • Loading branch information
leaysgur authored Dec 23, 2024
1 parent 3057686 commit 922670e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
26 changes: 16 additions & 10 deletions crates/oxc_prettier/src/format/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,36 @@ impl<'a> Format<'a> for Program<'a> {
impl<'a> Format<'a> for Hashbang<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = Vec::new_in(p.allocator);

parts.push(dynamic_text!(p, self.span.source_text(p.source_text)));
parts.extend(hardline!());
// Preserve original newline
if let Some(c) = p.source_text[self.span.end as usize..].chars().nth(1) {
if is_line_terminator(c) {
parts.extend(hardline!());
}
if p.is_next_line_empty(self.span) {
parts.extend(hardline!());
}

array!(p, parts)
}
}

impl<'a> Format<'a> for Directive<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = Vec::new_in(p.allocator);
parts.push(literal::print_string_from_not_quoted_raw_text(
p,
self.directive.as_str(),
p.options.single_quote,
));

let not_quoted_raw_text = &self.directive.as_str();
// If quote is used, don't replace enclosing quotes, keep as is
if not_quoted_raw_text.contains('"') || not_quoted_raw_text.contains('\'') {
parts.push(dynamic_text!(p, &self.span.source_text(p.source_text)));
} else {
let enclosing_quote = || text!(if p.options.single_quote { "'" } else { "\"" });
parts.push(enclosing_quote());
parts.push(dynamic_text!(p, &not_quoted_raw_text));
parts.push(enclosing_quote());
}
if let Some(semi) = p.semi() {
parts.push(semi);
}
parts.extend(hardline!());

array!(p, parts)
}
}
Expand Down
20 changes: 19 additions & 1 deletion crates/oxc_prettier/src/format/print/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,25 @@ pub fn print_block_body<'a>(

if has_directives {
if let Some(directives) = directives {
parts.extend(directives.iter().map(|d| d.format(p)));
let mut last_directive = &directives[0];
for (idx, directive) in directives.iter().enumerate() {
parts.push(directive.format(p));
if idx != directives.len() - 1 {
parts.extend(hardline!());
if p.is_next_line_empty(directive.span) {
parts.extend(hardline!());
}
}

last_directive = directive;
}

if has_body {
parts.extend(hardline!());
if p.is_next_line_empty(last_directive.span) {
parts.extend(hardline!());
}
}
}
}

Expand Down
6 changes: 1 addition & 5 deletions tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js compatibility: 242/641 (37.75%)
js compatibility: 246/641 (38.38%)

# Failed

Expand Down Expand Up @@ -213,11 +213,7 @@ js compatibility: 242/641 (37.75%)
### js/directives
* js/directives/escaped.js
* js/directives/issue-7346.js
* js/directives/last-line-0.js
* js/directives/last-line-1.js
* js/directives/last-line-2.js
* js/directives/newline.js
* js/directives/test.js

### js/empty-paren-comment
* js/empty-paren-comment/class-property.js
Expand Down

0 comments on commit 922670e

Please sign in to comment.