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): Print directive #8066

Merged
merged 3 commits into from
Dec 23, 2024
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: 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
Loading