From eaba7cca42572591d7155b0820653c7e3952c245 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 13 Sep 2024 14:52:42 -0400 Subject: [PATCH] Fix comments after : --- src/parser.rs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index d3b93fc..6e6b092 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -630,10 +630,23 @@ where self.toggle_awaiting_key()?; } // // -> [/{ - (Token::Comment { ty: _, own_line: _ }, Token::CollectionStart { ty }) => { + ( + Token::Comment { + ty: comment_ty, + own_line: comment_own_line, + }, + Token::CollectionStart { ty }, + ) => { // TODO: The newline should be conditional // {"a":/**/[]} should not have a newline before the `[` - self.newline()?; + if comment_ty == CommentType::Block + && !comment_own_line + && !self.is_after_value()? + { + self.extra_spacing()?; + } else { + self.newline()?; + } w!(self.write, ty.start_str()); self.state_stack.push_back(ty.as_state()); } @@ -652,10 +665,23 @@ where self.drain_comment(ty)?; } // // -> "" - (Token::Comment { ty: _, own_line: _ }, Token::Value { ty, first_char }) => { + ( + Token::Comment { + ty: comment_ty, + own_line: comment_own_line, + }, + Token::Value { ty, first_char }, + ) => { // TODO: The newline should be conditional // {"a":/**/"b"} should not have a newline - self.newline()?; + if comment_ty == CommentType::Block + && !comment_own_line + && !self.is_after_value()? + { + self.extra_spacing()?; + } else { + self.newline()?; + } self.drain_value(ty, *first_char)?; if self.is_awaiting_key()? {