[internal] Move Linter OperatorPrecedence into ruff_python_ast crate (#16162)

## Summary

This change begins to resolve #16071 by moving the `OperatorPrecedence`
structs from the `ruff_python_linter` crate into `ruff_python_ast`. This
PR also implements `precedence()` methods on the `Expr` and `ExprRef`
enums.

## Test Plan

Since this change mainly shifts existing logic, I didn't add any
additional tests. Existing tests do pass.
This commit is contained in:
Junhson Jean-Baptiste 2025-02-14 09:55:07 -05:00 committed by GitHub
parent 63dd68e0ed
commit fa28dc5ccf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 193 additions and 168 deletions

View file

@ -18,7 +18,8 @@ use crate::{
name::Name,
str::{Quote, TripleQuotes},
str_prefix::{AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix},
ExceptHandler, Expr, FStringElement, LiteralExpressionRef, Pattern, Stmt, TypeParam,
ExceptHandler, Expr, ExprRef, FStringElement, LiteralExpressionRef, OperatorPrecedence,
Pattern, Stmt, TypeParam,
};
/// See also [Module](https://docs.python.org/3/library/ast.html#ast.Module)
@ -365,6 +366,17 @@ impl Expr {
_ => None,
}
}
/// Return the [`OperatorPrecedence`] of this expression
pub fn precedence(&self) -> OperatorPrecedence {
OperatorPrecedence::from(self)
}
}
impl ExprRef<'_> {
pub fn precedence(&self) -> OperatorPrecedence {
OperatorPrecedence::from(self)
}
}
/// An AST node used to represent a IPython escape command at the expression level.