Visit Identifier node as part of the SourceOrderVisitor (#17110)

## Summary

I don't remember exactly when we made `Identifier` a node but it is now
considered a node (it implements `AnyNodeRef`, it has a range). However,
we never updated
the `SourceOrderVisitor` to visit identifiers because we never had a use
case for it and visiting new nodes can change how the formatter
associates comments (breaking change!).
This PR updates the `SourceOrderVisitor` to visit identifiers and
changes the formatter comment visitor to skip identifiers (updating the
visitor might be desired because it could help simplifying some comment
placement logic but this is out of scope for this PR).

## Test Plan

Tests, updated snapshot tests
This commit is contained in:
Micha Reiser 2025-04-01 16:58:09 +02:00 committed by GitHub
parent 49c25993eb
commit 5a876ed25e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 142 additions and 35 deletions

View file

@ -1,10 +1,10 @@
use crate::AnyNodeRef;
use crate::{
Alias, Arguments, BoolOp, BytesLiteral, CmpOp, Comprehension, Decorator, ElifElseClause,
ExceptHandler, Expr, FString, FStringElement, Keyword, MatchCase, Mod, Operator, Parameter,
ParameterWithDefault, Parameters, Pattern, PatternArguments, PatternKeyword, Singleton, Stmt,
StringLiteral, TypeParam, TypeParams, UnaryOp, WithItem,
};
use crate::{AnyNodeRef, Identifier};
/// Visitor that traverses all nodes recursively in the order they appear in the source.
///
@ -170,6 +170,11 @@ pub trait SourceOrderVisitor<'a> {
fn visit_bytes_literal(&mut self, bytes_literal: &'a BytesLiteral) {
walk_bytes_literal(self, bytes_literal);
}
#[inline]
fn visit_identifier(&mut self, identifier: &'a Identifier) {
walk_identifier(self, identifier);
}
}
pub fn walk_module<'a, V>(visitor: &mut V, module: &'a Mod)
@ -580,3 +585,15 @@ where
}
visitor.leave_node(node);
}
#[inline]
pub fn walk_identifier<'a, V: SourceOrderVisitor<'a> + ?Sized>(
visitor: &mut V,
identifier: &'a Identifier,
) {
let node = AnyNodeRef::from(identifier);
if visitor.enter_node(node).is_traverse() {
identifier.visit_source_order(visitor);
}
visitor.leave_node(node);
}