From c762a85fccb3e8cceddc9cf7316671159293e422 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 21 Nov 2023 17:26:36 +0800 Subject: [PATCH] feat(prettier): print inner comment in PropertyKey (#1479) --- crates/oxc_prettier/src/comment.rs | 1 + crates/oxc_prettier/src/format/call_expression.rs | 2 +- crates/oxc_prettier/src/format/mod.rs | 8 +++++++- tasks/prettier_conformance/prettier.snap.md | 5 +---- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/crates/oxc_prettier/src/comment.rs b/crates/oxc_prettier/src/comment.rs index 87de4f6a8..938f3868f 100644 --- a/crates/oxc_prettier/src/comment.rs +++ b/crates/oxc_prettier/src/comment.rs @@ -58,6 +58,7 @@ impl<'a> Prettier<'a> { break; } } + parts } diff --git a/crates/oxc_prettier/src/format/call_expression.rs b/crates/oxc_prettier/src/format/call_expression.rs index ed134cf64..12820d055 100644 --- a/crates/oxc_prettier/src/format/call_expression.rs +++ b/crates/oxc_prettier/src/format/call_expression.rs @@ -80,7 +80,7 @@ fn print_call_expression_arguments<'a>( let arguments = expression.arguments(); let should_break = !is_commons_js_or_amd_call(expression.callee(), arguments); - if arguments.len() == 0 { + if arguments.is_empty() { parts.extend(p.print_inner_comment(Span::new(callee.span().end, expression.span().end))); } diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index 336407b82..2ee26bbe2 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -25,7 +25,7 @@ use std::borrow::Cow; use oxc_allocator::{Box, Vec}; use oxc_ast::{ast::*, AstKind}; -use oxc_span::GetSpan; +use oxc_span::{GetSpan, Span}; use crate::{ array, @@ -1422,6 +1422,12 @@ impl<'a> Format<'a> for ObjectProperty<'a> { } } else { parts.push(format!(p, self.key)); + let comments = + p.print_inner_comment(Span::new(self.key.span().end, self.value.span().start)); + if !comments.is_empty() { + parts.push(ss!(" ")); + parts.extend(comments); + } parts.push(ss!(": ")); parts.push(format!(p, self.value)); } diff --git a/tasks/prettier_conformance/prettier.snap.md b/tasks/prettier_conformance/prettier.snap.md index c1166b525..2be724717 100644 --- a/tasks/prettier_conformance/prettier.snap.md +++ b/tasks/prettier_conformance/prettier.snap.md @@ -1,4 +1,4 @@ -Compatibility: 133/601 (22.13%) +Compatibility: 134/601 (22.30%) # Failed @@ -484,9 +484,6 @@ Compatibility: 133/601 (22.13%) * object-prop-break-in/short-keys.js * object-prop-break-in/test.js -### object-property-comment -* object-property-comment/after-key.js - ### object-property-ignore * object-property-ignore/ignore.js * object-property-ignore/issue-5678.js