Skip to content

Commit 3a745e4

Browse files
committed
refactor(formatter): remove SiblingNode
1 parent 85ce0af commit 3a745e4

File tree

6 files changed

+1522
-4990
lines changed

6 files changed

+1522
-4990
lines changed

crates/oxc_formatter/src/formatter/comments.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ use oxc_span::{GetSpan, Span};
109109
use crate::{
110110
Format, FormatResult, SyntaxTriviaPieceComments,
111111
formatter::{Formatter, SourceText},
112-
generated::ast_nodes::SiblingNode,
113112
};
114113

115114
#[derive(Debug, Clone)]
@@ -296,26 +295,25 @@ impl<'a> Comments<'a> {
296295
/// Returns comments that should be printed as trailing comments for `preceding_node`.
297296
pub fn get_trailing_comments(
298297
&self,
299-
enclosing_node: &SiblingNode<'a>,
300-
preceding_node: &SiblingNode<'a>,
301-
mut following_node: Option<&SiblingNode<'a>>,
298+
enclosing_span: Span,
299+
preceding_span: Span,
300+
mut following_span: Option<Span>,
302301
) -> &'a [Comment] {
303302
let comments = self.unprinted_comments();
304303
if comments.is_empty() {
305304
return &[];
306305
}
307306

308307
let source_text = self.source_text;
309-
let preceding_span = preceding_node.span();
310308

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

316-
let Some(following_node) = following_node else {
314+
let Some(following_span) = following_span else {
317315
// Find dangling comments at the end of the enclosing node
318-
let comments = self.comments_before(enclosing_node.span().end);
316+
let comments = self.comments_before(enclosing_span.end);
319317

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

339-
let following_span = following_node.span();
340-
341337
let mut comment_index = 0;
342338
while let Some(comment) = comments.get(comment_index) {
343339
// Check if the comment is before the following node's span

crates/oxc_formatter/src/formatter/trivia.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use oxc_ast::{
6363
use oxc_span::{GetSpan, Span};
6464
use oxc_syntax::comment_node;
6565

66-
use crate::{generated::ast_nodes::SiblingNode, write};
66+
use crate::write;
6767

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

@@ -161,23 +161,23 @@ impl<'a> Format<'a> for FormatLeadingComments<'a> {
161161
}
162162

163163
/// Formats the trailing comments of `node`.
164-
pub const fn format_trailing_comments<'a, 'b>(
165-
enclosing_node: &'b SiblingNode<'a>,
166-
preceding_node: &'b SiblingNode<'a>,
167-
following_node: Option<&'b SiblingNode<'a>>,
168-
) -> FormatTrailingComments<'a, 'b> {
169-
FormatTrailingComments::Node((enclosing_node, preceding_node, following_node))
164+
pub const fn format_trailing_comments<'a>(
165+
enclosing_span: Span,
166+
preceding_span: Span,
167+
following_span: Option<Span>,
168+
) -> FormatTrailingComments<'a> {
169+
FormatTrailingComments::Node((enclosing_span, preceding_span, following_span))
170170
}
171171

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

180-
impl<'a> Format<'a> for FormatTrailingComments<'a, '_> {
180+
impl<'a> Format<'a> for FormatTrailingComments<'a> {
181181
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
182182
fn format_trailing_comments_impl<'a>(
183183
comments: impl IntoIterator<Item = &'a Comment>,
@@ -256,11 +256,11 @@ impl<'a> Format<'a> for FormatTrailingComments<'a, '_> {
256256
}
257257

258258
match self {
259-
Self::Node((enclosing_node, preceding_node, following_node)) => {
259+
Self::Node((enclosing_span, preceding_span, following_span)) => {
260260
let comments = f.context().comments().get_trailing_comments(
261-
enclosing_node,
262-
preceding_node,
263-
*following_node,
261+
*enclosing_span,
262+
*preceding_span,
263+
*following_span,
264264
);
265265

266266
format_trailing_comments_impl(comments, f)

0 commit comments

Comments
 (0)