mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 04:45:01 +00:00
Split Constant
to individual literal nodes (#8064)
## Summary This PR splits the `Constant` enum as individual literal nodes. It introduces the following new nodes for each variant: * `ExprStringLiteral` * `ExprBytesLiteral` * `ExprNumberLiteral` * `ExprBooleanLiteral` * `ExprNoneLiteral` * `ExprEllipsisLiteral` The main motivation behind this refactor is to introduce the new AST node for implicit string concatenation in the coming PR. The elements of that node will be either a string literal, bytes literal or a f-string which can be implemented using an enum. This means that a string or bytes literal cannot be represented by `Constant::Str` / `Constant::Bytes` which creates an inconsistency. This PR avoids that inconsistency by splitting the constant nodes into it's own literal nodes, literal being the more appropriate naming convention from a static analysis tool perspective. This also makes working with literals in the linter and formatter much more ergonomic like, for example, if one would want to check if this is a string literal, it can be done easily using `Expr::is_string_literal_expr` or matching against `Expr::StringLiteral` as oppose to matching against the `ExprConstant` and enum `Constant`. A few AST helper methods can be simplified as well which will be done in a follow-up PR. This introduces a new `Expr::is_literal_expr` method which is the same as `Expr::is_constant_expr`. There are also intermediary changes related to implicit string concatenation which are quiet less. This is done so as to avoid having a huge PR which this already is. ## Test Plan 1. Verify and update all of the existing snapshots (parser, visitor) 2. Verify that the ecosystem check output remains **unchanged** for both the linter and formatter ### Formatter ecosystem check #### `main` | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75803 | 1799 | 1647 | | django | 0.99983 | 2772 | 34 | | home-assistant | 0.99953 | 10596 | 186 | | poetry | 0.99891 | 317 | 17 | | transformers | 0.99966 | 2657 | 330 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99978 | 3669 | 20 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 22 | #### `dhruv/constant-to-literal` | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75803 | 1799 | 1647 | | django | 0.99983 | 2772 | 34 | | home-assistant | 0.99953 | 10596 | 186 | | poetry | 0.99891 | 317 | 17 | | transformers | 0.99966 | 2657 | 330 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99978 | 3669 | 20 | | warehouse | 0.99977 | 654 | 13 | | zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
parent
78bbf6d403
commit
230c9ce236
268 changed files with 6663 additions and 6741 deletions
|
@ -5,8 +5,7 @@ use smallvec::SmallVec;
|
|||
|
||||
use ruff_formatter::write;
|
||||
use ruff_python_ast::{
|
||||
Constant, Expr, ExprAttribute, ExprBinOp, ExprBoolOp, ExprCompare, ExprConstant, ExprUnaryOp,
|
||||
UnaryOp,
|
||||
Expr, ExprAttribute, ExprBinOp, ExprBoolOp, ExprCompare, ExprUnaryOp, UnaryOp,
|
||||
};
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use ruff_python_trivia::{SimpleToken, SimpleTokenKind, SimpleTokenizer};
|
||||
|
@ -505,15 +504,7 @@ const fn is_simple_power_operand(expr: &Expr) -> bool {
|
|||
Expr::UnaryOp(ExprUnaryOp {
|
||||
op: UnaryOp::Not, ..
|
||||
}) => false,
|
||||
Expr::Constant(ExprConstant {
|
||||
value:
|
||||
Constant::Complex { .. }
|
||||
| Constant::Float(_)
|
||||
| Constant::Int(_)
|
||||
| Constant::None
|
||||
| Constant::Bool(_),
|
||||
..
|
||||
}) => true,
|
||||
Expr::NumberLiteral(_) | Expr::NoneLiteral(_) | Expr::BooleanLiteral(_) => true,
|
||||
Expr::Name(_) => true,
|
||||
Expr::UnaryOp(ExprUnaryOp { operand, .. }) => is_simple_power_operand(operand),
|
||||
Expr::Attribute(ExprAttribute { value, .. }) => is_simple_power_operand(value),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use ruff_formatter::{write, FormatRuleWithOptions};
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{Constant, Expr, ExprAttribute, ExprConstant};
|
||||
use ruff_python_ast::{Expr, ExprAttribute, ExprNumberLiteral, Number};
|
||||
use ruff_python_trivia::{find_only_token_in_range, SimpleTokenKind};
|
||||
use ruff_text_size::{Ranged, TextRange};
|
||||
|
||||
|
@ -167,17 +167,17 @@ impl NeedsParentheses for ExprAttribute {
|
|||
// Non Hex, octal or binary number literals need parentheses to disambiguate the attribute `.` from
|
||||
// a decimal point. Floating point numbers don't strictly need parentheses but it reads better (rather than 0.0.test()).
|
||||
fn is_base_ten_number_literal(expr: &Expr, source: &str) -> bool {
|
||||
if let Some(ExprConstant { value, range }) = expr.as_constant_expr() {
|
||||
if let Some(ExprNumberLiteral { value, range }) = expr.as_number_literal_expr() {
|
||||
match value {
|
||||
Constant::Float(_) => true,
|
||||
Constant::Int(_) => {
|
||||
Number::Float(_) => true,
|
||||
Number::Int(_) => {
|
||||
let text = &source[*range];
|
||||
!matches!(
|
||||
text.as_bytes().get(0..2),
|
||||
Some([b'0', b'x' | b'X' | b'o' | b'O' | b'b' | b'B'])
|
||||
)
|
||||
}
|
||||
_ => false,
|
||||
Number::Complex { .. } => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{Expr, ExprBinOp};
|
||||
use ruff_python_ast::ExprBinOp;
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::binary_like::BinaryLike;
|
||||
use crate::expression::expr_constant::is_multiline_string;
|
||||
use crate::expression::expr_string_literal::is_multiline_string;
|
||||
use crate::expression::has_parentheses;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
@ -35,10 +35,10 @@ impl NeedsParentheses for ExprBinOp {
|
|||
) -> OptionalParentheses {
|
||||
if parent.is_expr_await() {
|
||||
OptionalParentheses::Always
|
||||
} else if let Expr::Constant(constant) = self.left.as_ref() {
|
||||
} else if self.left.is_literal_expr() {
|
||||
// Multiline strings are guaranteed to never fit, avoid adding unnecessary parentheses
|
||||
if !constant.value.is_implicit_concatenated()
|
||||
&& is_multiline_string(constant, context.source())
|
||||
if !self.left.is_implicit_concatenated_string()
|
||||
&& is_multiline_string(self.left.as_ref().into(), context.source())
|
||||
&& has_parentheses(&self.right, context).is_some()
|
||||
&& !context.comments().has_dangling(self)
|
||||
&& !context.comments().has(self.left.as_ref())
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprBooleanLiteral;
|
||||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprBooleanLiteral;
|
||||
|
||||
impl FormatNodeRule<ExprBooleanLiteral> for FormatExprBooleanLiteral {
|
||||
fn fmt_fields(&self, item: &ExprBooleanLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if item.value {
|
||||
token("True").fmt(f)
|
||||
} else {
|
||||
token("False").fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprBooleanLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprBytesLiteral;
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::expr_string_literal::is_multiline_string;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::expression::string::{AnyString, FormatString};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprBytesLiteral;
|
||||
|
||||
impl FormatNodeRule<ExprBytesLiteral> for FormatExprBytesLiteral {
|
||||
fn fmt_fields(&self, item: &ExprBytesLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatString::new(&AnyString::Bytes(item)).fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
// Handled as part of `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprBytesLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if self.implicit_concatenated {
|
||||
OptionalParentheses::Multiline
|
||||
} else if is_multiline_string(self.into(), context.source()) {
|
||||
OptionalParentheses::Never
|
||||
} else {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule};
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{CmpOp, Expr, ExprCompare};
|
||||
use ruff_python_ast::{CmpOp, ExprCompare};
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::binary_like::BinaryLike;
|
||||
use crate::expression::expr_constant::is_multiline_string;
|
||||
use crate::expression::expr_string_literal::is_multiline_string;
|
||||
use crate::expression::has_parentheses;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
@ -37,10 +37,10 @@ impl NeedsParentheses for ExprCompare {
|
|||
) -> OptionalParentheses {
|
||||
if parent.is_expr_await() {
|
||||
OptionalParentheses::Always
|
||||
} else if let Expr::Constant(constant) = self.left.as_ref() {
|
||||
} else if self.left.is_literal_expr() {
|
||||
// Multiline strings are guaranteed to never fit, avoid adding unnecessary parentheses
|
||||
if !constant.value.is_implicit_concatenated()
|
||||
&& is_multiline_string(constant, context.source())
|
||||
if !self.left.is_implicit_concatenated_string()
|
||||
&& is_multiline_string(self.left.as_ref().into(), context.source())
|
||||
&& !context.comments().has(self.left.as_ref())
|
||||
&& self.comparators.first().is_some_and(|right| {
|
||||
has_parentheses(right, context).is_some() && !context.comments().has(right)
|
||||
|
|
|
@ -1,99 +0,0 @@
|
|||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{Constant, ExprConstant};
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange};
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::number::{FormatComplex, FormatFloat, FormatInt};
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::expression::string::{
|
||||
AnyString, FormatString, StringLayout, StringPrefix, StringQuotes,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprConstant {
|
||||
layout: ExprConstantLayout,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, Default)]
|
||||
pub enum ExprConstantLayout {
|
||||
#[default]
|
||||
Default,
|
||||
|
||||
String(StringLayout),
|
||||
}
|
||||
|
||||
impl FormatRuleWithOptions<ExprConstant, PyFormatContext<'_>> for FormatExprConstant {
|
||||
type Options = ExprConstantLayout;
|
||||
|
||||
fn with_options(mut self, options: Self::Options) -> Self {
|
||||
self.layout = options;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
||||
fn fmt_fields(&self, item: &ExprConstant, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let ExprConstant { range: _, value } = item;
|
||||
|
||||
match value {
|
||||
Constant::Ellipsis => token("...").fmt(f),
|
||||
Constant::None => token("None").fmt(f),
|
||||
Constant::Bool(value) => match value {
|
||||
true => token("True").fmt(f),
|
||||
false => token("False").fmt(f),
|
||||
},
|
||||
Constant::Int(_) => FormatInt::new(item).fmt(f),
|
||||
Constant::Float(_) => FormatFloat::new(item).fmt(f),
|
||||
Constant::Complex { .. } => FormatComplex::new(item).fmt(f),
|
||||
Constant::Str(_) | Constant::Bytes(_) => {
|
||||
let string_layout = match self.layout {
|
||||
ExprConstantLayout::Default => StringLayout::Default,
|
||||
ExprConstantLayout::String(layout) => layout,
|
||||
};
|
||||
FormatString::new(&AnyString::Constant(item))
|
||||
.with_layout(string_layout)
|
||||
.fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprConstant {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if self.value.is_implicit_concatenated() {
|
||||
OptionalParentheses::Multiline
|
||||
} else if is_multiline_string(self, context.source()) {
|
||||
OptionalParentheses::Never
|
||||
} else {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_multiline_string(constant: &ExprConstant, source: &str) -> bool {
|
||||
if constant.value.is_str() || constant.value.is_bytes() {
|
||||
let contents = &source[constant.range()];
|
||||
let prefix = StringPrefix::parse(contents);
|
||||
let quotes =
|
||||
StringQuotes::parse(&contents[TextRange::new(prefix.text_len(), contents.text_len())]);
|
||||
|
||||
quotes.is_some_and(StringQuotes::is_triple)
|
||||
&& memchr::memchr2(b'\n', b'\r', contents.as_bytes()).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprEllipsisLiteral;
|
||||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprEllipsisLiteral;
|
||||
|
||||
impl FormatNodeRule<ExprEllipsisLiteral> for FormatExprEllipsisLiteral {
|
||||
fn fmt_fields(&self, _item: &ExprEllipsisLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
token("...").fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprEllipsisLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprNoneLiteral;
|
||||
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprNoneLiteral;
|
||||
|
||||
impl FormatNodeRule<ExprNoneLiteral> for FormatExprNoneLiteral {
|
||||
fn fmt_fields(&self, _item: &ExprNoneLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
token("None").fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprNoneLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{ExprNumberLiteral, Number};
|
||||
|
||||
use crate::expression::number::{FormatComplex, FormatFloat, FormatInt};
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprNumberLiteral;
|
||||
|
||||
impl FormatNodeRule<ExprNumberLiteral> for FormatExprNumberLiteral {
|
||||
fn fmt_fields(&self, item: &ExprNumberLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item.value {
|
||||
Number::Int(_) => FormatInt::new(item).fmt(f),
|
||||
Number::Float(_) => FormatFloat::new(item).fmt(f),
|
||||
Number::Complex { .. } => FormatComplex::new(item).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprNumberLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
_context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
|
@ -209,7 +209,7 @@ fn is_simple_expr(expr: &Expr) -> bool {
|
|||
{
|
||||
is_simple_expr(operand)
|
||||
} else {
|
||||
matches!(expr, Expr::Constant(_) | Expr::Name(_))
|
||||
expr.is_literal_expr() || expr.is_name_expr()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
use ruff_formatter::FormatRuleWithOptions;
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::ExprStringLiteral;
|
||||
use ruff_text_size::{Ranged, TextLen, TextRange};
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
|
||||
use crate::expression::string::{
|
||||
AnyString, FormatString, StringLayout, StringPrefix, StringQuotes,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprStringLiteral {
|
||||
layout: StringLayout,
|
||||
}
|
||||
|
||||
impl FormatRuleWithOptions<ExprStringLiteral, PyFormatContext<'_>> for FormatExprStringLiteral {
|
||||
type Options = StringLayout;
|
||||
|
||||
fn with_options(mut self, options: Self::Options) -> Self {
|
||||
self.layout = options;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl FormatNodeRule<ExprStringLiteral> for FormatExprStringLiteral {
|
||||
fn fmt_fields(&self, item: &ExprStringLiteral, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
FormatString::new(&AnyString::String(item))
|
||||
.with_layout(self.layout)
|
||||
.fmt(f)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
&self,
|
||||
_dangling_comments: &[SourceComment],
|
||||
_f: &mut PyFormatter,
|
||||
) -> FormatResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl NeedsParentheses for ExprStringLiteral {
|
||||
fn needs_parentheses(
|
||||
&self,
|
||||
_parent: AnyNodeRef,
|
||||
context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
if self.implicit_concatenated {
|
||||
OptionalParentheses::Multiline
|
||||
} else if is_multiline_string(self.into(), context.source()) {
|
||||
OptionalParentheses::Never
|
||||
} else {
|
||||
OptionalParentheses::BestFit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn is_multiline_string(expr: AnyNodeRef, source: &str) -> bool {
|
||||
if expr.is_expr_string_literal() || expr.is_expr_bytes_literal() {
|
||||
let contents = &source[expr.range()];
|
||||
let prefix = StringPrefix::parse(contents);
|
||||
let quotes =
|
||||
StringQuotes::parse(&contents[TextRange::new(prefix.text_len(), contents.text_len())]);
|
||||
|
||||
quotes.is_some_and(StringQuotes::is_triple)
|
||||
&& memchr::memchr2(b'\n', b'\r', contents.as_bytes()).is_some()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
|
@ -7,7 +7,7 @@ use ruff_formatter::{
|
|||
use ruff_python_ast as ast;
|
||||
use ruff_python_ast::parenthesize::parentheses_iterator;
|
||||
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
|
||||
use ruff_python_ast::{AnyNodeRef, Constant, Expr, ExpressionRef, Operator};
|
||||
use ruff_python_ast::{AnyNodeRef, Expr, ExpressionRef, Operator};
|
||||
use ruff_python_trivia::CommentRanges;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
|
@ -27,11 +27,13 @@ pub(crate) mod expr_attribute;
|
|||
pub(crate) mod expr_await;
|
||||
pub(crate) mod expr_bin_op;
|
||||
pub(crate) mod expr_bool_op;
|
||||
pub(crate) mod expr_boolean_literal;
|
||||
pub(crate) mod expr_bytes_literal;
|
||||
pub(crate) mod expr_call;
|
||||
pub(crate) mod expr_compare;
|
||||
pub(crate) mod expr_constant;
|
||||
pub(crate) mod expr_dict;
|
||||
pub(crate) mod expr_dict_comp;
|
||||
pub(crate) mod expr_ellipsis_literal;
|
||||
pub(crate) mod expr_f_string;
|
||||
pub(crate) mod expr_formatted_value;
|
||||
pub(crate) mod expr_generator_exp;
|
||||
|
@ -42,10 +44,13 @@ pub(crate) mod expr_list;
|
|||
pub(crate) mod expr_list_comp;
|
||||
pub(crate) mod expr_name;
|
||||
pub(crate) mod expr_named_expr;
|
||||
pub(crate) mod expr_none_literal;
|
||||
pub(crate) mod expr_number_literal;
|
||||
pub(crate) mod expr_set;
|
||||
pub(crate) mod expr_set_comp;
|
||||
pub(crate) mod expr_slice;
|
||||
pub(crate) mod expr_starred;
|
||||
pub(crate) mod expr_string_literal;
|
||||
pub(crate) mod expr_subscript;
|
||||
pub(crate) mod expr_tuple;
|
||||
pub(crate) mod expr_unary_op;
|
||||
|
@ -94,7 +99,12 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
|
|||
Expr::Call(expr) => expr.format().fmt(f),
|
||||
Expr::FormattedValue(expr) => expr.format().fmt(f),
|
||||
Expr::FString(expr) => expr.format().fmt(f),
|
||||
Expr::Constant(expr) => expr.format().fmt(f),
|
||||
Expr::StringLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::BytesLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::NumberLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::BooleanLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::NoneLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::EllipsisLiteral(expr) => expr.format().fmt(f),
|
||||
Expr::Attribute(expr) => expr.format().fmt(f),
|
||||
Expr::Subscript(expr) => expr.format().fmt(f),
|
||||
Expr::Starred(expr) => expr.format().fmt(f),
|
||||
|
@ -274,7 +284,12 @@ fn format_with_parentheses_comments(
|
|||
Expr::Call(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::FormattedValue(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::FString(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Constant(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::StringLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::BytesLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::NumberLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::BooleanLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::NoneLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::EllipsisLiteral(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Attribute(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Subscript(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
Expr::Starred(expr) => FormatNodeRule::fmt_fields(expr.format().rule(), expr, f),
|
||||
|
@ -468,7 +483,12 @@ impl NeedsParentheses for Expr {
|
|||
Expr::Call(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::FormattedValue(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::FString(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Constant(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::StringLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::BytesLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::NumberLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::BooleanLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::NoneLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::EllipsisLiteral(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Attribute(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Subscript(expr) => expr.needs_parentheses(parent, context),
|
||||
Expr::Starred(expr) => expr.needs_parentheses(parent, context),
|
||||
|
@ -692,16 +712,12 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
|||
return;
|
||||
}
|
||||
|
||||
Expr::Constant(ast::ExprConstant {
|
||||
value:
|
||||
Constant::Str(ast::StringConstant {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Constant::Bytes(ast::BytesConstant {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
}),
|
||||
Expr::StringLiteral(ast::ExprStringLiteral {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Expr::BytesLiteral(ast::ExprBytesLiteral {
|
||||
implicit_concatenated: true,
|
||||
..
|
||||
})
|
||||
| Expr::FString(ast::ExprFString {
|
||||
|
@ -726,7 +742,12 @@ impl<'input> CanOmitOptionalParenthesesVisitor<'input> {
|
|||
| Expr::GeneratorExp(_)
|
||||
| Expr::FormattedValue(_)
|
||||
| Expr::FString(_)
|
||||
| Expr::Constant(_)
|
||||
| Expr::StringLiteral(_)
|
||||
| Expr::BytesLiteral(_)
|
||||
| Expr::NumberLiteral(_)
|
||||
| Expr::BooleanLiteral(_)
|
||||
| Expr::NoneLiteral(_)
|
||||
| Expr::EllipsisLiteral(_)
|
||||
| Expr::Name(_)
|
||||
| Expr::Slice(_)
|
||||
| Expr::IpyEscapeCommand(_) => {}
|
||||
|
|
|
@ -1,24 +1,24 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use ruff_python_ast::ExprConstant;
|
||||
use ruff_python_ast::ExprNumberLiteral;
|
||||
use ruff_text_size::{Ranged, TextSize};
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
pub(super) struct FormatInt<'a> {
|
||||
constant: &'a ExprConstant,
|
||||
number: &'a ExprNumberLiteral,
|
||||
}
|
||||
|
||||
impl<'a> FormatInt<'a> {
|
||||
pub(super) fn new(constant: &'a ExprConstant) -> Self {
|
||||
debug_assert!(constant.value.is_int());
|
||||
Self { constant }
|
||||
pub(super) fn new(number: &'a ExprNumberLiteral) -> Self {
|
||||
debug_assert!(number.value.is_int());
|
||||
Self { number }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatInt<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let range = self.number.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
let normalized = normalize_integer(content);
|
||||
|
@ -31,19 +31,19 @@ impl Format<PyFormatContext<'_>> for FormatInt<'_> {
|
|||
}
|
||||
|
||||
pub(super) struct FormatFloat<'a> {
|
||||
constant: &'a ExprConstant,
|
||||
number: &'a ExprNumberLiteral,
|
||||
}
|
||||
|
||||
impl<'a> FormatFloat<'a> {
|
||||
pub(super) fn new(constant: &'a ExprConstant) -> Self {
|
||||
debug_assert!(constant.value.is_float());
|
||||
Self { constant }
|
||||
pub(super) fn new(number: &'a ExprNumberLiteral) -> Self {
|
||||
debug_assert!(number.value.is_float());
|
||||
Self { number }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatFloat<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let range = self.number.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
let normalized = normalize_floating_number(content);
|
||||
|
@ -56,19 +56,19 @@ impl Format<PyFormatContext<'_>> for FormatFloat<'_> {
|
|||
}
|
||||
|
||||
pub(super) struct FormatComplex<'a> {
|
||||
constant: &'a ExprConstant,
|
||||
number: &'a ExprNumberLiteral,
|
||||
}
|
||||
|
||||
impl<'a> FormatComplex<'a> {
|
||||
pub(super) fn new(constant: &'a ExprConstant) -> Self {
|
||||
debug_assert!(constant.value.is_complex());
|
||||
Self { constant }
|
||||
pub(super) fn new(number: &'a ExprNumberLiteral) -> Self {
|
||||
debug_assert!(number.value.is_complex());
|
||||
Self { number }
|
||||
}
|
||||
}
|
||||
|
||||
impl Format<PyFormatContext<'_>> for FormatComplex<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let range = self.constant.range();
|
||||
let range = self.number.range();
|
||||
let content = f.context().locator().slice(range);
|
||||
|
||||
let normalized = normalize_floating_number(content.trim_end_matches(['j', 'J']));
|
||||
|
|
|
@ -4,7 +4,9 @@ use bitflags::bitflags;
|
|||
|
||||
use ruff_formatter::{format_args, write, FormatError};
|
||||
use ruff_python_ast::AnyNodeRef;
|
||||
use ruff_python_ast::{self as ast, Constant, ExprConstant, ExprFString, ExpressionRef};
|
||||
use ruff_python_ast::{
|
||||
self as ast, ExprBytesLiteral, ExprFString, ExprStringLiteral, ExpressionRef,
|
||||
};
|
||||
use ruff_python_parser::lexer::{lex_starts_at, LexicalError, LexicalErrorType};
|
||||
use ruff_python_parser::{Mode, Tok};
|
||||
use ruff_source_file::Locator;
|
||||
|
@ -26,19 +28,16 @@ enum Quoting {
|
|||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(super) enum AnyString<'a> {
|
||||
Constant(&'a ExprConstant),
|
||||
String(&'a ExprStringLiteral),
|
||||
Bytes(&'a ExprBytesLiteral),
|
||||
FString(&'a ExprFString),
|
||||
}
|
||||
|
||||
impl<'a> AnyString<'a> {
|
||||
pub(crate) fn from_expression(expression: &'a Expr) -> Option<AnyString<'a>> {
|
||||
match expression {
|
||||
Expr::Constant(
|
||||
constant @ ExprConstant {
|
||||
value: Constant::Str(_) | Constant::Bytes(_),
|
||||
..
|
||||
},
|
||||
) => Some(AnyString::Constant(constant)),
|
||||
Expr::StringLiteral(string) => Some(AnyString::String(string)),
|
||||
Expr::BytesLiteral(bytes) => Some(AnyString::Bytes(bytes)),
|
||||
Expr::FString(fstring) => Some(AnyString::FString(fstring)),
|
||||
_ => None,
|
||||
}
|
||||
|
@ -46,7 +45,7 @@ impl<'a> AnyString<'a> {
|
|||
|
||||
fn quoting(&self, locator: &Locator) -> Quoting {
|
||||
match self {
|
||||
Self::Constant(_) => Quoting::CanChange,
|
||||
Self::String(_) | Self::Bytes(_) => Quoting::CanChange,
|
||||
Self::FString(f_string) => {
|
||||
let unprefixed = locator
|
||||
.slice(f_string.range)
|
||||
|
@ -75,7 +74,14 @@ impl<'a> AnyString<'a> {
|
|||
/// Returns `true` if the string is implicitly concatenated.
|
||||
pub(super) fn is_implicit_concatenated(&self) -> bool {
|
||||
match self {
|
||||
Self::Constant(ExprConstant { value, .. }) => value.is_implicit_concatenated(),
|
||||
Self::String(ExprStringLiteral {
|
||||
implicit_concatenated,
|
||||
..
|
||||
}) => *implicit_concatenated,
|
||||
Self::Bytes(ExprBytesLiteral {
|
||||
implicit_concatenated,
|
||||
..
|
||||
}) => *implicit_concatenated,
|
||||
Self::FString(ExprFString {
|
||||
implicit_concatenated,
|
||||
..
|
||||
|
@ -87,7 +93,8 @@ impl<'a> AnyString<'a> {
|
|||
impl Ranged for AnyString<'_> {
|
||||
fn range(&self) -> TextRange {
|
||||
match self {
|
||||
Self::Constant(expr) => expr.range(),
|
||||
Self::String(expr) => expr.range(),
|
||||
Self::Bytes(expr) => expr.range(),
|
||||
Self::FString(expr) => expr.range(),
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +103,8 @@ impl Ranged for AnyString<'_> {
|
|||
impl<'a> From<&AnyString<'a>> for AnyNodeRef<'a> {
|
||||
fn from(value: &AnyString<'a>) -> Self {
|
||||
match value {
|
||||
AnyString::Constant(expr) => AnyNodeRef::ExprConstant(expr),
|
||||
AnyString::String(expr) => AnyNodeRef::ExprStringLiteral(expr),
|
||||
AnyString::Bytes(expr) => AnyNodeRef::ExprBytesLiteral(expr),
|
||||
AnyString::FString(expr) => AnyNodeRef::ExprFString(expr),
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +113,8 @@ impl<'a> From<&AnyString<'a>> for AnyNodeRef<'a> {
|
|||
impl<'a> From<&AnyString<'a>> for ExpressionRef<'a> {
|
||||
fn from(value: &AnyString<'a>) -> Self {
|
||||
match value {
|
||||
AnyString::Constant(expr) => ExpressionRef::Constant(expr),
|
||||
AnyString::String(expr) => ExpressionRef::StringLiteral(expr),
|
||||
AnyString::Bytes(expr) => ExpressionRef::BytesLiteral(expr),
|
||||
AnyString::FString(expr) => ExpressionRef::FString(expr),
|
||||
}
|
||||
}
|
||||
|
@ -130,9 +139,6 @@ pub enum StringLayout {
|
|||
|
||||
impl<'a> FormatString<'a> {
|
||||
pub(super) fn new(string: &'a AnyString<'a>) -> Self {
|
||||
if let AnyString::Constant(constant) = string {
|
||||
debug_assert!(constant.value.is_str() || constant.value.is_bytes());
|
||||
}
|
||||
Self {
|
||||
string,
|
||||
layout: StringLayout::Default,
|
||||
|
@ -247,9 +253,6 @@ struct FormatStringContinuation<'a> {
|
|||
|
||||
impl<'a> FormatStringContinuation<'a> {
|
||||
fn new(string: &'a AnyString<'a>) -> Self {
|
||||
if let AnyString::Constant(constant) = string {
|
||||
debug_assert!(constant.value.is_str() || constant.value.is_bytes());
|
||||
}
|
||||
Self { string }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue