Skip to content
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
14 changes: 5 additions & 9 deletions crates/oxc_formatter/src/formatter/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ use oxc_span::{GetSpan, Span};
use crate::{
Format, FormatResult, SyntaxTriviaPieceComments,
formatter::{Formatter, SourceText},
generated::ast_nodes::SiblingNode,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -296,26 +295,25 @@ impl<'a> Comments<'a> {
/// Returns comments that should be printed as trailing comments for `preceding_node`.
pub fn get_trailing_comments(
&self,
enclosing_node: &SiblingNode<'a>,
preceding_node: &SiblingNode<'a>,
mut following_node: Option<&SiblingNode<'a>>,
enclosing_span: Span,
preceding_span: Span,
mut following_span: Option<Span>,
) -> &'a [Comment] {
let comments = self.unprinted_comments();
if comments.is_empty() {
return &[];
}

let source_text = self.source_text;
let preceding_span = preceding_node.span();

// All of the comments before this node are printed already.
debug_assert!(
comments.first().is_none_or(|comment| comment.span.end > preceding_span.start)
);

let Some(following_node) = following_node else {
let Some(following_span) = following_span else {
// Find dangling comments at the end of the enclosing node
let comments = self.comments_before(enclosing_node.span().end);
let comments = self.comments_before(enclosing_span.end);

let mut start = preceding_span.end;
for (idx, comment) in comments.iter().enumerate() {
Expand All @@ -336,8 +334,6 @@ impl<'a> Comments<'a> {
return comments;
};

let following_span = following_node.span();

let mut comment_index = 0;
while let Some(comment) = comments.get(comment_index) {
// Check if the comment is before the following node's span
Expand Down
30 changes: 15 additions & 15 deletions crates/oxc_formatter/src/formatter/trivia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use oxc_ast::{
use oxc_span::{GetSpan, Span};
use oxc_syntax::comment_node;

use crate::{generated::ast_nodes::SiblingNode, write};
use crate::write;

use super::{Argument, Arguments, GroupId, SourceText, SyntaxToken, prelude::*};

Expand Down Expand Up @@ -161,23 +161,23 @@ impl<'a> Format<'a> for FormatLeadingComments<'a> {
}

/// Formats the trailing comments of `node`.
pub const fn format_trailing_comments<'a, 'b>(
enclosing_node: &'b SiblingNode<'a>,
preceding_node: &'b SiblingNode<'a>,
following_node: Option<&'b SiblingNode<'a>>,
) -> FormatTrailingComments<'a, 'b> {
FormatTrailingComments::Node((enclosing_node, preceding_node, following_node))
pub const fn format_trailing_comments<'a>(
enclosing_span: Span,
preceding_span: Span,
following_span: Option<Span>,
) -> FormatTrailingComments<'a> {
FormatTrailingComments::Node((enclosing_span, preceding_span, following_span))
}

/// Formats the trailing comments of `node`
#[derive(Debug, Clone, Copy)]
pub enum FormatTrailingComments<'a, 'b> {
// (enclosing_node, preceding_node, following_node)
Node((&'b SiblingNode<'a>, &'b SiblingNode<'a>, Option<&'b SiblingNode<'a>>)),
pub enum FormatTrailingComments<'a> {
// (enclosing_span, preceding_span, following_span)
Node((Span, Span, Option<Span>)),
Comments(&'a [Comment]),
}

impl<'a> Format<'a> for FormatTrailingComments<'a, '_> {
impl<'a> Format<'a> for FormatTrailingComments<'a> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
fn format_trailing_comments_impl<'a>(
comments: impl IntoIterator<Item = &'a Comment>,
Expand Down Expand Up @@ -256,11 +256,11 @@ impl<'a> Format<'a> for FormatTrailingComments<'a, '_> {
}

match self {
Self::Node((enclosing_node, preceding_node, following_node)) => {
Self::Node((enclosing_span, preceding_span, following_span)) => {
let comments = f.context().comments().get_trailing_comments(
enclosing_node,
preceding_node,
*following_node,
*enclosing_span,
*preceding_span,
*following_span,
);

format_trailing_comments_impl(comments, f)
Expand Down
Loading
Loading